From a4de6a345572012346e68ec3edfa1f09bc3e0d6d Mon Sep 17 00:00:00 2001 From: Greg Hewgill Date: Thu, 17 Feb 2011 21:46:14 +1300 Subject: [PATCH] be sure to list unmatched suppressions only for the currently processed file --- lib/cppcheck.cpp | 2 +- lib/settings.cpp | 5 +++-- lib/settings.h | 2 +- test/testsuppressions.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 4febd096b..b23fd311d 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -197,7 +197,7 @@ unsigned int CppCheck::check() _errorLogger.reportOut("Bailing out from checking " + fixedpath + ": " + e.what()); } - reportUnmatchedSuppressions(_settings.nomsg.getUnmatchedLocalSuppressions()); + reportUnmatchedSuppressions(_settings.nomsg.getUnmatchedLocalSuppressions(fname)); _errorLogger.reportStatus(c + 1, (unsigned int)_filenames.size()); } diff --git a/lib/settings.cpp b/lib/settings.cpp index b523c1903..2aeeb6c1a 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -312,12 +312,13 @@ bool Settings::Suppressions::isSuppressedLocal(const std::string &errorId, const return _suppressions[errorId].isSuppressedLocal(file, line); } -std::list Settings::Suppressions::getUnmatchedLocalSuppressions() const +std::list Settings::Suppressions::getUnmatchedLocalSuppressions(const std::string &file) const { std::list r; for (std::map::const_iterator i = _suppressions.begin(); i != _suppressions.end(); ++i) { - for (std::map >::const_iterator f = i->second._files.begin(); f != i->second._files.end(); ++f) + std::map >::const_iterator f = i->second._files.find(file); + if (f != i->second._files.end()) { for (std::map::const_iterator l = f->second.begin(); l != f->second.end(); ++l) { diff --git a/lib/settings.h b/lib/settings.h index b65d741c4..3ce7f4e2d 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -239,7 +239,7 @@ public: * @brief Returns list of unmatched local (per-file) suppressions. * @return list of unmatched suppressions */ - std::list getUnmatchedLocalSuppressions() const; + std::list getUnmatchedLocalSuppressions(const std::string &file) const; /** * @brief Returns list of unmatched global (glob pattern) suppressions. diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index b8adaad47..d27f75f56 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -36,6 +36,7 @@ private: void run() { TEST_CASE(suppressionsSettings); + TEST_CASE(suppressionsMultiFile); } // Check the suppression @@ -84,6 +85,26 @@ private: reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions()); } + // Check the suppression for multiple files + void checkSuppression(const char *names[], const char *codes[], const std::string &suppression = "") + { + // Clear the error log + errout.str(""); + + Settings settings; + settings._inlineSuppressions = true; + if (!suppression.empty()) + settings.nomsg.addSuppressionLine(suppression); + + CppCheck cppCheck(*this, true); + cppCheck.settings(settings); + for (int i = 0; names[i] != NULL; ++i) + cppCheck.addFile(names[i], codes[i]); + cppCheck.check(); + + reportUnmatchedSuppressions(cppCheck.settings().nomsg.getUnmatchedGlobalSuppressions()); + } + void runChecks(void (TestSuppressions::*check)(const char[], const std::string &)) { // check to make sure the appropriate error is present @@ -167,6 +188,23 @@ private: runChecks(&TestSuppressions::checkSuppressionThreads); } + void suppressionsMultiFile() + { + const char *names[] = {"abc.cpp", "xyz.cpp", NULL}; + const char *codes[] = { + "void f() {\n" + "}\n", + "void f() {\n" + " int a;\n" + " a++;\n" + "}\n", + }; + + // suppress uninitvar for this file and line + checkSuppression(names, codes, "uninitvar:xyz.cpp:3"); + ASSERT_EQUALS("", errout.str()); + } + }; REGISTER_TEST(TestSuppressions)