Skip to content

Commit 0f0aba6

Browse files
authored
Report the usages of a macro that is undefined or redefined (#764)
1 parent 96e5990 commit 0f0aba6

2 files changed

Lines changed: 98 additions & 18 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4191,6 +4191,27 @@ static std::string getTimeDefine(const struct tm *timep)
41914191
return std::string("\"").append(buf).append("\"");
41924192
}
41934193

4194+
/** Report the usages of a macro. This is done when its definition goes away, so that #undef and redefinition do not lose them. */
4195+
static void addMacroUsage(const simplecpp::Macro &macro, std::map<std::string, std::list<simplecpp::Location>> &maybeUsedMacros, std::list<simplecpp::MacroUsage> *macroUsage)
4196+
{
4197+
if (!macroUsage)
4198+
return;
4199+
std::list<simplecpp::Macro::Usage> usage = macro.usage();
4200+
const std::map<std::string, std::list<simplecpp::Location>>::iterator maybeUsed = maybeUsedMacros.find(macro.name());
4201+
if (maybeUsed != maybeUsedMacros.end()) {
4202+
usage.insert(usage.end(), maybeUsed->second.begin(), maybeUsed->second.end());
4203+
maybeUsedMacros.erase(maybeUsed);
4204+
}
4205+
for (std::list<simplecpp::Macro::Usage>::iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
4206+
simplecpp::MacroUsage mu(macro.valueDefinedInCode());
4207+
mu.macroName = macro.name();
4208+
mu.macroLocation = macro.defineLocation();
4209+
mu.useLocation = usageIt->location;
4210+
mu.arg = std::move(usageIt->arg);
4211+
macroUsage->emplace_back(std::move(mu));
4212+
}
4213+
}
4214+
41944215
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)
41954216
{
41964217
#ifdef SIMPLECPP_WINDOWS
@@ -4334,6 +4355,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
43344355
}
43354356

43364357
std::map<std::string, std::list<Location>> maybeUsedMacros;
4358+
// usages of macros that are undefined or redefined, handed over only when preprocessing succeeds
4359+
std::list<MacroUsage> pendingUsage;
43374360

43384361
for (const Token *rawtok = nullptr; rawtok || !includetokenstack.empty();) {
43394362
if (rawtok == nullptr) {
@@ -4398,10 +4421,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
43984421
const Macro &macro = Macro(rawtok->previous, files);
43994422
if (dui.undefined.find(macro.name()) == dui.undefined.end()) {
44004423
const MacroMap::iterator it = macros.find(macro.name());
4401-
if (it == macros.end())
4424+
if (it == macros.end()) {
44024425
macros.insert(std::pair<TokenString, Macro>(macro.name(), macro));
4403-
else
4426+
} else {
4427+
addMacroUsage(it->second, maybeUsedMacros, macroUsage ? &pendingUsage : nullptr);
44044428
it->second = macro;
4429+
}
44054430
}
44064431
} catch (const std::runtime_error &err) {
44074432
if (outputList) {
@@ -4690,8 +4715,13 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
46904715
const Token *tok = rawtok->next;
46914716
while (sameline(rawtok,tok) && tok->comment)
46924717
tok = tok->next;
4693-
if (sameline(rawtok, tok))
4694-
macros.erase(tok->str());
4718+
if (sameline(rawtok, tok)) {
4719+
const MacroMap::iterator it = macros.find(tok->str());
4720+
if (it != macros.end()) {
4721+
addMacroUsage(it->second, maybeUsedMacros, macroUsage ? &pendingUsage : nullptr);
4722+
macros.erase(it);
4723+
}
4724+
}
46954725
}
46964726
} else if (ifstates.top() == True && rawtok->str() == PRAGMA && rawtok->next && rawtok->next->str() == ONCE && sameline(rawtok,rawtok->next)) {
46974727
pragmaOnce.insert(rawtokens.file(rawtok->location));
@@ -4744,20 +4774,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
47444774
}
47454775

47464776
if (macroUsage) {
4747-
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt) {
4748-
const Macro &macro = macroIt->second;
4749-
std::list<Macro::Usage> usage = macro.usage();
4750-
const std::list<Location>& temp = maybeUsedMacros[macro.name()];
4751-
usage.insert(usage.end(), temp.begin(), temp.end());
4752-
for (std::list<Macro::Usage>::iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
4753-
MacroUsage mu(macro.valueDefinedInCode());
4754-
mu.macroName = macro.name();
4755-
mu.macroLocation = macro.defineLocation();
4756-
mu.useLocation = usageIt->location;
4757-
mu.arg = std::move(usageIt->arg);
4758-
macroUsage->emplace_back(std::move(mu));
4759-
}
4760-
}
4777+
macroUsage->splice(macroUsage->end(), pendingUsage);
4778+
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt)
4779+
addMacroUsage(macroIt->second, maybeUsedMacros, macroUsage);
47614780
}
47624781
}
47634782

‎test.cpp‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5809,6 +5809,63 @@ static void macroUsage()
58095809
}
58105810
}
58115811

5812+
static void macroUsageUndef()
5813+
{
5814+
const char code[] = "#define A 1\n"
5815+
"A\n"
5816+
"#undef A\n"
5817+
"A\n";
5818+
std::list<simplecpp::MacroUsage> macroUsage;
5819+
ASSERT_EQUALS("1@A(2:1) A(4:1)", expandedTokens(code, simplecpp::DUI(), &macroUsage));
5820+
ASSERT_EQUALS("A 1:9 use 2:1\n", toString(macroUsage));
5821+
5822+
// the argument of an integer constant macro is kept
5823+
ASSERT_EQUALS("UINT32_C use 2:1: [5]\n", macroUsageArg("#define UINT32_C(x) x\nUINT32_C(5)\n#undef UINT32_C\n"));
5824+
}
5825+
5826+
static void macroUsageUndefError()
5827+
{
5828+
// no usages are reported when preprocessing fails
5829+
const char code[] = "#define A 1\n"
5830+
"A\n"
5831+
"#undef A\n"
5832+
"#error stop\n";
5833+
std::list<simplecpp::MacroUsage> macroUsage;
5834+
simplecpp::OutputList outputList;
5835+
preprocess(code, simplecpp::DUI(), &outputList, &macroUsage);
5836+
ASSERT_EQUALS("", toString(macroUsage));
5837+
}
5838+
5839+
static void macroUsageRedefine()
5840+
{
5841+
const char code[] = "#define A 1\n"
5842+
"A\n"
5843+
"#undef A\n"
5844+
"#define A 2\n"
5845+
"A\n"
5846+
"#define B 3\n"
5847+
"B\n"
5848+
"#define B 4\n"
5849+
"B\n";
5850+
std::list<simplecpp::MacroUsage> macroUsage;
5851+
ASSERT_EQUALS("1@A(2:1) 2@A(5:1) 3@B(7:1) 4@B(9:1)", expandedTokens(code, simplecpp::DUI(), &macroUsage));
5852+
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));
5853+
}
5854+
5855+
static void macroUsageUndefIfdef()
5856+
{
5857+
const char code[] = "#define A\n"
5858+
"#ifdef A\n"
5859+
"#endif\n"
5860+
"#undef A\n"
5861+
"#define A\n"
5862+
"#ifdef A\n"
5863+
"#endif\n";
5864+
std::list<simplecpp::MacroUsage> macroUsage;
5865+
ASSERT_EQUALS("", expandedTokens(code, simplecpp::DUI(), &macroUsage));
5866+
ASSERT_EQUALS("A 1:9 use 2:8\nA 5:9 use 6:8\n", toString(macroUsage));
5867+
}
5868+
58125869
static void isAbsolutePath() {
58135870
#ifdef _WIN32
58145871
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("C:\\foo\\bar"));
@@ -6234,6 +6291,10 @@ static void runTests(int argc, char **argv, Input input)
62346291

62356292
TEST_CASE(ifCond);
62366293
TEST_CASE(macroUsage);
6294+
TEST_CASE(macroUsageUndef);
6295+
TEST_CASE(macroUsageUndefError);
6296+
TEST_CASE(macroUsageRedefine);
6297+
TEST_CASE(macroUsageUndefIfdef);
62376298

62386299
TEST_CASE(fuzz_crash);
62396300

0 commit comments

Comments
 (0)