Skip to content

Commit 5beedaf

Browse files
committed
Record a usage for a function-like macro name without ( in #if
In an #if or #elif condition the usage of a defined macro is left to expand(), which no longer records one for a function-like macro name that is not followed by (. So `#if F` was a usage when F was defined after it, but not when F was defined before it. Record such names like undefined ones.
1 parent 7e57597 commit 5beedaf

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4617,8 +4617,10 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
46174617
continue;
46184618
}
46194619

4620-
// usage of defined macros is recorded when they are expanded
4621-
if (macros.find(tok->str()) == macros.end())
4620+
// usage of defined macros is recorded when they are expanded,
4621+
// but a function-like macro without ( is not expanded
4622+
const MacroMap::const_iterator macroIt = macros.find(tok->str());
4623+
if (macroIt == macros.end() || (macroIt->second.functionLike() && tok->next && tok->next->op != '('))
46224624
maybeUsedMacros[tok->str()].emplace_back(tok->location);
46234625

46244626
const Token *tmp = tok;

‎test.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4986,6 +4986,20 @@ static void tokenMacro16()
49864986
ASSERT_EQUALS("F 1:9 use 2:9\n", toString(macroUsage));
49874987
}
49884988

4989+
static void tokenMacro17()
4990+
{
4991+
// a function-like macro name without ( in #if is a usage, also when the macro is defined before
4992+
std::list<simplecpp::MacroUsage> macroUsage;
4993+
preprocess("#define F(x) 1\n#if F\n#endif\n", &macroUsage);
4994+
ASSERT_EQUALS("F 1:9 use 2:5\n", toString(macroUsage));
4995+
macroUsage.clear();
4996+
preprocess("#define F(x) 1\n#if 0\n#elif F || 1\n#endif\n", &macroUsage);
4997+
ASSERT_EQUALS("F 1:9 use 3:7\n", toString(macroUsage));
4998+
macroUsage.clear();
4999+
preprocess("#define F(x) 1\n#if F(0)\n#endif\n", &macroUsage);
5000+
ASSERT_EQUALS("F 1:9 use 2:5\n", toString(macroUsage));
5001+
}
5002+
49895003
/** The argument of each macro usage as "name use line:col: [arg]", sorted. */
49905004
static std::string argToString(const std::list<simplecpp::MacroUsage> &macroUsage)
49915005
{
@@ -6157,6 +6171,7 @@ static void runTests(int argc, char **argv, Input input)
61576171
TEST_CASE(tokenMacro14);
61586172
TEST_CASE(tokenMacro15);
61596173
TEST_CASE(tokenMacro16);
6174+
TEST_CASE(tokenMacro17);
61606175
TEST_CASE(macroUsageArg1);
61616176
TEST_CASE(macroUsageArg2);
61626177
TEST_CASE(macroUsageArg3);

0 commit comments

Comments
 (0)