Skip to content

Commit e377a41

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 31989dd commit e377a41

2 files changed

Lines changed: 76 additions & 18 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3978,6 +3978,26 @@ static std::string getTimeDefine(const struct tm *timep)
39783978
return std::string("\"").append(buf).append("\"");
39793979
}
39803980

3981+
/** Report the usages of a macro. This is done when its definition goes away, so that #undef and redefinition do not lose them. */
3982+
static void addMacroUsage(const simplecpp::Macro &macro, std::map<std::string, std::list<simplecpp::Location>> &maybeUsedMacros, std::list<simplecpp::MacroUsage> *macroUsage)
3983+
{
3984+
if (!macroUsage)
3985+
return;
3986+
std::list<simplecpp::Location> usage = macro.usage();
3987+
const std::map<std::string, std::list<simplecpp::Location>>::iterator maybeUsed = maybeUsedMacros.find(macro.name());
3988+
if (maybeUsed != maybeUsedMacros.end()) {
3989+
usage.splice(usage.end(), maybeUsed->second);
3990+
maybeUsedMacros.erase(maybeUsed);
3991+
}
3992+
for (std::list<simplecpp::Location>::const_iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
3993+
simplecpp::MacroUsage mu(macro.valueDefinedInCode());
3994+
mu.macroName = macro.name();
3995+
mu.macroLocation = macro.defineLocation();
3996+
mu.useLocation = *usageIt;
3997+
macroUsage->emplace_back(std::move(mu));
3998+
}
3999+
}
4000+
39814001
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)
39824002
{
39834003
#ifdef SIMPLECPP_WINDOWS
@@ -4187,8 +4207,10 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
41874207
const MacroMap::iterator it = macros.find(macro.name());
41884208
if (it == macros.end())
41894209
macros.insert(std::pair<TokenString, Macro>(macro.name(), macro));
4190-
else
4210+
else {
4211+
addMacroUsage(it->second, maybeUsedMacros, macroUsage);
41914212
it->second = macro;
4213+
}
41924214
}
41934215
} catch (const std::runtime_error &err) {
41944216
if (outputList) {
@@ -4477,8 +4499,13 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
44774499
const Token *tok = rawtok->next;
44784500
while (sameline(rawtok,tok) && tok->comment)
44794501
tok = tok->next;
4480-
if (sameline(rawtok, tok))
4481-
macros.erase(tok->str());
4502+
if (sameline(rawtok, tok)) {
4503+
const MacroMap::iterator it = macros.find(tok->str());
4504+
if (it != macros.end()) {
4505+
addMacroUsage(it->second, maybeUsedMacros, macroUsage);
4506+
macros.erase(it);
4507+
}
4508+
}
44824509
}
44834510
} else if (ifstates.top() == True && rawtok->str() == PRAGMA && rawtok->next && rawtok->next->str() == ONCE && sameline(rawtok,rawtok->next)) {
44844511
pragmaOnce.insert(rawtokens.file(rawtok->location));
@@ -4530,21 +4557,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
45304557
}
45314558
}
45324559

4533-
if (macroUsage) {
4534-
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt) {
4535-
const Macro &macro = macroIt->second;
4536-
std::list<Location> usage = macro.usage();
4537-
const std::list<Location>& temp = maybeUsedMacros[macro.name()];
4538-
usage.insert(usage.end(), temp.begin(), temp.end());
4539-
for (std::list<Location>::const_iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
4540-
MacroUsage mu(macro.valueDefinedInCode());
4541-
mu.macroName = macro.name();
4542-
mu.macroLocation = macro.defineLocation();
4543-
mu.useLocation = *usageIt;
4544-
macroUsage->emplace_back(std::move(mu));
4545-
}
4546-
}
4547-
}
4560+
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt)
4561+
addMacroUsage(macroIt->second, maybeUsedMacros, macroUsage);
45484562
}
45494563

45504564
void simplecpp::cleanup(FileDataCache &cache)

‎test.cpp‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5489,6 +5489,47 @@ static void macroUsage()
54895489
}
54905490
}
54915491

5492+
static void macroUsageUndef()
5493+
{
5494+
const char code[] = "#define A 1\n"
5495+
"A\n"
5496+
"#undef A\n"
5497+
"A\n";
5498+
std::list<simplecpp::MacroUsage> macroUsage;
5499+
ASSERT_EQUALS("1@A(2:1) A(4:1)", expandedTokens(code, simplecpp::DUI(), &macroUsage));
5500+
ASSERT_EQUALS("A 1:9 use 2:1\n", toString(macroUsage));
5501+
}
5502+
5503+
static void macroUsageRedefine()
5504+
{
5505+
const char code[] = "#define A 1\n"
5506+
"A\n"
5507+
"#undef A\n"
5508+
"#define A 2\n"
5509+
"A\n"
5510+
"#define B 3\n"
5511+
"B\n"
5512+
"#define B 4\n"
5513+
"B\n";
5514+
std::list<simplecpp::MacroUsage> macroUsage;
5515+
ASSERT_EQUALS("1@A(2:1) 2@A(5:1) 3@B(7:1) 4@B(9:1)", expandedTokens(code, simplecpp::DUI(), &macroUsage));
5516+
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));
5517+
}
5518+
5519+
static void macroUsageUndefIfdef()
5520+
{
5521+
const char code[] = "#define A\n"
5522+
"#ifdef A\n"
5523+
"#endif\n"
5524+
"#undef A\n"
5525+
"#define A\n"
5526+
"#ifdef A\n"
5527+
"#endif\n";
5528+
std::list<simplecpp::MacroUsage> macroUsage;
5529+
ASSERT_EQUALS("", expandedTokens(code, simplecpp::DUI(), &macroUsage));
5530+
ASSERT_EQUALS("A 1:9 use 2:8\nA 5:9 use 6:8\n", toString(macroUsage));
5531+
}
5532+
54925533
static void isAbsolutePath() {
54935534
#ifdef _WIN32
54945535
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("C:\\foo\\bar"));
@@ -5905,6 +5946,9 @@ static void runTests(int argc, char **argv, Input input)
59055946

59065947
TEST_CASE(ifCond);
59075948
TEST_CASE(macroUsage);
5949+
TEST_CASE(macroUsageUndef);
5950+
TEST_CASE(macroUsageRedefine);
5951+
TEST_CASE(macroUsageUndefIfdef);
59085952

59095953
TEST_CASE(fuzz_crash);
59105954

0 commit comments

Comments
 (0)