Skip to content

Commit a328c4d

Browse files
committed
Fix: simplecpp ## fails to expand function-like macro when '(' is not adjacent
When the ## operator concatenates two tokens to form a function-like macro name (e.g. PREFIX_ ## kind → PREFIX_SCALAR), simplecpp looked for the argument list '(...)' only at B->next. In PAR-style indirection patterns the '(' is separated from B by a comma or a variadic parameter token: #define PAR(a, ...) a __VA_ARGS__ #define PREFIX_SCALAR(T, N) T N #define DISPATCH(kind, ...) PAR(PREFIX_ ## kind, (__VA_ARGS__)) DISPATCH(SCALAR, int, x) // was: [unknownMacro] — now: int x Because '(' was not found, expansion was aborted and the macro was reported as unknownMacro, causing cppcheck to skip the entire translation unit. Fix: when B->next is not '(' and we are in the appendTokens context (expandResult==false), walk forward on the same line skipping ',' separators and resolving named parameter tokens via expandArg(). The first '(' found (literally or as the head of an expanded argument) is used as lpar and passed to appendTokens() as before. The forwardScan flag ensures expandToken() is called on the result even when expandResult is false. The forward scan is restricted to expandResult==false to avoid unintended side-effects in the main expansion loop.
1 parent 74a5a63 commit a328c4d

1 file changed

Lines changed: 51 additions & 6 deletions

File tree

simplecpp.cpp

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,16 +2453,61 @@ namespace simplecpp {
24532453
output.deleteToken(A);
24542454
TokenList tokens(files);
24552455
tokens.push_back(new Token(strAB, tok->location));
2456-
// for function like macros, push the (...)
2457-
if (tokensB.empty() && sameline(B,B->next) && B->next->op=='(') {
2458-
const MacroMap::const_iterator it = macros.find(strAB);
2459-
if (it != macros.end() && expandedmacros.find(strAB) == expandedmacros.end() && it->second.functionLike()) {
2460-
const Token * const tok2 = appendTokens(tokens, loc, B->next, macros, expandedmacros, parametertokens);
2456+
// If strAB names a function-like macro, locate and append its argument list '(...)'
2457+
// from the remaining replacement tokens, then expand the whole call.
2458+
// 'forwardScan' is true when the argument list was consumed via the forward scan
2459+
// path below (not adjacent), so that expandToken() is called instead of
2460+
// the normal takeTokens() path even when expandResult is false.
2461+
bool forwardScan = false;
2462+
const MacroMap::const_iterator it = macros.find(strAB);
2463+
const bool isFunctionLikeMacro = (it != macros.end() && expandedmacros.find(strAB) == expandedmacros.end() && it->second.functionLike());
2464+
if (tokensB.empty() && isFunctionLikeMacro) {
2465+
// Fast path: '(' is the very next token after B on the same line.
2466+
const Token *lpar = (sameline(B, B->next) && B->next->op == '(') ? B->next : nullptr;
2467+
if (!lpar && !expandResult) {
2468+
// Forward-scan path: '(' is not immediately adjacent to B.
2469+
// This handles PAR-style indirection patterns such as:
2470+
// #define PAR(a, ...) a __VA_ARGS__
2471+
// #define DISPATCH(kind, ...) PAR(PREFIX_ ## kind, (__VA_ARGS__))
2472+
// where the '(' for PREFIX_kind belongs to the __VA_ARGS__ parameter
2473+
// and is separated from B by a comma in the replacement text.
2474+
// Only active in the appendTokens context (expandResult==false) to
2475+
// avoid unintended side-effects inside the main expansion loop.
2476+
for (const Token *scan = nextTok; sameline(B, scan); scan = scan->next) {
2477+
if (scan->op == '(') {
2478+
// Found a literal '(' after skipping separators.
2479+
lpar = scan;
2480+
forwardScan = true;
2481+
break;
2482+
}
2483+
if (scan->op == ',') {
2484+
// Argument separator — skip and keep scanning.
2485+
continue;
2486+
}
2487+
if (scan->name) {
2488+
// Named token: expand it and check whether it starts with '(...)'
2489+
// (covers the case where __VA_ARGS__ expands to a parenthesised list).
2490+
TokenList expanded(files);
2491+
if (expandArg(expanded, scan, loc, macros, expandedmacros, parametertokens) &&
2492+
expanded.cfront() && expanded.cfront()->op == '(') {
2493+
for (Token *t = expanded.front(); t; t = t->next)
2494+
t->location = loc;
2495+
tokens.takeTokens(expanded);
2496+
nextTok = scan->next;
2497+
forwardScan = true;
2498+
}
2499+
break; // stop at any name token, whether consumed or not
2500+
}
2501+
break; // any other operator — stop scan
2502+
}
2503+
}
2504+
if (lpar) {
2505+
const Token * const tok2 = appendTokens(tokens, loc, lpar, macros, expandedmacros, parametertokens);
24612506
if (tok2)
24622507
nextTok = tok2->next;
24632508
}
24642509
}
2465-
if (expandResult)
2510+
if (expandResult || forwardScan)
24662511
expandToken(output, loc, tokens.cfront(), macros, expandedmacros, parametertokens);
24672512
else
24682513
output.takeTokens(tokens);

0 commit comments

Comments
 (0)