commit cae3fc327c4969ad626fbbe90f00026e977671e0
parent db7877e7b5317f8c3fef848096fbffd498f4eb69
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Thu, 7 Apr 2022 10:48:23 +0200
cc1: Allocate macro input before copying definition
This change makes possible to trace a parameter expansion
because if you run over the list of stacked inputs we can
find an IMACRO input from any IPARAM input.
Diffstat:
2 files changed, 14 insertions(+), 17 deletions(-)
diff --git a/src/cmd/cc/cc1/cpp.c b/src/cmd/cc/cc1/cpp.c
@@ -211,6 +211,11 @@ copymacro(Macro *mp)
char *s, *p, *arg, *bp;
int size, bufsiz;
+ if (mp->sym == symfile)
+ return sprintf(mp->buffer, "\"%s\" ", filenam);
+ if (mp->sym == symline)
+ return sprintf(mp->buffer, "%d ", lineno);
+
bp = mp->buffer;
bufsiz = mp->bufsiz;
for (s = mp->def; c = *s; ++s) {
@@ -322,6 +327,7 @@ newmacro(Symbol *sym)
mp->arglist = NULL;
mp->def = sym->u.s + 3;
mp->npars = 0;
+ mp->buffer = NULL;
if (sym->u.s)
mp->npars = atoi(sym->u.s);
@@ -333,7 +339,6 @@ expand(Symbol *sym)
{
int elen;
Macro *mp;
- char buffer[INPUTSIZ];
DBG("MACRO '%s' detected disexpand=%d hide=%d",
sym->name, disexpand, sym->hide);
@@ -343,28 +348,20 @@ expand(Symbol *sym)
mp = newmacro(sym);
mp->fname = filenam;
- mp->buffer = buffer;
- mp->bufsiz = INPUTSIZ-1;
-
- if (sym == symfile) {
- elen = sprintf(buffer, "\"%s\" ", filenam);
- goto substitute;
- }
- if (sym == symline) {
- elen = sprintf(buffer, "%d ", lineno);
- goto substitute;
- }
if (!parsepars(mp)) {
delmacro(mp);
return 0;
}
- elen = copymacro(mp);
-substitute:
- buffer[elen] = '\0';
- DBG("MACRO '%s' expanded to :'%s'", mp->sym->name, buffer);
addinput(IMACRO, mp, FAIL);
+ mp->buffer = input->line;
+ mp->bufsiz = INPUTSIZ-1;
+
+ elen = copymacro(mp);
+ mp->buffer[elen] = '\0';
+
+ DBG("MACRO '%s' expanded to :'%s'", mp->sym->name, mp->buffer);
return 1;
}
diff --git a/src/cmd/cc/cc1/lex.c b/src/cmd/cc/cc1/lex.c
@@ -85,7 +85,7 @@ addinput(int type, void *arg, int fail)
fname = mp->fname;
buffer = mp->buffer;
hide(sym);
- DBG("INPUT: macro %s expanded to '%s'", sym->name, buffer);
+ DBG("INPUT: expanding macro %s", sym->name);
break;
case IPARAM:
fp = NULL;