From eeeb816db957d41f937ea3248b35517942803150 Mon Sep 17 00:00:00 2001 From: Simon Martin Date: Fri, 5 Sep 2014 00:31:58 +0200 Subject: [PATCH] Ticket #6103: Simplify "new (type)" constructs into "new type" to avoid confusion upon certain input. --- lib/tokenize.cpp | 7 +++++++ test/testtokenize.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 62fa44f03..e0e390884 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7108,6 +7108,13 @@ bool Tokenizer::simplifyRedundantParentheses() ret = true; } + if (isCPP() && Token::simpleMatch(tok->previous(), "new (") && Token::Match(tok->link(), ") [;,{}[]")) { + // Remove the parentheses in "new (type)" constructs + tok->link()->deleteThis(); + tok->deleteThis(); + ret = true; + } + if (Token::Match(tok->previous(), "! ( %var% )")) { // Remove the parentheses tok->deleteThis(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index ae03a8873..775a92d33 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -384,6 +384,7 @@ private: TEST_CASE(removeParentheses20); // Ticket #5479: a>(2); TEST_CASE(removeParentheses21); // Don't "simplify" casts TEST_CASE(removeParentheses22); + TEST_CASE(removeParentheses23); // Ticket #6103 - Infinite loop upon valid input TEST_CASE(tokenize_double); TEST_CASE(tokenize_strings); @@ -5911,6 +5912,31 @@ private: ASSERT_EQUALS(exp, tokenizeAndStringify(code)); } + void removeParentheses23() { // Ticket #6103 + // Reported case + { + static char code[] = "* * p f ( ) int = { new int ( * [ 2 ] ) ; void }"; + static char exp[] = "* * p f ( ) int = { new int ( * [ 2 ] ) ; void }"; + ASSERT_EQUALS(exp, tokenizeAndStringify(code)); + } + // Various valid cases + { + static char code[] = "int * f [ 1 ] = { new ( int ) } ;"; + static char exp[] = "int * f [ 1 ] = { new int } ;"; + ASSERT_EQUALS(exp, tokenizeAndStringify(code)); + } + { + static char code[] = "int * * f [ 1 ] = { new ( int ) [ 1 ] } ;"; + static char exp[] = "int * * f [ 1 ] = { new int [ 1 ] } ;"; + ASSERT_EQUALS(exp, tokenizeAndStringify(code)); + } + { + static char code[] = "list < int > * f [ 1 ] = { new ( list < int > ) } ;"; + static char exp[] = "list < int > * f [ 1 ] = { new list < int > } ;"; + ASSERT_EQUALS(exp, tokenizeAndStringify(code)); + } + } + void tokenize_double() { const char code[] = "void f()\n" "{\n"