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
50 changes: 32 additions & 18 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3978,6 +3978,26 @@ static std::string getTimeDefine(const struct tm *timep)
return std::string("\"").append(buf).append("\"");
}

/** Report the usages of a macro. This is done when its definition goes away, so that #undef and redefinition do not lose them. */
static void addMacroUsage(const simplecpp::Macro &macro, std::map<std::string, std::list<simplecpp::Location>> &maybeUsedMacros, std::list<simplecpp::MacroUsage> *macroUsage)
{
if (!macroUsage)
return;
std::list<simplecpp::Location> usage = macro.usage();
const std::map<std::string, std::list<simplecpp::Location>>::iterator maybeUsed = maybeUsedMacros.find(macro.name());
if (maybeUsed != maybeUsedMacros.end()) {
usage.splice(usage.end(), maybeUsed->second);
maybeUsedMacros.erase(maybeUsed);
}
for (std::list<simplecpp::Location>::const_iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
simplecpp::MacroUsage mu(macro.valueDefinedInCode());
mu.macroName = macro.name();
mu.macroLocation = macro.defineLocation();
mu.useLocation = *usageIt;
macroUsage->emplace_back(std::move(mu));
}
}

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)
{
#ifdef SIMPLECPP_WINDOWS
Expand Down Expand Up @@ -4187,8 +4207,10 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
const MacroMap::iterator it = macros.find(macro.name());
if (it == macros.end())
macros.insert(std::pair<TokenString, Macro>(macro.name(), macro));
else
else {
addMacroUsage(it->second, maybeUsedMacros, macroUsage);
it->second = macro;
}
}
} catch (const std::runtime_error &err) {
if (outputList) {
Expand Down Expand Up @@ -4477,8 +4499,13 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
const Token *tok = rawtok->next;
while (sameline(rawtok,tok) && tok->comment)
tok = tok->next;
if (sameline(rawtok, tok))
macros.erase(tok->str());
if (sameline(rawtok, tok)) {
const MacroMap::iterator it = macros.find(tok->str());
if (it != macros.end()) {
addMacroUsage(it->second, maybeUsedMacros, macroUsage);
macros.erase(it);
}
}
}
} else if (ifstates.top() == True && rawtok->str() == PRAGMA && rawtok->next && rawtok->next->str() == ONCE && sameline(rawtok,rawtok->next)) {
pragmaOnce.insert(rawtokens.file(rawtok->location));
Expand Down Expand Up @@ -4530,21 +4557,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
}
}

if (macroUsage) {
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt) {
const Macro &macro = macroIt->second;
std::list<Location> usage = macro.usage();
const std::list<Location>& temp = maybeUsedMacros[macro.name()];
usage.insert(usage.end(), temp.begin(), temp.end());
for (std::list<Location>::const_iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
MacroUsage mu(macro.valueDefinedInCode());
mu.macroName = macro.name();
mu.macroLocation = macro.defineLocation();
mu.useLocation = *usageIt;
macroUsage->emplace_back(std::move(mu));
}
}
}
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt)
addMacroUsage(macroIt->second, maybeUsedMacros, macroUsage);
}

void simplecpp::cleanup(FileDataCache &cache)
Expand Down
44 changes: 44 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5489,6 +5489,47 @@ static void macroUsage()
}
}

static void macroUsageUndef()
{
const char code[] = "#define A 1\n"
"A\n"
"#undef A\n"
"A\n";
std::list<simplecpp::MacroUsage> macroUsage;
ASSERT_EQUALS("1@A(2:1) A(4:1)", expandedTokens(code, simplecpp::DUI(), &macroUsage));
ASSERT_EQUALS("A 1:9 use 2:1\n", toString(macroUsage));
}

static void macroUsageRedefine()
{
const char code[] = "#define A 1\n"
"A\n"
"#undef A\n"
"#define A 2\n"
"A\n"
"#define B 3\n"
"B\n"
"#define B 4\n"
"B\n";
std::list<simplecpp::MacroUsage> macroUsage;
ASSERT_EQUALS("1@A(2:1) 2@A(5:1) 3@B(7:1) 4@B(9:1)", expandedTokens(code, simplecpp::DUI(), &macroUsage));
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));
}

static void macroUsageUndefIfdef()
{
const char code[] = "#define A\n"
"#ifdef A\n"
"#endif\n"
"#undef A\n"
"#define A\n"
"#ifdef A\n"
"#endif\n";
std::list<simplecpp::MacroUsage> macroUsage;
ASSERT_EQUALS("", expandedTokens(code, simplecpp::DUI(), &macroUsage));
ASSERT_EQUALS("A 1:9 use 2:8\nA 5:9 use 6:8\n", toString(macroUsage));
}

static void isAbsolutePath() {
#ifdef _WIN32
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("C:\\foo\\bar"));
Expand Down Expand Up @@ -5905,6 +5946,9 @@ static void runTests(int argc, char **argv, Input input)

TEST_CASE(ifCond);
TEST_CASE(macroUsage);
TEST_CASE(macroUsageUndef);
TEST_CASE(macroUsageRedefine);
TEST_CASE(macroUsageUndefIfdef);

TEST_CASE(fuzz_crash);

Expand Down
Loading