Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1608,14 +1608,8 @@ void CppCheck::reportErr(const ErrorMessage &msg)
// TODO: only convert if necessary
const Suppressions::ErrorMessage errorMessage = msg.toSuppressionsErrorMessage();

if (mUseGlobalSuppressions) {
if (mSettings.nomsg.isSuppressed(errorMessage)) {
return;
}
} else {
if (mSettings.nomsg.isSuppressedLocal(errorMessage)) {
return;
}
if (mSettings.nomsg.isSuppressed(errorMessage, mUseGlobalSuppressions)) {
return;
}

if (!mSettings.nofail.isSuppressed(errorMessage) && !mSettings.nomsg.isSuppressed(errorMessage)) {
Expand Down
3 changes: 1 addition & 2 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1274,8 +1274,7 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti

for (const std::string &p : paths)
guiProject.pathNames.push_back(p);
for (const Suppressions::Suppression &supp : suppressions)
settings->nomsg.addSuppression(supp);
settings->nomsg.addSuppressions(std::move(suppressions));
settings->checkHeaders = temp.checkHeaders;
settings->checkUnusedTemplates = temp.checkUnusedTemplates;
settings->maxCtuDepth = temp.maxCtuDepth;
Expand Down
2 changes: 1 addition & 1 deletion lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ static void addinlineSuppressions(const simplecpp::TokenList &tokens, const Sett
suppr.fileName = relativeFilename;
suppr.lineNumber = tok->location.line;
suppr.thisAndNextLine = thisAndNextLine;
suppressions.addSuppression(suppr);
suppressions.addSuppression(std::move(suppr));
}
}
}
Expand Down
32 changes: 10 additions & 22 deletions lib/suppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ std::string Suppressions::parseXmlFile(const char *filename)
return "Unknown suppression element \"" + std::string(e2->Name()) + "\", expected id/fileName/lineNumber/symbolName/hash";
}

const std::string err = addSuppression(s);
const std::string err = addSuppression(std::move(s));
if (!err.empty())
return err;
}
Expand Down Expand Up @@ -218,10 +218,10 @@ std::string Suppressions::addSuppressionLine(const std::string &line)

suppression.fileName = Path::simplifyPath(suppression.fileName);

return addSuppression(suppression);
return addSuppression(std::move(suppression));
}

std::string Suppressions::addSuppression(const Suppressions::Suppression &suppression)
std::string Suppressions::addSuppression(Suppressions::Suppression suppression)
{
// Check if suppression is already in list
auto foundSuppression = std::find_if(mSuppressions.begin(), mSuppressions.end(),
Expand Down Expand Up @@ -253,15 +253,15 @@ std::string Suppressions::addSuppression(const Suppressions::Suppression &suppre
if (!isValidGlobPattern(suppression.fileName))
return "Failed to add suppression. Invalid glob pattern '" + suppression.fileName + "'.";

mSuppressions.push_back(suppression);
mSuppressions.push_back(std::move(suppression));

return "";
}

std::string Suppressions::addSuppressions(const std::list<Suppression> &suppressions)
std::string Suppressions::addSuppressions(std::list<Suppression> suppressions)
{
for (const auto &newSuppression : suppressions) {
auto errmsg = addSuppression(newSuppression);
for (auto &newSuppression : suppressions) {
auto errmsg = addSuppression(std::move(newSuppression));
if (!errmsg.empty())
return errmsg;
}
Expand Down Expand Up @@ -367,10 +367,12 @@ std::string Suppressions::Suppression::getText() const
return ret;
}

bool Suppressions::isSuppressed(const Suppressions::ErrorMessage &errmsg)
bool Suppressions::isSuppressed(const Suppressions::ErrorMessage &errmsg, bool global)
{
const bool unmatchedSuppression(errmsg.errorId == "unmatchedSuppression");
for (Suppression &s : mSuppressions) {
if (!global && !s.isLocal())
continue;
if (unmatchedSuppression && s.errorId != errmsg.errorId)
continue;
if (s.isMatch(errmsg))
Expand All @@ -386,20 +388,6 @@ bool Suppressions::isSuppressed(const ::ErrorMessage &errmsg)
return isSuppressed(errmsg.toSuppressionsErrorMessage());
}

bool Suppressions::isSuppressedLocal(const Suppressions::ErrorMessage &errmsg)
{
const bool unmatchedSuppression(errmsg.errorId == "unmatchedSuppression");
for (Suppression &s : mSuppressions) {
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) const
{
out << " <suppressions>" << std::endl;
Expand Down
29 changes: 4 additions & 25 deletions lib/suppressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,8 @@ class CPPCHECKLIB Suppressions {

struct CPPCHECKLIB Suppression {
Suppression() : lineNumber(NO_LINE), hash(0), thisAndNextLine(false), matched(false), checked(false) {}
Suppression(const Suppression &other) {
*this = other;
}
Suppression(std::string id, std::string file, int line=NO_LINE) : errorId(std::move(id)), fileName(std::move(file)), lineNumber(line), hash(0), thisAndNextLine(false), matched(false), checked(false) {}

Suppression & operator=(const Suppression &other) {
errorId = other.errorId;
fileName = other.fileName;
lineNumber = other.lineNumber;
symbolName = other.symbolName;
hash = other.hash;
thisAndNextLine = other.thisAndNextLine;
matched = other.matched;
checked = other.checked;
return *this;
}

bool operator<(const Suppression &other) const {
if (errorId != other.errorId)
return errorId < other.errorId;
Expand Down Expand Up @@ -163,21 +148,22 @@ class CPPCHECKLIB Suppressions {
* @param suppression suppression details
* @return error message. empty upon success
*/
std::string addSuppression(const Suppression &suppression);
std::string addSuppression(Suppression suppression);

/**
* @brief Combine list of suppressions into the current suppressions.
* @param suppressions list of suppression details
* @return error message. empty upon success
*/
std::string addSuppressions(const std::list<Suppression> &suppressions);
std::string addSuppressions(std::list<Suppression> suppressions);

/**
* @brief Returns true if this message should not be shown to the user.
* @param errmsg error message
* @param global use global suppressions
* @return true if this error is suppressed.
*/
bool isSuppressed(const ErrorMessage &errmsg);
bool isSuppressed(const ErrorMessage &errmsg, bool global = true);

/**
* @brief Returns true if this message should not be shown to the user.
Expand All @@ -186,13 +172,6 @@ class CPPCHECKLIB Suppressions {
*/
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
* @param out stream to write XML to
Expand Down
46 changes: 36 additions & 10 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class TestSuppressions : public TestFixture {
TEST_CASE(unusedFunction);

TEST_CASE(suppressingSyntaxErrorAndExitCode);
TEST_CASE(suppressLocal);
}

void suppressionsBadId1() const {
Expand Down Expand Up @@ -112,15 +113,17 @@ class TestSuppressions : public TestFixture {

void suppressionsDosFormat() const {
Suppressions suppressions;
std::istringstream s("abc\r\ndef\r\n");
std::istringstream s("abc\r\n"
"def\r\n");
ASSERT_EQUALS("", suppressions.parseFile(s));
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("abc")));
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("def")));
}

void suppressionsFileNameWithColon() const {
Suppressions suppressions;
std::istringstream s("errorid:c:\\foo.cpp\nerrorid:c:\\bar.cpp:12");
std::istringstream s("errorid:c:\\foo.cpp\n"
"errorid:c:\\bar.cpp:12");
ASSERT_EQUALS("", suppressions.parseFile(s));
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("errorid", "c:/foo.cpp", 1111)));
ASSERT_EQUALS(false, suppressions.isSuppressed(errorMessage("errorid", "c:/bar.cpp", 10)));
Expand All @@ -138,7 +141,9 @@ class TestSuppressions : public TestFixture {
// Check that globbing works
{
Suppressions suppressions;
std::istringstream s("errorid:x*.cpp\nerrorid:y?.cpp\nerrorid:test.c*");
std::istringstream s("errorid:x*.cpp\n"
"errorid:y?.cpp\n"
"errorid:test.c*");
ASSERT_EQUALS("", suppressions.parseFile(s));
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("errorid", "xyz.cpp", 1)));
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("errorid", "xyz.cpp.cpp", 1)));
Expand All @@ -152,7 +157,10 @@ class TestSuppressions : public TestFixture {
// Check that both a filename match and a glob match apply
{
Suppressions suppressions;
std::istringstream s("errorid:x*.cpp\nerrorid:xyz.cpp:1\nerrorid:a*.cpp:1\nerrorid:abc.cpp:2");
std::istringstream s("errorid:x*.cpp\n"
"errorid:xyz.cpp:1\n"
"errorid:a*.cpp:1\n"
"errorid:abc.cpp:2");
ASSERT_EQUALS("", suppressions.parseFile(s));
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("errorid", "xyz.cpp", 1)));
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("errorid", "xyz.cpp", 2)));
Expand Down Expand Up @@ -526,12 +534,14 @@ class TestSuppressions : public TestFixture {
}

void suppressionsFileComment() const {
std::istringstream file1("# comment\nabc");
std::istringstream file1("# comment\n"
"abc");
Suppressions suppressions1;
suppressions1.parseFile(file1);
ASSERT_EQUALS(true, suppressions1.isSuppressed(errorMessage("abc", "test.cpp", 123)));

std::istringstream file2("// comment\nabc");
std::istringstream file2("// comment\n"
"abc");
Suppressions suppressions2;
suppressions2.parseFile(file2);
ASSERT_EQUALS(true, suppressions2.isSuppressed(errorMessage("abc", "test.cpp", 123)));
Expand Down Expand Up @@ -700,9 +710,9 @@ class TestSuppressions : public TestFixture {

void inlinesuppress_unusedFunction() const { // #4210, #4946 - wrong report of "unmatchedSuppression" for "unusedFunction"
Suppressions suppressions;
auto suppression = Suppressions::Suppression("unusedFunction", "test.c", 3);
Suppressions::Suppression suppression("unusedFunction", "test.c", 3);
suppression.checked = true; // have to do this because fixes for #5704
suppressions.addSuppression(suppression);
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());
Expand Down Expand Up @@ -795,9 +805,11 @@ class TestSuppressions : public TestFixture {
ASSERT_EQUALS(false, s.isSuppressed(errorMsg));
errorMsg.symbolNames = "array1\n";
ASSERT_EQUALS(true, s.isSuppressed(errorMsg));
errorMsg.symbolNames = "x\narray2\n";
errorMsg.symbolNames = "x\n"
"array2\n";
ASSERT_EQUALS(true, s.isSuppressed(errorMsg));
errorMsg.symbolNames = "array3\nx\n";
errorMsg.symbolNames = "array3\n"
"x\n";
ASSERT_EQUALS(true, s.isSuppressed(errorMsg));
}

Expand Down Expand Up @@ -833,6 +845,20 @@ class TestSuppressions : public TestFixture {
ASSERT_EQUALS(2, checkSuppression(code3, "zerodiv:test.cpp:3")); // suppress 'errordiv' at line 3 of test.cpp
}

void suppressLocal() const {
Suppressions suppressions;
std::istringstream s("errorid:test.cpp\n"
"errorid2");
ASSERT_EQUALS("", suppressions.parseFile(s));
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("errorid", "test.cpp", 1)));
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("errorid", "test.cpp", 1), false));
ASSERT_EQUALS(false, suppressions.isSuppressed(errorMessage("errorid", "test2.cpp", 1)));
ASSERT_EQUALS(false, suppressions.isSuppressed(errorMessage("errorid", "test2.cpp", 1), false));
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("errorid2", "test.cpp", 1)));
ASSERT_EQUALS(false, suppressions.isSuppressed(errorMessage("errorid2", "test.cpp", 1), false));
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("errorid2", "test2.cpp", 1)));
ASSERT_EQUALS(false, suppressions.isSuppressed(errorMessage("errorid2", "test2.cpp", 1), false));
}
};

REGISTER_TEST(TestSuppressions)