Skip to content
Open
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ Jakub Melka
Jan Egil Ruud
Jan Hellwig
János Maros
Jay Beavers
Jay Sigbrandt
Jean-François Deverge
Jedrzej Klocek
Expand Down
21 changes: 18 additions & 3 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,27 @@ class CppCheck::CppCheckLogger : public ErrorLogger
mRemarkComments.insert(mRemarkComments.end(), remarkComments.begin(), remarkComments.end());
}

void setLocationMacros(const Token* startTok, const std::vector<std::string>& files)
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[Location(files[tok->fileIndex()], tok->linenr())].emplace(tok->getMacroName());
mLocationMacros[loc].emplace(tok->getMacroName());
else if (tok->isName() && usedNames.count(tok->str()) != 0)
unexpanded.emplace(loc, tok->column());
}
// a token only knows the innermost macro it was expanded from; usages that produced no tokens are skipped
for (const simplecpp::MacroUsage& usage : macroUsage) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The macro usage list from simplecpp only has usages of macros that are still defined at the end of preprocessing. simplecpp::preprocess builds it by iterating over the final macros map. If the outer macro is #undef'd (or redefined) after it is used, its usages are gone and the suppression still doesn't match:

#define DIV(A,B) A/B
// cppcheck-suppress-macro zerodiv
#define ALIAS(A,B) DIV(A,B)
void f() {
    int a = ALIAS(1,0);
}
#undef ALIAS

The PR still reports zerodiv on line 5 plus Unmatched suppression: zerodiv. Fixing this probably needs a change in simplecpp. Otherwise, a TODO test or a note would help.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, #undef ALIAS after the use still gives zerodiv plus the unmatched suppression. That one is simplecpp, the usage list only has the macros still defined when preprocessing ends.

Opened cppcheck-opensource/simplecpp#764 for it. It reports the usages when the definition goes away, so #undef and redefinition both keep them. With that dropped into externals/simplecpp locally your example is suppressed.

6c9ac81 has it as a TODO test here until simplecpp gets bumped.

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)
it->second.emplace(usage.macroName);
}
}

Expand Down Expand Up @@ -1228,7 +1243,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
tokenizer.setDirectives(directives); // TODO: how to avoid repeated copies?

// locations macros
mLogger->setLocationMacros(tokenizer.tokens(), files);
mLogger->setLocationMacros(tokenizer.tokens(), files, preprocessor.getMacroUsage());

// If only errors are printed, print filename after the check
if (!mSettings.quiet && (!currentConfig.empty() || checkCount > 1)) {
Expand Down
4 changes: 4 additions & 0 deletions lib/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {

std::vector<RemarkComment> getRemarkComments(const simplecpp::TokenList &tokens) const;

const std::list<simplecpp::MacroUsage>& getMacroUsage() const {
return mMacroUsage;
}

bool loadAllIncludes(std::vector<std::string> &files);

simplecpp::FileData *loadFile(std::vector<std::string> &files, const std::string &file);
Expand Down
59 changes: 59 additions & 0 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,65 @@ class TestSuppressions : public TestFixture {
"a = DIV(10,1);\n",
"");
ASSERT_EQUALS("[test.cpp:2:1]: (information) Unmatched suppression: abc [unmatchedSuppression]\n", errout_str());

// #15208 - macro whose body is a single call to another macro
ASSERT_EQUALS(0,
(this->*check)("#define DIV(A,B) A/B\n"
"// cppcheck-suppress-macro zerodiv\n"
"#define ALIAS(A,B) DIV(A,B)\n"
"void f() {\n"
" int a = ALIAS(10,0);\n"
"}\n",
""));
ASSERT_EQUALS("", errout_str());

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

// empty macro on the line
ASSERT_EQUALS(1,
(this->*check)("// cppcheck-suppress-macro zerodiv\n"
"#define EMPTY\n"
"void f() {\n"
" EMPTY int a = 1/0;\n"
"}\n",
""));
ASSERT_EQUALS("[test.cpp:4:20]: (error) Division by zero. [zerodiv]\n"
"[test.cpp:2:1]: (information) Unmatched suppression: zerodiv [unmatchedSuppression]\n", errout_str());

// macro expanded on a line where its name is also a plain identifier
ASSERT_EQUALS(0,
(this->*check)("#define DIV(A,B) A/B\n"
"// cppcheck-suppress-macro zerodiv\n"
"#define ALIAS(A,B) DIV(A,B)\n"
"struct S { int ALIAS; };\n"
"int f(struct S s) {\n"
" return ALIAS(1,0) + s.ALIAS;\n"
"}\n",
""));
ASSERT_EQUALS("", errout_str());

// TODO: simplecpp only reports the usages of macros that are defined when preprocessing ends
TODO_ASSERT_EQUALS(0, 1,
(this->*check)("#define DIV(A,B) A/B\n"
"// cppcheck-suppress-macro zerodiv\n"
"#define ALIAS(A,B) DIV(A,B)\n"
"void f() {\n"
" int a = ALIAS(1,0);\n"
"}\n"
"#undef ALIAS\n",
""));
TODO_ASSERT_EQUALS("",
"[test.cpp:5:13]: (error) Division by zero. [zerodiv]\n"
"[test.cpp:3:1]: (information) Unmatched suppression: zerodiv [unmatchedSuppression]\n", errout_str());
}

void suppressionsSettingsFiles() {
Expand Down
Loading