Skip to content

Commit

Permalink
Refactoring suppressions code. (danmar#5550)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar authored Oct 13, 2023
1 parent efd488b commit ec15772
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett
}
}

std::for_each(inlineSuppressionsBlockBegin.begin(), inlineSuppressionsBlockBegin.end(), [&](const Suppressions::Suppression & suppr) {
for (const Suppressions::Suppression & suppr: inlineSuppressionsBlockBegin)
// cppcheck-suppress useStlAlgorithm
bad.emplace_back(suppr.fileName, suppr.lineNumber, "Suppress Begin: No matching end");
});
}

void Preprocessor::inlineSuppressions(const simplecpp::TokenList &tokens, Suppressions &suppressions)
Expand Down
27 changes: 16 additions & 11 deletions lib/suppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ std::vector<Suppressions::Suppression> Suppressions::parseMultiSuppressComment(c
return suppressions;
}

const std::string SymbolNameString = "symbolName=";
const std::string symbolNameString = "symbolName=";

while (iss) {
std::string word;
Expand All @@ -174,8 +174,8 @@ std::vector<Suppressions::Suppression> Suppressions::parseMultiSuppressComment(c
break;
if (word.find_first_not_of("+-*/%#;") == std::string::npos)
break;
if (startsWith(word, SymbolNameString)) {
s.symbolName = word.substr(SymbolNameString.size());
if (startsWith(word, symbolNameString)) {
s.symbolName = word.substr(symbolNameString.size());
} else {
if (errorMessage && errorMessage->empty())
*errorMessage = "Bad multi suppression '" + comment + "'. legal format is cppcheck-suppress[errorId, errorId symbolName=arr, ...]";
Expand Down Expand Up @@ -304,28 +304,33 @@ bool Suppressions::Suppression::parseComment(std::string comment, std::string *e
if (comment.compare(comment.size() - 2, 2, "*/") == 0)
comment.erase(comment.size() - 2, 2);

const std::string cppchecksuppress = "cppcheck-suppress";
const std::set<std::string> cppchecksuppress{
"cppcheck-suppress",
"cppcheck-suppress-begin",
"cppcheck-suppress-end",
"cppcheck-suppress-file"
};

std::istringstream iss(comment.substr(2));
std::string word;
iss >> word;
if (word.substr(0, cppchecksuppress.size()) != cppchecksuppress)
if (!cppchecksuppress.count(word))
return false;

iss >> errorId;
if (!iss)
return false;

const std::string SymbolNameString = "symbolName=";
const std::string symbolNameString = "symbolName=";

while (iss) {
iss >> word;
if (!iss)
break;
if (word.find_first_not_of("+-*/%#;") == std::string::npos)
break;
if (startsWith(word, SymbolNameString))
symbolName = word.substr(SymbolNameString.size());
if (startsWith(word, symbolNameString))
symbolName = word.substr(symbolNameString.size());
else if (errorMessage && errorMessage->empty())
*errorMessage = "Bad suppression attribute '" + word + "'. You can write comments in the comment after a ; or //. Valid suppression attributes; symbolName=sym";
}
Expand Down Expand Up @@ -395,16 +400,16 @@ std::string Suppressions::Suppression::getText() const
bool Suppressions::isSuppressed(const Suppressions::ErrorMessage &errmsg, bool global)
{
const bool unmatchedSuppression(errmsg.errorId == "unmatchedSuppression");
bool return_value = false;
bool returnValue = false;
for (Suppression &s : mSuppressions) {
if (!global && !s.isLocal())
continue;
if (unmatchedSuppression && s.errorId != errmsg.errorId)
continue;
if (s.isMatch(errmsg))
return_value = true;
returnValue = true;
}
return return_value;
return returnValue;
}

bool Suppressions::isSuppressed(const ::ErrorMessage &errmsg)
Expand Down
3 changes: 3 additions & 0 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,9 @@ class TestSuppressions : public TestFixture {
ASSERT_EQUALS(true, s.parseComment("/* cppcheck-suppress-end id */", &msg));
ASSERT_EQUALS("", msg);

// Bad cppcheck-suppress comment
ASSERT_EQUALS(false, s.parseComment("/* cppcheck-suppress-beggin id */", &msg));

// Bad attribute construction
const std::string badSuppressionAttribute = "Bad suppression attribute 'some'. You can write comments in the comment after a ; or //. Valid suppression attributes; symbolName=sym";

Expand Down

0 comments on commit ec15772

Please sign in to comment.