scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit ead5f758ec8bf1a9e0a2d80e34d09f8d78b47526
parent 38e6f6b3509088e152f21f77b97f75ba5a4309ed
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 12 Aug 2015 12:30:24 +0200

Force correct order in hash in nextsym()

Diffstat:
Mcc1/cc1.h | 1-
Mcc1/cpp.c | 9+++++++--
Mcc1/lex.c | 6+++---
Mcc1/symbol.c | 26++++----------------------
4 files changed, 14 insertions(+), 28 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -307,7 +307,6 @@ extern Symbol *install(unsigned ns, Symbol *sym); 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 */ diff --git a/cc1/cpp.c b/cc1/cpp.c @@ -26,8 +26,12 @@ int disexpand; 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 @@ define(void) warn("'%s' redefined", yytext); free(sym->u.s); } else { - sym = addmacro(); + sym = lookup(NS_CPP); + sym->flags |= ISDECLARED; } next(); diff --git a/cc1/lex.c b/cc1/lex.c @@ -378,17 +378,17 @@ iden(void) /* 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) diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -154,7 +154,7 @@ newsym(unsigned ns) 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 @@ lookup(unsigned ns) 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 @@ 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; @@ -254,11 +238,9 @@ nextsym(Symbol *sym, unsigned ns) 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 *