Skip to content
Merged
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
13 changes: 8 additions & 5 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3935,14 +3935,15 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
const bool par = (tok && tok->op == '(');
if (par)
tok = tok->next;
maybeUsedMacros[rawtok->next->str()].emplace_back(rawtok->next->location);
if (tok) {
if (macros.find(tok->str()) != macros.end())
if (macros.find(tok->str()) != macros.end()) {
maybeUsedMacros[tok->str()].emplace_back(tok->location);
expr.push_back(new Token("1", tok->location));
else if (hasInclude && tok->str() == HAS_INCLUDE)
} else if (hasInclude && tok->str() == HAS_INCLUDE) {
expr.push_back(new Token("1", tok->location));
else
} else {
expr.push_back(new Token("0", tok->location));
Comment thread
danmar marked this conversation as resolved.
Resolved
}
}
if (par)
tok = tok ? tok->next : nullptr;
Expand Down Expand Up @@ -4004,7 +4005,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
continue;
}

maybeUsedMacros[rawtok->next->str()].emplace_back(rawtok->next->location);
// usage of defined macros is recorded when they are expanded
if (macros.find(tok->str()) == macros.end())
maybeUsedMacros[tok->str()].emplace_back(tok->location);

const Token *tmp = tok;
if (!preprocessToken(expr, tmp, macros, files, outputList, dui)) {
Expand Down
24 changes: 24 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4697,6 +4697,30 @@ static void macroUsage()
ASSERT_EQUALS(2, it->useLocation.line);
ASSERT_EQUALS(8, it->useLocation.col);
}
{
const char code[] = "#define X 1\n"
"#if defined(X)\n"
"#endif\n";
std::list<simplecpp::MacroUsage> macroUsage;
ASSERT_EQUALS("", preprocess(code, &macroUsage));
ASSERT_EQUALS(1, macroUsage.size());
auto it = macroUsage.cbegin();
ASSERT_EQUALS("X", it->macroName);
ASSERT_EQUALS(2, it->useLocation.line);
ASSERT_EQUALS(13, it->useLocation.col);
}
{
const char code[] = "#define X 1\n"
"#if X\n"
"#endif\n";
std::list<simplecpp::MacroUsage> macroUsage;
ASSERT_EQUALS("", preprocess(code, &macroUsage));
ASSERT_EQUALS(1, macroUsage.size());
auto it = macroUsage.cbegin();
ASSERT_EQUALS("X", it->macroName);
ASSERT_EQUALS(2, it->useLocation.line);
ASSERT_EQUALS(5, it->useLocation.col);
}
}

static void isAbsolutePath() {
Expand Down
Loading