shithub: scc

Download patch

ref: 38e6f6b3509088e152f21f77b97f75ba5a4309ed
parent: 454cbc4039b4f3c7efb737b04259c0e4ab73ba40
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Aug 11 18:02:15 EDT 2015

Add cpperror()

This function is like printerr(), but it also discards the
rest of the line, and calls to next() to obtain a EOF in yytoken.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -291,6 +291,7 @@
 extern void warn(char *fmt, ...);
 extern void unexpected(void);
 extern void printerr(char *fmt, ...);
+extern void cpperror(char *fmt, ...);
 
 /* types.c */
 extern bool eqtype(Type *tp1, Type *tp2);
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
@@ -244,11 +244,11 @@
 
 	do {
 		if (n == NR_MACROARG) {
-			printerr("too much parameters in macro");
+			cpperror("too much parameters in macro");
 			return NR_MACROARG;
 		}
 		if (yytoken != IDEN) {
-			printerr("macro arguments must be identifiers");
+			cpperror("macro arguments must be identifiers");
 			return NR_MACROARG;
 		}
 		args[n++] = yylval.sym;
@@ -279,7 +279,7 @@
 			}
 		}
 		if (prevc == '#' && !ispar) {
-			printerr("'#' is not followed by a macro parameter");
+			cpperror("'#' is not followed by a macro parameter");
 			return 0;
 		}
 		if (yytoken == EOFTOK)
@@ -286,7 +286,7 @@
 			break;
 
 		if ((len = strlen(yytext)) >= bufsiz) {
-			printerr("too long macro");
+			cpperror("too long macro");
 			return 0;
 		}
 		memcpy(bp, yytext, len);
@@ -391,15 +391,15 @@
 	}
 
 	if (*bp)
-		printerr("included file '%s' not found", file);
+		cpperror("included file '%s' not found", file);
 
 	return;
 
 bad_include:
-	printerr("#include expects \"FILENAME\" or <FILENAME>");
+	cpperror("#include expects \"FILENAME\" or <FILENAME>");
 	return;
 too_long:
-	printerr("#include FILENAME too long");
+	cpperror("#include FILENAME too long");
 	return;
 }
 
@@ -412,11 +412,10 @@
 	if (cppoff)
 		return;
 
-	setnamespace(NS_IDEN);
 	next();
 	n = strtol(yytext, &endp, 10);
 	if (n <= 0 || n > USHRT_MAX || *endp != '\0') {
-		printerr("first parameter of #line is not a positive integer");
+		cpperror("first parameter of #line is not a positive integer");
 		return;
 	}
 
@@ -425,7 +424,7 @@
 		goto set_line;
 
 	if (*yytext != '\"' || yylen == 1) {
-		printerr("second parameter of #line is not a valid filename");
+		cpperror("second parameter of #line is not a valid filename");
 		return;
 	}
 
@@ -434,7 +433,7 @@
 	next();
 
 set_line:
-	input->nline = yylval.sym->u.i;
+	input->nline = n;
 }
 
 static void
@@ -451,7 +450,7 @@
 {
 	if (cppoff)
 		return;
-	printerr("#error %s", input->p);
+	cpperror("#error %s", input->p);
 	*input->p = '\0';
 	next();
 }
@@ -470,7 +469,7 @@
 
 	if (isifdef) {
 		if (yytoken != IDEN) {
-			printerr("no macro name given in #%s directive",
+			cpperror("no macro name given in #%s directive",
 			         (negate) ? "ifndef" : "ifdef");
 			return;
 		}
@@ -482,7 +481,7 @@
 	} else {
 		/* TODO: catch recovery here */
 		if ((expr = iconstexpr()) == NULL) {
-			printerr("parameter of #if is not an integer constant expression");
+			cpperror("parameter of #if is not an integer constant expression");
 			return;
 		}
 		status = expr->sym->u.i != 0;
--- a/cc1/error.c
+++ b/cc1/error.c
@@ -13,7 +13,7 @@
 static unsigned nerrors;
 
 static void
-warn_helper(int flag, char *fmt, va_list va)
+warn_error(int flag, char *fmt, va_list va)
 {
 	if (flag == 0)
 		return;
@@ -39,7 +39,7 @@
 
 	va_list va;
 	va_start(va, fmt);
-	warn_helper(warnings, fmt, va);
+	warn_error(warnings, fmt, va);
 	va_end(va);
 }
 
@@ -49,7 +49,7 @@
 	va_list va;
 
 	va_start(va, fmt);
-	warn_helper(-1, fmt, va);
+	warn_error(-1, fmt, va);
 	va_end(va);
 	exit(1);
 	discard();
@@ -60,8 +60,21 @@
 {
 	va_list va;
 	va_start(va, fmt);
-	warn_helper(-1, fmt, va);
+	warn_error(-1, fmt, va);
 	va_end(va);
+}
+
+void
+cpperror(char *fmt, ...)
+{
+	va_list va;
+	va_start(va, fmt);
+	warn_error(-1, fmt, va);
+	va_end(va);
+
+	/* discard input until the end of the line */
+	*input->p = '\0';
+	next();
 }
 
 void