Fixed #8515 (Wrong info message about unmatched suppression)

This commit is contained in:
Daniel Marjamäki 2018-05-11 09:01:08 +02:00
parent 9d30496ea1
commit 1e7c1841f7
4 changed files with 53 additions and 5 deletions

View File

@ -747,10 +747,15 @@ void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg)
const Suppressions::ErrorMessage errorMessage = msg.toSuppressionsErrorMessage(); const Suppressions::ErrorMessage errorMessage = msg.toSuppressionsErrorMessage();
if (_useGlobalSuppressions) {
if (_settings.nomsg.isSuppressed(errorMessage)) if (_settings.nomsg.isSuppressed(errorMessage))
return; return;
} else {
if (_settings.nomsg.isSuppressedLocal(errorMessage))
return;
}
if (!_settings.nofail.isSuppressed(errorMessage)) if (!_settings.nofail.isSuppressed(errorMessage) && (_useGlobalSuppressions || !_settings.nomsg.isSuppressed(errorMessage)))
exitcode = 1; exitcode = 1;
_errorList.push_back(errmsg); _errorList.push_back(errmsg);

View File

@ -277,6 +277,21 @@ bool Suppressions::isSuppressed(const Suppressions::ErrorMessage &errmsg)
return false; return false;
} }
bool Suppressions::isSuppressedLocal(const Suppressions::ErrorMessage &errmsg)
{
const bool unmatchedSuppression(errmsg.errorId == "unmatchedSuppression");
for (std::list<Suppression>::iterator it = _suppressions.begin(); it != _suppressions.end(); ++it) {
Suppression &s = *it;
if (!s.isLocal())
continue;
if (unmatchedSuppression && s.errorId != errmsg.errorId)
continue;
if (s.isMatch(errmsg))
return true;
}
return false;
}
void Suppressions::dump(std::ostream & out) void Suppressions::dump(std::ostream & out)
{ {
out << " <suppressions>" << std::endl; out << " <suppressions>" << std::endl;
@ -294,6 +309,8 @@ void Suppressions::dump(std::ostream & out)
out << " </suppressions>" << std::endl; out << " </suppressions>" << std::endl;
} }
#include <iostream>
std::list<Suppressions::Suppression> Suppressions::getUnmatchedLocalSuppressions(const std::string &file, const bool unusedFunctionChecking) const std::list<Suppressions::Suppression> Suppressions::getUnmatchedLocalSuppressions(const std::string &file, const bool unusedFunctionChecking) const
{ {
std::list<Suppression> result; std::list<Suppression> result;
@ -303,7 +320,7 @@ std::list<Suppressions::Suppression> Suppressions::getUnmatchedLocalSuppressions
continue; continue;
if (!unusedFunctionChecking && s.errorId == "unusedFunction") if (!unusedFunctionChecking && s.errorId == "unusedFunction")
continue; continue;
if (!file.empty() && !s.fileName.empty() && s.fileName != file) if (file.empty() || !s.isLocal() || s.fileName != file)
continue; continue;
result.push_back(s); result.push_back(s);
} }
@ -319,7 +336,7 @@ std::list<Suppressions::Suppression> Suppressions::getUnmatchedGlobalSuppression
continue; continue;
if (!unusedFunctionChecking && s.errorId == "unusedFunction") if (!unusedFunctionChecking && s.errorId == "unusedFunction")
continue; continue;
if (s.fileName.find_first_of("?*") == std::string::npos) if (s.isLocal())
continue; continue;
result.push_back(s); result.push_back(s);
} }

View File

@ -88,6 +88,10 @@ public:
bool isMatch(const ErrorMessage &errmsg); bool isMatch(const ErrorMessage &errmsg);
std::string getText() const; std::string getText() const;
bool isLocal() const {
return !fileName.empty() && fileName.find_first_of("?*") == std::string::npos;
}
std::string errorId; std::string errorId;
std::string fileName; std::string fileName;
int lineNumber; int lineNumber;
@ -135,6 +139,13 @@ public:
*/ */
bool isSuppressed(const ErrorMessage &errmsg); bool isSuppressed(const ErrorMessage &errmsg);
/**
* @brief Returns true if this message should not be shown to the user, only uses local suppressions.
* @param errmsg error message
* @return true if this error is suppressed.
*/
bool isSuppressedLocal(const ErrorMessage &errmsg);
/** /**
* @brief Create an xml dump of suppressions * @brief Create an xml dump of suppressions
* @param out stream to write XML to * @param out stream to write XML to

View File

@ -50,6 +50,8 @@ private:
TEST_CASE(inlinesuppress); TEST_CASE(inlinesuppress);
TEST_CASE(inlinesuppress_symbolname); TEST_CASE(inlinesuppress_symbolname);
TEST_CASE(globalSuppressions); // Testing that global suppressions work (#8515)
TEST_CASE(inlinesuppress_unusedFunction); // #4210 - unusedFunction TEST_CASE(inlinesuppress_unusedFunction); // #4210 - unusedFunction
TEST_CASE(globalsuppress_unusedFunction); // #4946 TEST_CASE(globalsuppress_unusedFunction); // #4946
TEST_CASE(suppressionWithRelativePaths); // #4733 TEST_CASE(suppressionWithRelativePaths); // #4733
@ -432,6 +434,19 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
} }
void globalSuppressions() { // Testing that Cppcheck::useGlobalSuppressions works (#8515)
errout.str("");
CppCheck cppCheck(*this, false); // <- do not "use global suppressions". pretend this is a thread that just checks a file.
Settings& settings = cppCheck.settings();
settings.nomsg.addSuppressionLine("uninitvar");
settings.exitCode = 1;
const char code[] = "int f() { int a; return a; }";
ASSERT_EQUALS(0, cppCheck.check("test.c", code)); // <- no unsuppressed error is seen
ASSERT_EQUALS("[test.c:1]: (error) Uninitialized variable: a\n", errout.str()); // <- report error so ThreadExecutor can suppress it and make sure the global suppression is matched.
}
void inlinesuppress_unusedFunction() const { // #4210, #4946 - wrong report of "unmatchedSuppression" for "unusedFunction" void inlinesuppress_unusedFunction() const { // #4210, #4946 - wrong report of "unmatchedSuppression" for "unusedFunction"
Suppressions suppressions; Suppressions suppressions;
suppressions.addSuppression(Suppressions::Suppression("unusedFunction", "test.c", 3)); suppressions.addSuppression(Suppressions::Suppression("unusedFunction", "test.c", 3));