Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 104
Fix: simplecpp ## fails to expand function-like macro when '(' is not adjacent#655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -2381,16 +2381,63 @@ namespace simplecpp { | ||||||
| output.deleteToken(A); | ||||||
| TokenList tokens(files); | ||||||
| tokens.push_back(new Token(strAB, tok->location)); | ||||||
| // for function like macros, push the (...) | ||||||
| if (tokensB.empty() && sameline(B,B->next) && B->next->op=='(') { | ||||||
| const MacroMap::const_iterator it = macros.find(strAB); | ||||||
| if (it != macros.end() && expandedmacros.find(strAB) == expandedmacros.end() && it->second.functionLike()) { | ||||||
| const Token * const tok2 = appendTokens(tokens, loc, B->next, macros, expandedmacros, parametertokens); | ||||||
| // If strAB names a function-like macro, locate and append its argument list '(...)' | ||||||
| // from the remaining replacement tokens, then expand the whole call. | ||||||
| // 'forwardScan' is true when the argument list was consumed via the forward scan | ||||||
| // path below (not adjacent), so that expandToken() is called instead of | ||||||
| // the normal takeTokens() path even when expandResult is false. | ||||||
| bool forwardScan = false; | ||||||
| const MacroMap::const_iterator it = macros.find(strAB); | ||||||
| const bool isFunctionLikeMacro = (it != macros.end() && expandedmacros.find(strAB) == expandedmacros.end() && it->second.functionLike()); | ||||||
| if (tokensB.empty() && isFunctionLikeMacro) { | ||||||
| // Fast path: '(' is the very next token after B on the same line. | ||||||
| const Token *lpar = (sameline(B, B->next) && B->next->op == '(') ? B->next : nullptr; | ||||||
| if (!lpar && !expandResult) { | ||||||
| // Forward-scan path: '(' is not immediately adjacent to B. | ||||||
| // This handles PAR-style indirection patterns such as: | ||||||
| // #define PAR(a, ...) a __VA_ARGS__ | ||||||
| // #define DISPATCH(kind, ...) PAR(PREFIX_ ## kind, (__VA_ARGS__)) | ||||||
| // where the '(' for PREFIX_kind belongs to the __VA_ARGS__ parameter | ||||||
| // and is separated from B by a comma in the replacement text. | ||||||
| // Only active in the appendTokens context (expandResult==false) to | ||||||
| // avoid unintended side-effects inside the main expansion loop. | ||||||
| const Token *scan = nextTok; | ||||||
| while (scan && sameline(B, scan)) { | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this
Suggested change
| ||||||
| if (scan->op == '(') { | ||||||
| // Found a literal '(' after skipping separators. | ||||||
| lpar = scan; | ||||||
| forwardScan = true; | ||||||
| break; | ||||||
| } | ||||||
| if (scan->op == ',') { | ||||||
| // Argument separator — skip and keep scanning. | ||||||
| scan = scan->next; | ||||||
| continue; | ||||||
| } | ||||||
| if (scan->name) { | ||||||
| // Named token: expand it and check whether it starts with '(...)' | ||||||
| // (covers the case where __VA_ARGS__ expands to a parenthesised list). | ||||||
| TokenList expanded(files); | ||||||
| if (expandArg(expanded, scan, loc, macros, expandedmacros, parametertokens) && | ||||||
| expanded.cfront() && expanded.cfront()->op == '(') { | ||||||
| for (Token *t = expanded.front(); t; t = t->next) | ||||||
| t->location = loc; | ||||||
| tokens.takeTokens(expanded); | ||||||
| nextTok = scan->next; | ||||||
| forwardScan = true; | ||||||
| } | ||||||
| break; // stop at any name token, whether consumed or not | ||||||
| } | ||||||
| break; // any other operator — stop scan | ||||||
| } | ||||||
| } | ||||||
| if (lpar) { | ||||||
| const Token * const tok2 = appendTokens(tokens, loc, lpar, macros, expandedmacros, parametertokens); | ||||||
| if (tok2) | ||||||
| nextTok = tok2->next; | ||||||
| } | ||||||
| } | ||||||
| if (expandResult) | ||||||
| if (expandResult || forwardScan) | ||||||
| expandToken(output, loc, tokens.cfront(), macros, expandedmacros, parametertokens); | ||||||
| else | ||||||
| output.takeTokens(tokens); | ||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since scan is only used in the while loop, I believe it would be nicer to write a for loop.