From 7e57597e6e314ecdc33c079a95783258884bfbce Mon Sep 17 00:00:00 2001 From: Jay Beavers Date: Thu, 12 Nov 2026 20:06:54 -0800 Subject: [PATCH 1/2] 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. --- simplecpp.cpp | 12 ++++++------ test.cpp | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 36daf92a..f4bc2943 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -2395,6 +2395,12 @@ namespace simplecpp { std::cout << " expand " << name() << " " << locstring(defineLocation()) << std::endl; #endif + // No arguments => not macro expansion + if (functionLike() && nameTokInst->next && nameTokInst->next->op != '(') { + output.push_back(new Token(nameTokInst->str(), loc)); + return nameTokInst->next; + } + usageList.emplace_back(loc); if (expandBuiltin(output, loc, nameTokInst)) @@ -2403,12 +2409,6 @@ namespace simplecpp { const bool calledInDefine = (loc.fileIndex != nameTokInst->location.fileIndex || loc.line < nameTokInst->location.line); - // No arguments => not macro expansion - if (functionLike() && nameTokInst->next && nameTokInst->next->op != '(') { - output.push_back(new Token(nameTokInst->str(), loc)); - return nameTokInst->next; - } - if (functionLike() && nameTokInst->next && nameTokInst->next->op == '(' && isIntegerConstantMacro(name())) usageList.back().arg = writtenArgument(nameTokInst, calledInDefine); diff --git a/test.cpp b/test.cpp index f100175f..3c25ece7 100644 --- a/test.cpp +++ b/test.cpp @@ -4976,6 +4976,16 @@ static void tokenMacro15() ASSERT_EQUALS("D 1:1 use 1:3 unknown\n", toString(macroUsage)); } +static void tokenMacro16() +{ + // a function-like macro name without ( is not expanded, so it is not a usage + const char code[] = "#define F(x) x\n" + "int F = F(1);\n"; + std::list macroUsage; + ASSERT_EQUALS("int(2:1) F(2:5) =(2:7) 1(2:9) ;(2:13)", expandedTokens(code, simplecpp::DUI(), ¯oUsage)); + ASSERT_EQUALS("F 1:9 use 2:9\n", toString(macroUsage)); +} + /** The argument of each macro usage as "name use line:col: [arg]", sorted. */ static std::string argToString(const std::list ¯oUsage) { @@ -5025,9 +5035,9 @@ static void macroUsageArg3() static void macroUsageArg4() { - // object-like macro and function-like macro name without call have no argument + // object-like macro has no argument, function-like macro name without call is no usage ASSERT_EQUALS("INT64_C use 2:1: []\n", macroUsageArg("#define INT64_C 1\nINT64_C\n")); - ASSERT_EQUALS("INT64_C use 2:1: []\n", macroUsageArg("#define INT64_C(x) x\nINT64_C;\n")); + ASSERT_EQUALS("", macroUsageArg("#define INT64_C(x) x\nINT64_C;\n")); } static void macroUsageArg5() @@ -6146,6 +6156,7 @@ static void runTests(int argc, char **argv, Input input) TEST_CASE(tokenMacro13); TEST_CASE(tokenMacro14); TEST_CASE(tokenMacro15); + TEST_CASE(tokenMacro16); TEST_CASE(macroUsageArg1); TEST_CASE(macroUsageArg2); TEST_CASE(macroUsageArg3); From 5beedaf07abf2ee9bda3fad4469b0e6d36e6616d Mon Sep 17 00:00:00 2001 From: Jay Beavers Date: Thu, 19 Nov 2026 10:01:05 -0800 Subject: [PATCH 2/2] 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. --- simplecpp.cpp | 6 ++++-- test.cpp | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index f4bc2943..8f1bdc7f 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -4617,8 +4617,10 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL continue; } - // usage of defined macros is recorded when they are expanded - if (macros.find(tok->str()) == macros.end()) + // usage of defined macros is recorded when they are expanded, + // but a function-like macro without ( is not expanded + const MacroMap::const_iterator macroIt = macros.find(tok->str()); + if (macroIt == macros.end() || (macroIt->second.functionLike() && tok->next && tok->next->op != '(')) maybeUsedMacros[tok->str()].emplace_back(tok->location); const Token *tmp = tok; diff --git a/test.cpp b/test.cpp index 3c25ece7..06d72bf4 100644 --- a/test.cpp +++ b/test.cpp @@ -4986,6 +4986,20 @@ static void tokenMacro16() ASSERT_EQUALS("F 1:9 use 2:9\n", toString(macroUsage)); } +static void tokenMacro17() +{ + // a function-like macro name without ( in #if is a usage, also when the macro is defined before + std::list macroUsage; + preprocess("#define F(x) 1\n#if F\n#endif\n", ¯oUsage); + ASSERT_EQUALS("F 1:9 use 2:5\n", toString(macroUsage)); + macroUsage.clear(); + preprocess("#define F(x) 1\n#if 0\n#elif F || 1\n#endif\n", ¯oUsage); + ASSERT_EQUALS("F 1:9 use 3:7\n", toString(macroUsage)); + macroUsage.clear(); + preprocess("#define F(x) 1\n#if F(0)\n#endif\n", ¯oUsage); + ASSERT_EQUALS("F 1:9 use 2:5\n", toString(macroUsage)); +} + /** The argument of each macro usage as "name use line:col: [arg]", sorted. */ static std::string argToString(const std::list ¯oUsage) { @@ -6157,6 +6171,7 @@ static void runTests(int argc, char **argv, Input input) TEST_CASE(tokenMacro14); TEST_CASE(tokenMacro15); TEST_CASE(tokenMacro16); + TEST_CASE(tokenMacro17); TEST_CASE(macroUsageArg1); TEST_CASE(macroUsageArg2); TEST_CASE(macroUsageArg3);