From 7b942650c278dfadc728fdf3c97e2e2307dfa085 Mon Sep 17 00:00:00 2001 From: Simon Martin Date: Sat, 28 Jun 2014 09:36:51 +0200 Subject: [PATCH] Ticket #5952: Simplify redundant parentheses in pointer variable declarations. --- lib/tokenize.cpp | 14 ++++++++++++++ test/testtokenize.cpp | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 992449e39..19865dd53 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7026,6 +7026,20 @@ bool Tokenizer::simplifyRedundantParentheses() tok->deleteThis(); ret = true; } + + if (Token::Match(tok->previous(), "*|& ( %var% )")) { + // We may have a variable declaration looking like "type_name *(var_name)" + Token *tok2 = tok->tokAt(-2); + while (tok2 && Token::Match(tok2, "%type%|static|const|extern") && tok2->str() != "operator") { + tok2 = tok2->previous(); + } + if (tok2 && !Token::Match(tok2, "[;,{]")) { + // Not a variable declaration + } else { + tok->deleteThis(); + tok->deleteNext(); + } + } } return ret; } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index f13dceeb0..dd03dd7cc 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -377,6 +377,7 @@ private: TEST_CASE(removeParentheses19); // ((typeof(x) *)0) TEST_CASE(removeParentheses20); // Ticket #5479: a>(2); TEST_CASE(removeParentheses21); // Don't "simplify" casts + TEST_CASE(removeParentheses22); TEST_CASE(tokenize_double); TEST_CASE(tokenize_strings); @@ -5751,6 +5752,20 @@ private: ASSERT_EQUALS("a = ( int ) - b ;", tokenizeAndStringify("a = ((int)-b);", false)); } + void removeParentheses22() { + static char code[] = "struct S { " + "char *(a); " + "char &(b); " + "const static char *(c); " + "} ;"; + static char exp[] = "struct S { " + "char * a ; " + "char & b ; " + "const static char * c ; " + "} ;"; + ASSERT_EQUALS(exp, tokenizeAndStringify(code)); + } + void tokenize_double() { const char code[] = "void f()\n" "{\n"