scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 4dd8392ce7ff341b5d19cf8a9d1473aa94b9d807
parent 665c38d088b19183501943cd0b179bc8d6b75357
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 21 Apr 2015 15:12:32 +0200

Add longjmp point of recover

This is the first commit of the non exit feature.
At this point when there is an error the program try
an external declaration again. This is a very bad
error recovery, because it is going to generate
error for all the symbols after the first, but it is
a first step.

Diffstat:
Mcc1/error.c | 23++++++++++++++---------
Mcc1/main.c | 21++++++++++++---------
Mlib/die.c | 4++++
3 files changed, 30 insertions(+), 18 deletions(-)

diff --git a/cc1/error.c b/cc1/error.c @@ -1,27 +1,32 @@ +#include <setjmp.h> #include <stdarg.h> +#include <stdio.h> #include <stdlib.h> #include <inttypes.h> -#include <stdio.h> #include "../inc/cc.h" #include "cc1.h" -extern unsigned linenum; -extern unsigned columnum; -extern const char *filename; - static void -warn_helper(signed char flag, const char *fmt, va_list va) +warn_helper(int8_t flag, const char *fmt, va_list va) { + extern unsigned linenum; + extern unsigned columnum; + extern const char *filename; + extern uint8_t failure; + extern jmp_buf recover; + if (!flag) return; fprintf(stderr, "%s:%s:%u: ", - (!flag) ? "warning" : "error", filename, linenum); + (flag < 0) ? "warning" : "error", filename, linenum); vfprintf(stderr, fmt, va); putc('\n', stderr); - if (flag < 0) - exit(EXIT_FAILURE); /* TODO: uhmmmm */ + if (flag < 0) { + failure = 1; + longjmp(recover, 1); + } } void diff --git a/cc1/main.c b/cc1/main.c @@ -1,5 +1,6 @@ #include <inttypes.h> +#include <setjmp.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -12,16 +13,16 @@ extern void init_keywords(void), open_file(const char *file), init_expr(void); uint8_t npromote, warnings; +jmp_buf recover; -static uint8_t noerror; static char *output; static void clean(void) { - if (noerror) - return; - if (output) + extern uint8_t failure; + + if (failure && output) remove(output); } @@ -38,6 +39,7 @@ main(int argc, char *argv[]) char c, *input, *cp; atexit(clean); + repeat: --argc, ++argv; if (*argv && argv[0][0] == '-' && argv[0][1] != '-') { @@ -69,15 +71,16 @@ repeat: init_keywords(); init_expr(); open_file(input); + + setjmp(recover); next(); while (yytoken != EOFTOK) extdecl(); - if (ferror(stdin) || ferror(stdout)) { - die("error reading/writing from input/output:%s", - strerror(errno)); - } - noerror = 1; + if (fclose(stdin)) + die("error reading from input:%s", strerror(errno)); + if (fclose(stdout)) + die("error writing in output:%s", strerror(errno)); return 0; } diff --git a/lib/die.c b/lib/die.c @@ -1,13 +1,17 @@ +#include <inttypes.h> #include <stdarg.h> #include <stdlib.h> #include <stdio.h> #include "../inc/cc.h" +uint8_t failure; + void die(const char *fmt, ...) { + failure = 1; va_list va; va_start(va, fmt); fprintf(stderr, fmt, va);