Skip to content

Commit 920637c

Browse files
authored
Fix #773 Do not record a usage for a function-like macro name without ( (#759)
1 parent 0f0aba6 commit 920637c

2 files changed

Lines changed: 57 additions & 11 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,6 +2398,12 @@ namespace simplecpp {
23982398
std::cout << " expand " << name() << " " << locstring(defineLocation()) << std::endl;
23992399
#endif
24002400

2401+
// No arguments => not macro expansion
2402+
if (functionLike() && nameTokInst->next && nameTokInst->next->op != '(') {
2403+
output.push_back(new Token(nameTokInst->str(), loc));
2404+
return nameTokInst->next;
2405+
}
2406+
24012407
usageList.emplace_back(loc);
24022408

24032409
if (expandBuiltin(output, loc, nameTokInst))
@@ -2406,13 +2412,7 @@ namespace simplecpp {
24062412
const bool calledInDefine = (loc.fileIndex != nameTokInst->location.fileIndex ||
24072413
loc.line < nameTokInst->location.line);
24082414

2409-
// No arguments => not macro expansion
2410-
if (functionLike() && nameTokInst->next && nameTokInst->next->op != '(') {
2411-
output.push_back(new Token(nameTokInst->str(), loc));
2412-
return nameTokInst->next;
2413-
}
2414-
2415-
if (functionLike() && nameTokInst->next && nameTokInst->next->op == '(' && isIntegerConstantMacro(name()))
2415+
if (functionLike() && nameTokInst->next && isIntegerConstantMacro(name()))
24162416
usageList.back().arg = writtenArgument(nameTokInst, calledInDefine);
24172417

24182418
std::vector<const Token*> parametertokens1(getMacroParameters(nameTokInst, calledInDefine));
@@ -4645,8 +4645,10 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
46454645
continue;
46464646
}
46474647

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

46524654
const Token *tmp = tok;

‎test.cpp‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5015,6 +5015,47 @@ static void tokenMacro15()
50155015
ASSERT_EQUALS("D 1:1 use 1:3 unknown\n", toString(macroUsage));
50165016
}
50175017

5018+
static void tokenMacro16()
5019+
{
5020+
// a function-like macro name without ( is not expanded, so it is not a usage
5021+
const char code[] = "#define F(x) x\n"
5022+
"int F = F(1);\n";
5023+
std::list<simplecpp::MacroUsage> macroUsage;
5024+
ASSERT_EQUALS("int(2:1) F(2:5) =(2:7) 1(2:9) ;(2:13)", expandedTokens(code, simplecpp::DUI(), &macroUsage));
5025+
ASSERT_EQUALS("F 1:9 use 2:9\n", toString(macroUsage));
5026+
}
5027+
5028+
static void tokenMacro17()
5029+
{
5030+
// a function-like macro name without ( in #if is a usage, also when the macro is defined before
5031+
std::list<simplecpp::MacroUsage> macroUsage;
5032+
preprocess("#define F(x) 1\n#if F\n#endif\n", &macroUsage);
5033+
ASSERT_EQUALS("F 1:9 use 2:5\n", toString(macroUsage));
5034+
macroUsage.clear();
5035+
preprocess("#define F(x) 1\n#if 0\n#elif F || 1\n#endif\n", &macroUsage);
5036+
ASSERT_EQUALS("F 1:9 use 3:7\n", toString(macroUsage));
5037+
macroUsage.clear();
5038+
preprocess("#define F(x) 1\n#if F(0)\n#endif\n", &macroUsage);
5039+
ASSERT_EQUALS("F 1:9 use 2:5\n", toString(macroUsage));
5040+
}
5041+
5042+
static void tokenMacro18()
5043+
{
5044+
// a bare function-like macro name in an argument or a replacement list is not a usage
5045+
std::list<simplecpp::MacroUsage> macroUsage;
5046+
ASSERT_EQUALS("\n\nF ;", preprocess("#define F(x) x\n#define G(a) a\nG(F);\n", &macroUsage));
5047+
ASSERT_EQUALS("G 2:9 use 3:1\n", toString(macroUsage));
5048+
macroUsage.clear();
5049+
ASSERT_EQUALS("\n\nF F 5 ;", preprocess("#define F(x) x\n#define H(a) a F a(5)\nH(F);\n", &macroUsage));
5050+
ASSERT_EQUALS("F 1:9 use 3:1\nH 2:9 use 3:1\n", toString(macroUsage));
5051+
macroUsage.clear();
5052+
ASSERT_EQUALS("\n1", preprocess("#define F(x) x\nF\n(1)\n", &macroUsage));
5053+
ASSERT_EQUALS("F 1:9 use 2:1\n", toString(macroUsage));
5054+
macroUsage.clear();
5055+
ASSERT_EQUALS("\nF ; 1 ;", preprocess("#define F() 1\nF; F();\n", &macroUsage));
5056+
ASSERT_EQUALS("F 1:9 use 2:4\n", toString(macroUsage));
5057+
}
5058+
50185059
/** The argument of each macro usage as "name use line:col: [arg]", sorted. */
50195060
static std::string argToString(const std::list<simplecpp::MacroUsage> &macroUsage)
50205061
{
@@ -5064,9 +5105,9 @@ static void macroUsageArg3()
50645105

50655106
static void macroUsageArg4()
50665107
{
5067-
// object-like macro and function-like macro name without call have no argument
5108+
// object-like macro has no argument, function-like macro name without call is no usage
50685109
ASSERT_EQUALS("INT64_C use 2:1: []\n", macroUsageArg("#define INT64_C 1\nINT64_C\n"));
5069-
ASSERT_EQUALS("INT64_C use 2:1: []\n", macroUsageArg("#define INT64_C(x) x\nINT64_C;\n"));
5110+
ASSERT_EQUALS("", macroUsageArg("#define INT64_C(x) x\nINT64_C;\n"));
50705111
}
50715112

50725113
static void macroUsageArg5()
@@ -6243,6 +6284,9 @@ static void runTests(int argc, char **argv, Input input)
62436284
TEST_CASE(tokenMacro13);
62446285
TEST_CASE(tokenMacro14);
62456286
TEST_CASE(tokenMacro15);
6287+
TEST_CASE(tokenMacro16);
6288+
TEST_CASE(tokenMacro17);
6289+
TEST_CASE(tokenMacro18);
62466290
TEST_CASE(macroUsageArg1);
62476291
TEST_CASE(macroUsageArg2);
62486292
TEST_CASE(macroUsageArg3);

0 commit comments

Comments
 (0)