Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
30 changes: 28 additions & 2 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4976,6 +4976,30 @@ 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<simplecpp::MacroUsage> macroUsage;
ASSERT_EQUALS("int(2:1) F(2:5) =(2:7) 1(2:9) ;(2:13)", expandedTokens(code, simplecpp::DUI(), &macroUsage));
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<simplecpp::MacroUsage> macroUsage;
preprocess("#define F(x) 1\n#if F\n#endif\n", &macroUsage);
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", &macroUsage);
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", &macroUsage);
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<simplecpp::MacroUsage> &macroUsage)
{
Expand Down Expand Up @@ -5025,9 +5049,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()
Expand Down Expand Up @@ -6146,6 +6170,8 @@ static void runTests(int argc, char **argv, Input input)
TEST_CASE(tokenMacro13);
TEST_CASE(tokenMacro14);
TEST_CASE(tokenMacro15);
TEST_CASE(tokenMacro16);
TEST_CASE(tokenMacro17);
TEST_CASE(macroUsageArg1);
TEST_CASE(macroUsageArg2);
TEST_CASE(macroUsageArg3);
Expand Down