Skip to content

Commit b59b105

Browse files
dmcppcheckclaude
andcommitted
HideSetExpander: add getOperand() for what a replacement list token is replaced by
## needs the operands before they are joined. No behaviour change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 06d3eec commit b59b105

1 file changed

Lines changed: 38 additions & 19 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,6 +3126,37 @@ namespace simplecpp {
31263126
return (tok && tok != macro.endToken && tok->name) ? macro.getArgNum(tok->str()) : ~0U;
31273127
}
31283128

3129+
/**
3130+
* The tokens that a token in the replacement list is replaced by: a parameter by its
3131+
* argument, # and a parameter by a string literal, and other tokens by a copy.
3132+
* @param operand destination tokenlist
3133+
* @param macro the macro
3134+
* @param tok the token in the replacement list; for # it is moved to the parameter
3135+
* @param arguments the arguments as written in the call
3136+
* @param expandedArguments the arguments, macro expanded
3137+
* @param expand use the expanded argument for a parameter
3138+
*/
3139+
void getOperand(TokenList &operand, const Macro &macro, const Token *&tok, const std::vector<TokenList> &arguments, const std::vector<TokenList> &expandedArguments, bool expand) const {
3140+
const Token * const next = (tok->next != macro.endToken) ? tok->next : nullptr;
3141+
// # is an operator in a function-like macro when it is followed by a parameter
3142+
const unsigned int hashArg = (tok->op == '#') ? getParameter(macro, next) : ~0U;
3143+
if (hashArg < arguments.size()) {
3144+
operand.push_back(new Token(stringify(arguments[hashArg], loc, macro.name(), outputList), loc, next->whitespaceahead));
3145+
operand.back()->macro = macro.name();
3146+
tok = next;
3147+
return;
3148+
}
3149+
const unsigned int argnr = getParameter(macro, tok);
3150+
if (argnr < arguments.size()) {
3151+
const TokenList &argument = expand ? expandedArguments[argnr] : arguments[argnr];
3152+
for (const Token *argtok = argument.cfront(); argtok; argtok = argtok->next)
3153+
operand.push_back(new Token(*argtok));
3154+
return;
3155+
}
3156+
operand.push_back(new Token(tok->str(), loc, tok->whitespaceahead));
3157+
operand.back()->macro = macro.name();
3158+
}
3159+
31293160
/**
31303161
* The replacement list of the macro, with the parameters replaced by the arguments.
31313162
* @param result destination tokenlist
@@ -3144,25 +3175,13 @@ namespace simplecpp {
31443175
throw unsupported(loc, "##");
31453176
if (tok->str() == DEFINED)
31463177
throw unsupported(loc, "defined");
3147-
// # is an operator in a function-like macro when it is followed by a parameter
3148-
const unsigned int hashArg = (tok->op == '#') ? getParameter(macro, next) : ~0U;
3149-
if (hashArg < arguments.size()) {
3150-
result.push_back(new Token(stringify(arguments[hashArg], loc, macro.name(), outputList), loc, next->whitespaceahead));
3151-
result.back()->macro = macro.name();
3152-
tok = next;
3153-
continue;
3154-
}
3155-
const unsigned int argnr = getParameter(macro, tok);
3156-
if (argnr < expandedArguments.size()) {
3157-
for (const Token *argtok = expandedArguments[argnr].cfront(); argtok; argtok = argtok->next)
3158-
result.push_back(new Token(*argtok));
3159-
// the whitespace after the parameter comes after the argument
3160-
if (tok->whitespaceahead && result.back())
3161-
result.back()->whitespaceahead = true;
3162-
} else {
3163-
result.push_back(new Token(tok->str(), loc, tok->whitespaceahead));
3164-
result.back()->macro = macro.name();
3165-
}
3178+
const bool parameter = getParameter(macro, tok) < arguments.size();
3179+
TokenList operand(files);
3180+
getOperand(operand, macro, tok, arguments, expandedArguments, true);
3181+
result.takeTokens(operand);
3182+
// the whitespace after a parameter comes after its argument
3183+
if (parameter && tok->whitespaceahead && result.back())
3184+
result.back()->whitespaceahead = true;
31663185
}
31673186
for (Token *tok = result.front(); tok; tok = tok->next) {
31683187
tok->location = loc;

0 commit comments

Comments
 (0)