shithub: scc

Download patch

ref: ead5f758ec8bf1a9e0a2d80e34d09f8d78b47526
parent: 38e6f6b3509088e152f21f77b97f75ba5a4309ed
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Aug 12 08:30:24 EDT 2015

Force correct order in hash in nextsym()

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -307,7 +307,6 @@
 extern Symbol *newsym(unsigned ns);
 extern void pushctx(void), popctx(void);
 extern void ikeywords(void);
-extern Symbol *addmacro(void);
 extern void delmacro(Symbol *sym);
 
 /* stmt.c */
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
@@ -26,8 +26,12 @@
 static Symbol *
 defmacro(char *s)
 {
+	Symbol *sym;
+
 	strcpy(yytext, s);
-	return addmacro();
+	sym = lookup(NS_CPP);
+	sym->flags |= ISDECLARED;
+	return sym;
 }
 
 void
@@ -319,7 +323,8 @@
 		warn("'%s' redefined", yytext);
 		free(sym->u.s);
 	} else {
-		sym = addmacro();
+		sym = lookup(NS_CPP);
+		sym->flags |= ISDECLARED;
 	}
 
 	next();
--- a/cc1/lex.c
+++ b/cc1/lex.c
@@ -378,17 +378,17 @@
 		/* nothing */;
 	input->p = p;
 	tok2str();
-	yylval.sym = sym = lookup(lex_ns);
+	sym = lookup(lex_ns);
 	if (sym->ns == NS_CPP && lexmode == CCMODE) {
 		if (!disexpand && expand(begin, sym))
 			return next();
 		/*
 		 * it is not a correct macro call, so try to find
-		 * another definition. This is going to be expensive
-		 * but I think it is not going to be a common case.
+		 * another definition.
 		 */
 		sym = nextsym(sym, lex_ns);
 	}
+	yylval.sym = sym;
 	if (sym->flags & ISCONSTANT)
 		return CONSTANT;
 	if (sym->token != IDEN)
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -154,7 +154,7 @@
 	sym = malloc(sizeof(*sym));
 	sym->id = 0;
 	sym->ns = ns;
-	sym->ctx = curctx;
+	sym->ctx = (ns == NS_CPP) ? UCHAR_MAX : curctx;
 	sym->token = IDEN;
 	sym->flags = ISDECLARED;
 	sym->u.s = sym->name = NULL;
@@ -210,20 +210,6 @@
 	return sym;
 }
 
-Symbol *
-addmacro(void)
-{
-	unsigned ctx = curctx;
-	Symbol *sym;
-
-	/* Force cpp symbols to be at the beginning of the hash */
-	curctx = UCHAR_MAX;
-	sym = lookup(NS_CPP);
-	sym->flags |= ISDECLARED;
-	curctx = ctx;
-	return sym;
-}
-
 void
 delmacro(Symbol *sym)
 {
@@ -239,13 +225,11 @@
 	char *s, *t, c;
 	Symbol *new, *p;
 
-	/* FIXME:
+	/*
 	 * This function is only called when a macro with parameters
 	 * is called without them.
 	 *      #define x(y) ((y) + 1)
 	 *      int x = x(y);
-	 * This solution fixes the problem but destroy the order of
-	 * contexts in the hash table.
 	 */
 	s = sym->name;
 	c = *s;
@@ -254,11 +238,9 @@
 		if (c == *t && !strcmp(s, t))
 			return sym;
 	}
-	new = newsym(ns);
+	new = linkhash(newsym(ns), s);
 	new->flags &= ~ISDECLARED;
-	new->name = xstrdup(yytext);
-	new->hash = sym->hash;
-	return sym->hash = new;
+	return new;
 }
 
 Symbol *