diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 79d3c919b..6e2b7b9b5 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -2579,6 +2579,24 @@ bool Tokenizer::simplifyRedundantParanthesis() ret = true; } + while (Token::Match(tok->previous(), "[;{] ( delete %var% ) ;")) + { + // We have "( delete var )", remove the outer + // paranthesis + tok->tokAt(3)->deleteThis(); + tok->deleteThis(); + ret = true; + } + + while (Token::Match(tok->previous(), "[;{] ( delete [ ] %var% ) ;")) + { + // We have "( delete [] var )", remove the outer + // paranthesis + tok->tokAt(5)->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 90578556f..092c47f2a 100644 --- a/src/tokenize.h +++ b/src/tokenize.h @@ -212,6 +212,8 @@ private: * Remove redundant paranthesis: * - "((x))" => "(x)" * - "(function())" => "function()" + * - "(delete x)" => "delete x" + * - "(delete [] x)" => "delete [] x" * @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 f9eae28c8..49d2f869f 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -131,6 +131,7 @@ private: TEST_CASE(removeParantheses2); TEST_CASE(removeParantheses3); TEST_CASE(removeParantheses4); // Ticket #390 + TEST_CASE(removeParantheses5); // Ticket #392 TEST_CASE(simplify_numeric_condition); TEST_CASE(tokenize_double); @@ -1748,6 +1749,49 @@ private: ASSERT_EQUALS(" void foo ( ) { free ( p ) ; }", ostr.str()); } + void removeParantheses5() + { + // Simplify "( delete x )" into "delete x" + { + const char code[] = "void foo()\n" + "{\n" + " (delete 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 ( ) { delete p ; }", ostr.str()); + } + + // Simplify "( delete [] x )" into "delete [] x" + { + const char code[] = "void foo()\n" + "{\n" + " (delete [] 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 ( ) { delete [ ] p ; }", ostr.str()); + } + } + void simplify_numeric_condition() { {