Skip to content

Commit 825a8cb

Browse files
committed
suprr
1 parent 5871750 commit 825a8cb

File tree

6 files changed

+46
-23
lines changed

6 files changed

+46
-23
lines changed

Diff for: gui/mainwindow.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -993,9 +993,7 @@ Settings MainWindow::getCppcheckSettings()
993993
tryLoadLibrary(&result.library, filename);
994994
}
995995

996-
for (const Suppressions::Suppression &suppression : mProjectFile->getSuppressions()) {
997-
result.nomsg.addSuppression(suppression);
998-
}
996+
result.nomsg.addSuppressions(mProjectFile->getSuppressions().toStdList()); // TODO: check result
999997

1000998
// Only check the given -D configuration
1001999
if (!defines.isEmpty())

Diff for: lib/cppcheck.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,11 @@ unsigned int CppCheck::check(const FileSettings &fs)
569569
return temp.check(Path::simplifyPath(fs.filename));
570570
}
571571
const unsigned int returnValue = temp.checkFile(Path::simplifyPath(fs.filename), fs.cfg);
572-
mSettings.nomsg.addSuppressions(temp.mSettings.nomsg.getSuppressions());
572+
for (const auto& suppr : temp.mSettings.nomsg.getSuppressions())
573+
{
574+
const bool res = mSettings.nomsg.updateSuppressionState(suppr);
575+
assert(res);
576+
}
573577
return returnValue;
574578
}
575579

Diff for: lib/preprocessor.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
254254
suppr.lineNumber = supprBegin->lineNumber;
255255
suppr.type = Suppressions::Type::block;
256256
inlineSuppressionsBlockBegin.erase(supprBegin);
257-
suppressions.addSuppression(std::move(suppr));
257+
suppressions.addSuppression(std::move(suppr)); // TODO: check result
258258
throwError = false;
259259
break;
260260
}
@@ -279,10 +279,10 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
279279
suppr.thisAndNextLine = thisAndNextLine;
280280
suppr.lineNumber = tok->location.line;
281281
suppr.macroName = macroName;
282-
suppressions.addSuppression(std::move(suppr));
282+
suppressions.addSuppression(std::move(suppr)); // TODO: check result
283283
} else if (Suppressions::Type::file == suppr.type) {
284284
if (onlyComments)
285-
suppressions.addSuppression(std::move(suppr));
285+
suppressions.addSuppression(std::move(suppr)); // TODO: check result
286286
else
287287
bad.emplace_back(suppr.fileName, suppr.lineNumber, "File suppression should be at the top of the file");
288288
}

Diff for: lib/suppressions.cpp

+17-5
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,7 @@ std::string Suppressions::addSuppression(Suppressions::Suppression suppression)
249249
auto foundSuppression = std::find_if(mSuppressions.begin(), mSuppressions.end(),
250250
std::bind(&Suppression::isSameParameters, &suppression, std::placeholders::_1));
251251
if (foundSuppression != mSuppressions.end()) {
252-
if (suppression.checked)
253-
foundSuppression->checked = suppression.checked;
254-
if (suppression.matched)
255-
foundSuppression->matched = suppression.matched;
256-
return "";
252+
return "suppression already exists";
257253
}
258254

259255
// Check that errorId is valid..
@@ -289,6 +285,22 @@ std::string Suppressions::addSuppressions(std::list<Suppression> suppressions)
289285
return "";
290286
}
291287

288+
bool Suppressions::updateSuppressionState(const Suppressions::Suppression& suppression)
289+
{
290+
// Check if suppression is already in list
291+
auto foundSuppression = std::find_if(mSuppressions.begin(), mSuppressions.end(),
292+
std::bind(&Suppression::isSameParameters, &suppression, std::placeholders::_1));
293+
if (foundSuppression != mSuppressions.end()) {
294+
if (suppression.checked)
295+
foundSuppression->checked = suppression.checked;
296+
if (suppression.matched)
297+
foundSuppression->matched = suppression.matched;
298+
return true;
299+
}
300+
301+
return false;
302+
}
303+
292304
void Suppressions::ErrorMessage::setFileName(std::string s)
293305
{
294306
mFileName = Path::simplifyPath(std::move(s));

Diff for: lib/suppressions.h

+7
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ class CPPCHECKLIB Suppressions {
194194
*/
195195
std::string addSuppressions(std::list<Suppression> suppressions);
196196

197+
/**
198+
* @brief Updates the state of the given suppression.
199+
* @param suppression the suppression to update
200+
* @return true if suppression to update was found
201+
*/
202+
bool updateSuppressionState(const Suppression& suppression);
203+
197204
/**
198205
* @brief Returns true if this message should not be shown to the user.
199206
* @param errmsg error message

Diff for: test/testsuppressions.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class TestSuppressions : public TestFixture {
195195
void suppressionsFileNameWithExtraPath() const {
196196
// Ticket #2797
197197
Suppressions suppressions;
198-
suppressions.addSuppressionLine("errorid:./a.c:123");
198+
ASSERT_EQUALS("", suppressions.addSuppressionLine("errorid:./a.c:123"));
199199
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("errorid", "a.c", 123)));
200200
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("errorid", "x/../a.c", 123)));
201201
}
@@ -940,41 +940,41 @@ class TestSuppressions : public TestFixture {
940940

941941
void suppressionsLine0() const {
942942
Suppressions suppressions;
943-
suppressions.addSuppressionLine("syntaxError:*:0");
943+
ASSERT_EQUALS("", suppressions.addSuppressionLine("syntaxError:*:0"));
944944
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("syntaxError", "test.cpp", 0)));
945945
}
946946

947947
void suppressionsFileComment() const {
948948
std::istringstream file1("# comment\n"
949949
"abc");
950950
Suppressions suppressions1;
951-
suppressions1.parseFile(file1);
951+
ASSERT_EQUALS("", suppressions1.parseFile(file1));
952952
ASSERT_EQUALS(true, suppressions1.isSuppressed(errorMessage("abc", "test.cpp", 123)));
953953

954954
std::istringstream file2("// comment\n"
955955
"abc");
956956
Suppressions suppressions2;
957-
suppressions2.parseFile(file2);
957+
ASSERT_EQUALS("", suppressions2.parseFile(file2));
958958
ASSERT_EQUALS(true, suppressions2.isSuppressed(errorMessage("abc", "test.cpp", 123)));
959959

960960
std::istringstream file3("abc // comment");
961961
Suppressions suppressions3;
962-
suppressions3.parseFile(file3);
962+
ASSERT_EQUALS("", suppressions3.parseFile(file3));
963963
ASSERT_EQUALS(true, suppressions3.isSuppressed(errorMessage("abc", "test.cpp", 123)));
964964

965965
std::istringstream file4("abc\t\t # comment");
966966
Suppressions suppressions4;
967-
suppressions4.parseFile(file4);
967+
ASSERT_EQUALS("", suppressions4.parseFile(file4));
968968
ASSERT_EQUALS(true, suppressions4.isSuppressed(errorMessage("abc", "test.cpp", 123)));
969969

970970
std::istringstream file5("abc:test.cpp\t\t # comment");
971971
Suppressions suppressions5;
972-
suppressions5.parseFile(file5);
972+
ASSERT_EQUALS("", suppressions5.parseFile(file5));
973973
ASSERT_EQUALS(true, suppressions5.isSuppressed(errorMessage("abc", "test.cpp", 123)));
974974

975975
std::istringstream file6("abc:test.cpp:123\t\t # comment with . inside");
976976
Suppressions suppressions6;
977-
suppressions6.parseFile(file6);
977+
ASSERT_EQUALS("", suppressions6.parseFile(file6));
978978
ASSERT_EQUALS(true, suppressions6.isSuppressed(errorMessage("abc", "test.cpp", 123)));
979979
}
980980

@@ -1192,7 +1192,7 @@ class TestSuppressions : public TestFixture {
11921192
CppCheck cppCheck(*this, false, nullptr); // <- do not "use global suppressions". pretend this is a thread that just checks a file.
11931193
Settings& settings = cppCheck.settings();
11941194
settings.quiet = true;
1195-
settings.nomsg.addSuppressionLine("uninitvar");
1195+
ASSERT_EQUALS("", settings.nomsg.addSuppressionLine("uninitvar"));
11961196
settings.exitCode = 1;
11971197

11981198
const char code[] = "int f() { int a; return a; }";
@@ -1204,7 +1204,7 @@ class TestSuppressions : public TestFixture {
12041204
Suppressions suppressions;
12051205
Suppressions::Suppression suppression("unusedFunction", "test.c", 3);
12061206
suppression.checked = true; // have to do this because fixes for #5704
1207-
suppressions.addSuppression(std::move(suppression));
1207+
ASSERT_EQUALS("", suppressions.addSuppression(std::move(suppression)));
12081208
ASSERT_EQUALS(true, !suppressions.getUnmatchedLocalSuppressions("test.c", true).empty());
12091209
ASSERT_EQUALS(false, !suppressions.getUnmatchedGlobalSuppressions(true).empty());
12101210
ASSERT_EQUALS(false, !suppressions.getUnmatchedLocalSuppressions("test.c", false).empty());
@@ -1213,7 +1213,7 @@ class TestSuppressions : public TestFixture {
12131213

12141214
void globalsuppress_unusedFunction() const { // #4946 - wrong report of "unmatchedSuppression" for "unusedFunction"
12151215
Suppressions suppressions;
1216-
suppressions.addSuppressionLine("unusedFunction:*");
1216+
ASSERT_EQUALS("", suppressions.addSuppressionLine("unusedFunction:*"));
12171217
ASSERT_EQUALS(false, !suppressions.getUnmatchedLocalSuppressions("test.c", true).empty());
12181218
ASSERT_EQUALS(true, !suppressions.getUnmatchedGlobalSuppressions(true).empty());
12191219
ASSERT_EQUALS(false, !suppressions.getUnmatchedLocalSuppressions("test.c", false).empty());
@@ -1460,6 +1460,8 @@ class TestSuppressions : public TestFixture {
14601460
Suppressions::reportUnmatchedSuppressions(suppressions, *this);
14611461
ASSERT_EQUALS("[a.c:10]: (information) Unmatched suppression: abc\n", errout.str());
14621462
}
1463+
1464+
// TODO: test updateSuppressionState
14631465
};
14641466

14651467
REGISTER_TEST(TestSuppressions)

0 commit comments

Comments
 (0)