From 25b4e5f1eb70287b3591ac95152458cc95ab24c0 Mon Sep 17 00:00:00 2001 From: thanhtam92 Date: Sun, 12 Aug 2018 01:33:37 +0700 Subject: [PATCH 01/10] Allow syntax "typedef 'typename' unsigned 'typename' (functon_name)()" for some old libraries https://trac.cppcheck.net/ticket/7792 --- lib/tokenize.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4e69ecbe8ca..aff98f0982a 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -674,6 +674,12 @@ void Tokenizer::simplifyTypedef() typeEnd = tokOffset; tokOffset = tokOffset->next(); + while(tokOffset && Token::Match(tokOffset, "%type%") && + (tokOffset->isStandardType() || Token::Match(tokOffset, "unsigned|signed")) ) { + typeEnd = tokOffset; + tokOffset = tokOffset->next(); + } + bool atEnd = false; while (!atEnd) { if (tokOffset && tokOffset->str() == "::") { From 98315ddd681c192f78f50ecdcc62bc735a2c0772 Mon Sep 17 00:00:00 2001 From: thanhtam92 Date: Sun, 12 Aug 2018 02:27:43 +0700 Subject: [PATCH 02/10] Unnecessary check of "tokOffset", match-function already checks if it is null --- lib/tokenize.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index aff98f0982a..52fee7e7847 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -674,11 +674,11 @@ void Tokenizer::simplifyTypedef() typeEnd = tokOffset; tokOffset = tokOffset->next(); - while(tokOffset && Token::Match(tokOffset, "%type%") && - (tokOffset->isStandardType() || Token::Match(tokOffset, "unsigned|signed")) ) { - typeEnd = tokOffset; - tokOffset = tokOffset->next(); - } + while(Token::Match(tokOffset, "%type%") && + (tokOffset->isStandardType() || Token::Match(tokOffset, "unsigned|signed")) ) { + typeEnd = tokOffset; + tokOffset = tokOffset->next(); + } bool atEnd = false; while (!atEnd) { From 4b67cb593641b086142700e25af1de43a741baaa Mon Sep 17 00:00:00 2001 From: thanhtam92 Date: Sun, 12 Aug 2018 21:18:37 +0700 Subject: [PATCH 03/10] add testcase ticket #7792: simplifyTypedef124 --- test/testsimplifytypedef.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index cc5cb871a7c..755778cdd1c 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -161,6 +161,7 @@ class TestSimplifyTypedef : public TestFixture { TEST_CASE(simplifyTypedef121); // ticket #5766 TEST_CASE(simplifyTypedef122); // segmentation fault TEST_CASE(simplifyTypedef123); // ticket #7406 + TEST_CASE(simplifyTypedef124); // ticket #7792 TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -2508,6 +2509,22 @@ class TestSimplifyTypedef : public TestFixture { ASSERT_EQUALS("", errout.str()); } + void simplifyTypedef124() { // ticket #7792 + const char code[] = "typedef long unsigned int size_t;\n" + "typedef size_t (my_func)(char *, size_t, size_t, void *);"; + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS_WITHOUT_LINENUMBERS("[test.cpp:1]: (debug) Failed to parse 'typedef long unsigned int size_t ;'. The checking continues anyway.\n", errout.str()); + + const char code1[] = "typedef long unsigned int uint32_t;\n" + "typedef uint32_t (my_func)(char *, uint32_t, uint32_t, void *);"; + + // Check for output.. + checkSimplifyTypedef(code1); + ASSERT_EQUALS("", errout.str()); + + } void simplifyTypedefFunction1() { { From cfc077bd310e40751d7c8f901bb17a57ed4d2d2b Mon Sep 17 00:00:00 2001 From: thanhtam92 Date: Fri, 17 Aug 2018 00:30:04 +0700 Subject: [PATCH 04/10] Ticket 7792: suppression both exit_code when cppcheck suppressions --- lib/cppcheck.cpp | 15 ++++++++++++--- lib/cppcheck.h | 2 ++ test/testsuppressions.cpp | 11 +++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index b5f48723e68..de6eb1a1574 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), mSuppressExitCode(false), mUseGlobalSuppressions(useGlobalSuppressions), mTooManyConfigs(false), mSimplify(true) { } @@ -499,6 +499,11 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string if (internalErrorFound && (mExitCode==0)) { mExitCode = 1; } + + if (mSuppressExitCode) { + mExitCode = 0; + } + return mExitCode; } @@ -763,11 +768,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)) { + mSuppressExitCode = true; return; + } } else { - if (mSettings.nomsg.isSuppressedLocal(errorMessage)) + if (mSettings.nomsg.isSuppressedLocal(errorMessage)) { + mSuppressExitCode = true; return; + } } if (!mSettings.nofail.isSuppressed(errorMessage) && (mUseGlobalSuppressions || !mSettings.nomsg.isSuppressed(errorMessage))) diff --git a/lib/cppcheck.h b/lib/cppcheck.h index de3236e27e7..02a0f5ce85e 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -215,6 +215,8 @@ class CPPCHECKLIB CppCheck : ErrorLogger { unsigned int mExitCode; + bool mSuppressExitCode; + bool mUseGlobalSuppressions; /** Are there too many configs? */ diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index f3b8c389f03..521e14fce71 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -64,6 +64,8 @@ class TestSuppressions : public TestFixture { TEST_CASE(unusedFunction); TEST_CASE(matchglob); + + TEST_CASE(suppressingSyntaxErrorAndExitCode); } void suppressionsBadId1() const { @@ -564,6 +566,15 @@ class TestSuppressions : public TestFixture { 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()); + } + }; REGISTER_TEST(TestSuppressions) From f2a7b754dad422cb8d354169a0bc40693aedf726 Mon Sep 17 00:00:00 2001 From: thanhtam92 Date: Fri, 17 Aug 2018 00:35:02 +0700 Subject: [PATCH 05/10] Ticket 7792: suppression both exit_code when cppcheck suppressions - reset value mSuppressExitCode every call check --- lib/cppcheck.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index de6eb1a1574..ca613ea4ce4 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -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; + mSuppressExitCode = false; // only show debug warnings for accepted C/C++ source files if (!Path::acceptFile(filename)) From 5d98fc438bbb32429dd7a3a36bc16f08cb0780d8 Mon Sep 17 00:00:00 2001 From: tamdo12 Date: Fri, 17 Aug 2018 17:08:53 +0700 Subject: [PATCH 06/10] Clean up comment for simplifytypedef124 --- test/testsimplifytypedef.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 755778cdd1c..613e4a0e168 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -161,7 +161,7 @@ class TestSimplifyTypedef : public TestFixture { TEST_CASE(simplifyTypedef121); // ticket #5766 TEST_CASE(simplifyTypedef122); // segmentation fault TEST_CASE(simplifyTypedef123); // ticket #7406 - TEST_CASE(simplifyTypedef124); // ticket #7792 + TEST_CASE(simplifyTypedef124); TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -2509,7 +2509,7 @@ class TestSimplifyTypedef : public TestFixture { ASSERT_EQUALS("", errout.str()); } - void simplifyTypedef124() { // ticket #7792 + void simplifyTypedef124() { const char code[] = "typedef long unsigned int size_t;\n" "typedef size_t (my_func)(char *, size_t, size_t, void *);"; From 9ae8afc4ff3461e9c8e3753286ef94d1d59574ae Mon Sep 17 00:00:00 2001 From: tamdo12 Date: Fri, 17 Aug 2018 21:19:10 +0700 Subject: [PATCH 07/10] Add case suppress only one error in file Add more testcase to clear --- lib/cppcheck.cpp | 2 ++ test/testsuppressions.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 1f6da6f45f1..ebd985cbd3d 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -755,6 +755,8 @@ void CppCheck::purgedConfigurationMessage(const std::string &file, const std::st void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg) { + mSuppressExitCode = false; + if (!mSettings.library.reportErrors(msg.file0)) return; diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index e79bb78599d..25498a3450e 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -592,6 +592,20 @@ class TestSuppressions : public TestFixture { 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()); } }; From fe11344d49ab2e216c3dd5820c8b48ed353e215a Mon Sep 17 00:00:00 2001 From: thanhtam92 Date: Sat, 8 Sep 2018 12:18:52 +0700 Subject: [PATCH 08/10] Update ticket 7792: - Suppression both exit_code and syntaxError when call cppcheck suppressions - Addition testcase in testsuppressions.cpp --- lib/cppcheck.cpp | 8 ++++---- test/testsuppressions.cpp | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index ebd985cbd3d..5146b6663e6 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -451,6 +451,10 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string false); reportErr(errmsg); + if(mSuppressExitCode){ + internalErrorFound = false; + continue; + } } } @@ -501,10 +505,6 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string mExitCode = 1; } - if (mSuppressExitCode) { - mExitCode = 0; - } - return mExitCode; } diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index 25498a3450e..9e102510067 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -606,6 +606,15 @@ class TestSuppressions : public TestFixture { "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 } }; From 29d67b1b3827f2e4440e90888d31b5fc5abcde24 Mon Sep 17 00:00:00 2001 From: thanhtam92 Date: Sat, 8 Sep 2018 14:34:53 +0700 Subject: [PATCH 09/10] Revert "Clean up comment for simplifytypedef124" This reverts commit 5d98fc438bbb32429dd7a3a36bc16f08cb0780d8. --- test/testsimplifytypedef.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 613e4a0e168..755778cdd1c 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -161,7 +161,7 @@ class TestSimplifyTypedef : public TestFixture { TEST_CASE(simplifyTypedef121); // ticket #5766 TEST_CASE(simplifyTypedef122); // segmentation fault TEST_CASE(simplifyTypedef123); // ticket #7406 - TEST_CASE(simplifyTypedef124); + TEST_CASE(simplifyTypedef124); // ticket #7792 TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -2509,7 +2509,7 @@ class TestSimplifyTypedef : public TestFixture { ASSERT_EQUALS("", errout.str()); } - void simplifyTypedef124() { + void simplifyTypedef124() { // ticket #7792 const char code[] = "typedef long unsigned int size_t;\n" "typedef size_t (my_func)(char *, size_t, size_t, void *);"; From 773f3afbccc817ffeee8f998f0abbb5f383d738f Mon Sep 17 00:00:00 2001 From: thanhtam92 Date: Sat, 8 Sep 2018 14:44:19 +0700 Subject: [PATCH 10/10] Clean code: Change variable mSuppressExitCode -> mSuppressInternalErrorFound --- lib/cppcheck.cpp | 12 ++++++------ lib/cppcheck.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 5146b6663e6..0779562706b 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), mSuppressExitCode(false), mUseGlobalSuppressions(useGlobalSuppressions), mTooManyConfigs(false), mSimplify(true) + : mErrorLogger(errorLogger), mExitCode(0), mSuppressInternalErrorFound(false), mUseGlobalSuppressions(useGlobalSuppressions), mTooManyConfigs(false), mSimplify(true) { } @@ -108,7 +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; - mSuppressExitCode = false; + mSuppressInternalErrorFound = false; // only show debug warnings for accepted C/C++ source files if (!Path::acceptFile(filename)) @@ -451,7 +451,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string false); reportErr(errmsg); - if(mSuppressExitCode){ + if(mSuppressInternalErrorFound){ internalErrorFound = false; continue; } @@ -755,7 +755,7 @@ void CppCheck::purgedConfigurationMessage(const std::string &file, const std::st void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg) { - mSuppressExitCode = false; + mSuppressInternalErrorFound = false; if (!mSettings.library.reportErrors(msg.file0)) return; @@ -772,12 +772,12 @@ void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg) if (mUseGlobalSuppressions) { if (mSettings.nomsg.isSuppressed(errorMessage)) { - mSuppressExitCode = true; + mSuppressInternalErrorFound = true; return; } } else { if (mSettings.nomsg.isSuppressedLocal(errorMessage)) { - mSuppressExitCode = true; + mSuppressInternalErrorFound = true; return; } } diff --git a/lib/cppcheck.h b/lib/cppcheck.h index 02a0f5ce85e..efbd31f6a78 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -215,7 +215,7 @@ class CPPCHECKLIB CppCheck : ErrorLogger { unsigned int mExitCode; - bool mSuppressExitCode; + bool mSuppressInternalErrorFound; bool mUseGlobalSuppressions;