[hackers] [scc] Force correct order in hash in nextsym() || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Wed, 12 Aug 2015 17:43:18 +0200 (CEST)

commit ead5f758ec8bf1a9e0a2d80e34d09f8d78b47526
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Wed Aug 12 12:30:24 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Wed Aug 12 13:18:02 2015 +0200

    Force correct order in hash in nextsym()

diff --git a/cc1/cc1.h b/cc1/cc1.h
index c13b9b0..299566f 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -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
index d3856b2..13e587a 100644
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
_AT_@ -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
_AT_@ -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
index 6b7ce44..2c8dd39 100644
--- a/cc1/lex.c
+++ b/cc1/lex.c
_AT_@ -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
index cc736e8..2e36ded 100644
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
_AT_@ -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;
_AT_@ -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)
 {
_AT_@ -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;
_AT_@ -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 *
Received on Wed Aug 12 2015 - 17:43:18 CEST

This archive was generated by hypermail 2.3.0 : Wed Aug 12 2015 - 17:48:12 CEST