From 441dad512b47a4ff3998f4f1c81068d40d32dc75 Mon Sep 17 00:00:00 2001 From: lemon Date: Thu, 26 Feb 2026 12:27:35 +0100 Subject: cpp: allow comments between function-like macro name and arguments --- c/lex.c | 35 +++++++++++++++++++++++++++-------- test/07-pp.c | 3 ++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/c/lex.c b/c/lex.c index fcf8f82..ce73155 100644 --- a/c/lex.c +++ b/c/lex.c @@ -469,14 +469,11 @@ Begin: lx->chridx = lx->chridxbuf[lx->chrbuf0++]; lx->eof = lx->chridx >= lx->ndat; RET('\n'); - } + } else if (lx->eof) RET(TKEOF); } while (++lx->chrbuf0 < countof(lx->chrbuf)); fillchrbuf(lx); lx->chridx = lx->chridxbuf[lx->chrbuf0]; - if ((lx->eof = (lx->chridx >= lx->ndat))) { - struct span span = {{ idx, lx->chridx - idx, lx->fileid }}; - fatal(&span, "unterminated comment"); - } + lx->eof = lx->chridx >= lx->ndat; } } else if (match(lx, '*')) { // /* multi line comment */ @@ -1051,13 +1048,35 @@ tryexpand(struct lexer *lx, struct token *tk) popmac(lx, 1); s = lx->macstk; } - if (!s) { + if (!s) { /* top-level context: looking ahead in file data */ struct token tk; int t; - while (aisspace(t = peek(lx, 0))) next(lx); + for (;;) { /* skip whitespace and comments */ + if (aisspace(t = peek(lx, 0))) next(lx); + else if (t == '/') { + int idx = lx->chridx; + switch (peek(lx, 1)) { + case '/': + while (!lx->eof && next(lx) != '\n') ; + continue; + case '*': + next(lx), next(lx); + while (peek(lx, 0) != '*' || peek(lx, 1) != '/') { + if (lx->eof) { + struct span span = {{ idx, lx->chridx - idx, lx->fileid }}; + lxfatal(lx, &span, "unterminated comment"); + } + next(lx); + } + next(lx), next(lx); + continue; + } + break; + } else break; + } if (t != '(') return 0; lex0(lx, &tk); - } else { + } else { /* expansion context: look ahead in macro stack */ if (s->idx >= s->rl.n || stkgetrl(s)[s->idx].t != '(') return 0; ++s->idx; } diff --git a/test/07-pp.c b/test/07-pp.c index be1dfe0..3d2534a 100644 --- a/test/07-pp.c +++ b/test/07-pp.c @@ -101,7 +101,8 @@ S\ gnu_ext(printf, "ok %s", "...") gnu_ext(printf, "\n"); countertest(ww); - countertest(ww); + countertest/* comment + */(ww); CAT(ret,urn) 0; } -- cgit v1.2.3