Skip to content

Commit

Permalink
suprr
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Jan 4, 2024
1 parent 5871750 commit 825a8cb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
4 changes: 1 addition & 3 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 5 additions & 1 deletion lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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");
}
Expand Down
22 changes: 17 additions & 5 deletions lib/suppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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..
Expand Down Expand Up @@ -289,6 +285,22 @@ std::string Suppressions::addSuppressions(std::list<Suppression> 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));
Expand Down
7 changes: 7 additions & 0 deletions lib/suppressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ class CPPCHECKLIB Suppressions {
*/
std::string addSuppressions(std::list<Suppression> 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
Expand Down
24 changes: 13 additions & 11 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down Expand Up @@ -940,41 +940,41 @@ 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)));
}

void suppressionsFileComment() const {
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)));
}

Expand Down Expand Up @@ -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; }";
Expand All @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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)

0 comments on commit 825a8cb

Please sign in to comment.