diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 91b2e1f9ebb..f964a367ec9 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -184,6 +184,15 @@ static std::string getRelativeFilename(const simplecpp::Token* tok, const Settin return Path::simplifyPath(std::move(relativeFilename)); } +static void addInlineSuppression(SuppressionList::Suppression suppr, SuppressionList &suppressions, std::list &bad) +{ + const std::string file = suppr.fileName; + const int line = suppr.lineNumber; + const std::string errmsg = suppressions.addSuppression(std::move(suppr)); + if (!errmsg.empty()) + bad.emplace_back(file, line, errmsg); +} + static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Settings &settings, SuppressionList &suppressions, std::list &bad) { std::list inlineSuppressionsBlockBegin; @@ -262,7 +271,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett suppr.lineNumber = supprBegin->lineNumber; suppr.type = SuppressionList::Type::block; inlineSuppressionsBlockBegin.erase(supprBegin); - suppressions.addSuppression(std::move(suppr)); // TODO: check result + addInlineSuppression(std::move(suppr), suppressions, bad); throwError = false; break; } @@ -287,10 +296,10 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett suppr.thisAndNextLine = thisAndNextLine; suppr.lineNumber = tok->location.line; suppr.macroName = macroName; - suppressions.addSuppression(std::move(suppr)); // TODO: check result + addInlineSuppression(std::move(suppr), suppressions, bad); } else if (SuppressionList::Type::file == suppr.type) { if (onlyComments) - suppressions.addSuppression(std::move(suppr)); // TODO: check result + addInlineSuppression(std::move(suppr), suppressions, bad); else bad.emplace_back(suppr.fileName, suppr.lineNumber, "File suppression should be at the top of the file"); } diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index 90bd5bac31f..d5536c7634a 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -703,6 +703,19 @@ class TestSuppressions : public TestFixture { "[test.cpp:3:5]: (error) Uninitialized variable: a [uninitvar]\n" "[test.cpp:5:5]: (error) Uninitialized variable: b [uninitvar]\n", errout_str()); + ASSERT_EQUALS(1, (this->*check)("// cppcheck-suppress-file :id0\n" + "// cppcheck-suppress :id\n" + "// cppcheck-suppress [:id1,id2]\n" + "// cppcheck-suppress-begin :id3\n" + "void f() {}\n" + "// cppcheck-suppress-end :id3\n", + "")); + ASSERT_EQUALS("[test.cpp:1:0]: (error) Failed to add suppression. Invalid id \":id0\" [preprocessorErrorDirective]\n" + "[test.cpp:5:0]: (error) Failed to add suppression. Invalid id \":id\" [preprocessorErrorDirective]\n" // TODO: should we report the location of the suppression instead? + "[test.cpp:5:0]: (error) Failed to add suppression. Invalid id \":id1\" [preprocessorErrorDirective]\n" // TODO: should we report the location of the suppression instead? + "[test.cpp:4:0]: (error) Failed to add suppression. Invalid id \":id3\" [preprocessorErrorDirective]\n" + "[test.cpp:5:0]: (information) Unmatched suppression: id2 [unmatchedSuppression]\n", errout_str()); // TODO: should we report the location of the suppression instead? + ASSERT_EQUALS(1, (this->*check)("void f() {\n" " int a;\n" " // cppcheck-suppress-begin uninitvar\n"