From ec1577238109217e8265a9f893e9affc6e21e7d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 13 Oct 2023 19:57:08 +0200 Subject: [PATCH] Refactoring suppressions code. (#5550) --- lib/preprocessor.cpp | 4 ++-- lib/suppressions.cpp | 27 ++++++++++++++++----------- test/testsuppressions.cpp | 3 +++ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index aa0d06c930f..1712ac86c34 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -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) diff --git a/lib/suppressions.cpp b/lib/suppressions.cpp index 288b9ea36af..284ba30d71a 100644 --- a/lib/suppressions.cpp +++ b/lib/suppressions.cpp @@ -165,7 +165,7 @@ std::vector Suppressions::parseMultiSuppressComment(c return suppressions; } - const std::string SymbolNameString = "symbolName="; + const std::string symbolNameString = "symbolName="; while (iss) { std::string word; @@ -174,8 +174,8 @@ std::vector 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, ...]"; @@ -304,19 +304,24 @@ 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 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; @@ -324,8 +329,8 @@ bool Suppressions::Suppression::parseComment(std::string comment, std::string *e 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"; } @@ -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) diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index e1921d5e341..20f192cac57 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -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";