shithub: scc

Download patch

ref: 4dd8392ce7ff341b5d19cf8a9d1473aa94b9d807
parent: 665c38d088b19183501943cd0b179bc8d6b75357
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Apr 21 11:12:32 EDT 2015

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.

--- 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
--- 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 @@
 	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 @@
 	char c, *input, *cp;
 
 	atexit(clean);
+
 repeat:
 	--argc, ++argv;
 	if (*argv && argv[0][0] == '-' && argv[0][1] != '-') {
@@ -69,15 +71,16 @@
 	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;
 }
--- a/lib/die.c
+++ b/lib/die.c
@@ -1,4 +1,5 @@
 
+#include <inttypes.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -5,9 +6,12 @@
 
 #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);