scc

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

commit 81afcf38de71354670102ab5fc305b929c606559
parent a84e6203a475174fccfd3210e95752a1b563caed
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 14 Nov 2021 07:37:51 +0100

cc1: Add more comments in copymacro()

Macro expansion is a complex process, and some of the tokens are a
bit obscure, like for example input ## produces the token $.
To make easier to read the code a few comments are added to show
these translations.

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

diff --git a/src/cmd/cc/cc1/cpp.c b/src/cmd/cc/cc1/cpp.c @@ -144,7 +144,8 @@ parameter(struct macroctx *mp) switch (yytoken) { case ')': case ',': - mp->argp -= 3; /* remove " , " or " ) "*/ + /* remove " , " or " ) "*/ + mp->argp -= 3; *mp->argp++ = '\0'; return; case '(': @@ -199,12 +200,6 @@ copymacro(struct macroctx *mp) bufsiz = mp->bufsiz; for (s = mp->def; c = *s; ++s) { switch (c) { - case '$': - while (bp[-1] == ' ') - --bp, ++bufsiz; - while (s[1] == ' ') - ++s; - break; case '\'': delim = '\''; goto search_delim; @@ -228,7 +223,15 @@ copymacro(struct macroctx *mp) bufsiz -= size; bp += size; break; + case '$': + /* token concatenation operator */ + while (bp[-1] == ' ') + --bp, ++bufsiz; + while (s[1] == ' ') + ++s; + break; case '#': + /* stringfier operator */ arg = mp->arglist[atoi(s += 2)]; s += 2; @@ -254,6 +257,7 @@ copymacro(struct macroctx *mp) break; case '@': + /* parameter substitution */ arg = mp->arglist[atoi(++s)]; s += 2; @@ -340,7 +344,9 @@ getpars(Symbol *args[NR_MACROARG]) next(); if (c != '(') return -1; - next(); /* skip the '(' */ + + /* skip the '(' */ + next(); if (accept(')')) return 0; @@ -403,6 +409,7 @@ getdefs(Symbol *args[NR_MACROARG], int nargs, char *bp, size_t bufsiz) cpperror("macro too long"); return 0; } + /* $ token is generated by ## */ if (yytoken == '$') { *bp++ = '$'; --bufsiz;