Skip to content

Commit 7e57597

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. macroUsageArg4 expected such a usage for `INT64_C;`; it now expects none.
1 parent e5cb618 commit 7e57597

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

‎simplecpp.cpp‎

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

2398+
// No arguments => not macro expansion
2399+
if (functionLike() && nameTokInst->next && nameTokInst->next->op != '(') {
2400+
output.push_back(new Token(nameTokInst->str(), loc));
2401+
return nameTokInst->next;
2402+
}
2403+
23982404
usageList.emplace_back(loc);
23992405

24002406
if (expandBuiltin(output, loc, nameTokInst))
@@ -2403,12 +2409,6 @@ namespace simplecpp {
24032409
const bool calledInDefine = (loc.fileIndex != nameTokInst->location.fileIndex ||
24042410
loc.line < nameTokInst->location.line);
24052411

2406-
// No arguments => not macro expansion
2407-
if (functionLike() && nameTokInst->next && nameTokInst->next->op != '(') {
2408-
output.push_back(new Token(nameTokInst->str(), loc));
2409-
return nameTokInst->next;
2410-
}
2411-
24122412
if (functionLike() && nameTokInst->next && nameTokInst->next->op == '(' && isIntegerConstantMacro(name()))
24132413
usageList.back().arg = writtenArgument(nameTokInst, calledInDefine);
24142414

‎test.cpp‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4976,6 +4976,16 @@ static void tokenMacro15()
49764976
ASSERT_EQUALS("D 1:1 use 1:3 unknown\n", toString(macroUsage));
49774977
}
49784978

4979+
static void tokenMacro16()
4980+
{
4981+
// a function-like macro name without ( is not expanded, so it is not a usage
4982+
const char code[] = "#define F(x) x\n"
4983+
"int F = F(1);\n";
4984+
std::list<simplecpp::MacroUsage> macroUsage;
4985+
ASSERT_EQUALS("int(2:1) F(2:5) =(2:7) 1(2:9) ;(2:13)", expandedTokens(code, simplecpp::DUI(), &macroUsage));
4986+
ASSERT_EQUALS("F 1:9 use 2:9\n", toString(macroUsage));
4987+
}
4988+
49794989
/** The argument of each macro usage as "name use line:col: [arg]", sorted. */
49804990
static std::string argToString(const std::list<simplecpp::MacroUsage> &macroUsage)
49814991
{
@@ -5025,9 +5035,9 @@ static void macroUsageArg3()
50255035

50265036
static void macroUsageArg4()
50275037
{
5028-
// object-like macro and function-like macro name without call have no argument
5038+
// object-like macro has no argument, function-like macro name without call is no usage
50295039
ASSERT_EQUALS("INT64_C use 2:1: []\n", macroUsageArg("#define INT64_C 1\nINT64_C\n"));
5030-
ASSERT_EQUALS("INT64_C use 2:1: []\n", macroUsageArg("#define INT64_C(x) x\nINT64_C;\n"));
5040+
ASSERT_EQUALS("", macroUsageArg("#define INT64_C(x) x\nINT64_C;\n"));
50315041
}
50325042

50335043
static void macroUsageArg5()
@@ -6146,6 +6156,7 @@ static void runTests(int argc, char **argv, Input input)
61466156
TEST_CASE(tokenMacro13);
61476157
TEST_CASE(tokenMacro14);
61486158
TEST_CASE(tokenMacro15);
6159+
TEST_CASE(tokenMacro16);
61496160
TEST_CASE(macroUsageArg1);
61506161
TEST_CASE(macroUsageArg2);
61516162
TEST_CASE(macroUsageArg3);

0 commit comments

Comments
 (0)