From b7171c3cd2cc50f0297a88a717ee2ff21137aab2 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Thu, 18 Jun 2009 23:26:21 +0300 Subject: [PATCH 1/2] Fix ticket #413 (false positive: C-style pointer casting for pure virtual function) http://sourceforge.net/apps/trac/cppcheck/ticket/413 --- src/checkother.cpp | 3 +++ test/testother.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/checkother.cpp b/src/checkother.cpp index d42781abc..2c2ac9223 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -48,6 +48,9 @@ void CheckOther::WarningOldStylePointerCast() if (!Token::Match(tok, "( %type% * ) %var%")) continue; + if (Token::simpleMatch(tok->tokAt(4), "const")) + continue; + // Is "type" a class? const std::string pattern("class " + tok->next()->str()); if (!Token::findmatch(_tokenizer->tokens(), pattern.c_str())) diff --git a/test/testother.cpp b/test/testother.cpp index aeb349ece..55fafa41e 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -58,6 +58,8 @@ private: TEST_CASE(nullpointer1); TEST_CASE(nullpointer2); + + TEST_CASE(oldStylePointerCast); } void check(const char code[]) @@ -427,6 +429,35 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); } + + void checkOldStylePointerCast(const char code[]) + { + // Tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + tokenizer.setVarId(); + + // Clear the error buffer.. + errout.str(""); + + // Check for redundant code.. + Settings settings; + settings._checkCodingStyle = true; + CheckOther checkOther(&tokenizer, &settings, this); + checkOther.WarningOldStylePointerCast(); + } + + void oldStylePointerCast() + { + checkOldStylePointerCast("class B;\n" + "class A\n" + "{\n" + " virtual void abc(B *) const = 0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + }; REGISTER_TEST(TestOther) From e8d1905e6d4881eeaa44874efbeb3f5f70e899d7 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Fri, 19 Jun 2009 00:00:16 +0300 Subject: [PATCH 2/2] Fix ticket #418 (delete a,b; not tokenized correctly) http://sourceforge.net/apps/trac/cppcheck/ticket/418 --- src/tokenize.cpp | 20 +++++++++++++++----- test/testsimplifytokens.cpp | 23 +++++++++++++++++------ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 4de0e4013..d3bf163e2 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -2976,11 +2976,21 @@ bool Tokenizer::simplifyCommaNearKeyWords() // We must not accept just any keyword, e.g. accepting int // would cause function parameters to corrupt. - if (!Token::Match(tok->next(), "delete")) - continue; - - tok->str(";"); - ret = true; + if (Token::Match(tok->next(), "delete")) + { + // Handle "delete a, delete b;" + tok->str(";"); + ret = true; + } + else if (tok->previous() && + Token::Match(tok->previous()->previous(), "delete") && + tok->next()->varId() != 0) + { + // Handle "delete a, b;" + tok->str(";"); + tok->insertToken("delete"); + ret = true; + } } return ret; diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index c715604b1..8808b601d 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -978,12 +978,23 @@ private: void comma_keyword() { - const char code[] = "void foo()\n" - "{\n" - " char *a, *b;\n" - " delete a, delete b;\n" - "}\n"; - ASSERT_EQUALS(" void foo ( ) { char * a ; char * b ; delete a ; delete b ; }", sizeof_(code)); + { + const char code[] = "void foo()\n" + "{\n" + " char *a, *b;\n" + " delete a, delete b;\n" + "}\n"; + ASSERT_EQUALS(" void foo ( ) { char * a ; char * b ; delete a ; delete b ; }", sizeof_(code)); + } + + { + const char code[] = "void foo()\n" + "{\n" + " char *a, *b;\n" + " delete a, b;\n" + "}\n"; + ASSERT_EQUALS(" void foo ( ) { char * a ; char * b ; delete a ; delete b ; }", sizeof_(code)); + } } };