Skip to content

Commit f516ba9

Browse files
committed
fixed #14276 - report inline suppressions with invalid error IDs [skip ci]
1 parent d8b6e08 commit f516ba9

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/preprocessor.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ static std::string getRelativeFilename(const simplecpp::Token* tok, const Settin
184184
return Path::simplifyPath(std::move(relativeFilename));
185185
}
186186

187+
static void addInlineSuppression(SuppressionList::Suppression suppr, SuppressionList &suppressions, std::list<BadInlineSuppression> &bad)
188+
{
189+
const std::string file = suppr.fileName;
190+
const int line = suppr.lineNumber;
191+
const std::string errmsg = suppressions.addSuppression(std::move(suppr));
192+
if (!errmsg.empty())
193+
bad.emplace_back(file, line, errmsg);
194+
}
195+
187196
static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Settings &settings, SuppressionList &suppressions, std::list<BadInlineSuppression> &bad)
188197
{
189198
std::list<SuppressionList::Suppression> inlineSuppressionsBlockBegin;
@@ -262,7 +271,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
262271
suppr.lineNumber = supprBegin->lineNumber;
263272
suppr.type = SuppressionList::Type::block;
264273
inlineSuppressionsBlockBegin.erase(supprBegin);
265-
suppressions.addSuppression(std::move(suppr)); // TODO: check result
274+
addInlineSuppression(std::move(suppr), suppressions, bad);
266275
throwError = false;
267276
break;
268277
}
@@ -287,10 +296,10 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
287296
suppr.thisAndNextLine = thisAndNextLine;
288297
suppr.lineNumber = tok->location.line;
289298
suppr.macroName = macroName;
290-
suppressions.addSuppression(std::move(suppr)); // TODO: check result
299+
addInlineSuppression(std::move(suppr), suppressions, bad);
291300
} else if (SuppressionList::Type::file == suppr.type) {
292301
if (onlyComments)
293-
suppressions.addSuppression(std::move(suppr)); // TODO: check result
302+
addInlineSuppression(std::move(suppr), suppressions, bad);
294303
else
295304
bad.emplace_back(suppr.fileName, suppr.lineNumber, "File suppression should be at the top of the file");
296305
}

test/testsuppressions.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,19 @@ class TestSuppressions : public TestFixture {
703703
"[test.cpp:3:5]: (error) Uninitialized variable: a [uninitvar]\n"
704704
"[test.cpp:5:5]: (error) Uninitialized variable: b [uninitvar]\n", errout_str());
705705

706+
ASSERT_EQUALS(1, (this->*check)("// cppcheck-suppress-file :id0\n"
707+
"// cppcheck-suppress :id\n"
708+
"// cppcheck-suppress [:id1,id2]\n"
709+
"// cppcheck-suppress-begin :id3\n"
710+
"void f() {}\n"
711+
"// cppcheck-suppress-end :id3\n",
712+
""));
713+
ASSERT_EQUALS("[test.cpp:1:0]: (error) Failed to add suppression. Invalid id \":id0\" [preprocessorErrorDirective]\n"
714+
"[test.cpp:5:0]: (error) Failed to add suppression. Invalid id \":id\" [preprocessorErrorDirective]\n" // TODO: should we report the location of the suppression instead?
715+
"[test.cpp:5:0]: (error) Failed to add suppression. Invalid id \":id1\" [preprocessorErrorDirective]\n" // TODO: should we report the location of the suppression instead?
716+
"[test.cpp:4:0]: (error) Failed to add suppression. Invalid id \":id3\" [preprocessorErrorDirective]\n"
717+
"[test.cpp:5:0]: (information) Unmatched suppression: id2 [unmatchedSuppression]\n", errout_str()); // TODO: should we report the location of the suppression instead?
718+
706719
ASSERT_EQUALS(1, (this->*check)("void f() {\n"
707720
" int a;\n"
708721
" // cppcheck-suppress-begin uninitvar\n"

0 commit comments

Comments
 (0)