scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit fa095decfa65ce7fa2d03ad6fb1d75640c0803b1
parent 7954fd897af0f78b514d09ed801cafafee4e8df5
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  6 Apr 2022 16:31:25 +0200

cc1: Move defined function to cpp.c

Defined() enables and disables the macro expansion, and as it is
a very delicated thing to do it is better to keep all the code
that modifies disexpand in only one file.

While this patch was written it was discovered that the function
notdefined() was not acutally needed because the code to handle
preprocessor defined was ran from unary().

Diffstat:
Msrc/cmd/cc/cc1/cc1.h | 2++
Msrc/cmd/cc/cc1/cpp.c | 25++++++++++++++++++++++++-
Msrc/cmd/cc/cc1/expr.c | 76++++++++++++++++------------------------------------------------------------
Msrc/cmd/cc/cc1/symbol.c | 5+++++
4 files changed, 47 insertions(+), 61 deletions(-)

diff --git a/src/cmd/cc/cc1/cc1.h b/src/cmd/cc/cc1/cc1.h @@ -196,6 +196,7 @@ enum tokens { BREAK, RETURN, DEFINE, + DEFINED, INCLUDE, LINE, PRAGMA, @@ -504,6 +505,7 @@ extern void undefmacro(char *s); extern void ppragmaln(void); extern void delmacro(Macro *); extern Macro *newmacro(Symbol *); +extern Node *defined(void); /* builtin.c */ diff --git a/src/cmd/cc/cc1/cpp.c b/src/cmd/cc/cc1/cpp.c @@ -338,7 +338,7 @@ expand(Symbol *sym) DBG("MACRO '%s' detected disexpand=%d hide=%d", sym->name, disexpand, sym->hide); - if (disexpand || sym->hide) + if (disexpand || sym->hide || sym->token != IDEN) return 0; mp = newmacro(sym); @@ -680,6 +680,29 @@ usererr(void) next(); } + +Node * +defined(void) +{ + Symbol *sym; + int paren; + + disexpand = 1; + next(); + paren = accept('('); + if (yytoken != IDEN && yytoken != TYPEIDEN) + cpperror("operator 'defined' requires an identifier"); + if (yytoken == TYPEIDEN || !(yylval.sym->flags & SDECLARED)) + sym = zero; + else + sym = one; + disexpand = 0; + next(); + if (paren) + expect(')'); + return constnode(sym); +} + static void ifclause(int negate, int isifdef) { diff --git a/src/cmd/cc/cc1/expr.c b/src/cmd/cc/cc1/expr.c @@ -568,32 +568,6 @@ negation(int op, Node *np) } static Symbol * -notdefined(Symbol *sym) -{ - int isdef; - - if (namespace == NS_CPP && !strcmp(sym->name, "defined")) { - disexpand = 1; - next(); - expect('('); - sym = yylval.sym; - expect(IDEN); - expect(')'); - - isdef = (sym->flags & SDECLARED) != 0; - sym = newsym(NS_IDEN, NULL); - sym->type = inttype; - sym->flags |= SCONSTANT; - sym->u.i = isdef; - disexpand = 0; - return sym; - } - errorp("'%s' undeclared", yytext); - sym->type = inttype; - return install(sym->ns, yylval.sym); -} - -static Symbol * adjstrings(Symbol *sym) { char *s, *t; @@ -650,17 +624,23 @@ primary(void) case CONSTANT: np = constnode(sym); break; + case DEFINED: + np = defined(); + break; case IDEN: assert((sym->flags & SCONSTANT) == 0); - if ((sym->flags & SDECLARED) == 0) { - if (namespace == NS_CPP) { - np = constnode(zero); - break; - } - sym = notdefined(sym); + if ((sym->flags & SDECLARED) != 0) { + sym->flags |= SUSED; + np = varnode(sym); + } else if (namespace == NS_CPP) { + np = constnode(zero); + } else { + errorp("'%s' undeclared", yytext); + sym->type = inttype; + sym = install(sym->ns, yylval.sym); + sym->flags |= SUSED; + np = varnode(sym); } - sym->flags |= SUSED; - np = varnode(sym); break; default: unexpected(); @@ -807,28 +787,6 @@ postfix(Node *lp) } } -static Node * -defined(void) -{ - Symbol *sym; - int paren; - - disexpand = 1; - next(); - paren = accept('('); - if (yytoken != IDEN && yytoken != TYPEIDEN) - cpperror("operator 'defined' requires an identifier"); - if (yytoken == TYPEIDEN || !(yylval.sym->flags & SDECLARED)) - sym = zero; - else - sym = one; - disexpand = 0; - next(); - if (paren) - expect(')'); - return constnode(sym); -} - static Node *cast(int); static Node * @@ -857,10 +815,8 @@ unary(int needdecay) next(); np = incdec(unary(1), op); goto chk_decay; - case IDEN: - case TYPEIDEN: - if (lexmode == CPPMODE && !strcmp(yylval.sym->name, "defined")) - return defined(); + case DEFINED: + return defined(); default: np = postfix(primary()); goto chk_decay; diff --git a/src/cmd/cc/cc1/symbol.c b/src/cmd/cc/cc1/symbol.c @@ -344,6 +344,10 @@ builtins(struct builtin *built) void isyms(void) { + static struct keyword cppoper[] = { + {"defined", DEFINED, DEFINED}, + {NULL, 0, 0} + }; static struct keyword cppkeys[] = { {"define", DEFINE, DEFINE}, {"include", INCLUDE, INCLUDE}, @@ -401,6 +405,7 @@ isyms(void) keywords(lexkeys, NS_KEYWORD); keywords(cppkeys, NS_CPPCLAUSES); + keywords(cppoper, NS_CPP); ibuilts(); /*