diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 371e531dd4b..8fac158ec51 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -110,23 +110,15 @@ class CppCheck::CppCheckLogger : public ErrorLogger void setLocationMacros(const Token* startTok, const std::vector& files, const std::list& macroUsage) { mLocationMacros.clear(); - std::set usedNames; - std::transform(macroUsage.cbegin(), macroUsage.cend(), std::inserter(usedNames, usedNames.end()), [](const simplecpp::MacroUsage& usage) { - return usage.macroName; - }); - std::set> 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); } } diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index 48054b44a65..6400ff24bf3 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -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"