Skip to content

Commit 67c3098

Browse files
dmcppcheckclaude
andcommitted
HideSetExpander: handle defined in a replacement list
findDefinedOperand() and definedMacroName() are used, as in the old expansion. 5 more tests pass with the hide-set expansion. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 295e155 commit 67c3098

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2988,7 +2988,7 @@ namespace simplecpp {
29882988
* The result of an expansion is pushed back onto the input and scanned again, so a
29892989
* function-like macro whose arguments follow the expansion in the source is found by
29902990
* normal rescanning.
2991-
* Work in progress: __VA_OPT__ and "defined" in a replacement list are not handled yet.
2991+
* Work in progress: __VA_OPT__ is not handled yet.
29922992
*/
29932993
class HideSetExpander {
29942994
public:
@@ -3243,14 +3243,30 @@ namespace simplecpp {
32433243

32443244
/**
32453245
* The tokens that a token in the replacement list is replaced by: a parameter by its
3246-
* argument, # and a parameter by a string literal, and other tokens by a copy.
3246+
* argument, # and a parameter by a string literal, "defined X" by 1 or 0, and other tokens by a copy.
32473247
* @param operand destination tokenlist
32483248
* @param macro the macro
32493249
* @param tok the token in the replacement list; for # it is moved to the parameter
32503250
* @param call the macro call
32513251
* @param expand use the expanded argument for a parameter
32523252
*/
32533253
void getOperand(TokenList &operand, const Macro &macro, const Token *&tok, const MacroCall &call, bool expand) const {
3254+
// "defined X" is replaced by 1 or 0
3255+
const Token *first;
3256+
const Token *second;
3257+
const Token *last;
3258+
if (tok->str() == DEFINED && Macro::findDefinedOperand(tok, first, second, last)) {
3259+
const std::string macroName = Macro::definedMacroName(first, second, [&](const Token *argtok, std::string &text) {
3260+
const unsigned int argnr = getParameter(macro, argtok);
3261+
if (argnr >= call.arguments.size())
3262+
return false;
3263+
text = call.arguments[argnr].cback() ? call.arguments[argnr].cback()->str() : "";
3264+
return true;
3265+
});
3266+
operand.push_back(newToken((macros.find(macroName) != macros.end()) ? "1" : "0", macro, last->whitespaceahead));
3267+
tok = last;
3268+
return;
3269+
}
32543270
const Token * const next = (tok->next != macro.endToken) ? tok->next : nullptr;
32553271
// # is an operator in a function-like macro when it is followed by a parameter
32563272
const unsigned int hashArg = (tok->op == '#') ? getParameter(macro, next) : ~0U;
@@ -3288,8 +3304,6 @@ namespace simplecpp {
32883304
tok = tok->next;
32893305
continue;
32903306
}
3291-
if (tok->str() == DEFINED)
3292-
throw unsupported(loc, "defined");
32933307
const Token * const operandTok = tok;
32943308
const bool parameter = getParameter(macro, tok) < call.arguments.size();
32953309
// an argument is not expanded when it is an operand of ##

‎test.cpp‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2996,7 +2996,6 @@ static void ifDefinedNoPar()
29962996

29972997
static void ifDefinedNested()
29982998
{
2999-
HIDESET_EXPANSION_TODO(); // defined in a replacement list
30002999
const char code[] = "#define FOODEF defined(FOO)\n"
30013000
"#if FOODEF\n"
30023001
"X\n"
@@ -3009,7 +3008,6 @@ static void ifDefinedNested()
30093008

30103009
static void ifDefinedNestedNoPar()
30113010
{
3012-
HIDESET_EXPANSION_TODO(); // defined in a replacement list
30133011
const char code[] = "#define FOODEF defined FOO\n"
30143012
"#if FOODEF\n"
30153013
"X\n"
@@ -3038,7 +3036,6 @@ static void ifDefinedInvalid2()
30383036

30393037
static void ifDefinedHashHash()
30403038
{
3041-
HIDESET_EXPANSION_TODO(); // defined in a replacement list
30423039
const char code[] = "#define ENABLE(FEATURE) defined ENABLE_##FEATURE\n"
30433040
"#define ENABLE_FOO 1\n"
30443041
"#if ENABLE(FOO)\n"
@@ -3053,7 +3050,6 @@ static void ifDefinedHashHash()
30533050

30543051
static void ifDefinedHashHash2()
30553052
{
3056-
HIDESET_EXPANSION_TODO(); // defined in a replacement list
30573053
// #409
30583054
// do not crash when expanding P() (as ## rhs is "null")
30593055
// note: gcc outputs "defined E"
@@ -3065,7 +3061,6 @@ static void ifDefinedHashHash2()
30653061

30663062
static void definedAtEndOfFile()
30673063
{
3068-
HIDESET_EXPANSION_TODO(); // defined in a replacement list
30693064
// "defined" from a macro expansion is undefined behavior; gcc outputs "defined" here.
30703065
// The operand is the last token of the file.
30713066
const char code[] = "#define X\n"

0 commit comments

Comments
 (0)