diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 584b177efe0d..70387317047e 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -993,9 +993,7 @@ Settings MainWindow::getCppcheckSettings() tryLoadLibrary(&result.library, filename); } - for (const Suppressions::Suppression &suppression : mProjectFile->getSuppressions()) { - result.nomsg.addSuppression(suppression); - } + result.nomsg.addSuppressions(mProjectFile->getSuppressions().toStdList()); // TODO: check result // Only check the given -D configuration if (!defines.isEmpty()) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 9f912697db8c..cb184870fb7a 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -569,7 +569,11 @@ unsigned int CppCheck::check(const FileSettings &fs) return temp.check(Path::simplifyPath(fs.filename)); } const unsigned int returnValue = temp.checkFile(Path::simplifyPath(fs.filename), fs.cfg); - mSettings.nomsg.addSuppressions(temp.mSettings.nomsg.getSuppressions()); + for (const auto& suppr : temp.mSettings.nomsg.getSuppressions()) + { + const bool res = mSettings.nomsg.updateSuppressionState(suppr); + assert(res); + } return returnValue; } diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index efad79ac3f7c..70d8949edc13 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -254,7 +254,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett suppr.lineNumber = supprBegin->lineNumber; suppr.type = Suppressions::Type::block; inlineSuppressionsBlockBegin.erase(supprBegin); - suppressions.addSuppression(std::move(suppr)); + suppressions.addSuppression(std::move(suppr)); // TODO: check result throwError = false; break; } @@ -279,10 +279,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)); + suppressions.addSuppression(std::move(suppr)); // TODO: check result } else if (Suppressions::Type::file == suppr.type) { if (onlyComments) - suppressions.addSuppression(std::move(suppr)); + suppressions.addSuppression(std::move(suppr)); // TODO: check result else bad.emplace_back(suppr.fileName, suppr.lineNumber, "File suppression should be at the top of the file"); } diff --git a/lib/suppressions.cpp b/lib/suppressions.cpp index 7d59a50c3d2b..2d21e1639e86 100644 --- a/lib/suppressions.cpp +++ b/lib/suppressions.cpp @@ -249,11 +249,7 @@ std::string Suppressions::addSuppression(Suppressions::Suppression suppression) auto foundSuppression = std::find_if(mSuppressions.begin(), mSuppressions.end(), std::bind(&Suppression::isSameParameters, &suppression, std::placeholders::_1)); if (foundSuppression != mSuppressions.end()) { - if (suppression.checked) - foundSuppression->checked = suppression.checked; - if (suppression.matched) - foundSuppression->matched = suppression.matched; - return ""; + return "suppression already exists"; } // Check that errorId is valid.. @@ -289,6 +285,22 @@ std::string Suppressions::addSuppressions(std::list suppressions) return ""; } +bool Suppressions::updateSuppressionState(const Suppressions::Suppression& suppression) +{ + // Check if suppression is already in list + auto foundSuppression = std::find_if(mSuppressions.begin(), mSuppressions.end(), + std::bind(&Suppression::isSameParameters, &suppression, std::placeholders::_1)); + if (foundSuppression != mSuppressions.end()) { + if (suppression.checked) + foundSuppression->checked = suppression.checked; + if (suppression.matched) + foundSuppression->matched = suppression.matched; + return true; + } + + return false; +} + void Suppressions::ErrorMessage::setFileName(std::string s) { mFileName = Path::simplifyPath(std::move(s)); diff --git a/lib/suppressions.h b/lib/suppressions.h index e6f6da830083..7697272f8484 100644 --- a/lib/suppressions.h +++ b/lib/suppressions.h @@ -194,6 +194,13 @@ class CPPCHECKLIB Suppressions { */ std::string addSuppressions(std::list suppressions); + /** + * @brief Updates the state of the given suppression. + * @param suppression the suppression to update + * @return true if suppression to update was found + */ + bool updateSuppressionState(const Suppression& suppression); + /** * @brief Returns true if this message should not be shown to the user. * @param errmsg error message diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index ab6b12c1a72b..25a54d3af210 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -195,7 +195,7 @@ class TestSuppressions : public TestFixture { void suppressionsFileNameWithExtraPath() const { // Ticket #2797 Suppressions suppressions; - suppressions.addSuppressionLine("errorid:./a.c:123"); + ASSERT_EQUALS("", suppressions.addSuppressionLine("errorid:./a.c:123")); ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("errorid", "a.c", 123))); ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("errorid", "x/../a.c", 123))); } @@ -940,7 +940,7 @@ class TestSuppressions : public TestFixture { void suppressionsLine0() const { Suppressions suppressions; - suppressions.addSuppressionLine("syntaxError:*:0"); + ASSERT_EQUALS("", suppressions.addSuppressionLine("syntaxError:*:0")); ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("syntaxError", "test.cpp", 0))); } @@ -948,33 +948,33 @@ class TestSuppressions : public TestFixture { std::istringstream file1("# comment\n" "abc"); Suppressions suppressions1; - suppressions1.parseFile(file1); + ASSERT_EQUALS("", suppressions1.parseFile(file1)); ASSERT_EQUALS(true, suppressions1.isSuppressed(errorMessage("abc", "test.cpp", 123))); std::istringstream file2("// comment\n" "abc"); Suppressions suppressions2; - suppressions2.parseFile(file2); + ASSERT_EQUALS("", suppressions2.parseFile(file2)); ASSERT_EQUALS(true, suppressions2.isSuppressed(errorMessage("abc", "test.cpp", 123))); std::istringstream file3("abc // comment"); Suppressions suppressions3; - suppressions3.parseFile(file3); + ASSERT_EQUALS("", suppressions3.parseFile(file3)); ASSERT_EQUALS(true, suppressions3.isSuppressed(errorMessage("abc", "test.cpp", 123))); std::istringstream file4("abc\t\t # comment"); Suppressions suppressions4; - suppressions4.parseFile(file4); + ASSERT_EQUALS("", suppressions4.parseFile(file4)); ASSERT_EQUALS(true, suppressions4.isSuppressed(errorMessage("abc", "test.cpp", 123))); std::istringstream file5("abc:test.cpp\t\t # comment"); Suppressions suppressions5; - suppressions5.parseFile(file5); + ASSERT_EQUALS("", suppressions5.parseFile(file5)); ASSERT_EQUALS(true, suppressions5.isSuppressed(errorMessage("abc", "test.cpp", 123))); std::istringstream file6("abc:test.cpp:123\t\t # comment with . inside"); Suppressions suppressions6; - suppressions6.parseFile(file6); + ASSERT_EQUALS("", suppressions6.parseFile(file6)); ASSERT_EQUALS(true, suppressions6.isSuppressed(errorMessage("abc", "test.cpp", 123))); } @@ -1192,7 +1192,7 @@ class TestSuppressions : public TestFixture { CppCheck cppCheck(*this, false, nullptr); // <- do not "use global suppressions". pretend this is a thread that just checks a file. Settings& settings = cppCheck.settings(); settings.quiet = true; - settings.nomsg.addSuppressionLine("uninitvar"); + ASSERT_EQUALS("", settings.nomsg.addSuppressionLine("uninitvar")); settings.exitCode = 1; const char code[] = "int f() { int a; return a; }"; @@ -1204,7 +1204,7 @@ class TestSuppressions : public TestFixture { Suppressions suppressions; Suppressions::Suppression suppression("unusedFunction", "test.c", 3); suppression.checked = true; // have to do this because fixes for #5704 - suppressions.addSuppression(std::move(suppression)); + ASSERT_EQUALS("", suppressions.addSuppression(std::move(suppression))); ASSERT_EQUALS(true, !suppressions.getUnmatchedLocalSuppressions("test.c", true).empty()); ASSERT_EQUALS(false, !suppressions.getUnmatchedGlobalSuppressions(true).empty()); ASSERT_EQUALS(false, !suppressions.getUnmatchedLocalSuppressions("test.c", false).empty()); @@ -1213,7 +1213,7 @@ class TestSuppressions : public TestFixture { void globalsuppress_unusedFunction() const { // #4946 - wrong report of "unmatchedSuppression" for "unusedFunction" Suppressions suppressions; - suppressions.addSuppressionLine("unusedFunction:*"); + ASSERT_EQUALS("", suppressions.addSuppressionLine("unusedFunction:*")); ASSERT_EQUALS(false, !suppressions.getUnmatchedLocalSuppressions("test.c", true).empty()); ASSERT_EQUALS(true, !suppressions.getUnmatchedGlobalSuppressions(true).empty()); ASSERT_EQUALS(false, !suppressions.getUnmatchedLocalSuppressions("test.c", false).empty()); @@ -1460,6 +1460,8 @@ class TestSuppressions : public TestFixture { Suppressions::reportUnmatchedSuppressions(suppressions, *this); ASSERT_EQUALS("[a.c:10]: (information) Unmatched suppression: abc\n", errout.str()); } + + // TODO: test updateSuppressionState }; REGISTER_TEST(TestSuppressions)