shithub: scc

Download patch

ref: eede1482b3b6450105ed2a24d4c71db47c43b9d0
parent: 0fba7162733650be3f0dab5c60ec4513b1775201
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue May 26 19:42:11 EDT 2015

Move cpp hack to symbol.c

This is a temporal hack to solve the problem of cpp namespace, which follows
different rules than other namespaces. It fixes the following case:
	#define x(y) ((y) + 1)
	int x = x(1);
but this solution is ugly because it destroy the order in the hash, and create
phantom variables in some cases.

--- a/cc1/lex.c
+++ b/cc1/lex.c
@@ -433,8 +433,6 @@
 	tok2str();
 	yylval.sym = lookup(lex_ns);
 	if (yylval.sym->ns == NS_CPP) {
-		Symbol *sym;
-
 		if (yylval.sym != input->macro && expand(yylval.sym))
 			return 0;
 		/*
@@ -442,19 +440,8 @@
 		 * another definition. This is going to be expensive
 		 * but I think it is not going to be a common case.
 		 */
-		p = yylval.sym->name;
-		c = *p;
-		for (sym = yylval.sym->hash; sym; sym = sym->hash) {
-			t = sym->name;
-			if (c == *t && !strcmp(p, t)) {
-				yylval.sym = sym;
-				goto found_iden;
-			}
-		}
-		yylval.sym = install(lex_ns);
-		yylval.sym->flags &= ~ISDEFINED;
+		yylval.sym = nextsym(yylval.sym, lex_ns);
 	}
-found_iden:
 	if (yylval.sym->token != IDEN)
 		yylval.token = yylval.sym->u.token;
 	return yylval.sym->token;
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -131,6 +131,33 @@
 }
 
 Symbol *
+nextsym(Symbol *sym, unsigned ns)
+{
+	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;
+	for (p = sym->hash; p; p = p->hash) {
+		t = p->name;
+		if (c == *t && !strcmp(s, t))
+			return sym;
+	}
+	new = newsym(ns);
+	new->name = xstrdup(yytext);
+	new->hash = sym->hash;
+	return sym->hash = new;
+}
+
+Symbol *
 install(unsigned ns)
 {
 	Symbol *sym, **h;
@@ -151,13 +178,6 @@
 
 	sym = newsym(ns);
 	sym->name = xstrdup(yytext);
-
-	if (yylval.sym->ns == NS_CPP) {
-		sym->hash = yylval.sym->hash;
-		yylval.sym->hash = sym;
-		return sym;
-	}
-
 	h = &htab[hash(yytext)];
 	sym->hash = *h;
 	*h = sym;