From 7bee0cd2df584cd6af4066cbdf945aa977a82a51 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Fri, 12 Jun 2009 20:04:58 +0700 Subject: [PATCH] Fixed ticket #390 (wrong allocation and deallocation not detected) http://sourceforge.net/apps/trac/cppcheck/ticket/390 --- src/tokenize.cpp | 10 ++++++++++ src/tokenize.h | 4 +++- test/testtokenize.cpp | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index d52572ef8..d33f5871c 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -2569,6 +2569,16 @@ bool Tokenizer::simplifyRedundantParanthesis() ret = true; } + while (Token::Match(tok->previous(), "[;{] ( %var% (") && + tok->link()->previous() == tok->tokAt(2)->link()) + { + // We have "( func ( *something* ))", remove the outer + // paranthesis + tok->link()->deleteThis(); + tok->deleteThis(); + ret = true; + } + if (Token::Match(tok, "( ( %bool% )") || Token::Match(tok, "( ( %num% )")) { diff --git a/src/tokenize.h b/src/tokenize.h index f7f3bb8cf..90578556f 100644 --- a/src/tokenize.h +++ b/src/tokenize.h @@ -209,7 +209,9 @@ private: bool simplifyFunctionReturn(); /** - * Remove redundant paranthesis: "((x))" => "(x)" + * Remove redundant paranthesis: + * - "((x))" => "(x)" + * - "(function())" => "function()" * @return true if modifications to token-list are done. * false if no modifications are done. */ diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 7171470b8..f9eae28c8 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -130,6 +130,7 @@ private: TEST_CASE(removeParantheses1); // Ticket #61 TEST_CASE(removeParantheses2); TEST_CASE(removeParantheses3); + TEST_CASE(removeParantheses4); // Ticket #390 TEST_CASE(simplify_numeric_condition); TEST_CASE(tokenize_double); @@ -1726,6 +1727,27 @@ private: } } + // Simplify "( function (..))" into "function (..)" + void removeParantheses4() + { + const char code[] = "void foo()\n" + "{\n" + " (free(p));\n" + "}"; + + // tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.simplifyTokenList(); + + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(" void foo ( ) { free ( p ) ; }", ostr.str()); + } + void simplify_numeric_condition() { {