From 1c7027140ad7b0aecbfeac5fcfb13b27cfdc8ebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 6 Sep 2012 16:16:29 +0200 Subject: [PATCH] Tokenizer: Improved simplifyRedundantParanthesis --- lib/tokenize.cpp | 9 +++------ test/testtokenize.cpp | 5 +++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a97a6d828..f1c433231 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6587,14 +6587,11 @@ bool Tokenizer::simplifyRedundantParenthesis() continue; } - if (Token::Match(tok->previous(), "(|[|,| ( %var% %op% %var% ) ,|]|)") || - Token::Match(tok->previous(), "(|[|,| ( %var% %op% %num% ) ,|]|)")) { - // We have "( var %op% var )", remove the parenthesis + while (Token::Match(tok->previous(), "[{([,:] ( !!{") && Token::Match(tok->link(), ") [;,])]")) { + // We have "( ... )", remove the parenthesis + tok->link()->deleteThis(); tok->deleteThis(); - tok = tok->tokAt(2); - tok->deleteNext(); ret = true; - continue; } if (Token::Match(tok, "( ( %bool% )") || diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 88078f588..1af90ef7a 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -295,6 +295,7 @@ private: TEST_CASE(removeParentheses12); // Ticket #2760 ',(b)=' TEST_CASE(removeParentheses13); TEST_CASE(removeParentheses14); // Ticket #3309 + TEST_CASE(removeParentheses15); // Ticket #4142 TEST_CASE(tokenize_double); TEST_CASE(tokenize_strings); @@ -4577,6 +4578,10 @@ private: ASSERT_EQUALS("; if ( ! ( i & 1 ) ) { ; } ;", tokenizeAndStringify("; if ( (i & 1) == 0 ); ;", false)); } + void removeParentheses15() { + ASSERT_EQUALS("a = b ? c : 123 ;", tokenizeAndStringify("a = b ? c : (123);", false)); + } + void tokenize_double() { const char code[] = "void f()\n" "{\n"