Remove some redundant parenthesis - part 9

This commit is contained in:
Edoardo Prezioso 2011-12-06 16:38:13 +01:00
parent 1b4b8fbefe
commit 0504952950
3 changed files with 68 additions and 79 deletions

View File

@ -2694,35 +2694,43 @@ static unsigned int templateParameters(const Token *tok)
*/ */
static void removeTemplates(Token *tok) static void removeTemplates(Token *tok)
{ {
bool goback = false;
for (; tok; tok = tok->next()) { for (; tok; tok = tok->next()) {
if (! Token::simpleMatch(tok, "template <")) if (goback) {
tok = tok->previous();
goback = false;
}
if (!Token::simpleMatch(tok, "template <"))
continue; continue;
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->str() == "{") { if (tok2->str() == "{") {
tok2 = tok2->link(); tok2 = tok2->link()->next();
tok2 = tok2 ? tok2->next() : 0;
Token::eraseTokens(tok, tok2); Token::eraseTokens(tok, tok2);
tok->str(";"); if (tok2 && tok2->str() == ";" && tok2->next())
tok->deleteNext();
tok->deleteThis();
goback = true;
break; break;
} }
// don't remove constructor // don't remove constructor
if (tok2->str() == "explicit") { if (tok2->str() == "explicit") {
Token::eraseTokens(tok, tok2); Token::eraseTokens(tok, tok2);
tok->str(";"); tok->deleteThis();
goback = true;
break; break;
} }
if (tok2->str() == "(") { if (tok2->str() == "(") {
tok2 = tok2->link(); tok2 = tok2->link();
if (!tok2)
break;
} else if (tok2->str() == ";") { } else if (tok2->str() == ";") {
Token::eraseTokens(tok, tok2->next()); Token::eraseTokens(tok, tok2->next());
tok->str(";"); tok->deleteThis();
goback = true;
break; break;
} else if (Token::Match(tok2, ">|>> class %var% [,)]")) { } else if (Token::Match(tok2, ">|>> class %var% [,)]")) {
Token::eraseTokens(tok,tok2->next()); Token::eraseTokens(tok,tok2->next());
tok->deleteThis(); tok->deleteThis();
goback = true;
break; break;
} }
} }
@ -3375,12 +3383,18 @@ void Tokenizer::simplifyTemplates()
void Tokenizer::simplifyTemplates2() void Tokenizer::simplifyTemplates2()
{ {
bool goback = false;
for (Token *tok = _tokens; tok; tok = tok->next()) { for (Token *tok = _tokens; tok; tok = tok->next()) {
if (goback) {
tok = tok->previous();
goback = false;
}
if (tok->str() == "(") if (tok->str() == "(")
tok = tok->link(); tok = tok->link();
else if (Token::Match(tok, "; %type% <")) { else if (Token::Match(tok, "%type% <") &&
const Token *tok2 = tok->tokAt(3); (!tok->previous() || tok->previous()->str() == ";")) {
const Token *tok2 = tok->tokAt(2);
std::string type; std::string type;
while (Token::Match(tok2, "%type% ,") || Token::Match(tok2, "%num% ,")) { while (Token::Match(tok2, "%type% ,") || Token::Match(tok2, "%num% ,")) {
type += tok2->str() + ","; type += tok2->str() + ",";
@ -3388,9 +3402,10 @@ void Tokenizer::simplifyTemplates2()
} }
if (Token::Match(tok2, "%type% > (") || Token::Match(tok2, "%num% > (")) { if (Token::Match(tok2, "%type% > (") || Token::Match(tok2, "%num% > (")) {
type += tok2->str(); type += tok2->str();
tok = tok->next();
tok->str(tok->str() + "<" + type + ">"); tok->str(tok->str() + "<" + type + ">");
Token::eraseTokens(tok, tok2->tokAt(2)); Token::eraseTokens(tok, tok2->tokAt(2));
if (tok == _tokens)
goback = true;
} }
} }
} }

View File

@ -1567,7 +1567,7 @@ private:
const char code[] = "template <classname T> void f(T val) { T a; }\n" const char code[] = "template <classname T> void f(T val) { T a; }\n"
"f<int>(10);"; "f<int>(10);";
const std::string expected("; f<int> ( 10 ) ; " const std::string expected("f<int> ( 10 ) ; "
"void f<int> ( int val ) { }"); "void f<int> ( int val ) { }");
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
@ -1577,8 +1577,7 @@ private:
const char code[] = "template <classname T> class Fred { T a; };\n" const char code[] = "template <classname T> class Fred { T a; };\n"
"Fred<int> fred;"; "Fred<int> fred;";
const std::string expected("; " const std::string expected("Fred<int> fred ; "
"Fred<int> fred ; "
"class Fred<int> { int a ; }"); "class Fred<int> { int a ; }");
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
@ -1588,8 +1587,7 @@ private:
const char code[] = "template <classname T, int sz> class Fred { T data[sz]; };\n" const char code[] = "template <classname T, int sz> class Fred { T data[sz]; };\n"
"Fred<float,4> fred;"; "Fred<float,4> fred;";
const std::string expected("; " const std::string expected("Fred<float,4> fred ; "
"Fred<float,4> fred ; "
"class Fred<float,4> { float data [ 4 ] ; }"); "class Fred<float,4> { float data [ 4 ] ; }");
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
@ -1599,8 +1597,7 @@ private:
const char code[] = "template <classname T> class Fred { Fred(); };\n" const char code[] = "template <classname T> class Fred { Fred(); };\n"
"Fred<float> fred;"; "Fred<float> fred;";
const std::string expected("; " const std::string expected("Fred<float> fred ; "
"Fred<float> fred ; "
"class Fred<float> { Fred<float> ( ) ; }"); "class Fred<float> { Fred<float> ( ) ; }");
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
@ -1611,8 +1608,7 @@ private:
"template <classname T> Fred<T>::Fred() { }\n" "template <classname T> Fred<T>::Fred() { }\n"
"Fred<float> fred;"; "Fred<float> fred;";
const std::string expected("; " const std::string expected("Fred<float> fred ; "
"Fred<float> fred ; "
"class Fred<float> { } " "class Fred<float> { } "
"Fred<float> :: Fred<float> ( ) { }"); "Fred<float> :: Fred<float> ( ) { }");
@ -1624,10 +1620,9 @@ private:
"Fred<float> fred1;\n" "Fred<float> fred1;\n"
"Fred<float> fred2;"; "Fred<float> fred2;";
const std::string expected(";" const std::string expected("Fred<float> fred1 ; "
" Fred<float> fred1 ;" "Fred<float> fred2 ; "
" Fred<float> fred2 ;" "class Fred<float> { }");
" class Fred<float> { }");
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
} }
@ -1659,15 +1654,13 @@ private:
" return 0;\n" " return 0;\n"
"}\n"; "}\n";
const std::string wanted("; " const std::string wanted("int main ( ) { "
"int main ( ) { "
"std :: vector < int > v ; " "std :: vector < int > v ; "
"v . push_back ( 4 ) ; " "v . push_back ( 4 ) ; "
"return 0 ; " "return 0 ; "
"}"); "}");
const std::string current("; " const std::string current("int main ( ) { "
"int main ( ) { "
"ABC < int > :: type v ; " "ABC < int > :: type v ; "
"v . push_back ( 4 ) ; " "v . push_back ( 4 ) ; "
"return 0 ; " "return 0 ; "
@ -1715,7 +1708,7 @@ private:
ASSERT_EQUALS(";", sizeof_(code)); ASSERT_EQUALS(";", sizeof_(code));
ASSERT_EQUALS("class A { ; } ;", sizeof_("class A{ template<typename T> int foo(T d);};")); ASSERT_EQUALS("class A { } ;", sizeof_("class A{ template<typename T> int foo(T d);};"));
} }
void template9() { void template9() {
@ -1733,7 +1726,7 @@ private:
"} ;\n"; "} ;\n";
// The expected result.. // The expected result..
std::string expected("; void f ( ) { A<int> a ; } ; class A<int> { } class A<T> { }"); std::string expected("void f ( ) { A<int> a ; } class A<int> { } class A<T> { }");
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
} }
@ -1748,8 +1741,7 @@ private:
"}\n"; "}\n";
// The expected result.. // The expected result..
const std::string expected("; " const std::string expected("void f ( ) "
"void f ( ) "
"{" "{"
" foo<3,int> ( ) ; " " foo<3,int> ( ) ; "
"} " "} "
@ -1767,8 +1759,7 @@ private:
"}\n"; "}\n";
// The expected result.. // The expected result..
const std::string expected("; " const std::string expected("void f ( ) "
"void f ( ) "
"{" "{"
" char * p ; p = foo<3,char> ( ) ; " " char * p ; p = foo<3,char> ( ) ; "
"} " "} "
@ -1787,8 +1778,7 @@ private:
"}\n"; "}\n";
// The expected result.. // The expected result..
const std::string expected("; " const std::string expected("void f ( ) "
"void f ( ) "
"{" "{"
" A<12,12,11> a ; " " A<12,12,11> a ; "
"} " "} "
@ -1857,8 +1847,7 @@ private:
"}\n"; "}\n";
// The expected result.. // The expected result..
const std::string expected("; " const std::string expected("void a<0> ( ) { } "
"void a<0> ( ) { } "
"int main ( ) " "int main ( ) "
"{ a<2> ( ) ; return 0 ; } " "{ a<2> ( ) ; return 0 ; } "
"void a<2> ( ) { a<1> ( ) ; } " "void a<2> ( ) { a<1> ( ) ; } "
@ -1880,8 +1869,7 @@ private:
" return 0;\n" " return 0;\n"
"}\n"; "}\n";
const std::string expected("; " const std::string expected("int main ( ) { b<2> ( ) ; return 0 ; } "
"int main ( ) { b<2> ( ) ; return 0 ; } "
"void b<2> ( ) { a<2> ( ) ; } " "void b<2> ( ) { a<2> ( ) ; } "
"void a<i> ( ) { } " "void a<i> ( ) { } "
"void a<2> ( ) { }"); "void a<2> ( ) { }");
@ -1909,8 +1897,7 @@ private:
const char code[] = "template <class T> class foo { T a; };\n" const char code[] = "template <class T> class foo { T a; };\n"
"foo<int> *f;"; "foo<int> *f;";
const std::string expected("; " const std::string expected("foo<int> * f ; "
"foo<int> * f ; "
"class foo<int> { int a ; }"); "class foo<int> { int a ; }");
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
@ -1926,8 +1913,7 @@ private:
"}\n"; "}\n";
// The expected result.. // The expected result..
const std::string expected("; " const std::string expected("void f ( ) "
"void f ( ) "
"{" "{"
" char p ; p = foo<char> ( ) ; " " char p ; p = foo<char> ( ) ; "
"} " "} "
@ -1950,7 +1936,7 @@ private:
"A<int> a;\n"; "A<int> a;\n";
// The expected result.. // The expected result..
const std::string expected("; A<int> a ; " const std::string expected("A<int> a ; "
"class A<int> { public: ~ A<int> ( ) ; } " "class A<int> { public: ~ A<int> ( ) ; } "
"A<int> :: ~ A<int> ( ) { }"); "A<int> :: ~ A<int> ( ) { }");
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
@ -1961,8 +1947,7 @@ private:
const char code[] = "template <classname T> struct Fred { T a; };\n" const char code[] = "template <classname T> struct Fred { T a; };\n"
"Fred<int> fred;"; "Fred<int> fred;";
const std::string expected("; " const std::string expected("Fred<int> fred ; "
"Fred<int> fred ; "
"struct Fred<int> { int a ; }"); "struct Fred<int> { int a ; }");
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
@ -1972,8 +1957,7 @@ private:
const char code[] = "template <classname T, int sz> struct Fred { T data[sz]; };\n" const char code[] = "template <classname T, int sz> struct Fred { T data[sz]; };\n"
"Fred<float,4> fred;"; "Fred<float,4> fred;";
const std::string expected("; " const std::string expected("Fred<float,4> fred ; "
"Fred<float,4> fred ; "
"struct Fred<float,4> { float data [ 4 ] ; }"); "struct Fred<float,4> { float data [ 4 ] ; }");
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
@ -1983,8 +1967,7 @@ private:
const char code[] = "template <classname T> struct Fred { Fred(); };\n" const char code[] = "template <classname T> struct Fred { Fred(); };\n"
"Fred<float> fred;"; "Fred<float> fred;";
const std::string expected("; " const std::string expected("Fred<float> fred ; "
"Fred<float> fred ; "
"struct Fred<float> { Fred<float> ( ) ; }"); "struct Fred<float> { Fred<float> ( ) ; }");
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
@ -1995,8 +1978,7 @@ private:
"template <classname T> Fred<T>::Fred() { }\n" "template <classname T> Fred<T>::Fred() { }\n"
"Fred<float> fred;"; "Fred<float> fred;";
const std::string expected("; " const std::string expected("Fred<float> fred ; "
"Fred<float> fred ; "
"struct Fred<float> { } " "struct Fred<float> { } "
"Fred<float> :: Fred<float> ( ) { }"); "Fred<float> :: Fred<float> ( ) { }");
@ -2008,10 +1990,9 @@ private:
"Fred<float> fred1;\n" "Fred<float> fred1;\n"
"Fred<float> fred2;"; "Fred<float> fred2;";
const std::string expected(";" const std::string expected("Fred<float> fred1 ; "
" Fred<float> fred1 ;" "Fred<float> fred2 ; "
" Fred<float> fred2 ;" "struct Fred<float> { }");
" struct Fred<float> { }");
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
} }
@ -2021,8 +2002,7 @@ private:
const char code[] = "template <classname T> struct Fred { T a; };\n" const char code[] = "template <classname T> struct Fred { T a; };\n"
"Fred<std::string> fred;"; "Fred<std::string> fred;";
const std::string expected("; " const std::string expected("Fred<std::string> fred ; "
"Fred<std::string> fred ; "
"struct Fred<std::string> { std :: string a ; }"); "struct Fred<std::string> { std :: string a ; }");
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
@ -2034,8 +2014,7 @@ private:
" std::cout << (foo<double>());\n" " std::cout << (foo<double>());\n"
"}"; "}";
const std::string expected("; " const std::string expected("void bar ( ) {"
"void bar ( ) {"
" std :: cout << ( foo<double> ( ) ) ; " " std :: cout << ( foo<double> ( ) ) ; "
"} " "} "
"void foo<double> ( ) { }"); "void foo<double> ( ) { }");
@ -2054,8 +2033,7 @@ private:
"{};\n" "{};\n"
"\n" "\n"
"bitset<1> z;"; "bitset<1> z;";
const char expected[] = "; " const char expected[] = "bitset<1> z ; "
"bitset<1> z ; "
"class bitset<1> : B<4> { } " "class bitset<1> : B<4> { } "
"struct B<4> { int a [ 4 ] ; }"; "struct B<4> { int a [ 4 ] ; }";
ASSERT_EQUALS(expected, sizeof_(code)); ASSERT_EQUALS(expected, sizeof_(code));
@ -2072,11 +2050,10 @@ private:
"\n" "\n"
"bitset<1> z;"; "bitset<1> z;";
const char actual[] = "; bitset<1> z ; " const char actual[] = "bitset<1> z ; "
"class bitset<1> : B < ( ) > { }"; "class bitset<1> : B < ( ) > { }";
const char expected[] = "; " const char expected[] = "bitset<1> z ; "
"bitset<1> z ; "
"class bitset<1> : B<4> { } " "class bitset<1> : B<4> { } "
"struct B<4> { int a [ 4 ] ; }"; "struct B<4> { int a [ 4 ] ; }";
@ -2094,7 +2071,7 @@ private:
"\n" "\n"
"C<2> a;\n"; "C<2> a;\n";
// TODO: expand A also // 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() { void template27() {
@ -2107,12 +2084,12 @@ private:
// #3226 - inner template // #3226 - inner template
const char code[] = "template<class A, class B> class Fred {};\n" const char code[] = "template<class A, class B> class Fred {};\n"
"Fred<int,Fred<int,int> > x;\n"; "Fred<int,Fred<int,int> > x;\n";
ASSERT_EQUALS("; Fred<int,Fred<int,int>> x ; class Fred<int,int> { } class Fred<int,Fred<int,int>> { }", sizeof_(code)); ASSERT_EQUALS("Fred<int,Fred<int,int>> x ; class Fred<int,int> { } class Fred<int,Fred<int,int>> { }", sizeof_(code));
} }
void template_unhandled() { void template_unhandled() {
// An unhandled template usage should be simplified.. // An unhandled template usage should be simplified..
ASSERT_EQUALS("; x<int> ( ) ;", sizeof_(";x<int>();")); ASSERT_EQUALS("x<int> ( ) ;", sizeof_("x<int>();"));
} }
void template_default_parameter() { void template_default_parameter() {
@ -2128,8 +2105,7 @@ private:
"}\n"; "}\n";
// The expected result.. // The expected result..
const std::string expected("; " const std::string expected("void f ( ) "
"void f ( ) "
"{" "{"
" A<int,2> a1 ;" " A<int,2> a1 ;"
" A<int,3> a2 ; " " A<int,3> a2 ; "
@ -2152,8 +2128,7 @@ private:
"}\n"; "}\n";
// The expected result.. // The expected result..
const std::string expected("; " const std::string expected("void f ( ) "
"void f ( ) "
"{" "{"
" A<int,3,2> a1 ;" " A<int,3,2> a1 ;"
" A<int,3,2> a2 ; " " A<int,3,2> a2 ; "
@ -2186,8 +2161,7 @@ private:
" class A<int,3>" " class A<int,3>"
" { int ar [ 3 ] ; }"); " { int ar [ 3 ] ; }");
const std::string current("; " const std::string current("void f ( ) "
"void f ( ) "
"{ " "{ "
"A < int , ( int ) 2 > a1 ; " "A < int , ( int ) 2 > a1 ; "
"A<int,3> a2 ; " "A<int,3> a2 ; "
@ -2270,7 +2244,7 @@ private:
const char code[] = "class Fred {\n" const char code[] = "class Fred {\n"
" template<class T> explicit Fred(T t) { }\n" " template<class T> 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() { void namespaces() {

View File

@ -5017,7 +5017,7 @@ private:
"{\n" "{\n"
" fn2<int>();\n" " fn2<int>();\n"
"}\n"; "}\n";
ASSERT_EQUALS(";\n\n\nint main ( )\n{\nfn2<int> ( ) ;\n}void fn2<int> ( int t = [ ] { return 1 ; } ( ) )\n{ }", tokenizeAndStringify(code)); ASSERT_EQUALS("int main ( )\n{\nfn2<int> ( ) ;\n}void fn2<int> ( int t = [ ] { return 1 ; } ( ) )\n{ }", tokenizeAndStringify(code));
} }
void cpp0xtemplate2() { void cpp0xtemplate2() {
@ -5033,8 +5033,8 @@ private:
"struct S\n" "struct S\n"
"{};\n" "{};\n"
"S<int> s;\n"; "S<int> s;\n";
TODO_ASSERT_EQUALS(";\n\n\nS < int , ( int ) 0 > s ;", // wanted result TODO_ASSERT_EQUALS("S < int , ( int ) 0 > s ;", // wanted result
";\n\n\nS < int , ( T ) 0 > s ;", // current result "S < int , ( T ) 0 > s ;", // current result
tokenizeAndStringify(code)); tokenizeAndStringify(code));
} }