Fixed reporting of unmatched suppressions for unusedFunction (#4946)

This commit is contained in:
PKEuS 2014-09-01 10:13:03 +02:00
parent 47764321f2
commit e35329aba3
6 changed files with 29 additions and 25 deletions

View File

@ -770,7 +770,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
} }
if (settings.isEnabled("information") || settings.checkConfiguration) if (settings.isEnabled("information") || settings.checkConfiguration)
reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions()); reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions(settings._jobs == 1 && settings.isEnabled("unusedFunction")));
if (!settings.checkConfiguration) { if (!settings.checkConfiguration) {
cppcheck.tooManyConfigsError("",0U); cppcheck.tooManyConfigsError("",0U);

View File

@ -242,7 +242,7 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin
} }
if (_settings.isEnabled("information") || _settings.checkConfiguration) if (_settings.isEnabled("information") || _settings.checkConfiguration)
reportUnmatchedSuppressions(_settings.nomsg.getUnmatchedLocalSuppressions(filename)); reportUnmatchedSuppressions(_settings.nomsg.getUnmatchedLocalSuppressions(filename, _settings._jobs == 1 && _settings.isEnabled("unusedFunction")));
_errorList.clear(); _errorList.clear();
return exitcode; return exitcode;

View File

@ -265,11 +265,11 @@ bool Suppressions::isSuppressedLocal(const std::string &errorId, const std::stri
return _suppressions[errorId].isSuppressedLocal(file, line); return _suppressions[errorId].isSuppressedLocal(file, line);
} }
std::list<Suppressions::SuppressionEntry> Suppressions::getUnmatchedLocalSuppressions(const std::string &file) const std::list<Suppressions::SuppressionEntry> Suppressions::getUnmatchedLocalSuppressions(const std::string &file, bool unusedFunctionChecking) const
{ {
std::list<SuppressionEntry> r; std::list<SuppressionEntry> r;
for (std::map<std::string, FileMatcher>::const_iterator i = _suppressions.begin(); i != _suppressions.end(); ++i) { for (std::map<std::string, FileMatcher>::const_iterator i = _suppressions.begin(); i != _suppressions.end(); ++i) {
if (i->first == "unusedFunction") if (!unusedFunctionChecking && i->first == "unusedFunction")
continue; // unusedFunction is not a "local" suppression continue; // unusedFunction is not a "local" suppression
std::map<std::string, std::map<unsigned int, bool> >::const_iterator f = i->second._files.find(file); std::map<std::string, std::map<unsigned int, bool> >::const_iterator f = i->second._files.find(file);
@ -284,10 +284,13 @@ std::list<Suppressions::SuppressionEntry> Suppressions::getUnmatchedLocalSuppres
return r; return r;
} }
std::list<Suppressions::SuppressionEntry> Suppressions::getUnmatchedGlobalSuppressions() const std::list<Suppressions::SuppressionEntry> Suppressions::getUnmatchedGlobalSuppressions(bool unusedFunctionChecking) const
{ {
std::list<SuppressionEntry> r; std::list<SuppressionEntry> r;
for (std::map<std::string, FileMatcher>::const_iterator i = _suppressions.begin(); i != _suppressions.end(); ++i) { for (std::map<std::string, FileMatcher>::const_iterator i = _suppressions.begin(); i != _suppressions.end(); ++i) {
if (!unusedFunctionChecking && i->first == "unusedFunction")
continue;
// global suppressions.. // global suppressions..
for (std::map<std::string, std::map<unsigned int, bool> >::const_iterator g = i->second._globs.begin(); g != i->second._globs.end(); ++g) { for (std::map<std::string, std::map<unsigned int, bool> >::const_iterator g = i->second._globs.begin(); g != i->second._globs.end(); ++g) {
for (std::map<unsigned int, bool>::const_iterator l = g->second.begin(); l != g->second.end(); ++l) { for (std::map<unsigned int, bool>::const_iterator l = g->second.begin(); l != g->second.end(); ++l) {
@ -296,17 +299,6 @@ std::list<Suppressions::SuppressionEntry> Suppressions::getUnmatchedGlobalSuppre
} }
} }
} }
// unusedFunction..
if (i->first == "unusedFunction") {
for (std::map<std::string, std::map<unsigned int, bool> >::const_iterator f = i->second._files.begin(); f != i->second._files.end(); ++f) {
for (std::map<unsigned int, bool>::const_iterator l = f->second.begin(); l != f->second.end(); ++l) {
if (!l->second) {
r.push_back(SuppressionEntry(i->first, f->first, l->first));
}
}
}
}
} }
return r; return r;
} }

View File

@ -133,13 +133,13 @@ public:
* @brief Returns list of unmatched local (per-file) suppressions. * @brief Returns list of unmatched local (per-file) suppressions.
* @return list of unmatched suppressions * @return list of unmatched suppressions
*/ */
std::list<SuppressionEntry> getUnmatchedLocalSuppressions(const std::string &file) const; std::list<SuppressionEntry> getUnmatchedLocalSuppressions(const std::string &file, bool unusedFunctionChecking) const;
/** /**
* @brief Returns list of unmatched global (glob pattern) suppressions. * @brief Returns list of unmatched global (glob pattern) suppressions.
* @return list of unmatched suppressions * @return list of unmatched suppressions
*/ */
std::list<SuppressionEntry> getUnmatchedGlobalSuppressions() const; std::list<SuppressionEntry> getUnmatchedGlobalSuppressions(bool unusedFunctionChecking) const;
}; };
/// @} /// @}

View File

@ -244,7 +244,7 @@ private:
CheckOther checkOther(&tokenizer, &settings, &logger); CheckOther checkOther(&tokenizer, &settings, &logger);
checkOther.checkSwitchCaseFallThrough(); checkOther.checkSwitchCaseFallThrough();
logger.reportUnmatchedSuppressions(settings.nomsg.getUnmatchedLocalSuppressions(filename)); logger.reportUnmatchedSuppressions(settings.nomsg.getUnmatchedLocalSuppressions(filename, false));
} }

View File

@ -44,6 +44,7 @@ private:
TEST_CASE(suppressionsPathSeparator); TEST_CASE(suppressionsPathSeparator);
TEST_CASE(inlinesuppress_unusedFunction); // #4210 - unusedFunction TEST_CASE(inlinesuppress_unusedFunction); // #4210 - unusedFunction
TEST_CASE(globalsuppress_unusedFunction); // #4946
TEST_CASE(suppressionWithRelativePaths); // #4733 TEST_CASE(suppressionWithRelativePaths); // #4733
} }
@ -130,7 +131,7 @@ private:
cppCheck.check("test.cpp", code); cppCheck.check("test.cpp", code);
reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions()); reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions(true));
} }
void checkSuppressionThreads(const char code[], const std::string &suppression = emptyString) { void checkSuppressionThreads(const char code[], const std::string &suppression = emptyString) {
@ -153,7 +154,7 @@ private:
executor.check(); executor.check();
reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions()); reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions(false));
} }
// Check the suppression for multiple files // Check the suppression for multiple files
@ -171,7 +172,7 @@ private:
for (int i = 0; names[i] != NULL; ++i) for (int i = 0; names[i] != NULL; ++i)
cppCheck.check(names[i], codes[i]); cppCheck.check(names[i], codes[i]);
reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions()); reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions(true));
} }
void runChecks(void (TestSuppressions::*check)(const char[], const std::string &)) { void runChecks(void (TestSuppressions::*check)(const char[], const std::string &)) {
@ -323,11 +324,22 @@ private:
ASSERT_EQUALS(true, suppressions.isSuppressed("someid", "test/foo/bar.cpp", 142)); ASSERT_EQUALS(true, suppressions.isSuppressed("someid", "test/foo/bar.cpp", 142));
} }
void inlinesuppress_unusedFunction() const { // #4210 - wrong report of "unmatchedSuppression" for "unusedFunction" void inlinesuppress_unusedFunction() const { // #4210, #4946 - wrong report of "unmatchedSuppression" for "unusedFunction"
Suppressions suppressions; Suppressions suppressions;
suppressions.addSuppression("unusedFunction", "test.c", 3U); suppressions.addSuppression("unusedFunction", "test.c", 3U);
ASSERT_EQUALS(true, suppressions.getUnmatchedLocalSuppressions("test.c").empty()); ASSERT_EQUALS(true, !suppressions.getUnmatchedLocalSuppressions("test.c", true).empty());
ASSERT_EQUALS(false, suppressions.getUnmatchedGlobalSuppressions().empty()); ASSERT_EQUALS(false, !suppressions.getUnmatchedGlobalSuppressions(true).empty());
ASSERT_EQUALS(false, !suppressions.getUnmatchedLocalSuppressions("test.c", false).empty());
ASSERT_EQUALS(false, !suppressions.getUnmatchedGlobalSuppressions(false).empty());
}
void globalsuppress_unusedFunction() const { // #4946 - wrong report of "unmatchedSuppression" for "unusedFunction"
Suppressions suppressions;
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());
ASSERT_EQUALS(false, !suppressions.getUnmatchedGlobalSuppressions(false).empty());
} }
void suppressionWithRelativePaths() { void suppressionWithRelativePaths() {