From a498f851ef2f50c9b8ac47e238137af52b54057d Mon Sep 17 00:00:00 2001 From: lemon Date: Sun, 1 Mar 2026 13:35:45 +0100 Subject: cpp: fix token spacing edge case when empty macros are involved --- c/lex.c | 6 +++++- test/07-pp.c | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/c/lex.c b/c/lex.c index 5ff30a2..07b5cb0 100644 --- a/c/lex.c +++ b/c/lex.c @@ -1214,8 +1214,10 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro arg->idx2 = argsbuf.n; arg->nfirstx = arg->nlastx = 1; int ilastx = -1; - for (;;) { + for (bool pad = 0;;) { + struct macrostack *sprev = lx->macstk; if (!advancemacstk(lx, &tk)) { + pad |= tk.space && lx->macstk == sprev; /* preserve whitespace empty macro */ if (lx->macstk == l && l->idx == 1) arg->nfirstx = argsbuf.n - arg->idx2; if (lx->macstk == l+1 && lx->macstk->idx == 0 && l->idx == l->rl.n) @@ -1224,8 +1226,10 @@ expandfnmacro(struct lexer *lx, struct span *span, internstr mname, struct macro } if (tk.t == TKEOF) break; size_t off = l->rl.p - argsbuf.p; + tk.space |= pad; vpush(&argsbuf, tk); l->rl.p = argsbuf.p + off; + pad = 0; } arg->n2 = argsbuf.n - arg->idx2; arg->nlastx = ilastx < 0 ? 1 : args->n2 - ilastx; diff --git a/test/07-pp.c b/test/07-pp.c index 2458111..47fb959 100644 --- a/test/07-pp.c +++ b/test/07-pp.c @@ -12,7 +12,7 @@ ok ... token spacing: ++ -- ++ === (0)~> ++ 3>0$ sum = 1 + 2 +3;$ -[ baz]$ +[ baz] ;$ +. *$ */ @@ -121,7 +121,8 @@ S\ #define foo bar #define bar EMPTY baz #define EMPTY - xstr([foo] EMPTY) "$\n" +#define EMPTY2 EMPTY + xstr([foo] EMPTY2;) "$\n" #define qx1(...)/**/__VA_ARGS__ #define qx2(a) qx1 ( a ) xstr(+qx2 ( .)// -- cgit v1.2.3