Skip to content

Commit 9d2bbe1

Browse files
dmcppcheckclaude
andcommitted
Split Macro::expandDefined() into findDefinedOperand() and definedMacroName()
They do not depend on how the old expansion stores the arguments, so the hide-set expansion can use them. No behaviour change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f2da8f7 commit 9d2bbe1

1 file changed

Lines changed: 64 additions & 28 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,6 +2616,55 @@ namespace simplecpp {
26162616
return tok->next;
26172617
}
26182618

2619+
/**
2620+
* Find the operand of "defined" in a replacement list: "defined X", "defined(X)" or "defined X ## Y".
2621+
* @param tok the "defined" token
2622+
* @param first output: X
2623+
* @param second output: Y for "X ## Y", otherwise nullptr
2624+
* @param last output: the last token of the operand
2625+
* @return false if there is no operand
2626+
*/
2627+
static bool findDefinedOperand(const Token *tok, const Token *&first, const Token *&second, const Token *&last) {
2628+
const Token * const tok2 = tok->next;
2629+
const Token * const tok3 = tok2 ? tok2->next : nullptr;
2630+
const Token * const tok4 = tok3 ? tok3->next : nullptr;
2631+
first = last = second = nullptr;
2632+
if (sameline(tok, tok4) && tok2->op == '(' && tok3->name && tok4->op == ')') {
2633+
first = tok3;
2634+
last = tok4;
2635+
} else if (sameline(tok,tok2) && tok2->name) {
2636+
first = last = tok2;
2637+
}
2638+
if (!first)
2639+
return false;
2640+
if (first->next && first->next->op == '#' && first->next->next && first->next->next->op == '#' && first->next->next->next && first->next->next->next->name && sameline(first,first->next->next->next)) {
2641+
second = first->next->next->next;
2642+
last = second;
2643+
}
2644+
return true;
2645+
}
2646+
2647+
/**
2648+
* The name of the macro that "defined" checks.
2649+
* @param first X in "defined X"
2650+
* @param second Y in "defined X ## Y", or nullptr
2651+
* @param argText for "X ## Y": gives the text of a parameter, and returns false if the token is not a parameter
2652+
*/
2653+
template<class ArgText>
2654+
static std::string definedMacroName(const Token *first, const Token *second, ArgText argText) {
2655+
std::string macroName = first->str();
2656+
if (second) {
2657+
std::string text;
2658+
if (argText(first, text))
2659+
macroName = text;
2660+
if (argText(second, text))
2661+
macroName += text;
2662+
else
2663+
macroName += second->str();
2664+
}
2665+
return macroName;
2666+
}
2667+
26192668
/**
26202669
* Expand "defined X" or "defined(X)" in the replacement list to 1 or 0.
26212670
* The operand can be a parameter, or two names pasted with ##.
@@ -2628,35 +2677,22 @@ namespace simplecpp {
26282677
* @return false if there is no operand, then nothing is added to output
26292678
*/
26302679
bool expandDefined(TokenList &output, const Location &loc, const Token *tok, const MacroMap &macros, const std::vector<const Token*> &parametertokens, const Token *&next) const {
2631-
const Token * const tok2 = tok->next;
2632-
const Token * const tok3 = tok2 ? tok2->next : nullptr;
2633-
const Token * const tok4 = tok3 ? tok3->next : nullptr;
2634-
const Token *defToken = nullptr;
2635-
const Token *lastToken = nullptr;
2636-
if (sameline(tok, tok4) && tok2->op == '(' && tok3->name && tok4->op == ')') {
2637-
defToken = tok3;
2638-
lastToken = tok4;
2639-
} else if (sameline(tok,tok2) && tok2->name) {
2640-
defToken = lastToken = tok2;
2641-
}
2642-
if (defToken) {
2643-
std::string macroName = defToken->str();
2644-
if (defToken->next && defToken->next->op == '#' && defToken->next->next && defToken->next->next->op == '#' && defToken->next->next->next && defToken->next->next->next->name && sameline(defToken,defToken->next->next->next)) {
2645-
TokenList temp(files);
2646-
if (expandArg(temp, defToken, parametertokens))
2647-
macroName = temp.cback()->str();
2648-
if (expandArg(temp, defToken->next->next->next, parametertokens))
2649-
macroName += temp.cback() ? temp.cback()->str() : "";
2650-
else
2651-
macroName += defToken->next->next->next->str();
2652-
lastToken = defToken->next->next->next;
2653-
}
2654-
const bool def = (macros.find(macroName) != macros.end());
2655-
output.push_back(newMacroToken(def ? "1" : "0", loc, true));
2656-
next = lastToken->next;
2680+
const Token *first;
2681+
const Token *second;
2682+
const Token *last;
2683+
if (!findDefinedOperand(tok, first, second, last))
2684+
return false;
2685+
TokenList temp(files);
2686+
const std::string macroName = definedMacroName(first, second, [&](const Token *argtok, std::string &text) {
2687+
if (!expandArg(temp, argtok, parametertokens))
2688+
return false;
2689+
text = temp.cback() ? temp.cback()->str() : "";
26572690
return true;
2658-
}
2659-
return false;
2691+
});
2692+
const bool def = (macros.find(macroName) != macros.end());
2693+
output.push_back(newMacroToken(def ? "1" : "0", loc, true));
2694+
next = last->next;
2695+
return true;
26602696
}
26612697

26622698
bool expandArg(TokenList &output, const Token *tok, const std::vector<const Token*> &parametertokens) const {

0 commit comments

Comments
 (0)