scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit eede1482b3b6450105ed2a24d4c71db47c43b9d0
parent 0fba7162733650be3f0dab5c60ec4513b1775201
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 26 May 2015 23:42:11 +0200

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.

Diffstat:
cc1/lex.c | 15+--------------
cc1/symbol.c | 34+++++++++++++++++++++++++++-------
2 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/cc1/lex.c b/cc1/lex.c @@ -433,8 +433,6 @@ iden(void) 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 @@ iden(void) * 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; diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -131,6 +131,33 @@ lookup(unsigned ns) } 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 @@ install(unsigned ns) 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;