From f677322c69762ae1ce7b61c95d355e9feec66645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 9 Apr 2018 11:50:59 +0200 Subject: [PATCH] Suppressions: Allow symbol-based inline suppressions --- lib/preprocessor.cpp | 30 +++++++++++++++++++++--------- test/testsuppressions.cpp | 22 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 31a3b472d..0ce52957a 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -70,8 +70,7 @@ Preprocessor::~Preprocessor() static void inlineSuppressions(const simplecpp::TokenList &tokens, Settings &_settings) { - std::list suppressionIDs; - + std::list inlineSuppressions; for (const simplecpp::Token *tok = tokens.cfront(); tok; tok = tok->next) { if (tok->comment) { std::istringstream iss(tok->str.substr(2)); @@ -79,13 +78,23 @@ static void inlineSuppressions(const simplecpp::TokenList &tokens, Settings &_se iss >> word; if (word != "cppcheck-suppress") continue; - iss >> word; - if (iss) - suppressionIDs.push_back(word); + + Suppressions::Suppression s; + iss >> s.errorId; + if (!iss) + continue; + while (iss) { + iss >> word; + if (!iss) + break; + if (word.compare(0,11,"symbolName=")==0) + s.symbolName = word.substr(11); + } + inlineSuppressions.push_back(s); continue; } - if (suppressionIDs.empty()) + if (inlineSuppressions.empty()) continue; // Relative filename @@ -98,12 +107,15 @@ static void inlineSuppressions(const simplecpp::TokenList &tokens, Settings &_se } } } + relativeFilename = Path::simplifyPath(relativeFilename); // Add the suppressions. - for (const std::string &errorId : suppressionIDs) { - _settings.nomsg.addSuppression(Suppressions::Suppression(errorId, relativeFilename, tok->location.line)); + for (Suppressions::Suppression &suppr : inlineSuppressions) { + suppr.fileName = relativeFilename; + suppr.lineNumber = tok->location.line; + _settings.nomsg.addSuppression(suppr); } - suppressionIDs.clear(); + inlineSuppressions.clear(); } } diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index 39a1453e5..8e42952e4 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -47,6 +47,8 @@ private: TEST_CASE(suppressionsMultiFile); TEST_CASE(suppressionsPathSeparator); + TEST_CASE(inlinesuppress_symbolname); + TEST_CASE(inlinesuppress_unusedFunction); // #4210 - unusedFunction TEST_CASE(globalsuppress_unusedFunction); // #4946 TEST_CASE(suppressionWithRelativePaths); // #4733 @@ -395,6 +397,26 @@ private: ASSERT_EQUALS(true, s2.isSuppressed(errorMessage("abc", "include/1.h", 142))); } + void inlinesuppress_symbolname() { + Suppressions suppressions; + + checkSuppression("void f() {\n" + " int a;\n" + " /* cppcheck-suppress uninitvar symbolName=a */\n" + " a++;\n" + "}\n", + ""); + ASSERT_EQUALS("", errout.str()); + + checkSuppression("void f() {\n" + " int a,b;\n" + " /* cppcheck-suppress uninitvar symbolName=b */\n" + " a++; b++;\n" + "}\n", + ""); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str()); + } + void inlinesuppress_unusedFunction() const { // #4210, #4946 - wrong report of "unmatchedSuppression" for "unusedFunction" Suppressions suppressions; suppressions.addSuppression(Suppressions::Suppression("unusedFunction", "test.c", 3));