From 54e0b60cc58ea491925629d3ceecc425ec7684a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 16 Feb 2011 20:33:24 +0100 Subject: [PATCH 1/4] Fixed #2584 (false positive 'variable n is never used') --- lib/tokenize.cpp | 2 +- test/testtokenize.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index be64337b5..90287bb39 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4712,7 +4712,7 @@ void Tokenizer::simplifyCompoundAssignment() // "a+=b" => "a = a + b" for (Token *tok = _tokens; tok; tok = tok->next()) { - if (Token::Match(tok, "[;{}:] *| (| %var%")) + if (Token::Match(tok, "[;{}] (") || Token::Match(tok, "[;{}:] *| (| %var%")) { if (tok->str() == ":") { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 76b3b7a40..978ec46d7 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5196,6 +5196,7 @@ private: ASSERT_EQUALS("{ x = x >> y ; }", tokenizeAndStringify("{ x >>= y;}")); ASSERT_EQUALS("; * p = * p + y ;", tokenizeAndStringify("; *p += y;")); + ASSERT_EQUALS("; ( * p ) = ( * p ) + y ;", tokenizeAndStringify("; (*p) += y;")); ASSERT_EQUALS("; * ( p [ 0 ] ) = * ( p [ 0 ] ) + y ;", tokenizeAndStringify("; *(p[0]) += y;")); ASSERT_EQUALS("case 0 : x = x + y ; break ;", tokenizeAndStringify("case 0: x += y; break;")); From 0ee583e324c2056fcb940c082739ef3dfffc8cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 16 Feb 2011 20:56:02 +0100 Subject: [PATCH 2/4] Fixed gcc compiler warnings (signedness) --- cli/cppcheckexecutor.cpp | 2 +- lib/cppcheck.cpp | 2 +- lib/errorlogger.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 491f48caa..33092b363 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -96,7 +96,7 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c std::vector::iterator iterBegin = filenames.begin(); for (int i = (int)filenames.size() - 1; i >= 0; i--) { - if (matcher.Match(filenames[i])) + if (matcher.Match(filenames[(unsigned int)i])) filenames.erase(iterBegin + i); } } diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 4febd096b..248bb6cb9 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -348,7 +348,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) unsigned int pos2 = (unsigned int)ovector[1]; // jump to the end of the match for the next pcre_exec - pos = pos2; + pos = (int)pos2; // determine location.. ErrorLogger::ErrorMessage::FileLocation loc; diff --git a/lib/errorlogger.h b/lib/errorlogger.h index 410bbf4c4..384562b54 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -110,7 +110,7 @@ public: line = 0; } - FileLocation(const std::string &file, int aline) + FileLocation(const std::string &file, unsigned int aline) : line(aline), _file(file) { } From 53aaf22633be5eddd35532767916786ea149350e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 16 Feb 2011 21:31:35 +0100 Subject: [PATCH 3/4] CheckClass::noMemset: Added TODO test case --- test/testclass.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/testclass.cpp b/test/testclass.cpp index b16e4978c..9ea23b8fb 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -2932,6 +2932,17 @@ private: " memset(&fail, 0, sizeof(struct A));\n" "}\n"); ASSERT_EQUALS("[test.cpp:10]: (error) Using 'memset' on struct that contains a 'std::string'\n", errout.str()); + + checkNoMemset("struct Fred\n" + "{\n" + " std::string s;\n" + "};\n" + "void f()\n" + "{\n" + " Fred fred;\n" + " memset(&fred, 0, sizeof(fred));\n" + "}\n"); + TODO_ASSERT_EQUALS("error", "", errout.str()); } void memsetVector() From a4de6a345572012346e68ec3edfa1f09bc3e0d6d Mon Sep 17 00:00:00 2001 From: Greg Hewgill Date: Thu, 17 Feb 2011 21:46:14 +1300 Subject: [PATCH 4/4] be sure to list unmatched suppressions only for the currently processed file --- lib/cppcheck.cpp | 2 +- lib/settings.cpp | 5 +++-- lib/settings.h | 2 +- test/testsuppressions.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 4febd096b..b23fd311d 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -197,7 +197,7 @@ unsigned int CppCheck::check() _errorLogger.reportOut("Bailing out from checking " + fixedpath + ": " + e.what()); } - reportUnmatchedSuppressions(_settings.nomsg.getUnmatchedLocalSuppressions()); + reportUnmatchedSuppressions(_settings.nomsg.getUnmatchedLocalSuppressions(fname)); _errorLogger.reportStatus(c + 1, (unsigned int)_filenames.size()); } diff --git a/lib/settings.cpp b/lib/settings.cpp index b523c1903..2aeeb6c1a 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -312,12 +312,13 @@ bool Settings::Suppressions::isSuppressedLocal(const std::string &errorId, const return _suppressions[errorId].isSuppressedLocal(file, line); } -std::list Settings::Suppressions::getUnmatchedLocalSuppressions() const +std::list Settings::Suppressions::getUnmatchedLocalSuppressions(const std::string &file) const { std::list r; for (std::map::const_iterator i = _suppressions.begin(); i != _suppressions.end(); ++i) { - for (std::map >::const_iterator f = i->second._files.begin(); f != i->second._files.end(); ++f) + std::map >::const_iterator f = i->second._files.find(file); + if (f != i->second._files.end()) { for (std::map::const_iterator l = f->second.begin(); l != f->second.end(); ++l) { diff --git a/lib/settings.h b/lib/settings.h index b65d741c4..3ce7f4e2d 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -239,7 +239,7 @@ public: * @brief Returns list of unmatched local (per-file) suppressions. * @return list of unmatched suppressions */ - std::list getUnmatchedLocalSuppressions() const; + std::list getUnmatchedLocalSuppressions(const std::string &file) const; /** * @brief Returns list of unmatched global (glob pattern) suppressions. diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index b8adaad47..d27f75f56 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -36,6 +36,7 @@ private: void run() { TEST_CASE(suppressionsSettings); + TEST_CASE(suppressionsMultiFile); } // Check the suppression @@ -84,6 +85,26 @@ private: reportUnmatchedSuppressions(settings.nomsg.getUnmatchedGlobalSuppressions()); } + // Check the suppression for multiple files + void checkSuppression(const char *names[], const char *codes[], const std::string &suppression = "") + { + // Clear the error log + errout.str(""); + + Settings settings; + settings._inlineSuppressions = true; + if (!suppression.empty()) + settings.nomsg.addSuppressionLine(suppression); + + CppCheck cppCheck(*this, true); + cppCheck.settings(settings); + for (int i = 0; names[i] != NULL; ++i) + cppCheck.addFile(names[i], codes[i]); + cppCheck.check(); + + reportUnmatchedSuppressions(cppCheck.settings().nomsg.getUnmatchedGlobalSuppressions()); + } + void runChecks(void (TestSuppressions::*check)(const char[], const std::string &)) { // check to make sure the appropriate error is present @@ -167,6 +188,23 @@ private: runChecks(&TestSuppressions::checkSuppressionThreads); } + void suppressionsMultiFile() + { + const char *names[] = {"abc.cpp", "xyz.cpp", NULL}; + const char *codes[] = { + "void f() {\n" + "}\n", + "void f() {\n" + " int a;\n" + " a++;\n" + "}\n", + }; + + // suppress uninitvar for this file and line + checkSuppression(names, codes, "uninitvar:xyz.cpp:3"); + ASSERT_EQUALS("", errout.str()); + } + }; REGISTER_TEST(TestSuppressions)