shithub: scc

Download patch

ref: ac1288f4d196d9eb41b35f4d38506e286e6a3212
parent: d7dc1a4fbb9f1e8af0fade6cea54855d4f7549c9
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu May 28 07:28:21 EDT 2015

Disable expansion while parsing parameters of macros

Parameters of macros are not expanded, but we called to next(), which
search for macros to expand them.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -308,7 +308,7 @@
 extern char yytext[];
 extern unsigned yytoken;
 extern unsigned short yylen;
-extern int cppoff;
+extern int cppoff, disexpand;
 extern unsigned cppctx;
 
 extern Type *voidtype, *pvoidtype, *booltype,	
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
@@ -18,6 +18,7 @@
 static int paramerr;
 
 unsigned cppctx;
+int disexpand;
 
 static Symbol *
 defmacro(char *s)
@@ -171,8 +172,9 @@
 
 	if (ahead() != '(')
 		return 0;
-	next();
 
+	disexpand = 1;
+	next();
 	paramerr = n = 0;
 	argp = buffer;
 	arglen = INPUTSIZ;
@@ -182,6 +184,7 @@
 			parameter();
 		} while (!paramerr && ++n < NR_MACROARG && yytoken == ',');
 	}
+	disexpand = 0;
 
 	if (paramerr)
 		return -1;
@@ -194,6 +197,7 @@
 		      macroname, n, nargs);
 		return -1;
 	}
+
 	return 1;
 }
 
--- a/cc1/lex.c
+++ b/cc1/lex.c
@@ -429,6 +429,7 @@
 static unsigned
 iden(void)
 {
+	Symbol *sym;
 	char *p, *t, c;
 
 	for (p = input->p; isalnum(*p) || *p == '_'; ++p)
@@ -435,9 +436,9 @@
 		/* nothing */;
 	input->p = p;
 	tok2str();
-	yylval.sym = lookup(lex_ns);
-	if (yylval.sym->ns == NS_CPP) {
-		if (yylval.sym != input->macro && expand(yylval.sym))
+	yylval.sym = sym = lookup(lex_ns);
+	if (sym->ns == NS_CPP) {
+		if (!disexpand && sym != input->macro && expand(sym))
 			return 0;
 		/*
 		 * it is not a correct macro call, so try to find
@@ -444,11 +445,11 @@
 		 * another definition. This is going to be expensive
 		 * but I think it is not going to be a common case.
 		 */
-		yylval.sym = nextsym(yylval.sym, lex_ns);
+		sym = nextsym(sym, lex_ns);
 	}
-	if (yylval.sym->token != IDEN)
-		yylval.token = yylval.sym->u.token;
-	return yylval.sym->token;
+	if (sym->token != IDEN)
+		yylval.token = sym->u.token;
+	return sym->token;
 }
 
 static unsigned