scc

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

commit 8cd311e6ab6c87983620397b63d120957d540d3c
parent c3bb2c04976c4e4ff610aec7b593c192e55a84b2
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue,  5 Apr 2022 18:43:06 +0200

cc1: Pass a macro to addinput()

Addinput() was receiving the symbol related to the macro expansion
but it makes more sense to receive the macro itself because we can
use other fields of the macro later.

Diffstat:
Msrc/cmd/cc/cc1/cc1.h | 4++--
Msrc/cmd/cc/cc1/cpp.c | 21+++++++++++++--------
Msrc/cmd/cc/cc1/lex.c | 11+++++++----
3 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/src/cmd/cc/cc1/cc1.h b/src/cmd/cc/cc1/cc1.h @@ -410,7 +410,7 @@ struct input { unsigned lineno; char *filenam; FILE *fp; - Symbol *macro; + Macro *macro; char *line, *begin, *p; struct input *next; }; @@ -459,7 +459,7 @@ extern int ahead(void); extern int next(void); extern void expect(int tok); extern void discard(void); -extern int addinput(char *fname, Symbol *hide, char *buffer, int fail); +extern int addinput(char *, Macro *, char *, int); extern void delinput(void); extern void setsafe(int type); extern void setloc(char *fname, unsigned line); diff --git a/src/cmd/cc/cc1/cpp.c b/src/cmd/cc/cc1/cpp.c @@ -20,18 +20,23 @@ int disexpand; int disescape; void -defdefine(char *macro, char *val, char *source) +defdefine(char *name, char *val, char *source) { char *def, *fmt = "#define %s %s\n"; - Symbol dummy = {.name = macro, .flags = SDECLARED}; + Symbol *sym = &(Symbol) { + .name = name, + .flags = SDECLARED, + }; + Macro *mp; if (!val) val = ""; - def = xmalloc(strlen(fmt) + strlen(macro) + strlen(val)); + def = xmalloc(strlen(fmt) + strlen(name) + strlen(val)); + sprintf(def, fmt, name, val); + mp = newmacro(sym); - sprintf(def, fmt, macro, val); lineno = ++ncmdlines; - addinput(source, &dummy, def, FAIL); + addinput(source, mp, def, FAIL); cpp(); delinput(); } @@ -311,7 +316,8 @@ newmacro(Symbol *sym) mp->arglist = NULL; mp->def = sym->u.s + 3; mp->npars = 0; - mp->npars = atoi(sym->u.s); + if (sym->u.s) + mp->npars = atoi(sym->u.s); return mp; } @@ -353,8 +359,7 @@ substitute: mp->bufsiz = 0; buffer[elen] = '\0'; DBG("MACRO '%s' expanded to :'%s'", mp->sym->name, buffer); - addinput(filenam, mp->sym, xstrdup(buffer), FAIL); - delmacro(mp); + addinput(filenam, mp, xstrdup(buffer), FAIL); return 1; } diff --git a/src/cmd/cc/cc1/lex.c b/src/cmd/cc/cc1/lex.c @@ -65,20 +65,22 @@ unhide(Symbol *sym) } int -addinput(char *fname, Symbol *sym, char *buffer, int fail) +addinput(char *fname, Macro *mp, char *buffer, int fail) { FILE *fp; char *extp; unsigned flags; int infileln; + Symbol *sym; Input *newip, *curip = input; if (curip) curip->lineno = lineno; - if (sym) { + if (mp) { /* this is a macro expansion */ fp = NULL; + sym = mp->sym; DBG("MACRO: %s expanded to '%s'", sym->name, buffer); hide(sym); flags = IMACRO; @@ -118,7 +120,7 @@ addinput(char *fname, Symbol *sym, char *buffer, int fail) newip = xmalloc(sizeof(*newip)); newip->next = curip; - newip->macro = sym; + newip->macro = mp; newip->lineno = lineno; newip->p = newip->begin = newip->line = buffer; newip->filenam = NULL; @@ -141,11 +143,12 @@ delinput(void) die("cc1: %s: %s", ip->filenam, strerror(errno)); break; case IMACRO: - unhide(ip->macro); + unhide(ip->macro->sym); break; } input = ip->next; + delmacro(ip->macro); free(ip->filenam); free(ip->line); free(ip);