Skip to content

Commit b277a55

Browse files
committed
Remove the unexpanded macro name filter from setLocationMacros
The filter skipped a macro usage whose location held the bare name of a function-like macro, because simplecpp recorded a usage there although nothing was expanded. simplecpp 1.10.2 no longer records it (cppcheck-opensource/simplecpp#759), so the filter has nothing left to do. The existing test for that case passes with or without the filter, as no other macro is expanded on its line. The new test puts DIV(1,0) on the same line as a bare ALIAS, and fails if simplecpp records the bare name and the filter is gone. The comment on the usage loop now says what it skips: usages on lines without macro-expanded tokens.
1 parent 3074865 commit b277a55

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)