Skip to content

Commit 05a5ed3

Browse files
committed
Do not record a usage for a function-like macro name without (
expand() added the use location to the usage list before checking whether a function-like macro is followed by (, so `int F = 1;` with F defined as a function-like macro was reported as a usage of F although nothing is expanded. Record the usage after that check.
1 parent 79c4663 commit 05a5ed3

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,6 +2355,12 @@ namespace simplecpp {
23552355
std::cout << " expand " << name() << " " << locstring(defineLocation()) << std::endl;
23562356
#endif
23572357

2358+
// No arguments => not macro expansion
2359+
if (functionLike() && nameTokInst->next && nameTokInst->next->op != '(') {
2360+
output.push_back(new Token(nameTokInst->str(), loc));
2361+
return nameTokInst->next;
2362+
}
2363+
23582364
usageList.emplace_back(loc);
23592365

23602366
if (expandBuiltin(output, loc, nameTokInst))
@@ -2363,12 +2369,6 @@ namespace simplecpp {
23632369
const bool calledInDefine = (loc.fileIndex != nameTokInst->location.fileIndex ||
23642370
loc.line < nameTokInst->location.line);
23652371

2366-
// No arguments => not macro expansion
2367-
if (functionLike() && nameTokInst->next && nameTokInst->next->op != '(') {
2368-
output.push_back(new Token(nameTokInst->str(), loc));
2369-
return nameTokInst->next;
2370-
}
2371-
23722372
std::vector<const Token*> parametertokens1(getMacroParameters(nameTokInst, calledInDefine));
23732373

23742374
// If macro call uses __COUNTER__ then expand that first

‎test.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4788,6 +4788,16 @@ static void tokenMacro15()
47884788
ASSERT_EQUALS("D 1:1 use 1:3 unknown\n", toString(macroUsage));
47894789
}
47904790

4791+
static void tokenMacro16()
4792+
{
4793+
// a function-like macro name without ( is not expanded, so it is not a usage
4794+
const char code[] = "#define F(x) x\n"
4795+
"int F = F(1);\n";
4796+
std::list<simplecpp::MacroUsage> macroUsage;
4797+
ASSERT_EQUALS("int(2:1) F(2:5) =(2:7) 1(2:9) ;(2:13)", expandedTokens(code, simplecpp::DUI(), &macroUsage));
4798+
ASSERT_EQUALS("F 1:9 use 2:9\n", toString(macroUsage));
4799+
}
4800+
47914801
static void undef()
47924802
{
47934803
const char code[] = "#define A\n"
@@ -5864,6 +5874,7 @@ static void runTests(int argc, char **argv, Input input)
58645874
TEST_CASE(tokenMacro13);
58655875
TEST_CASE(tokenMacro14);
58665876
TEST_CASE(tokenMacro15);
5877+
TEST_CASE(tokenMacro16);
58675878

58685879
TEST_CASE(undef);
58695880

0 commit comments

Comments
 (0)