Skip to content

Commit ac64111

Browse files
committed
fixed #14276 - report inline suppressions with invalid error IDs
1 parent 1c37d49 commit ac64111

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
@@ -179,6 +179,15 @@ static std::string getRelativeFilename(const simplecpp::Token* tok, const Settin
179179
return Path::simplifyPath(std::move(relativeFilename));
180180
}
181181

182+
static void addInlineSuppression(SuppressionList::Suppression suppr, SuppressionList &suppressions, std::list<BadInlineSuppression> &bad)
183+
{
184+
const std::string file = suppr.fileName;
185+
const int line = suppr.lineNumber;
186+
const std::string errmsg = suppressions.addSuppression(std::move(suppr));
187+
if (!errmsg.empty())
188+
bad.emplace_back(file, line, 0, errmsg); // TODO: set column
189+
}
190+
182191
static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Settings &settings, SuppressionList &suppressions, std::list<BadInlineSuppression> &bad)
183192
{
184193
std::list<SuppressionList::Suppression> inlineSuppressionsBlockBegin;
@@ -257,7 +266,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
257266
suppr.lineNumber = supprBegin->lineNumber;
258267
suppr.type = SuppressionList::Type::block;
259268
inlineSuppressionsBlockBegin.erase(supprBegin);
260-
suppressions.addSuppression(std::move(suppr)); // TODO: check result
269+
addInlineSuppression(std::move(suppr), suppressions, bad);
261270
throwError = false;
262271
break;
263272
}
@@ -282,10 +291,10 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
282291
suppr.thisAndNextLine = thisAndNextLine;
283292
suppr.lineNumber = tok->location.line;
284293
suppr.macroName = macroName;
285-
suppressions.addSuppression(std::move(suppr)); // TODO: check result
294+
addInlineSuppression(std::move(suppr), suppressions, bad);
286295
} else if (SuppressionList::Type::file == suppr.type) {
287296
if (onlyComments)
288-
suppressions.addSuppression(std::move(suppr)); // TODO: check result
297+
addInlineSuppression(std::move(suppr), suppressions, bad);
289298
else
290299
bad.emplace_back(suppr.fileName, suppr.lineNumber, 0, "File suppression should be at the top of the file"); // TODO: set column
291300
}

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\" [invalidSuppression]\n"
714+
"[test.cpp:5:0]: (error) Failed to add suppression. Invalid id \":id\" [invalidSuppression]\n" // TODO: should we report the location of the suppression instead?
715+
"[test.cpp:5:0]: (error) Failed to add suppression. Invalid id \":id1\" [invalidSuppression]\n" // TODO: should we report the location of the suppression instead?
716+
"[test.cpp:4:0]: (error) Failed to add suppression. Invalid id \":id3\" [invalidSuppression]\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)