From 71511f31311cc02a82ccdeb541b76c9b3f4b7105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 6 Feb 2018 08:59:36 +0100 Subject: [PATCH] Refactor f487182 --- lib/checkbufferoverrun.cpp | 2 +- lib/checkuninitvar.cpp | 2 +- lib/checkunusedfunctions.cpp | 2 +- lib/cppcheck.cpp | 2 +- lib/cppcheck.h | 2 -- lib/errorlogger.h | 6 ------ test/testsuppressions.cpp | 9 +++++++++ test/testunusedfunctions.cpp | 6 +++++- 8 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 65731ed08..adcd48696 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -2108,7 +2108,7 @@ bool CheckBufferOverrun::analyseWholeProgram(const std::list & errors = true; } } - return errors && errorLogger.hasErrors(); + return errors; } unsigned int CheckBufferOverrun::sizeOfType(const Token *type) const diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 61d4f1556..54729dbaf 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1606,5 +1606,5 @@ bool CheckUninitVar::analyseWholeProgram(const std::list &file } } - return foundErrors && errorLogger.hasErrors(); + return foundErrors; } diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index a0bbf1e0c..030487c86 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -264,7 +264,7 @@ bool CheckUnusedFunctions::check(ErrorLogger * const errorLogger, const Settings */ } } - return errors && errorLogger->hasErrors(); + return errors; } void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger, diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 2c44a0c56..7f2153c35 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -815,7 +815,7 @@ bool CppCheck::analyseWholeProgram() // Analyse the tokens for (std::list::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) errors |= (*it)->analyseWholeProgram(fileInfo, _settings, *this); - return errors; + return errors && (exitcode > 0); } void CppCheck::analyseWholeProgram(const std::string &buildDir, const std::map &files) diff --git a/lib/cppcheck.h b/lib/cppcheck.h index 74cca27f4..4758e1a57 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -191,8 +191,6 @@ private: */ virtual void reportErr(const ErrorLogger::ErrorMessage &msg); - virtual bool hasErrors() const { return exitcode > 0; } - /** * @brief Information about progress is directed here. * diff --git a/lib/errorlogger.h b/lib/errorlogger.h index 8d7c20cbc..a778a91d2 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -331,12 +331,6 @@ public: */ virtual void reportErr(const ErrorLogger::ErrorMessage &msg) = 0; - /** - * Returns true if an error has been reported which should - * cause a non-zero cppcheck exit code. - */ - virtual bool hasErrors() const { return false; } - /** * Report progress to client * @param filename main file that is checked diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index f440d7470..d6bf9a0c9 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -52,6 +52,8 @@ private: TEST_CASE(suppressionWithRelativePaths); // #4733 TEST_CASE(suppressingSyntaxErrors); // #7076 TEST_CASE(suppressingSyntaxErrorsInline); // #5917 + + TEST_CASE(unusedFunction); } void suppressionsBadId1() const { @@ -149,7 +151,10 @@ private: CppCheck cppCheck(*this, true); Settings& settings = cppCheck.settings(); + settings.exitCode = 1; settings.inlineSuppressions = true; + if (suppression == "unusedFunction") + settings.addEnabled("unusedFunction"); settings.addEnabled("information"); settings.jointSuppressionReport = true; if (!suppression.empty()) { @@ -435,6 +440,10 @@ private: checkSuppression(files, ""); ASSERT_EQUALS("", errout.str()); } + + void unusedFunction() { + ASSERT_EQUALS(0, checkSuppression("void f() {}", "unusedFunction")); + } }; REGISTER_TEST(TestSuppressions) diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index 4cc1862c4..275ce089b 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -77,7 +77,11 @@ private: // Check for unused functions.. CheckUnusedFunctions checkUnusedFunctions(&tokenizer, &settings, this); checkUnusedFunctions.parseTokens(tokenizer, "someFile.c", &settings); - ASSERT(!checkUnusedFunctions.check(this, settings)); + // check() returns error if and only if errout is not empty. + if (checkUnusedFunctions.check(this, settings)) + ASSERT(errout.str() != ""); + else + ASSERT_EQUALS("", errout.str()); } void incondition() {