Skip to content

Commit 7a3fc6c

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

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/preprocessor.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,10 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
262262
suppr.lineNumber = supprBegin->lineNumber;
263263
suppr.type = SuppressionList::Type::block;
264264
inlineSuppressionsBlockBegin.erase(supprBegin);
265-
suppressions.addSuppression(std::move(suppr)); // TODO: check result
265+
const int line = suppr.lineNumber;
266+
const std::string errmsg = suppressions.addSuppression(std::move(suppr));
267+
if (!errmsg.empty())
268+
bad.emplace_back(relativeFilename, line, errmsg);
266269
throwError = false;
267270
break;
268271
}
@@ -287,10 +290,18 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
287290
suppr.thisAndNextLine = thisAndNextLine;
288291
suppr.lineNumber = tok->location.line;
289292
suppr.macroName = macroName;
290-
suppressions.addSuppression(std::move(suppr)); // TODO: check result
293+
const int line = suppr.lineNumber;
294+
const std::string errmsg = suppressions.addSuppression(std::move(suppr));
295+
if (!errmsg.empty())
296+
bad.emplace_back(relativeFilename, line, errmsg);
291297
} else if (SuppressionList::Type::file == suppr.type) {
292298
if (onlyComments)
293-
suppressions.addSuppression(std::move(suppr)); // TODO: check result
299+
{
300+
const int line = suppr.lineNumber;
301+
const std::string errmsg = suppressions.addSuppression(std::move(suppr));
302+
if (!errmsg.empty())
303+
bad.emplace_back(relativeFilename, line, errmsg);
304+
}
294305
else
295306
bad.emplace_back(suppr.fileName, suppr.lineNumber, "File suppression should be at the top of the file");
296307
}

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)