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
14 changes: 3 additions & 11 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,15 @@ class CppCheck::CppCheckLogger : public ErrorLogger
void setLocationMacros(const Token* startTok, const std::vector<std::string>& files, const std::list<simplecpp::MacroUsage>& macroUsage)
{
mLocationMacros.clear();
std::set<std::string> usedNames;
std::transform(macroUsage.cbegin(), macroUsage.cend(), std::inserter(usedNames, usedNames.end()), [](const simplecpp::MacroUsage& usage) {
return usage.macroName;
});
std::set<std::pair<Location, int>> unexpanded;
for (const Token* tok = startTok; tok; tok = tok->next()) {
const Location loc(files[tok->fileIndex()], tok->linenr());
if (!tok->getMacroName().empty())
mLocationMacros[loc].emplace(tok->getMacroName());
else if (tok->isName() && usedNames.count(tok->str()) != 0)
unexpanded.emplace(loc, tok->column());
mLocationMacros[Location(files[tok->fileIndex()], tok->linenr())].emplace(tok->getMacroName());
}
// a token only knows the innermost macro it was expanded from; usages that produced no tokens are skipped
// a token only knows the innermost macro it was expanded from; usages on lines without macro-expanded tokens are skipped
for (const simplecpp::MacroUsage& usage : macroUsage) {
const Location loc(files[usage.useLocation.fileIndex], usage.useLocation.line);
const auto it = mLocationMacros.find(loc);
if (it != mLocationMacros.end() && unexpanded.count({loc, usage.useLocation.col}) == 0)
if (it != mLocationMacros.end())
it->second.emplace(usage.macroName);
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,18 @@ class TestSuppressions : public TestFixture {
ASSERT_EQUALS("[test.cpp:4:18]: (error) Division by zero. [zerodiv]\n"
"[test.cpp:2:1]: (information) Unmatched suppression: zerodiv [unmatchedSuppression]\n", errout_str());

// function-like macro name that is not expanded, on a line where another macro is
ASSERT_EQUALS(1,
(this->*check)("#define DIV(A,B) A/B\n"
"// cppcheck-suppress-macro zerodiv\n"
"#define ALIAS(A,B) A/B\n"
"void f() {\n"
" int ALIAS = DIV(1,0);\n"
"}\n",
""));
ASSERT_EQUALS("[test.cpp:5:17]: (error) Division by zero. [zerodiv]\n"
"[test.cpp:3:1]: (information) Unmatched suppression: zerodiv [unmatchedSuppression]\n", errout_str());

// empty macro on the line
ASSERT_EQUALS(1,
(this->*check)("// cppcheck-suppress-macro zerodiv\n"
Expand Down
Loading