Refactoring suppressions code. (#5550)

This commit is contained in:
Daniel Marjamäki 2023-10-13 19:57:08 +02:00 committed by GitHub
parent efd488b519
commit ec15772381
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 13 deletions

View File

@ -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"); bad.emplace_back(suppr.fileName, suppr.lineNumber, "Suppress Begin: No matching end");
});
} }
void Preprocessor::inlineSuppressions(const simplecpp::TokenList &tokens, Suppressions &suppressions) void Preprocessor::inlineSuppressions(const simplecpp::TokenList &tokens, Suppressions &suppressions)

View File

@ -165,7 +165,7 @@ std::vector<Suppressions::Suppression> Suppressions::parseMultiSuppressComment(c
return suppressions; return suppressions;
} }
const std::string SymbolNameString = "symbolName="; const std::string symbolNameString = "symbolName=";
while (iss) { while (iss) {
std::string word; std::string word;
@ -174,8 +174,8 @@ std::vector<Suppressions::Suppression> Suppressions::parseMultiSuppressComment(c
break; break;
if (word.find_first_not_of("+-*/%#;") == std::string::npos) if (word.find_first_not_of("+-*/%#;") == std::string::npos)
break; break;
if (startsWith(word, SymbolNameString)) { if (startsWith(word, symbolNameString)) {
s.symbolName = word.substr(SymbolNameString.size()); s.symbolName = word.substr(symbolNameString.size());
} else { } else {
if (errorMessage && errorMessage->empty()) if (errorMessage && errorMessage->empty())
*errorMessage = "Bad multi suppression '" + comment + "'. legal format is cppcheck-suppress[errorId, errorId symbolName=arr, ...]"; *errorMessage = "Bad multi suppression '" + comment + "'. legal format is cppcheck-suppress[errorId, errorId symbolName=arr, ...]";
@ -304,19 +304,24 @@ bool Suppressions::Suppression::parseComment(std::string comment, std::string *e
if (comment.compare(comment.size() - 2, 2, "*/") == 0) if (comment.compare(comment.size() - 2, 2, "*/") == 0)
comment.erase(comment.size() - 2, 2); 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::istringstream iss(comment.substr(2));
std::string word; std::string word;
iss >> word; iss >> word;
if (word.substr(0, cppchecksuppress.size()) != cppchecksuppress) if (!cppchecksuppress.count(word))
return false; return false;
iss >> errorId; iss >> errorId;
if (!iss) if (!iss)
return false; return false;
const std::string SymbolNameString = "symbolName="; const std::string symbolNameString = "symbolName=";
while (iss) { while (iss) {
iss >> word; iss >> word;
@ -324,8 +329,8 @@ bool Suppressions::Suppression::parseComment(std::string comment, std::string *e
break; break;
if (word.find_first_not_of("+-*/%#;") == std::string::npos) if (word.find_first_not_of("+-*/%#;") == std::string::npos)
break; break;
if (startsWith(word, SymbolNameString)) if (startsWith(word, symbolNameString))
symbolName = word.substr(SymbolNameString.size()); symbolName = word.substr(symbolNameString.size());
else if (errorMessage && errorMessage->empty()) 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"; *errorMessage = "Bad suppression attribute '" + word + "'. You can write comments in the comment after a ; or //. Valid suppression attributes; symbolName=sym";
} }
@ -395,16 +400,16 @@ std::string Suppressions::Suppression::getText() const
bool Suppressions::isSuppressed(const Suppressions::ErrorMessage &errmsg, bool global) bool Suppressions::isSuppressed(const Suppressions::ErrorMessage &errmsg, bool global)
{ {
const bool unmatchedSuppression(errmsg.errorId == "unmatchedSuppression"); const bool unmatchedSuppression(errmsg.errorId == "unmatchedSuppression");
bool return_value = false; bool returnValue = false;
for (Suppression &s : mSuppressions) { for (Suppression &s : mSuppressions) {
if (!global && !s.isLocal()) if (!global && !s.isLocal())
continue; continue;
if (unmatchedSuppression && s.errorId != errmsg.errorId) if (unmatchedSuppression && s.errorId != errmsg.errorId)
continue; continue;
if (s.isMatch(errmsg)) if (s.isMatch(errmsg))
return_value = true; returnValue = true;
} }
return return_value; return returnValue;
} }
bool Suppressions::isSuppressed(const ::ErrorMessage &errmsg) bool Suppressions::isSuppressed(const ::ErrorMessage &errmsg)

View File

@ -883,6 +883,9 @@ private:
ASSERT_EQUALS(true, s.parseComment("/* cppcheck-suppress-end id */", &msg)); ASSERT_EQUALS(true, s.parseComment("/* cppcheck-suppress-end id */", &msg));
ASSERT_EQUALS("", msg); ASSERT_EQUALS("", msg);
// Bad cppcheck-suppress comment
ASSERT_EQUALS(false, s.parseComment("/* cppcheck-suppress-beggin id */", &msg));
// Bad attribute construction // 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"; const std::string badSuppressionAttribute = "Bad suppression attribute 'some'. You can write comments in the comment after a ; or //. Valid suppression attributes; symbolName=sym";