Skip to content

Commit 07f5f84

Browse files
dmcppcheckclaude
andcommitted
HideSetExpander: add getDefined() and getStringified()
getOperand() was getting long. No behaviour change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7515422 commit 07f5f84

1 file changed

Lines changed: 58 additions & 33 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,49 +3269,74 @@ namespace simplecpp {
32693269
}
32703270

32713271
/**
3272-
* The tokens that a token in the replacement list is replaced by: a parameter by its
3273-
* argument, # and a parameter by a string literal, "defined X" by 1 or 0, and other tokens by a copy.
3274-
* @param operand destination tokenlist
3275-
* @param macro the macro
3276-
* @param tok the token in the replacement list; for # it is moved to the parameter
3277-
* @param call the macro call
3278-
* @param expand use the expanded argument for a parameter
3272+
* "defined X" in the replacement list is replaced by 1 or 0.
3273+
* @param operand destination tokenlist
3274+
* @param macro the macro
3275+
* @param tok the token in the replacement list; for "defined X" it is moved to the last token of X
3276+
* @param call the macro call
3277+
* @return true if tok is "defined" with an operand
32793278
*/
3280-
void getOperand(TokenList &operand, const Macro &macro, const Token *&tok, const MacroCall &call, bool expand) const {
3281-
// "defined X" is replaced by 1 or 0
3279+
bool getDefined(TokenList &operand, const Macro &macro, const Token *&tok, const MacroCall &call) const {
32823280
const Token *first;
32833281
const Token *second;
32843282
const Token *last;
3285-
if (tok->str() == DEFINED && Macro::findDefinedOperand(tok, first, second, last)) {
3286-
const std::string macroName = Macro::definedMacroName(first, second, [&](const Token *argtok, std::string &text) {
3287-
const unsigned int argnr = getParameter(macro, argtok);
3288-
if (argnr >= call.arguments.size())
3289-
return false;
3290-
text = call.arguments[argnr].cback() ? call.arguments[argnr].cback()->str() : "";
3291-
return true;
3292-
});
3293-
operand.push_back(newToken((macros.find(macroName) != macros.end()) ? "1" : "0", macro, last->whitespaceahead));
3294-
tok = last;
3295-
return;
3296-
}
3297-
if (isVaOpt(macro, tok)) {
3298-
getVaOpt(operand, macro, tok, call);
3299-
return;
3300-
}
3283+
if (tok->str() != DEFINED || !Macro::findDefinedOperand(tok, first, second, last))
3284+
return false;
3285+
const std::string macroName = Macro::definedMacroName(first, second, [&](const Token *argtok, std::string &text) {
3286+
const unsigned int argnr = getParameter(macro, argtok);
3287+
if (argnr >= call.arguments.size())
3288+
return false;
3289+
text = call.arguments[argnr].cback() ? call.arguments[argnr].cback()->str() : "";
3290+
return true;
3291+
});
3292+
operand.push_back(newToken((macros.find(macroName) != macros.end()) ? "1" : "0", macro, last->whitespaceahead));
3293+
tok = last;
3294+
return true;
3295+
}
3296+
3297+
/**
3298+
* # followed by a parameter or by __VA_OPT__(content) is replaced by a string literal.
3299+
* # is only an operator in a function-like macro.
3300+
* @param operand destination tokenlist
3301+
* @param macro the macro
3302+
* @param tok the token in the replacement list; for # it is moved to the last token of the operand
3303+
* @param call the macro call
3304+
* @return true if tok is the # operator
3305+
*/
3306+
bool getStringified(TokenList &operand, const Macro &macro, const Token *&tok, const MacroCall &call) const {
3307+
if (tok->op != '#')
3308+
return false;
33013309
const Token * const next = (tok->next != macro.endToken) ? tok->next : nullptr;
3302-
if (tok->op == '#' && isVaOpt(macro, next)) {
3303-
// #__VA_OPT__(content): the content as a string literal
3310+
if (isVaOpt(macro, next)) {
33043311
tok = next;
33053312
TokenList content(files);
33063313
getVaOpt(content, macro, tok, call);
33073314
operand.push_back(newToken(stringify(content, loc, macro.name(), outputList), macro, tok->whitespaceahead));
3308-
return;
3315+
return true;
33093316
}
3310-
// # is an operator in a function-like macro when it is followed by a parameter
3311-
const unsigned int hashArg = (tok->op == '#') ? getParameter(macro, next) : ~0U;
3312-
if (hashArg < call.arguments.size()) {
3313-
operand.push_back(newToken(stringify(call.arguments[hashArg], loc, macro.name(), outputList), macro, next->whitespaceahead));
3314-
tok = next;
3317+
const unsigned int argnr = getParameter(macro, next);
3318+
if (argnr >= call.arguments.size())
3319+
return false;
3320+
operand.push_back(newToken(stringify(call.arguments[argnr], loc, macro.name(), outputList), macro, next->whitespaceahead));
3321+
tok = next;
3322+
return true;
3323+
}
3324+
3325+
/**
3326+
* The tokens that a token in the replacement list is replaced by: a parameter by its
3327+
* argument, # and a parameter by a string literal, "defined X" by 1 or 0,
3328+
* __VA_OPT__(content) by its content or nothing, and other tokens by a copy.
3329+
* @param operand destination tokenlist
3330+
* @param macro the macro
3331+
* @param tok the token in the replacement list; it is moved to the last token that is used
3332+
* @param call the macro call
3333+
* @param expand use the expanded argument for a parameter
3334+
*/
3335+
void getOperand(TokenList &operand, const Macro &macro, const Token *&tok, const MacroCall &call, bool expand) const {
3336+
if (getDefined(operand, macro, tok, call) || getStringified(operand, macro, tok, call))
3337+
return;
3338+
if (isVaOpt(macro, tok)) {
3339+
getVaOpt(operand, macro, tok, call);
33153340
return;
33163341
}
33173342
const unsigned int argnr = getParameter(macro, tok);

0 commit comments

Comments
 (0)