Skip to content

Commit 5797017

Browse files
committed
Report no macro usages when preprocessing fails
The usages of a macro that is undefined or redefined were added to the caller's list right away, so an error later in the file left that list partly filled, where before it stayed empty. Keep them in a local list and hand it over only when preprocessing succeeds.
1 parent a5ca9c6 commit 5797017

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4352,6 +4352,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
43524352
}
43534353

43544354
std::map<std::string, std::list<Location>> maybeUsedMacros;
4355+
// usages of macros that are undefined or redefined, handed over only when preprocessing succeeds
4356+
std::list<MacroUsage> pendingUsage;
43554357

43564358
for (const Token *rawtok = nullptr; rawtok || !includetokenstack.empty();) {
43574359
if (rawtok == nullptr) {
@@ -4419,7 +4421,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
44194421
if (it == macros.end()) {
44204422
macros.insert(std::pair<TokenString, Macro>(macro.name(), macro));
44214423
} else {
4422-
addMacroUsage(it->second, maybeUsedMacros, macroUsage);
4424+
addMacroUsage(it->second, maybeUsedMacros, macroUsage ? &pendingUsage : nullptr);
44234425
it->second = macro;
44244426
}
44254427
}
@@ -4713,7 +4715,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
47134715
if (sameline(rawtok, tok)) {
47144716
const MacroMap::iterator it = macros.find(tok->str());
47154717
if (it != macros.end()) {
4716-
addMacroUsage(it->second, maybeUsedMacros, macroUsage);
4718+
addMacroUsage(it->second, maybeUsedMacros, macroUsage ? &pendingUsage : nullptr);
47174719
macros.erase(it);
47184720
}
47194721
}
@@ -4768,8 +4770,11 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
47684770
}
47694771
}
47704772

4771-
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt)
4772-
addMacroUsage(macroIt->second, maybeUsedMacros, macroUsage);
4773+
if (macroUsage) {
4774+
macroUsage->splice(macroUsage->end(), pendingUsage);
4775+
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt)
4776+
addMacroUsage(macroIt->second, maybeUsedMacros, macroUsage);
4777+
}
47734778
}
47744779

47754780
void simplecpp::cleanup(FileDataCache &cache)

‎test.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5784,6 +5784,19 @@ static void macroUsageUndef()
57845784
ASSERT_EQUALS("UINT32_C use 2:1: [5]\n", macroUsageArg("#define UINT32_C(x) x\nUINT32_C(5)\n#undef UINT32_C\n"));
57855785
}
57865786

5787+
static void macroUsageUndefError()
5788+
{
5789+
// no usages are reported when preprocessing fails
5790+
const char code[] = "#define A 1\n"
5791+
"A\n"
5792+
"#undef A\n"
5793+
"#error stop\n";
5794+
std::list<simplecpp::MacroUsage> macroUsage;
5795+
simplecpp::OutputList outputList;
5796+
preprocess(code, simplecpp::DUI(), &outputList, &macroUsage);
5797+
ASSERT_EQUALS("", toString(macroUsage));
5798+
}
5799+
57875800
static void macroUsageRedefine()
57885801
{
57895802
const char code[] = "#define A 1\n"
@@ -6239,6 +6252,7 @@ static void runTests(int argc, char **argv, Input input)
62396252
TEST_CASE(ifCond);
62406253
TEST_CASE(macroUsage);
62416254
TEST_CASE(macroUsageUndef);
6255+
TEST_CASE(macroUsageUndefError);
62426256
TEST_CASE(macroUsageRedefine);
62436257
TEST_CASE(macroUsageUndefIfdef);
62446258

0 commit comments

Comments
 (0)