scc

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

commit 740fd29faa97cc8a39c7e48c368191bc2df9cc99
parent 58a4f1584eb24dd4204d8b4d87b06a11acede509
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Thu, 29 Jan 2026 12:25:29 +0100

cc1: Simplify getdefs()

We can unify the IDEN and string cases if we use yytext/yylen in the
case of IDEN because the STRING case is handled by the tokenizer to
avoid code duplication.

Diffstat:
Msrc/cmd/scc-cc/cc1/cpp.c | 24+++++++++---------------
1 file changed, 9 insertions(+), 15 deletions(-)

diff --git a/src/cmd/scc-cc/cc1/cpp.c b/src/cmd/scc-cc/cc1/cpp.c @@ -530,9 +530,9 @@ static int getdefs(Symbol *args[NR_MACROARG], int nargs, char *buffer, size_t bufsiz) { size_t len; + char *bp, *p; Symbol **argp, *sym; int c, id, token, prevc, ispar; - char *bp, *p, iden[INTIDENTSIZ + 1]; while (isspace(*input->p)) ++input->p; @@ -560,10 +560,11 @@ getdefs(Symbol *args[NR_MACROARG], int nargs, char *buffer, size_t bufsiz) cpperror("identifier too long in macro definition"); return 0; } - memcpy(iden, input->p, len); - iden[len] = '\0'; + memcpy(yytext, input->p, len); + yytext[len] = '\0'; + yylen = len; input->p = p - 1; - sym = lookup(NS_IDEN, iden, NOALLOC); + sym = lookup(NS_IDEN, yytext, NOALLOC); } else if (c == '"') { next(); assert(yytoken == STRING); @@ -578,10 +579,10 @@ getdefs(Symbol *args[NR_MACROARG], int nargs, char *buffer, size_t bufsiz) } if (argp != &args[nargs]) { id = argp - args; - sprintf(iden, + sprintf(yytext, "%c%02d%c", MACROPAR, id, MACROPAR); ispar = 1; - len = 4; + yylen = len = 4; } } @@ -598,17 +599,10 @@ getdefs(Symbol *args[NR_MACROARG], int nargs, char *buffer, size_t bufsiz) return 0; } - switch (token) { - case IDEN: - memcpy(bp, iden, len); - break; - case STRING: + if (token == IDEN || token == STRING) memcpy(bp, yytext, yylen); - break; - default: + else *bp = token; - break; - } bp += len; bufsiz -= len;