Skip to content

Commit 953f255

Browse files
dmcppcheckclaude
andcommitted
Add Macro::expandDefined() for defined in a replacement list
No behaviour change. A test is added for defined at the end of the file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 52fb092 commit 953f255

2 files changed

Lines changed: 57 additions & 29 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2570,42 +2570,59 @@ namespace simplecpp {
25702570
return recursiveExpandToken(output, temp, loc, tok2, macros, expandedmacros, parametertokens, outputList);
25712571
}
25722572

2573-
if (tok->str() == DEFINED) {
2574-
const Token * const tok2 = tok->next;
2575-
const Token * const tok3 = tok2 ? tok2->next : nullptr;
2576-
const Token * const tok4 = tok3 ? tok3->next : nullptr;
2577-
const Token *defToken = nullptr;
2578-
const Token *lastToken = nullptr;
2579-
if (sameline(tok, tok4) && tok2->op == '(' && tok3->name && tok4->op == ')') {
2580-
defToken = tok3;
2581-
lastToken = tok4;
2582-
} else if (sameline(tok,tok2) && tok2->name) {
2583-
defToken = lastToken = tok2;
2584-
}
2585-
if (defToken) {
2586-
std::string macroName = defToken->str();
2587-
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)) {
2588-
TokenList temp(files);
2589-
if (expandArg(temp, defToken, parametertokens))
2590-
macroName = temp.cback()->str();
2591-
if (expandArg(temp, defToken->next->next->next, parametertokens))
2592-
macroName += temp.cback() ? temp.cback()->str() : "";
2593-
else
2594-
macroName += defToken->next->next->next->str();
2595-
lastToken = defToken->next->next->next;
2596-
}
2597-
const bool def = (macros.find(macroName) != macros.end());
2598-
output.push_back(newMacroToken(def ? "1" : "0", loc, true));
2599-
return lastToken->next;
2600-
}
2601-
}
2573+
const Token *next = nullptr;
2574+
if (tok->str() == DEFINED && expandDefined(output, loc, tok, macros, parametertokens, next))
2575+
return next;
26022576

26032577
output.push_back(newMacroToken(tok->str(), loc, true, tok));
26042578
if (it != macros.end())
26052579
output.back()->markExpandedFrom(&it->second);
26062580
return tok->next;
26072581
}
26082582

2583+
/**
2584+
* Expand "defined X" or "defined(X)" in the replacement list to 1 or 0.
2585+
* The operand can be a parameter, or two names pasted with ##.
2586+
* @param output destination tokenlist
2587+
* @param loc location for the result
2588+
* @param tok the "defined" token
2589+
* @param macros all macros
2590+
* @param parametertokens arguments of this macro call
2591+
* @param next output: the token after the operand
2592+
* @return false if there is no operand, then nothing is added to output
2593+
*/
2594+
bool expandDefined(TokenList &output, const Location &loc, const Token *tok, const MacroMap &macros, const std::vector<const Token*> &parametertokens, const Token *&next) const {
2595+
const Token * const tok2 = tok->next;
2596+
const Token * const tok3 = tok2 ? tok2->next : nullptr;
2597+
const Token * const tok4 = tok3 ? tok3->next : nullptr;
2598+
const Token *defToken = nullptr;
2599+
const Token *lastToken = nullptr;
2600+
if (sameline(tok, tok4) && tok2->op == '(' && tok3->name && tok4->op == ')') {
2601+
defToken = tok3;
2602+
lastToken = tok4;
2603+
} else if (sameline(tok,tok2) && tok2->name) {
2604+
defToken = lastToken = tok2;
2605+
}
2606+
if (defToken) {
2607+
std::string macroName = defToken->str();
2608+
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)) {
2609+
TokenList temp(files);
2610+
if (expandArg(temp, defToken, parametertokens))
2611+
macroName = temp.cback()->str();
2612+
if (expandArg(temp, defToken->next->next->next, parametertokens))
2613+
macroName += temp.cback() ? temp.cback()->str() : "";
2614+
else
2615+
macroName += defToken->next->next->next->str();
2616+
lastToken = defToken->next->next->next;
2617+
}
2618+
const bool def = (macros.find(macroName) != macros.end());
2619+
output.push_back(newMacroToken(def ? "1" : "0", loc, true));
2620+
next = lastToken->next;
2621+
return true;
2622+
}
2623+
return false;
2624+
}
2625+
26092626
bool expandArg(TokenList &output, const Token *tok, const std::vector<const Token*> &parametertokens) const {
26102627
if (!tok->name)
26112628
return false;

‎test.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,6 +3005,16 @@ static void ifDefinedHashHash2()
30053005
ASSERT_EQUALS("\n0", preprocess(code, &outputList));
30063006
}
30073007

3008+
static void definedAtEndOfFile()
3009+
{
3010+
// "defined" from a macro expansion is undefined behavior; gcc outputs "defined" here.
3011+
// The operand is the last token of the file.
3012+
const char code[] = "#define X\n"
3013+
"#define D defined X\n"
3014+
"D";
3015+
ASSERT_EQUALS("\n\n1", preprocess(code));
3016+
}
3017+
30083018
static void ifLogical()
30093019
{
30103020
const char code[] = "#if defined(A) || defined(B)\n"
@@ -5736,6 +5746,7 @@ static void runTests(int argc, char **argv, Input input)
57365746
TEST_CASE(ifDefinedInvalid2);
57375747
TEST_CASE(ifDefinedHashHash);
57385748
TEST_CASE(ifDefinedHashHash2);
5749+
TEST_CASE(definedAtEndOfFile);
57395750
TEST_CASE(ifLogical);
57405751
TEST_CASE(ifLogicalPrecedence);
57415752
TEST_CASE(ifSizeof);

0 commit comments

Comments
 (0)