From d88dc3ed3e9fe37c3b986ed1feaf85ebdce6980c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 6 Sep 2015 18:37:22 +0200 Subject: [PATCH] Reverted 00c54df07c5a58c8add187d01be2febf9f4ad3b5 (don't remove enum declarations) because it caused unexpected false positives --- lib/tokenize.cpp | 89 ++++++++++++++++++++- test/testconstructors.cpp | 2 +- test/testsimplifytemplate.cpp | 13 +-- test/testsimplifytokens.cpp | 146 +++++++++++++++------------------- test/testsimplifytypedef.cpp | 32 ++++---- test/testtokenize.cpp | 3 +- test/testuninitvar.cpp | 2 +- 7 files changed, 169 insertions(+), 118 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index f2f142747..9dedb2808 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7278,8 +7278,18 @@ void Tokenizer::simplifyEnum() { std::string className; int classLevel = 0; + bool goback = false; const bool printStyle = _settings->isEnabled("style"); for (Token *tok = list.front(); tok; tok = tok->next()) { + + if (goback) { + //jump back once, see the comment at the end of the function + goback = false; + tok = tok->previous(); + if (!tok) + break; + } + if (tok->next() && (!tok->previous() || (tok->previous()->str() != "enum")) && Token::Match(tok, "class|struct|namespace")) { @@ -7642,7 +7652,7 @@ void Tokenizer::simplifyEnum() tempTok->insertToken(";"); tempTok = tempTok->next(); if (typeTokenStart == 0) - tempTok->insertToken(enumType ? enumType->str() : std::string("int")); + tempTok->insertToken("int"); else { Token *tempTok1 = typeTokenStart; @@ -7656,6 +7666,83 @@ void Tokenizer::simplifyEnum() } } } + + if (enumType) { + const std::string pattern(className.empty() ? std::string("") : (className + " :: " + enumType->str())); + + // count { and } for tok2 + int level = 0; + bool inScope = true; + + bool exitThisScope = false; + int exitScope = 0; + bool simplify = false; + bool hasClass = false; + for (Token *tok2 = end->next(); tok2; tok2 = tok2->next()) { + if (tok2->str() == "}") { + --level; + if (level < 0) + inScope = false; + + if (exitThisScope) { + if (level < exitScope) + exitThisScope = false; + } + } else if (tok2->str() == "{") + ++level; + else if (!pattern.empty() && ((tok2->str() == "enum" && Token::Match(tok2->next(), pattern.c_str())) || Token::Match(tok2, pattern.c_str()))) { + simplify = true; + hasClass = true; + } else if (inScope && !exitThisScope && (tok2->str() == enumType->str() || (tok2->str() == "enum" && tok2->next() && tok2->next()->str() == enumType->str()))) { + if (tok2->strAt(-1) == "::") { + // Don't replace this enum if it's preceded by "::" + } else if (tok2->next() && + (tok2->next()->isName() || tok2->next()->str() == "(")) { + simplify = true; + hasClass = false; + } else if (tok2->previous()->str() == "(" && tok2->next()->str() == ")") { + simplify = true; + hasClass = false; + } + } + + if (simplify) { + if (tok2->str() == "enum") + tok2->deleteNext(); + if (typeTokenStart == 0) + tok2->str("int"); + else { + tok2->str(typeTokenStart->str()); + copyTokens(tok2, typeTokenStart->next(), typeTokenEnd); + } + + if (hasClass) { + tok2->deleteNext(2); + } + + simplify = false; + } + } + } + + tok1 = start; + Token::eraseTokens(tok1, end->next()); + if (start != list.front()) { + tok1 = start->previous(); + tok1->deleteNext(); + //no need to remove last token in the list + if (tok1->tokAt(2)) + tok1->deleteNext(); + tok = tok1; + } else { + list.front()->deleteThis(); + //no need to remove last token in the list + if (list.front()->next()) + list.front()->deleteThis(); + tok = list.front(); + //now the next token to process is 'tok', not 'tok->next()'; + goback = true; + } } } } diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index 92be58147..f643a875e 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -1423,7 +1423,7 @@ private: " ECODES _code;\n" "};"); - TODO_ASSERT_EQUALS("[test.cpp:10]: (warning) Member variable 'Fred::_code' is not initialized in the constructor.\n", "", errout.str()); + ASSERT_EQUALS("[test.cpp:10]: (warning) Member variable 'Fred::_code' is not initialized in the constructor.\n", errout.str()); check("class A{};\n" diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 65093ef8b..9eae5e2bf 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -1017,18 +1017,7 @@ private: "{\n" " enum {value = !type_equal::type>::value };\n" "};"; - const char expected1[] = "template < class T > struct Unconst { } ; " - "template < class T > struct type_equal { enum { value = 1 } ; } ; " - "template < class T > struct template_is_const { enum { value = ! type_equal < T , Unconst < T > :: type > :: value } ; } ; " - "struct type_equal { enum { value = 0 } ; } ; " - "struct Unconst { } ; " - "struct Unconst { } ; " - "struct Unconst { } ; " - "struct Unconst { } ; " - "struct Unconst { } ; " - "struct Unconst { } ; " - "struct Unconst<};template { } ; " - "struct Unconst<};template { } ;"; + const char expected1[]="template < class T > struct Unconst { } ; template < class T > struct type_equal { } ; template < class T > struct template_is_const { } ; struct type_equal { } ; struct Unconst { } ; struct Unconst { } ; struct Unconst { } ; struct Unconst { } ; struct Unconst { } ; struct Unconst { } ; struct Unconst<};template { } ; struct Unconst<};template { } ;"; ASSERT_EQUALS(expected1, tok(code1)); } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 9ee01c147..e3f2da661 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -2872,19 +2872,21 @@ private: void enum1() { const char code[] = "enum A { a, b, c }; A c1 = c;"; - const char expected[] = "enum A { a = 0 , b = 1 , c = 2 } ; A c1 ; c1 = 2 ;"; + const char expected[] = "int c1 ; c1 = 2 ;"; + ASSERT_EQUALS(expected, tok(code, false)); } void enum2() { const char code[] = "enum A { a, }; int array[a];"; - const char expected[] = "enum A { a = 0 , } ; int array [ 0 ] ;"; + const char expected[] = "int array [ 0 ] ;"; + ASSERT_EQUALS(expected, tok(code, false)); } void enum3() { const char code[] = "enum { a, }; int array[a];"; - const char expected[] = "enum { a = 0 , } ; int array [ 0 ] ;"; + const char expected[] = "int array [ 0 ] ;"; ASSERT_EQUALS(expected, tok(code, false)); } @@ -2904,14 +2906,14 @@ private: const char expected[] = "class A { " "public: " - "enum EA { a1 = 0 , a2 = 1 , a3 = 2 } ; " - "EA get ( ) const ; " - "void put ( EA a ) { ea = a ; ea = 0 ; } " + "" + "int get ( ) const ; " + "void put ( int a ) { ea = a ; ea = 0 ; } " "private: " - "EA ea ; " + "int ea ; " "} ; " - "A :: EA A :: get ( ) const { return ea ; } " - "A :: EA e ; e = 0 ;"; + "int A :: get ( ) const { return ea ; } " + "int e ; e = 0 ;"; ASSERT_EQUALS(expected, tok(code, false)); } @@ -2927,13 +2929,13 @@ private: "A::EA e = A::a1;"; const char expected[] = "struct A { " - "enum EA { a1 = 0 , a2 = 1 , a3 = 2 } ; " - "EA get ( ) const ; " - "void put ( EA a ) { ea = a ; ea = 0 ; } " - "EA ea ; " + "" + "int get ( ) const ; " + "void put ( int a ) { ea = a ; ea = 0 ; } " + "int ea ; " "} ; " - "A :: EA A :: get ( ) const { return ea ; } " - "A :: EA e ; e = 0 ;"; + "int A :: get ( ) const { return ea ; } " + "int e ; e = 0 ;"; ASSERT_EQUALS(expected, tok(code, false)); } @@ -2950,8 +2952,7 @@ private: " g\n" "};\n" "int sum = a + b + c + d + e + f + g;"; - const char expected[] = "enum ABC { a = sizeof ( int ) , b = 1 + sizeof ( int ) , c = 1 + sizeof ( int ) + 100 , d = 1 + sizeof ( int ) + 101 , e = 1 + sizeof ( int ) + 102 , f = 90 , g = 91 } ; " - "int sum ; sum = " + const char expected[] = "int sum ; sum = " "sizeof ( int ) + " "( 1 + sizeof ( int ) ) + " "( 1 + sizeof ( int ) + 100 ) + " // 101 = 100 + 1 @@ -2960,12 +2961,12 @@ private: ";"; ASSERT_EQUALS(expected, tok(code, false)); - ASSERT_EQUALS("enum ABC { a = 4 ; b = 5 ; c = 105 ; d = 106 ; e = 107 ; f = 90 ; g = 91 } ; int sum ; sum = 508 ;", tok(code, true)); + ASSERT_EQUALS("int sum ; sum = 508 ;", tok(code, true)); } void enum6() { const char code[] = "enum { a = MAC(A, B, C) }; void f(a) { }"; - const char expected[] = "enum { a = MAC ( A , B , C ) } ; void f ( a ) { }"; + const char expected[] = "void f ( a ) { }"; ASSERT_EQUALS(expected, tok(code, false)); } @@ -2978,8 +2979,7 @@ private: " int A = B;\n" " { float A = C; }\n" "}"; - const char expected[] = "enum FOO { A = 0 , B = 1 , C = 2 } ; " - "int main ( ) " + const char expected[] = "int main ( ) " "{ " "int A ; A = 1 ; " "{ float A ; A = 2 ; } " @@ -2990,8 +2990,7 @@ private: { const char code[] = "enum FOO {A,B,C};\n" "void f(int A, float B, char C) { }"; - const char expected[] = "enum FOO { A = 0 , B = 1 , C = 2 } ; " - "void f ( int A , float B , char C ) { }"; + const char expected[] = "void f ( int A , float B , char C ) { }"; ASSERT_EQUALS(expected, tok(code, false)); } } @@ -3069,8 +3068,7 @@ private: const char code[] = "enum {\n" "SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1,\n" "} e = SHELL_SIZE;"; - const char expected[] = "enum { SHELL_SIZE = sizeof ( union { int i ; char * cp ; double d ; } ) - 1 , } ; " - "int e ; e = sizeof ( union { int i ; char * cp ; double d ; } ) - 1 ;"; + const char expected[] = "int e ; e = sizeof ( union { int i ; char * cp ; double d ; } ) - 1 ;"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS("", errout.str()); @@ -3084,7 +3082,7 @@ private: "}"; const char expected[] = "int main ( ) " "{ " - "enum { u = 0 , v = 1 } ; " + "" "A u ; u = 1 ; A v ; v = 2 ; " "}"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); @@ -3099,7 +3097,7 @@ private: "{\n" " unsigned int fred = 0;\n" "}"; - const char expected[] = "enum fred { a = 0 , b = 1 } ; void foo ( ) { unsigned int fred ; fred = 0 ; }"; + const char expected[] = "void foo ( ) { unsigned int fred ; fred = 0 ; }"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); } @@ -3109,15 +3107,14 @@ private: "{\n" " unsigned int fred = a;\n" "}"; - const char expected[] = "enum ab { ENTRY ( 1 , a = 0 ) , ENTRY ( 2 , b ) } ; " - "void foo ( ) { unsigned int fred ; fred = a ; }"; + const char expected[] = "void foo ( ) { unsigned int fred ; fred = a ; }"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); } void enum14() { const char code[] = "enum ab { a };\n" "ab"; - const char expected[] = "enum ab { a = 0 } ; ab"; + const char expected[] = "ab"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); } @@ -3125,63 +3122,56 @@ private: { const char code[] = "enum : char { a = 99 };\n" "char c1 = a;"; - const char expected[] = "enum : char { a = 99 } ; " - "char c1 ; c1 = 99 ;"; + const char expected[] = "char c1 ; c1 = 99 ;"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); } { const char code[] = "enum class Enum1 { a };\n" "Enum1 e1 = Enum1::a;"; - const char expected[] = "enum Enum1 { a = 0 } ; " - "Enum1 e1 ; e1 = 0 ;"; + const char expected[] = "int e1 ; e1 = 0 ;"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); } { const char code[] = "enum class Enum1 { a };\n" "Enum1 e1 = a;"; - const char expected[] = "enum Enum1 { a = 0 } ; " - "Enum1 e1 ; e1 = a ;"; + const char expected[] = "int e1 ; e1 = a ;"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); } { const char code[] = "enum Enum1 : char { a };\n" "Enum1 e1 = a;"; - const char expected[] = "enum Enum1 : char { a = 0 } ; " - "Enum1 e1 ; e1 = 0 ;"; + const char expected[] = "char e1 ; e1 = 0 ;"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); } { const char code[] = "enum class Enum1 : unsigned char { a };\n" "Enum1 e1 = Enum1::a;"; - const char expected[] = "enum Enum1 : unsigned char { a = 0 } ; " - "Enum1 e1 ; e1 = 0 ;"; + const char expected[] = "unsigned char e1 ; e1 = 0 ;"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); } { const char code[] = "enum class Enum1 : unsigned int { a };\n" "Enum1 e1 = Enum1::a;"; - const char expected[] = "enum Enum1 : unsigned int { a = 0 } ; " - "Enum1 e1 ; e1 = 0 ;"; + const char expected[] = "unsigned int e1 ; e1 = 0 ;"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); } { const char code[] = "enum class Enum1 : unsigned long long int { a };\n" "Enum1 e1 = Enum1::a;"; - const char expected[] = "enum Enum1 : unsigned long long { a = 0 } ; " - "Enum1 e1 ; e1 = 0 ;"; + const char expected[] = "unsigned long long e1 ; e1 = 0 ;"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); } { const char code[] = "enum class { A };\n" "int i = A;"; - const char expected [] = "enum class { A = 0 } ; int i ; i = 0 ;"; + const char expected [] = "int i ; i = 0 ;"; ASSERT_EQUALS(expected, checkSimplifyEnum(code, false)); // Compile as C code: enum has name 'class' checkSimplifyEnum(code, true); // Compile as C++ code: Don't crash } @@ -3203,14 +3193,14 @@ private: // if header is included twice its enums will be duplicated const char code[] = "enum ab { a=0, b };" "enum ab { a=0, b };\n"; - ASSERT_EQUALS("enum ab { a = 0 , b = 1 } ;", checkSimplifyEnum(code)); + ASSERT_EQUALS(";", checkSimplifyEnum(code)); ASSERT_EQUALS("", errout.str()); } void enum18() { // ticket #2466 - array with same name as enum constant const char code[] = "enum ab { a=0, b };\n" "void f() { a[0]; }\n"; - ASSERT_EQUALS("enum ab { a = 0 , b = 1 } ; void f ( ) { a [ 0 ] ; }", checkSimplifyEnum(code)); + ASSERT_EQUALS("void f ( ) { a [ 0 ] ; }", checkSimplifyEnum(code)); } void enum19() { // ticket #2536 @@ -3221,12 +3211,12 @@ private: void enum20() { // ticket #2600 segmentation fault const char code[] = "enum { const }\n"; - ASSERT_EQUALS("enum { const = 0 }", checkSimplifyEnum(code)); + ASSERT_EQUALS("", checkSimplifyEnum(code)); } void enum21() { // ticket #2720 syntax error const char code[] = "enum E2 : signed const short { };\n"; - ASSERT_EQUALS("enum E2 : signed int const short { } ;", checkSimplifyEnum(code)); + ASSERT_EQUALS(";", checkSimplifyEnum(code)); ASSERT_EQUALS("", errout.str()); } @@ -3271,8 +3261,7 @@ private: void enum23() { // ticket #2804 const char code[] = "enum Enumerator : std::uint8_t { ITEM1, ITEM2, ITEM3 };\n" "Enumerator e = ITEM3;\n"; - const char expected[] = "enum Enumerator : std :: uint8_t { ITEM1 = 0 , ITEM2 = 1 , ITEM3 = 2 } ; " - "Enumerator e ; e = 2 ;"; + const char expected[] = "std :: uint8_t e ; e = 2 ;"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS("", errout.str()); } @@ -3282,10 +3271,7 @@ private: "void f(long style) {\n" " if (style & STYLE) { }\n" "}\n"; - const char expected[] = "enum EnumName { STYLE = 1 } ; " - "void f ( long style ) { " - "if ( style & 1 ) { } " - "}"; + const char expected[] = "void f ( long style ) { if ( style & 1 ) { } }"; ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS("", errout.str()); } @@ -3310,12 +3296,12 @@ private: "void f() { char x[4]; memset(x, 0, 4);\n" "{ x } };\n" "void g() { x; }"; - ASSERT_EQUALS("enum { x = 0 } ; void f ( ) { char x [ 4 ] ; memset ( x , 0 , 4 ) ; { x } } ; void g ( ) { 0 ; }", checkSimplifyEnum(code)); + ASSERT_EQUALS("void f ( ) { char x [ 4 ] ; memset ( x , 0 , 4 ) ; { x } } ; void g ( ) { 0 ; }", checkSimplifyEnum(code)); } void enum29() { // #3747 - bitwise or value const char code[] = "enum { x=1, y=x|2 }; i = (3==y);"; - ASSERT_EQUALS("enum { x = 1 , y = 3 } ; i = 3 == 3 ;", checkSimplifyEnum(code)); + ASSERT_EQUALS("i = 3 == 3 ;", checkSimplifyEnum(code)); } void enum30() { // #3852 - false positive @@ -3336,57 +3322,52 @@ private: "int main() {" " return TestIf::Bar::two;\n" "}"; - ASSERT_EQUALS("class TestIf { " - "public: " - "enum Foo { one = 0 , two = 1 } ; " - "enum Bar { one = 0 , two = 1 } ; " - "} ; " - "int main ( ) { return 1 ; }", checkSimplifyEnum(code)); + ASSERT_EQUALS("class TestIf { public: } ; int main ( ) { return 1 ; }", checkSimplifyEnum(code)); ASSERT_EQUALS("", errout.str()); } void enum31() { // #3934 - calculation in first item const char code[] = "enum { x=2*32, y }; i = y;"; - ASSERT_EQUALS("enum { x = 64 , y = 65 } ; i = 65 ;", checkSimplifyEnum(code)); + ASSERT_EQUALS("i = 65 ;", checkSimplifyEnum(code)); } void enum32() { // #3998 - wrong enum simplification => access violation const char code[] = "enum { x=(32), y=x, z }; { a, z }"; - ASSERT_EQUALS("enum { x = 32 , y = 32 , z = 33 } ; { a , ( 33 ) }", checkSimplifyEnum(code)); + ASSERT_EQUALS("{ a , ( 33 ) }", checkSimplifyEnum(code)); } void enum33() { // #4015 - segmentation fault const char code[] = "enum { A=SOME_VALUE, B=A };"; - ASSERT_EQUALS("enum { A = SOME_VALUE , B = SOME_VALUE } ;", checkSimplifyEnum(code)); + ASSERT_EQUALS(";", checkSimplifyEnum(code)); } void enum34() { // #4141 - division by zero const char code[] = "enum { A=1/0 };"; - ASSERT_EQUALS("enum { A = 1 / 0 } ;", checkSimplifyEnum(code)); + ASSERT_EQUALS(";", checkSimplifyEnum(code)); } void enum35() { // #3953 - avoid simplification of type - ASSERT_EQUALS("enum { A = 0 } ; void f ( A * a ) ;", checkSimplifyEnum("enum { A }; void f(A * a) ;")); - ASSERT_EQUALS("enum { A = 0 } ; void f ( A * a ) { }", checkSimplifyEnum("enum { A }; void f(A * a) { }")); + ASSERT_EQUALS("void f ( A * a ) ;", checkSimplifyEnum("enum { A }; void f(A * a) ;")); + ASSERT_EQUALS("void f ( A * a ) { }", checkSimplifyEnum("enum { A }; void f(A * a) { }")); } void enum36() { // #4378 const char code[] = "struct X { enum Y { a, b }; X(Y) { Y y = (Y)1; } };"; - ASSERT_EQUALS("struct X { enum Y { a = 0 , b = 1 } ; X ( Y ) { Y y ; y = ( Y ) 1 ; } } ;", checkSimplifyEnum(code)); + ASSERT_EQUALS("struct X { X ( int ) { int y ; y = ( int ) 1 ; } } ;", checkSimplifyEnum(code)); } void enum37() { // #4280 - shadow variables const char code1[] = "enum { a, b }; void f(int a) { return a + 1; }"; - ASSERT_EQUALS("enum { a = 0 , b = 1 } ; void f ( int a ) { return a + 1 ; }", checkSimplifyEnum(code1)); + ASSERT_EQUALS("void f ( int a ) { return a + 1 ; }", checkSimplifyEnum(code1)); const char code2[] = "enum { a, b }; void f() { int a; }"; - ASSERT_EQUALS("enum { a = 0 , b = 1 } ; void f ( ) { int a ; }", checkSimplifyEnum(code2)); + ASSERT_EQUALS("void f ( ) { int a ; }", checkSimplifyEnum(code2)); const char code3[] = "enum { a, b }; void f() { int *a=do_something(); }"; - ASSERT_EQUALS("enum { a = 0 , b = 1 } ; void f ( ) { int * a ; a = do_something ( ) ; }", checkSimplifyEnum(code3)); + ASSERT_EQUALS("void f ( ) { int * a ; a = do_something ( ) ; }", checkSimplifyEnum(code3)); const char code4[] = "enum { a, b }; void f() { int &a=x; }"; - ASSERT_EQUALS("enum { a = 0 , b = 1 } ; void f ( ) { int & a = x ; }", checkSimplifyEnum(code4)); + ASSERT_EQUALS("void f ( ) { int & a = x ; }", checkSimplifyEnum(code4)); // #4857 - not shadow variable checkSimplifyEnum("enum { a,b }; void f() { if (x) { } else if ( x & a ) {} }"); @@ -3407,7 +3388,7 @@ private: void enum40() { const char code[] = "enum { A=(1<<0)|(1<<1) }; void f() { x = y + A; }"; - ASSERT_EQUALS("enum { A = 1 | ( 2 ) } ; void f ( ) { x = y + ( 3 ) ; }", checkSimplifyEnum(code)); + ASSERT_EQUALS("void f ( ) { x = y + ( 3 ) ; }", checkSimplifyEnum(code)); } void enum41() { // ticket #5212 (valgrind errors during enum simplification) @@ -3419,38 +3400,35 @@ private: " };\n" "}\n" "int x = Foo::eAll;"; - ASSERT_EQUALS("namespace Foo { enum BarConfig { eBitOne = 1 , eBitTwo = 2 , eAll = 1 | ( 2 ) } ; } " - "int x ; x = ( 1 ) | 2 ;", checkSimplifyEnum(code)); + ASSERT_EQUALS("int x ; x = ( 1 ) | 2 ;", checkSimplifyEnum(code)); } void enum42() { // ticket #5182 (template function call in template value) const char code[] = "enum { A = f() };\n" "a = A;"; - ASSERT_EQUALS("enum { A = f < int , 2 > ( ) } ; a = f < int , 2 > ( ) ;", checkSimplifyEnum(code)); + ASSERT_EQUALS("a = f < int , 2 > ( ) ;", checkSimplifyEnum(code)); } void enum43() { // lhs in assignment const char code[] = "enum { A, B };\n" "A = 1;"; - ASSERT_EQUALS("enum { A = 0 , B = 1 } ; A = 1 ;", checkSimplifyEnum(code)); + ASSERT_EQUALS("A = 1 ;", checkSimplifyEnum(code)); } void enum44() { const char code1[] = "enum format_t { YYYYMMDD = datemask_traits< datemask<'Y', 'Y', 'Y', 'Y', '/', 'M', 'M', '/', 'D', 'D'> >::value, };\n" "YYYYMMDD;"; - ASSERT_EQUALS("enum format_t { YYYYMMDD = datemask_traits < datemask < 'Y' , 'Y' , 'Y' , 'Y' , '/' , 'M' , 'M' , '/' , 'D' , 'D' > > :: value , } ; " - "datemask_traits < datemask < 'Y' , 'Y' , 'Y' , 'Y' , '/' , 'M' , 'M' , '/' , 'D' , 'D' > > :: value ;", checkSimplifyEnum(code1)); + ASSERT_EQUALS("( datemask_traits < datemask < 'Y' , 'Y' , 'Y' , 'Y' , '/' , 'M' , 'M' , '/' , 'D' , 'D' > > :: value ) ;", checkSimplifyEnum(code1)); const char code2[] = "enum format_t { YYYYMMDD = datemask_traits< datemask<'Y', 'Y', 'Y', 'Y', '/', 'M', 'M', '/', 'D', 'D'>>::value, };\n" "YYYYMMDD;"; - ASSERT_EQUALS("enum format_t { YYYYMMDD = datemask_traits < datemask < 'Y' , 'Y' , 'Y' , 'Y' , '/' , 'M' , 'M' , '/' , 'D' , 'D' > > :: value , } ; " - "datemask_traits < datemask < 'Y' , 'Y' , 'Y' , 'Y' , '/' , 'M' , 'M' , '/' , 'D' , 'D' > > :: value ;", checkSimplifyEnum(code2)); + ASSERT_EQUALS("( datemask_traits < datemask < 'Y' , 'Y' , 'Y' , 'Y' , '/' , 'M' , 'M' , '/' , 'D' , 'D' > > :: value ) ;", checkSimplifyEnum(code2)); } void enumscope1() { // #3949 - don't simplify enum from one function in another function const char code[] = "void foo() { enum { A = 0, B = 1 }; }\n" "void bar() { int a = A; }"; - ASSERT_EQUALS("void foo ( ) { enum { A = 0 , B = 1 } ; } void bar ( ) { int a ; a = A ; }", checkSimplifyEnum(code)); + ASSERT_EQUALS("void foo ( ) { } void bar ( ) { int a ; a = A ; }", checkSimplifyEnum(code)); } void duplicateDefinition() { // #3565 - wrongly detects duplicate definition diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index b0b1a9b7e..f0211722a 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -425,10 +425,8 @@ private: "abc e1;\n" "XYZ e2;"; - const char expected[] = "enum abc { a = 0 , b = 1 , c = 2 } ; " - "enum xyz { x = 0 , y = 1 , z = 2 } ; " - "abc e1 ; " - "enum xyz e2 ;"; + const char expected[] = "int e1 ; " + "int e2 ;"; ASSERT_EQUALS(expected, tok(code, false)); } @@ -1624,16 +1622,13 @@ private: void simplifyTypedef60() { // ticket #2035 const char code[] = "typedef enum {qfalse, qtrue} qboolean;\n" "typedef qboolean (*localEntitiyAddFunc_t) (struct le_s * le, entity_t * ent);\n" - "void f() {\n" + "void f()\n" + "{\n" " qboolean b;\n" " localEntitiyAddFunc_t f;\n" "}"; // The expected result.. - const char expected[] = "enum qboolean { qfalse = 0 , qtrue = 1 } ; " - "void f ( ) { " - "qboolean b ; " - "qboolean * f ; " - "}"; + const char expected[] = "void f ( ) { int b ; int * f ; }"; ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS("", errout.str()); } @@ -2427,8 +2422,10 @@ private: " domain_dim = TrafoEvaluator::domain_dim,\n" " };\n" "};"; - (void)tok(code); - ASSERT_EQUALS("", errout.str()); // <- no syntax error + + const char expected[] = "template < typename DataType , typename SpaceType , typename TrafoConfig > class AsmTraits1 { } ;"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); } void simplifyTypedefFunction1() { @@ -3010,12 +3007,13 @@ private: "typedef MySpace::Format_E2 (**PtrToFunPtr_Type2)();\n" "PtrToFunPtr_Type1 t1;\n" "PtrToFunPtr_Type2 t2;"; - ASSERT_EQUALS("enum Format_E1 { FORMAT11 FORMAT12 } ; Format_E1 Format_T1 ; " - "namespace MySpace { " - "enum Format_E2 { FORMAT21 FORMAT22 } ; Format_E2 Format_T2 ; " + ASSERT_EQUALS("int Format_T1 ; " + "namespace MySpace " + "{ " + "int Format_T2 ; " "} " - "Format_E1 ( * * t1 ) ( ) ; " - "MySpace :: Format_E2 ( * * t2 ) ( ) ;", + "int ( * * t1 ) ( ) ; " + "int ( * * t2 ) ( ) ;", tok(code,false)); } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index af2abd553..a153186e6 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -2672,8 +2672,7 @@ private: "int baz() { " " return sizeof(arr_t); " "}"; - ASSERT_EQUALS("enum e { VAL1 = 1 ; VAL2 = 2 } ; " - "int foo ( int ) ; " + ASSERT_EQUALS("int foo ( int ) ; " "void bar ( ) { throw foo ( 1 ) ; } " "int baz ( ) { return 2 ; }", tokenizeAndStringify(code, true)); } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 55feb6d14..1c7f2ab65 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1702,7 +1702,7 @@ private: " AB ab;\n" " if (ab);\n" "}"); - TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: ab\n", "", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: ab\n", errout.str()); } // references..