From 7ad09b44c30dc68a18a4a915f20198ce7a0691f6 Mon Sep 17 00:00:00 2001 From: tam do thanh Date: Sat, 8 Sep 2018 16:09:49 +0700 Subject: [PATCH] Ticket 7792: Suppression both exit_code and syntaxError when call cppcheck suppressions (#1345) --- lib/cppcheck.cpp | 18 +++++++++++++++--- lib/cppcheck.h | 2 ++ test/testsuppressions.cpp | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 182edeba1..077956270 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -54,7 +54,7 @@ static TimerResults S_timerResults; static const CWE CWE398(398U); // Indicator of Poor Code Quality CppCheck::CppCheck(ErrorLogger &errorLogger, bool useGlobalSuppressions) - : mErrorLogger(errorLogger), mExitCode(0), mUseGlobalSuppressions(useGlobalSuppressions), mTooManyConfigs(false), mSimplify(true) + : mErrorLogger(errorLogger), mExitCode(0), mSuppressInternalErrorFound(false), mUseGlobalSuppressions(useGlobalSuppressions), mTooManyConfigs(false), mSimplify(true) { } @@ -108,6 +108,7 @@ unsigned int CppCheck::check(const ImportProject::FileSettings &fs) unsigned int CppCheck::checkFile(const std::string& filename, const std::string &cfgname, std::istream& fileStream) { mExitCode = 0; + mSuppressInternalErrorFound = false; // only show debug warnings for accepted C/C++ source files if (!Path::acceptFile(filename)) @@ -450,6 +451,10 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string false); reportErr(errmsg); + if(mSuppressInternalErrorFound){ + internalErrorFound = false; + continue; + } } } @@ -499,6 +504,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string if (internalErrorFound && (mExitCode==0)) { mExitCode = 1; } + return mExitCode; } @@ -749,6 +755,8 @@ void CppCheck::purgedConfigurationMessage(const std::string &file, const std::st void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg) { + mSuppressInternalErrorFound = false; + if (!mSettings.library.reportErrors(msg.file0)) return; @@ -763,11 +771,15 @@ void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg) const Suppressions::ErrorMessage errorMessage = msg.toSuppressionsErrorMessage(); if (mUseGlobalSuppressions) { - if (mSettings.nomsg.isSuppressed(errorMessage)) + if (mSettings.nomsg.isSuppressed(errorMessage)) { + mSuppressInternalErrorFound = true; return; + } } else { - if (mSettings.nomsg.isSuppressedLocal(errorMessage)) + if (mSettings.nomsg.isSuppressedLocal(errorMessage)) { + mSuppressInternalErrorFound = true; return; + } } if (!mSettings.nofail.isSuppressed(errorMessage) && (mUseGlobalSuppressions || !mSettings.nomsg.isSuppressed(errorMessage))) diff --git a/lib/cppcheck.h b/lib/cppcheck.h index de3236e27..efbd31f6a 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -215,6 +215,8 @@ private: unsigned int mExitCode; + bool mSuppressInternalErrorFound; + bool mUseGlobalSuppressions; /** Are there too many configs? */ diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index 167f640b5..9e1025100 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -64,6 +64,8 @@ private: TEST_CASE(unusedFunction); TEST_CASE(matchglob); + + TEST_CASE(suppressingSyntaxErrorAndExitCode); } void suppressionsBadId1() const { @@ -583,6 +585,38 @@ private: ASSERT_EQUALS(true, Suppressions::matchglob("?y?", "xyz")); ASSERT_EQUALS(true, Suppressions::matchglob("?/?/?", "x/y/z")); } + + void suppressingSyntaxErrorAndExitCode() { + std::map files; + files["test.cpp"] = "fi if;"; + + ASSERT_EQUALS(0, checkSuppression(files, "*:test.cpp")); + ASSERT_EQUALS("", errout.str()); + + // multi files, but only suppression one + std::map mfiles; + mfiles["test.cpp"] = "fi if;"; + mfiles["test2.cpp"] = "fi if"; + ASSERT_EQUALS(1, checkSuppression(mfiles, "*:test.cpp")); + ASSERT_EQUALS("[test2.cpp:1]: (error) syntax error\n", errout.str()); + + // multi error in file, but only suppression one error + std::map file2; + file2["test.cpp"] = "fi fi\n" + "if if;"; + ASSERT_EQUALS(1, checkSuppression(file2, "*:test.cpp:1")); // suppress all error at line 1 of test.cpp + ASSERT_EQUALS("[test.cpp:2]: (error) syntax error\n", errout.str()); + + // multi error in file, but only suppression one error (2) + std::map file3; + file3["test.cpp"] = "void f(int x, int y){\n" + " int a = x/0;\n" + " int b = y/0;\n" + "}\n" + "f(0, 1);\n"; + ASSERT_EQUALS(1, checkSuppression(file3, "zerodiv:test.cpp:3")); // suppress 'errordiv' at line 3 of test.cpp + } + }; REGISTER_TEST(TestSuppressions)