Skip to content

Commit a5ca9c6

Browse files
committed
Report the usages of a macro that is undefined or redefined
preprocess() built the macro usage list from the macros still defined when preprocessing ended, so the usages of a macro were lost after #undef, and after a redefinition the usages of the first definition were lost. An #ifdef before the #undef was reported against the later definition. The usages are now reported when the definition goes away.
1 parent e5cb618 commit a5ca9c6

2 files changed

Lines changed: 81 additions & 20 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4188,6 +4188,27 @@ static std::string getTimeDefine(const struct tm *timep)
41884188
return std::string("\"").append(buf).append("\"");
41894189
}
41904190

4191+
/** Report the usages of a macro. This is done when its definition goes away, so that #undef and redefinition do not lose them. */
4192+
static void addMacroUsage(const simplecpp::Macro &macro, std::map<std::string, std::list<simplecpp::Location>> &maybeUsedMacros, std::list<simplecpp::MacroUsage> *macroUsage)
4193+
{
4194+
if (!macroUsage)
4195+
return;
4196+
std::list<simplecpp::Macro::Usage> usage = macro.usage();
4197+
const std::map<std::string, std::list<simplecpp::Location>>::iterator maybeUsed = maybeUsedMacros.find(macro.name());
4198+
if (maybeUsed != maybeUsedMacros.end()) {
4199+
usage.insert(usage.end(), maybeUsed->second.begin(), maybeUsed->second.end());
4200+
maybeUsedMacros.erase(maybeUsed);
4201+
}
4202+
for (std::list<simplecpp::Macro::Usage>::iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
4203+
simplecpp::MacroUsage mu(macro.valueDefinedInCode());
4204+
mu.macroName = macro.name();
4205+
mu.macroLocation = macro.defineLocation();
4206+
mu.useLocation = usageIt->location;
4207+
mu.arg = std::move(usageIt->arg);
4208+
macroUsage->emplace_back(std::move(mu));
4209+
}
4210+
}
4211+
41914212
void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenList &rawtokens, std::vector<std::string> &files, simplecpp::FileDataCache &cache, const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage, std::list<simplecpp::IfCond> *ifCond)
41924213
{
41934214
#ifdef SIMPLECPP_WINDOWS
@@ -4395,10 +4416,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
43954416
const Macro &macro = Macro(rawtok->previous, files);
43964417
if (dui.undefined.find(macro.name()) == dui.undefined.end()) {
43974418
const MacroMap::iterator it = macros.find(macro.name());
4398-
if (it == macros.end())
4419+
if (it == macros.end()) {
43994420
macros.insert(std::pair<TokenString, Macro>(macro.name(), macro));
4400-
else
4421+
} else {
4422+
addMacroUsage(it->second, maybeUsedMacros, macroUsage);
44014423
it->second = macro;
4424+
}
44024425
}
44034426
} catch (const std::runtime_error &err) {
44044427
if (outputList) {
@@ -4687,8 +4710,13 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
46874710
const Token *tok = rawtok->next;
46884711
while (sameline(rawtok,tok) && tok->comment)
46894712
tok = tok->next;
4690-
if (sameline(rawtok, tok))
4691-
macros.erase(tok->str());
4713+
if (sameline(rawtok, tok)) {
4714+
const MacroMap::iterator it = macros.find(tok->str());
4715+
if (it != macros.end()) {
4716+
addMacroUsage(it->second, maybeUsedMacros, macroUsage);
4717+
macros.erase(it);
4718+
}
4719+
}
46924720
}
46934721
} else if (ifstates.top() == True && rawtok->str() == PRAGMA && rawtok->next && rawtok->next->str() == ONCE && sameline(rawtok,rawtok->next)) {
46944722
pragmaOnce.insert(rawtokens.file(rawtok->location));
@@ -4740,22 +4768,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
47404768
}
47414769
}
47424770

4743-
if (macroUsage) {
4744-
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt) {
4745-
const Macro &macro = macroIt->second;
4746-
std::list<Macro::Usage> usage = macro.usage();
4747-
const std::list<Location>& temp = maybeUsedMacros[macro.name()];
4748-
usage.insert(usage.end(), temp.begin(), temp.end());
4749-
for (std::list<Macro::Usage>::iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
4750-
MacroUsage mu(macro.valueDefinedInCode());
4751-
mu.macroName = macro.name();
4752-
mu.macroLocation = macro.defineLocation();
4753-
mu.useLocation = usageIt->location;
4754-
mu.arg = std::move(usageIt->arg);
4755-
macroUsage->emplace_back(std::move(mu));
4756-
}
4757-
}
4758-
}
4771+
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt)
4772+
addMacroUsage(macroIt->second, maybeUsedMacros, macroUsage);
47594773
}
47604774

47614775
void simplecpp::cleanup(FileDataCache &cache)

‎test.cpp‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5770,6 +5770,50 @@ static void macroUsage()
57705770
}
57715771
}
57725772

5773+
static void macroUsageUndef()
5774+
{
5775+
const char code[] = "#define A 1\n"
5776+
"A\n"
5777+
"#undef A\n"
5778+
"A\n";
5779+
std::list<simplecpp::MacroUsage> macroUsage;
5780+
ASSERT_EQUALS("1@A(2:1) A(4:1)", expandedTokens(code, simplecpp::DUI(), &macroUsage));
5781+
ASSERT_EQUALS("A 1:9 use 2:1\n", toString(macroUsage));
5782+
5783+
// the argument of an integer constant macro is kept
5784+
ASSERT_EQUALS("UINT32_C use 2:1: [5]\n", macroUsageArg("#define UINT32_C(x) x\nUINT32_C(5)\n#undef UINT32_C\n"));
5785+
}
5786+
5787+
static void macroUsageRedefine()
5788+
{
5789+
const char code[] = "#define A 1\n"
5790+
"A\n"
5791+
"#undef A\n"
5792+
"#define A 2\n"
5793+
"A\n"
5794+
"#define B 3\n"
5795+
"B\n"
5796+
"#define B 4\n"
5797+
"B\n";
5798+
std::list<simplecpp::MacroUsage> macroUsage;
5799+
ASSERT_EQUALS("1@A(2:1) 2@A(5:1) 3@B(7:1) 4@B(9:1)", expandedTokens(code, simplecpp::DUI(), &macroUsage));
5800+
ASSERT_EQUALS("A 1:9 use 2:1\nA 4:9 use 5:1\nB 6:9 use 7:1\nB 8:9 use 9:1\n", toString(macroUsage));
5801+
}
5802+
5803+
static void macroUsageUndefIfdef()
5804+
{
5805+
const char code[] = "#define A\n"
5806+
"#ifdef A\n"
5807+
"#endif\n"
5808+
"#undef A\n"
5809+
"#define A\n"
5810+
"#ifdef A\n"
5811+
"#endif\n";
5812+
std::list<simplecpp::MacroUsage> macroUsage;
5813+
ASSERT_EQUALS("", expandedTokens(code, simplecpp::DUI(), &macroUsage));
5814+
ASSERT_EQUALS("A 1:9 use 2:8\nA 5:9 use 6:8\n", toString(macroUsage));
5815+
}
5816+
57735817
static void isAbsolutePath() {
57745818
#ifdef _WIN32
57755819
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("C:\\foo\\bar"));
@@ -6194,6 +6238,9 @@ static void runTests(int argc, char **argv, Input input)
61946238

61956239
TEST_CASE(ifCond);
61966240
TEST_CASE(macroUsage);
6241+
TEST_CASE(macroUsageUndef);
6242+
TEST_CASE(macroUsageRedefine);
6243+
TEST_CASE(macroUsageUndefIfdef);
61976244

61986245
TEST_CASE(fuzz_crash);
61996246

0 commit comments

Comments
 (0)