Skip to content

Commit 24c2d28

Browse files
authored
Fix #15208 (cppcheck-suppress-macro has no effect on a macro whose body is a single macro call) (#9018)
1 parent 38eef52 commit 24c2d28

4 files changed

Lines changed: 82 additions & 3 deletions

File tree

‎AUTHORS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ Jakub Melka
193193
Jan Egil Ruud
194194
Jan Hellwig
195195
János Maros
196+
Jay Beavers
196197
Jay Sigbrandt
197198
Jean-François Deverge
198199
Jedrzej Klocek

‎lib/cppcheck.cpp‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,27 @@ class CppCheck::CppCheckLogger : public ErrorLogger
107107
mRemarkComments.insert(mRemarkComments.end(), remarkComments.begin(), remarkComments.end());
108108
}
109109

110-
void setLocationMacros(const Token* startTok, const std::vector<std::string>& files)
110+
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;
113118
for (const Token* tok = startTok; tok; tok = tok->next()) {
119+
const Location loc(files[tok->fileIndex()], tok->linenr());
114120
if (!tok->getMacroName().empty())
115-
mLocationMacros[Location(files[tok->fileIndex()], tok->linenr())].emplace(tok->getMacroName());
121+
mLocationMacros[loc].emplace(tok->getMacroName());
122+
else if (tok->isName() && usedNames.count(tok->str()) != 0)
123+
unexpanded.emplace(loc, tok->column());
124+
}
125+
// a token only knows the innermost macro it was expanded from; usages that produced no tokens are skipped
126+
for (const simplecpp::MacroUsage& usage : macroUsage) {
127+
const Location loc(files[usage.useLocation.fileIndex], usage.useLocation.line);
128+
const auto it = mLocationMacros.find(loc);
129+
if (it != mLocationMacros.end() && unexpanded.count({loc, usage.useLocation.col}) == 0)
130+
it->second.emplace(usage.macroName);
116131
}
117132
}
118133

@@ -1240,7 +1255,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
12401255
tokenizer.setDirectives(directives); // TODO: how to avoid repeated copies?
12411256

12421257
// locations macros
1243-
mLogger->setLocationMacros(tokenizer.tokens(), files);
1258+
mLogger->setLocationMacros(tokenizer.tokens(), files, preprocessor.getMacroUsage());
12441259

12451260
// If only errors are printed, print filename after the check
12461261
if (!mSettings.quiet && (!currentConfig.empty() || checkCount > 1)) {

‎lib/preprocessor.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
124124

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

127+
const std::list<simplecpp::MacroUsage>& getMacroUsage() const {
128+
return mMacroUsage;
129+
}
130+
127131
bool loadAllIncludes(std::vector<std::string> &files);
128132

129133
simplecpp::FileData *loadFile(std::vector<std::string> &files, const std::string &file);

‎test/testsuppressions.cpp‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,65 @@ class TestSuppressions : public TestFixture {
10521052
"a = DIV(10,1);\n",
10531053
"");
10541054
ASSERT_EQUALS("[test.cpp:2:1]: (information) Unmatched suppression: abc [unmatchedSuppression]\n", errout_str());
1055+
1056+
// #15208 - macro whose body is a single call to another macro
1057+
ASSERT_EQUALS(0,
1058+
(this->*check)("#define DIV(A,B) A/B\n"
1059+
"// cppcheck-suppress-macro zerodiv\n"
1060+
"#define ALIAS(A,B) DIV(A,B)\n"
1061+
"void f() {\n"
1062+
" int a = ALIAS(10,0);\n"
1063+
"}\n",
1064+
""));
1065+
ASSERT_EQUALS("", errout_str());
1066+
1067+
// function-like macro name that is not expanded
1068+
ASSERT_EQUALS(1,
1069+
(this->*check)("// cppcheck-suppress-macro zerodiv\n"
1070+
"#define ALIAS(A,B) A/B\n"
1071+
"void f() {\n"
1072+
" int ALIAS = 1/0;\n"
1073+
"}\n",
1074+
""));
1075+
ASSERT_EQUALS("[test.cpp:4:18]: (error) Division by zero. [zerodiv]\n"
1076+
"[test.cpp:2:1]: (information) Unmatched suppression: zerodiv [unmatchedSuppression]\n", errout_str());
1077+
1078+
// empty macro on the line
1079+
ASSERT_EQUALS(1,
1080+
(this->*check)("// cppcheck-suppress-macro zerodiv\n"
1081+
"#define EMPTY\n"
1082+
"void f() {\n"
1083+
" EMPTY int a = 1/0;\n"
1084+
"}\n",
1085+
""));
1086+
ASSERT_EQUALS("[test.cpp:4:20]: (error) Division by zero. [zerodiv]\n"
1087+
"[test.cpp:2:1]: (information) Unmatched suppression: zerodiv [unmatchedSuppression]\n", errout_str());
1088+
1089+
// macro expanded on a line where its name is also a plain identifier
1090+
ASSERT_EQUALS(0,
1091+
(this->*check)("#define DIV(A,B) A/B\n"
1092+
"// cppcheck-suppress-macro zerodiv\n"
1093+
"#define ALIAS(A,B) DIV(A,B)\n"
1094+
"struct S { int ALIAS; };\n"
1095+
"int f(struct S s) {\n"
1096+
" return ALIAS(1,0) + s.ALIAS;\n"
1097+
"}\n",
1098+
""));
1099+
ASSERT_EQUALS("", errout_str());
1100+
1101+
// TODO: simplecpp only reports the usages of macros that are defined when preprocessing ends
1102+
TODO_ASSERT_EQUALS(0, 1,
1103+
(this->*check)("#define DIV(A,B) A/B\n"
1104+
"// cppcheck-suppress-macro zerodiv\n"
1105+
"#define ALIAS(A,B) DIV(A,B)\n"
1106+
"void f() {\n"
1107+
" int a = ALIAS(1,0);\n"
1108+
"}\n"
1109+
"#undef ALIAS\n",
1110+
""));
1111+
TODO_ASSERT_EQUALS("",
1112+
"[test.cpp:5:13]: (error) Division by zero. [zerodiv]\n"
1113+
"[test.cpp:3:1]: (information) Unmatched suppression: zerodiv [unmatchedSuppression]\n", errout_str());
10551114
}
10561115

10571116
void suppressionsSettingsFiles() {

0 commit comments

Comments
 (0)