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)
{
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;
}
}
}

View File

@ -1567,7 +1567,7 @@ private:
const char code[] = "template <classname T> void f(T val) { T a; }\n"
"f<int>(10);";
const std::string expected("; f<int> ( 10 ) ; "
const std::string expected("f<int> ( 10 ) ; "
"void f<int> ( int val ) { }");
ASSERT_EQUALS(expected, sizeof_(code));
@ -1577,8 +1577,7 @@ private:
const char code[] = "template <classname T> class Fred { T a; };\n"
"Fred<int> fred;";
const std::string expected("; "
"Fred<int> fred ; "
const std::string expected("Fred<int> fred ; "
"class Fred<int> { int a ; }");
ASSERT_EQUALS(expected, sizeof_(code));
@ -1588,8 +1587,7 @@ private:
const char code[] = "template <classname T, int sz> class Fred { T data[sz]; };\n"
"Fred<float,4> fred;";
const std::string expected("; "
"Fred<float,4> fred ; "
const std::string expected("Fred<float,4> fred ; "
"class Fred<float,4> { float data [ 4 ] ; }");
ASSERT_EQUALS(expected, sizeof_(code));
@ -1599,8 +1597,7 @@ private:
const char code[] = "template <classname T> class Fred { Fred(); };\n"
"Fred<float> fred;";
const std::string expected("; "
"Fred<float> fred ; "
const std::string expected("Fred<float> fred ; "
"class Fred<float> { Fred<float> ( ) ; }");
ASSERT_EQUALS(expected, sizeof_(code));
@ -1611,8 +1608,7 @@ private:
"template <classname T> Fred<T>::Fred() { }\n"
"Fred<float> fred;";
const std::string expected("; "
"Fred<float> fred ; "
const std::string expected("Fred<float> fred ; "
"class Fred<float> { } "
"Fred<float> :: Fred<float> ( ) { }");
@ -1624,10 +1620,9 @@ private:
"Fred<float> fred1;\n"
"Fred<float> fred2;";
const std::string expected(";"
" Fred<float> fred1 ;"
" Fred<float> fred2 ;"
" class Fred<float> { }");
const std::string expected("Fred<float> fred1 ; "
"Fred<float> fred2 ; "
"class Fred<float> { }");
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<typename T> int foo(T d);};"));
ASSERT_EQUALS("class A { } ;", sizeof_("class A{ template<typename T> int foo(T d);};"));
}
void template9() {
@ -1733,7 +1726,7 @@ private:
"} ;\n";
// 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));
}
@ -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<i> ( ) { } "
"void a<2> ( ) { }");
@ -1909,8 +1897,7 @@ private:
const char code[] = "template <class T> class foo { T a; };\n"
"foo<int> *f;";
const std::string expected("; "
"foo<int> * f ; "
const std::string expected("foo<int> * f ; "
"class foo<int> { 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<char> ( ) ; "
"} "
@ -1950,7 +1936,7 @@ private:
"A<int> a;\n";
// The expected result..
const std::string expected("; A<int> a ; "
const std::string expected("A<int> a ; "
"class A<int> { public: ~ A<int> ( ) ; } "
"A<int> :: ~ A<int> ( ) { }");
ASSERT_EQUALS(expected, sizeof_(code));
@ -1961,8 +1947,7 @@ private:
const char code[] = "template <classname T> struct Fred { T a; };\n"
"Fred<int> fred;";
const std::string expected("; "
"Fred<int> fred ; "
const std::string expected("Fred<int> fred ; "
"struct Fred<int> { int a ; }");
ASSERT_EQUALS(expected, sizeof_(code));
@ -1972,8 +1957,7 @@ private:
const char code[] = "template <classname T, int sz> struct Fred { T data[sz]; };\n"
"Fred<float,4> fred;";
const std::string expected("; "
"Fred<float,4> fred ; "
const std::string expected("Fred<float,4> fred ; "
"struct Fred<float,4> { float data [ 4 ] ; }");
ASSERT_EQUALS(expected, sizeof_(code));
@ -1983,8 +1967,7 @@ private:
const char code[] = "template <classname T> struct Fred { Fred(); };\n"
"Fred<float> fred;";
const std::string expected("; "
"Fred<float> fred ; "
const std::string expected("Fred<float> fred ; "
"struct Fred<float> { Fred<float> ( ) ; }");
ASSERT_EQUALS(expected, sizeof_(code));
@ -1995,8 +1978,7 @@ private:
"template <classname T> Fred<T>::Fred() { }\n"
"Fred<float> fred;";
const std::string expected("; "
"Fred<float> fred ; "
const std::string expected("Fred<float> fred ; "
"struct Fred<float> { } "
"Fred<float> :: Fred<float> ( ) { }");
@ -2008,10 +1990,9 @@ private:
"Fred<float> fred1;\n"
"Fred<float> fred2;";
const std::string expected(";"
" Fred<float> fred1 ;"
" Fred<float> fred2 ;"
" struct Fred<float> { }");
const std::string expected("Fred<float> fred1 ; "
"Fred<float> fred2 ; "
"struct Fred<float> { }");
ASSERT_EQUALS(expected, sizeof_(code));
}
@ -2021,8 +2002,7 @@ private:
const char code[] = "template <classname T> struct Fred { T a; };\n"
"Fred<std::string> fred;";
const std::string expected("; "
"Fred<std::string> fred ; "
const std::string expected("Fred<std::string> fred ; "
"struct Fred<std::string> { std :: string a ; }");
ASSERT_EQUALS(expected, sizeof_(code));
@ -2034,8 +2014,7 @@ private:
" std::cout << (foo<double>());\n"
"}";
const std::string expected("; "
"void bar ( ) {"
const std::string expected("void bar ( ) {"
" std :: cout << ( foo<double> ( ) ) ; "
"} "
"void foo<double> ( ) { }");
@ -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 A, class B> class Fred {};\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() {
// 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() {
@ -2128,8 +2105,7 @@ private:
"}\n";
// The expected result..
const std::string expected("; "
"void f ( ) "
const std::string expected("void f ( ) "
"{"
" A<int,2> a1 ;"
" A<int,3> a2 ; "
@ -2152,8 +2128,7 @@ private:
"}\n";
// The expected result..
const std::string expected("; "
"void f ( ) "
const std::string expected("void f ( ) "
"{"
" A<int,3,2> a1 ;"
" A<int,3,2> a2 ; "
@ -2186,8 +2161,7 @@ private:
" class A<int,3>"
" { int ar [ 3 ] ; }");
const std::string current("; "
"void f ( ) "
const std::string current("void f ( ) "
"{ "
"A < int , ( int ) 2 > a1 ; "
"A<int,3> a2 ; "
@ -2270,7 +2244,7 @@ private:
const char code[] = "class Fred {\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() {

View File

@ -5017,7 +5017,7 @@ private:
"{\n"
" fn2<int>();\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() {
@ -5033,8 +5033,8 @@ private:
"struct S\n"
"{};\n"
"S<int> 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));
}