Skip to content

Commit 7ce8186

Browse files
authored
Fix #15246 (Remove the unexpanded macro name filter from setLocationMacros) (#9056)
1 parent 3074865 commit 7ce8186

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

‎lib/cppcheck.cpp‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,15 @@ class CppCheck::CppCheckLogger : public ErrorLogger
110110
void setLocationMacros(const Token* startTok, const std::vector<std::string>& files, const std::list<simplecpp::MacroUsage>& macroUsage)
111111
{
112112
mLocationMacros.clear();
113-
std::set<std::string> usedNames;
114-
std::transform(macroUsage.cbegin(), macroUsage.cend(), std::inserter(usedNames, usedNames.end()), [](const simplecpp::MacroUsage& usage) {
115-
return usage.macroName;
116-
});
117-
std::set<std::pair<Location, int>> unexpanded;
118113
for (const Token* tok = startTok; tok; tok = tok->next()) {
119-
const Location loc(files[tok->fileIndex()], tok->linenr());
120114
if (!tok->getMacroName().empty())
121-
mLocationMacros[loc].emplace(tok->getMacroName());
122-
else if (tok->isName() && usedNames.count(tok->str()) != 0)
123-
unexpanded.emplace(loc, tok->column());
115+
mLocationMacros[Location(files[tok->fileIndex()], tok->linenr())].emplace(tok->getMacroName());
124116
}
125-
// a token only knows the innermost macro it was expanded from; usages that produced no tokens are skipped
117+
// a token only knows the innermost macro it was expanded from; usages on lines without macro-expanded tokens are skipped
126118
for (const simplecpp::MacroUsage& usage : macroUsage) {
127119
const Location loc(files[usage.useLocation.fileIndex], usage.useLocation.line);
128120
const auto it = mLocationMacros.find(loc);
129-
if (it != mLocationMacros.end() && unexpanded.count({loc, usage.useLocation.col}) == 0)
121+
if (it != mLocationMacros.end())
130122
it->second.emplace(usage.macroName);
131123
}
132124
}

‎test/testsuppressions.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,18 @@ class TestSuppressions : public TestFixture {
10751075
ASSERT_EQUALS("[test.cpp:4:18]: (error) Division by zero. [zerodiv]\n"
10761076
"[test.cpp:2:1]: (information) Unmatched suppression: zerodiv [unmatchedSuppression]\n", errout_str());
10771077

1078+
// function-like macro name that is not expanded, on a line where another macro is
1079+
ASSERT_EQUALS(1,
1080+
(this->*check)("#define DIV(A,B) A/B\n"
1081+
"// cppcheck-suppress-macro zerodiv\n"
1082+
"#define ALIAS(A,B) A/B\n"
1083+
"void f() {\n"
1084+
" int ALIAS = DIV(1,0);\n"
1085+
"}\n",
1086+
""));
1087+
ASSERT_EQUALS("[test.cpp:5:17]: (error) Division by zero. [zerodiv]\n"
1088+
"[test.cpp:3:1]: (information) Unmatched suppression: zerodiv [unmatchedSuppression]\n", errout_str());
1089+
10781090
// empty macro on the line
10791091
ASSERT_EQUALS(1,
10801092
(this->*check)("// cppcheck-suppress-macro zerodiv\n"

0 commit comments

Comments
 (0)