From 2eca0a93a73e93ea33e1f46f9f4e37d477ae8a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 28 Aug 2013 06:46:32 +0200 Subject: [PATCH] Fixed #4733 (defective unusedStructMember warning) --- lib/preprocessor.cpp | 13 ++++++++++++- test/testsuppressions.cpp | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 32b67ac0d..92cdfaf0c 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -555,9 +555,20 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri // Add any pending inline suppressions that have accumulated. if (!suppressionIDs.empty()) { if (_settings != NULL) { + // Relative filename + std::string relativeFilename(filename); + if (_settings->_relativePaths) { + for (std::size_t j = 0U; j < _settings->_basePaths.size(); ++j) { + const std::string bp = _settings->_basePaths[j] + "/"; + if (relativeFilename.compare(0,bp.size(),bp)==0) { + relativeFilename = relativeFilename.substr(bp.size()); + } + } + } + // Add the suppressions. for (std::size_t j = 0; j < suppressionIDs.size(); ++j) { - const std::string errmsg(_settings->nomsg.addSuppression(suppressionIDs[j], filename, lineno)); + const std::string errmsg(_settings->nomsg.addSuppression(suppressionIDs[j], relativeFilename, lineno)); if (!errmsg.empty()) { writeError(filename, lineno, _errorLogger, "cppcheckError", errmsg); } diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index d7f8e337b..ebde27214 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -44,6 +44,7 @@ private: TEST_CASE(suppressionsPathSeparator); TEST_CASE(inlinesuppress_unusedFunction); // #4210 - unusedFunction + TEST_CASE(suppressionWithRelativePaths); // #4733 } void suppressionsBadId1() const { @@ -328,6 +329,28 @@ private: ASSERT_EQUALS(true, suppressions.getUnmatchedLocalSuppressions("test.c").empty()); ASSERT_EQUALS(false, suppressions.getUnmatchedGlobalSuppressions().empty()); } + + void suppressionWithRelativePaths() { + // Clear the error log + errout.str(""); + + CppCheck cppCheck(*this, true); + Settings& settings = cppCheck.settings(); + settings.addEnabled("style"); + settings._inlineSuppressions = true; + settings._relativePaths = true; + settings._basePaths.push_back("/somewhere"); + const char code[] = + "struct Point\n" + "{\n" + " // cppcheck-suppress unusedStructMember\n" + " int x;\n" + " // cppcheck-suppress unusedStructMember\n" + " int y;\n" + "};"; + cppCheck.check("/somewhere/test.cpp", code); + ASSERT_EQUALS("",errout.str()); + } }; REGISTER_TEST(TestSuppressions)