From 7eb98551042b3920691f72cc765b54ce6337ba88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 18 Dec 2010 09:44:58 +0100 Subject: [PATCH] Tokenizer: Remove redundant parantheses in rhs. Ticket: #2320 --- lib/tokenize.cpp | 11 +++++++++++ test/testtokenize.cpp | 18 ++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index fdfd84202..c06cc5fb4 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6426,6 +6426,17 @@ bool Tokenizer::simplifyRedundantParanthesis() if (tok->str() != "(") continue; + // !!operator = ( x ) ; + if (tok->strAt(-2) != "operator" && + tok->strAt(-1) == "=" && + tok->strAt(1) != "{" && + Token::simpleMatch(tok->link(), ") ;")) + { + tok->link()->deleteThis(); + tok->deleteThis(); + continue; + } + while (Token::simpleMatch(tok, "( (") && tok->link()->previous() == tok->next()->link()) { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 50fda2caa..1f985ff50 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -196,6 +196,7 @@ private: TEST_CASE(removeParantheses7); TEST_CASE(removeParantheses8); // Ticket #1865 TEST_CASE(removeParantheses9); // Ticket #1962 + TEST_CASE(removeParantheses10); // Ticket #2320 TEST_CASE(tokenize_double); TEST_CASE(tokenize_strings); @@ -552,7 +553,7 @@ private: std::ostringstream ostr; for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); - ASSERT_EQUALS(" t = ( & p ) ;", ostr.str()); + ASSERT_EQUALS(" t = & p ;", ostr.str()); } void removeCast3() @@ -1259,7 +1260,7 @@ private: "}\n"; ASSERT_EQUALS( - "void foo ( ) { int n ; n = 10 ; i = ( 10 >> 1 ) ; }", + "void foo ( ) { int n ; n = 10 ; i = 10 >> 1 ; }", simplifyKnownVariables(code)); } { @@ -1270,7 +1271,7 @@ private: "}\n"; ASSERT_EQUALS( - "void foo ( ) { int n ; n = 10 ; i = ( 10 << 1 ) ; }", + "void foo ( ) { int n ; n = 10 ; i = 10 << 1 ; }", simplifyKnownVariables(code)); } { @@ -1281,7 +1282,7 @@ private: "}\n"; ASSERT_EQUALS( - "void foo ( ) { int n ; n = 10 ; i = ( 1 << 10 ) ; }", + "void foo ( ) { int n ; n = 10 ; i = 1 << 10 ; }", simplifyKnownVariables(code)); } { @@ -1292,7 +1293,7 @@ private: "}\n"; ASSERT_EQUALS( - "void foo ( ) { int n ; n = 10 ; i = ( 1 >> 10 ) ; }", + "void foo ( ) { int n ; n = 10 ; i = 1 >> 10 ; }", simplifyKnownVariables(code)); } } @@ -2343,7 +2344,7 @@ private: "1: void foo ( )\n" "2: {\n" "3: int x@1 ; x@1 = 1 ;\n" - "4: y = ( z * x@1 ) ;\n" + "4: y = z * x@1 ;\n" "5: }\n"); ASSERT_EQUALS(expected, tokenizeDebugListing(code)); @@ -3478,6 +3479,11 @@ private: ASSERT_EQUALS("void delete ( double num ) ;", tokenizeAndStringify("void delete(double num);", false)); } + void removeParantheses10() + { + ASSERT_EQUALS("p = buf + 8 ;", tokenizeAndStringify("p = (buf + 8);", false)); + } + void tokenize_double() { const char code[] = "void f()\n"