From b277a55d67e6a47d56c3a283a7baf262a48ef995 Mon Sep 17 00:00:00 2001 From: Jay Beavers Date: Sat, 21 Nov 2026 05:57:34 -0800 Subject: [PATCH] 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 (danmar/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. --- lib/cppcheck.cpp | 14 +++----------- test/testsuppressions.cpp | 12 ++++++++++++ 2 files changed, 15 insertions(+), 11 deletions(-) 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"