[hackers] [scc] Fix #if with not defined macros || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Thu, 13 Aug 2015 20:02:11 +0200 (CEST)

commit f6d8f8f304878b3c0e4544d055b29482f818f13e
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Thu Aug 13 16:21:01 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Thu Aug 13 17:17:24 2015 +0200

    Fix #if with not defined macros
    
    When a not defined macro appears in a #if then it must be evaluated
    to 0.

diff --git a/cc1/cpp.c b/cc1/cpp.c
index e554b47..a645809 100644
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
_AT_@ -195,6 +195,16 @@ expand(char *begin, Symbol *sym)
         char *arglist[NR_MACROARG], arguments[INPUTSIZ], buffer[BUFSIZE];
 
         macroname = sym->name;
+ if (!(sym->flags & ISDECLARED)) {
+ /*
+ * This case happens in #if were macro not defined must
+ * be expanded to 0
+ */
+ buffer[0] = '0';
+ buffer[1] = '\0';
+ elen = 1;
+ goto substitute;
+ }
         if (sym == symfile) {
                 elen = sprintf(buffer, "\"%s\" ", input->fname);
                 goto substitute;
_AT_@ -231,6 +241,9 @@ substitute:
 
         input->p = input->begin = begin;
 
+ if (!(sym->flags & ISDECLARED))
+ delmacro(sym);
+
         return 1;
 }
 #undef BUFSIZE
_AT_@ -327,6 +340,7 @@ define(void)
                 sym->flags |= ISDECLARED;
         }
 
+ setnamespace(NS_IDEN); /* Avoid polution in NS_CPP */
         next();
         if ((n = getpars(args)) == NR_MACROARG)
                 goto delete;
_AT_@ -502,6 +516,7 @@ static void
 cppif(void)
 {
         setnamespace(NS_CPP);
+ disexpand = 0;
         next();
         ifclause(0, 0);
 }
_AT_@ -548,6 +563,8 @@ elseclause(void)
 static void
 elif(void)
 {
+ setnamespace(NS_CPP);
+ disexpand = 0;
         elseclause();
         ifclause(0, 0);
 }
diff --git a/cc1/lex.c b/cc1/lex.c
index 2220592..35ff72f 100644
--- a/cc1/lex.c
+++ b/cc1/lex.c
_AT_@ -18,7 +18,7 @@ unsigned short yylen;
 int cppoff;
 int lexmode = CCMODE;
 
-static unsigned lex_ns = NS_IDEN;
+static unsigned lex_ns = NS_IDEN, saved_ns;
 static int safe, eof;
 Input *input;
 
_AT_@ -380,7 +380,7 @@ iden(void)
         input->p = p;
         tok2str();
         sym = lookup(lex_ns);
- if (sym->ns == NS_CPP && lexmode == CCMODE) {
+ if (sym->ns == NS_CPP) {
                 if (!disexpand && expand(begin, sym))
                         return next();
                 /*
_AT_@ -493,6 +493,7 @@ operator(void)
 void
 setnamespace(int ns)
 {
+ saved_ns = (ns == NS_CPPCLAUSES) ? lex_ns : 0;
         lex_ns = ns;
 }
 
_AT_@ -518,6 +519,8 @@ next(void)
         skipspaces();
         c = *input->begin;
         if ((eof || lexmode == CPPMODE) && c == '\0') {
+ if (lexmode == CPPMODE)
+ lex_ns = saved_ns;
                 strcpy(yytext, "<EOF>");
                 if (cppctx && eof)
                         error("#endif expected");
_AT_@ -538,7 +541,8 @@ next(void)
 
 exit:
         DBG(stderr, "TOKEN %s\n", yytext);
- lex_ns = NS_IDEN;
+ if (lexmode == CCMODE)
+ lex_ns = NS_IDEN;
         return yytoken;
 }
 
Received on Thu Aug 13 2015 - 20:02:11 CEST

This archive was generated by hypermail 2.3.0 : Thu Aug 13 2015 - 20:12:24 CEST