From 05049529508809264487d306457eca046be1e3b2 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Tue, 6 Dec 2011 16:38:13 +0100 Subject: [PATCH] Remove some redundant parenthesis - part 9 --- lib/tokenize.cpp | 37 +++++++++---- test/testsimplifytokens.cpp | 104 ++++++++++++++---------------------- test/testtokenize.cpp | 6 +-- 3 files changed, 68 insertions(+), 79 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 3970e0fad..3c79f7617 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2694,35 +2694,43 @@ static unsigned int templateParameters(const Token *tok) */ static void removeTemplates(Token *tok) { + bool goback = false; for (; tok; tok = tok->next()) { - if (! Token::simpleMatch(tok, "template <")) + if (goback) { + tok = tok->previous(); + goback = false; + } + if (!Token::simpleMatch(tok, "template <")) continue; for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { if (tok2->str() == "{") { - tok2 = tok2->link(); - tok2 = tok2 ? tok2->next() : 0; + tok2 = tok2->link()->next(); Token::eraseTokens(tok, tok2); - tok->str(";"); + if (tok2 && tok2->str() == ";" && tok2->next()) + tok->deleteNext(); + tok->deleteThis(); + goback = true; break; } // don't remove constructor if (tok2->str() == "explicit") { Token::eraseTokens(tok, tok2); - tok->str(";"); + tok->deleteThis(); + goback = true; break; } if (tok2->str() == "(") { tok2 = tok2->link(); - if (!tok2) - break; } else if (tok2->str() == ";") { Token::eraseTokens(tok, tok2->next()); - tok->str(";"); + tok->deleteThis(); + goback = true; break; } else if (Token::Match(tok2, ">|>> class %var% [,)]")) { Token::eraseTokens(tok,tok2->next()); tok->deleteThis(); + goback = true; break; } } @@ -3375,12 +3383,18 @@ void Tokenizer::simplifyTemplates() void Tokenizer::simplifyTemplates2() { + bool goback = false; for (Token *tok = _tokens; tok; tok = tok->next()) { + if (goback) { + tok = tok->previous(); + goback = false; + } if (tok->str() == "(") tok = tok->link(); - else if (Token::Match(tok, "; %type% <")) { - const Token *tok2 = tok->tokAt(3); + else if (Token::Match(tok, "%type% <") && + (!tok->previous() || tok->previous()->str() == ";")) { + const Token *tok2 = tok->tokAt(2); std::string type; while (Token::Match(tok2, "%type% ,") || Token::Match(tok2, "%num% ,")) { type += tok2->str() + ","; @@ -3388,9 +3402,10 @@ void Tokenizer::simplifyTemplates2() } if (Token::Match(tok2, "%type% > (") || Token::Match(tok2, "%num% > (")) { type += tok2->str(); - tok = tok->next(); tok->str(tok->str() + "<" + type + ">"); Token::eraseTokens(tok, tok2->tokAt(2)); + if (tok == _tokens) + goback = true; } } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 32f1d97e4..c9fc2f590 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -1567,7 +1567,7 @@ private: const char code[] = "template void f(T val) { T a; }\n" "f(10);"; - const std::string expected("; f ( 10 ) ; " + const std::string expected("f ( 10 ) ; " "void f ( int val ) { }"); ASSERT_EQUALS(expected, sizeof_(code)); @@ -1577,8 +1577,7 @@ private: const char code[] = "template class Fred { T a; };\n" "Fred fred;"; - const std::string expected("; " - "Fred fred ; " + const std::string expected("Fred fred ; " "class Fred { int a ; }"); ASSERT_EQUALS(expected, sizeof_(code)); @@ -1588,8 +1587,7 @@ private: const char code[] = "template class Fred { T data[sz]; };\n" "Fred fred;"; - const std::string expected("; " - "Fred fred ; " + const std::string expected("Fred fred ; " "class Fred { float data [ 4 ] ; }"); ASSERT_EQUALS(expected, sizeof_(code)); @@ -1599,8 +1597,7 @@ private: const char code[] = "template class Fred { Fred(); };\n" "Fred fred;"; - const std::string expected("; " - "Fred fred ; " + const std::string expected("Fred fred ; " "class Fred { Fred ( ) ; }"); ASSERT_EQUALS(expected, sizeof_(code)); @@ -1611,8 +1608,7 @@ private: "template Fred::Fred() { }\n" "Fred fred;"; - const std::string expected("; " - "Fred fred ; " + const std::string expected("Fred fred ; " "class Fred { } " "Fred :: Fred ( ) { }"); @@ -1624,10 +1620,9 @@ private: "Fred fred1;\n" "Fred fred2;"; - const std::string expected(";" - " Fred fred1 ;" - " Fred fred2 ;" - " class Fred { }"); + const std::string expected("Fred fred1 ; " + "Fred fred2 ; " + "class Fred { }"); ASSERT_EQUALS(expected, sizeof_(code)); } @@ -1659,15 +1654,13 @@ private: " return 0;\n" "}\n"; - const std::string wanted("; " - "int main ( ) { " + const std::string wanted("int main ( ) { " "std :: vector < int > v ; " "v . push_back ( 4 ) ; " "return 0 ; " "}"); - const std::string current("; " - "int main ( ) { " + const std::string current("int main ( ) { " "ABC < int > :: type v ; " "v . push_back ( 4 ) ; " "return 0 ; " @@ -1715,7 +1708,7 @@ private: ASSERT_EQUALS(";", sizeof_(code)); - ASSERT_EQUALS("class A { ; } ;", sizeof_("class A{ template int foo(T d);};")); + ASSERT_EQUALS("class A { } ;", sizeof_("class A{ template int foo(T d);};")); } void template9() { @@ -1733,7 +1726,7 @@ private: "} ;\n"; // The expected result.. - std::string expected("; void f ( ) { A a ; } ; class A { } class A { }"); + std::string expected("void f ( ) { A a ; } class A { } class A { }"); ASSERT_EQUALS(expected, sizeof_(code)); } @@ -1748,8 +1741,7 @@ private: "}\n"; // The expected result.. - const std::string expected("; " - "void f ( ) " + const std::string expected("void f ( ) " "{" " foo<3,int> ( ) ; " "} " @@ -1767,8 +1759,7 @@ private: "}\n"; // The expected result.. - const std::string expected("; " - "void f ( ) " + const std::string expected("void f ( ) " "{" " char * p ; p = foo<3,char> ( ) ; " "} " @@ -1787,8 +1778,7 @@ private: "}\n"; // The expected result.. - const std::string expected("; " - "void f ( ) " + const std::string expected("void f ( ) " "{" " A<12,12,11> a ; " "} " @@ -1857,8 +1847,7 @@ private: "}\n"; // The expected result.. - const std::string expected("; " - "void a<0> ( ) { } " + const std::string expected("void a<0> ( ) { } " "int main ( ) " "{ a<2> ( ) ; return 0 ; } " "void a<2> ( ) { a<1> ( ) ; } " @@ -1880,8 +1869,7 @@ private: " return 0;\n" "}\n"; - const std::string expected("; " - "int main ( ) { b<2> ( ) ; return 0 ; } " + const std::string expected("int main ( ) { b<2> ( ) ; return 0 ; } " "void b<2> ( ) { a<2> ( ) ; } " "void a ( ) { } " "void a<2> ( ) { }"); @@ -1909,8 +1897,7 @@ private: const char code[] = "template class foo { T a; };\n" "foo *f;"; - const std::string expected("; " - "foo * f ; " + const std::string expected("foo * f ; " "class foo { int a ; }"); ASSERT_EQUALS(expected, sizeof_(code)); @@ -1926,8 +1913,7 @@ private: "}\n"; // The expected result.. - const std::string expected("; " - "void f ( ) " + const std::string expected("void f ( ) " "{" " char p ; p = foo ( ) ; " "} " @@ -1950,7 +1936,7 @@ private: "A a;\n"; // The expected result.. - const std::string expected("; A a ; " + const std::string expected("A a ; " "class A { public: ~ A ( ) ; } " "A :: ~ A ( ) { }"); ASSERT_EQUALS(expected, sizeof_(code)); @@ -1961,8 +1947,7 @@ private: const char code[] = "template struct Fred { T a; };\n" "Fred fred;"; - const std::string expected("; " - "Fred fred ; " + const std::string expected("Fred fred ; " "struct Fred { int a ; }"); ASSERT_EQUALS(expected, sizeof_(code)); @@ -1972,8 +1957,7 @@ private: const char code[] = "template struct Fred { T data[sz]; };\n" "Fred fred;"; - const std::string expected("; " - "Fred fred ; " + const std::string expected("Fred fred ; " "struct Fred { float data [ 4 ] ; }"); ASSERT_EQUALS(expected, sizeof_(code)); @@ -1983,8 +1967,7 @@ private: const char code[] = "template struct Fred { Fred(); };\n" "Fred fred;"; - const std::string expected("; " - "Fred fred ; " + const std::string expected("Fred fred ; " "struct Fred { Fred ( ) ; }"); ASSERT_EQUALS(expected, sizeof_(code)); @@ -1995,8 +1978,7 @@ private: "template Fred::Fred() { }\n" "Fred fred;"; - const std::string expected("; " - "Fred fred ; " + const std::string expected("Fred fred ; " "struct Fred { } " "Fred :: Fred ( ) { }"); @@ -2008,10 +1990,9 @@ private: "Fred fred1;\n" "Fred fred2;"; - const std::string expected(";" - " Fred fred1 ;" - " Fred fred2 ;" - " struct Fred { }"); + const std::string expected("Fred fred1 ; " + "Fred fred2 ; " + "struct Fred { }"); ASSERT_EQUALS(expected, sizeof_(code)); } @@ -2021,8 +2002,7 @@ private: const char code[] = "template struct Fred { T a; };\n" "Fred fred;"; - const std::string expected("; " - "Fred fred ; " + const std::string expected("Fred fred ; " "struct Fred { std :: string a ; }"); ASSERT_EQUALS(expected, sizeof_(code)); @@ -2034,8 +2014,7 @@ private: " std::cout << (foo());\n" "}"; - const std::string expected("; " - "void bar ( ) {" + const std::string expected("void bar ( ) {" " std :: cout << ( foo ( ) ) ; " "} " "void foo ( ) { }"); @@ -2054,8 +2033,7 @@ private: "{};\n" "\n" "bitset<1> z;"; - const char expected[] = "; " - "bitset<1> z ; " + const char expected[] = "bitset<1> z ; " "class bitset<1> : B<4> { } " "struct B<4> { int a [ 4 ] ; }"; ASSERT_EQUALS(expected, sizeof_(code)); @@ -2072,11 +2050,10 @@ private: "\n" "bitset<1> z;"; - const char actual[] = "; bitset<1> z ; " + const char actual[] = "bitset<1> z ; " "class bitset<1> : B < ( ) > { }"; - const char expected[] = "; " - "bitset<1> z ; " + const char expected[] = "bitset<1> z ; " "class bitset<1> : B<4> { } " "struct B<4> { int a [ 4 ] ; }"; @@ -2094,7 +2071,7 @@ private: "\n" "C<2> a;\n"; // TODO: expand A also - ASSERT_EQUALS("; C<2> a ; class C<2> : public A < char [ 2 ] > { }", sizeof_(code)); + ASSERT_EQUALS("C<2> a ; class C<2> : public A < char [ 2 ] > { }", sizeof_(code)); } void template27() { @@ -2107,12 +2084,12 @@ private: // #3226 - inner template const char code[] = "template class Fred {};\n" "Fred > x;\n"; - ASSERT_EQUALS("; Fred> x ; class Fred { } class Fred> { }", sizeof_(code)); + ASSERT_EQUALS("Fred> x ; class Fred { } class Fred> { }", sizeof_(code)); } void template_unhandled() { // An unhandled template usage should be simplified.. - ASSERT_EQUALS("; x ( ) ;", sizeof_(";x();")); + ASSERT_EQUALS("x ( ) ;", sizeof_("x();")); } void template_default_parameter() { @@ -2128,8 +2105,7 @@ private: "}\n"; // The expected result.. - const std::string expected("; " - "void f ( ) " + const std::string expected("void f ( ) " "{" " A a1 ;" " A a2 ; " @@ -2152,8 +2128,7 @@ private: "}\n"; // The expected result.. - const std::string expected("; " - "void f ( ) " + const std::string expected("void f ( ) " "{" " A a1 ;" " A a2 ; " @@ -2186,8 +2161,7 @@ private: " class A" " { int ar [ 3 ] ; }"); - const std::string current("; " - "void f ( ) " + const std::string current("void f ( ) " "{ " "A < int , ( int ) 2 > a1 ; " "A a2 ; " @@ -2270,7 +2244,7 @@ private: const char code[] = "class Fred {\n" " template explicit Fred(T t) { }\n" "}"; - ASSERT_EQUALS("class Fred { ; explicit Fred ( T t ) { } }", tok(code)); + ASSERT_EQUALS("class Fred { explicit Fred ( T t ) { } }", tok(code)); } void namespaces() { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 28baaf551..654f43fe0 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5017,7 +5017,7 @@ private: "{\n" " fn2();\n" "}\n"; - ASSERT_EQUALS(";\n\n\nint main ( )\n{\nfn2 ( ) ;\n}void fn2 ( int t = [ ] { return 1 ; } ( ) )\n{ }", tokenizeAndStringify(code)); + ASSERT_EQUALS("int main ( )\n{\nfn2 ( ) ;\n}void fn2 ( int t = [ ] { return 1 ; } ( ) )\n{ }", tokenizeAndStringify(code)); } void cpp0xtemplate2() { @@ -5033,8 +5033,8 @@ private: "struct S\n" "{};\n" "S s;\n"; - TODO_ASSERT_EQUALS(";\n\n\nS < int , ( int ) 0 > s ;", // wanted result - ";\n\n\nS < int , ( T ) 0 > s ;", // current result + TODO_ASSERT_EQUALS("S < int , ( int ) 0 > s ;", // wanted result + "S < int , ( T ) 0 > s ;", // current result tokenizeAndStringify(code)); }