Reverted 00c54df07c (don't remove enum declarations) because it caused unexpected false positives

This commit is contained in:
Daniel Marjamäki 2015-09-06 18:37:22 +02:00
parent 00c54df07c
commit d88dc3ed3e
7 changed files with 169 additions and 118 deletions

View File

@ -7278,8 +7278,18 @@ void Tokenizer::simplifyEnum()
{ {
std::string className; std::string className;
int classLevel = 0; int classLevel = 0;
bool goback = false;
const bool printStyle = _settings->isEnabled("style"); const bool printStyle = _settings->isEnabled("style");
for (Token *tok = list.front(); tok; tok = tok->next()) { 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() && if (tok->next() &&
(!tok->previous() || (tok->previous()->str() != "enum")) && (!tok->previous() || (tok->previous()->str() != "enum")) &&
Token::Match(tok, "class|struct|namespace")) { Token::Match(tok, "class|struct|namespace")) {
@ -7642,7 +7652,7 @@ void Tokenizer::simplifyEnum()
tempTok->insertToken(";"); tempTok->insertToken(";");
tempTok = tempTok->next(); tempTok = tempTok->next();
if (typeTokenStart == 0) if (typeTokenStart == 0)
tempTok->insertToken(enumType ? enumType->str() : std::string("int")); tempTok->insertToken("int");
else { else {
Token *tempTok1 = typeTokenStart; 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;
}
} }
} }
} }

View File

@ -1423,7 +1423,7 @@ private:
" ECODES _code;\n" " 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" check("class A{};\n"

View File

@ -1017,18 +1017,7 @@ private:
"{\n" "{\n"
" enum {value = !type_equal<T, typename Unconst<T>::type>::value };\n" " enum {value = !type_equal<T, typename Unconst<T>::type>::value };\n"
"};"; "};";
const char expected1[] = "template < class T > struct Unconst { } ; " const char expected1[]="template < class T > struct Unconst { } ; template < class T > struct type_equal<T,T> { } ; template < class T > struct template_is_const { } ; struct type_equal<T,T> { } ; struct Unconst<constT*const> { } ; struct Unconst<constT&*const> { } ; struct Unconst<T*const*const> { } ; struct Unconst<T*const> { } ; struct Unconst<T*const> { } ; struct Unconst<T*const> { } ; struct Unconst<constT&><};template<T> { } ; struct Unconst<constT><};template<T> { } ;";
"template < class T > struct type_equal<T,T> { enum { value = 1 } ; } ; "
"template < class T > struct template_is_const { enum { value = ! type_equal < T , Unconst < T > :: type > :: value } ; } ; "
"struct type_equal<T,T> { enum { value = 0 } ; } ; "
"struct Unconst<constT*const> { } ; "
"struct Unconst<constT&*const> { } ; "
"struct Unconst<T*const*const> { } ; "
"struct Unconst<T*const> { } ; "
"struct Unconst<T*const> { } ; "
"struct Unconst<T*const> { } ; "
"struct Unconst<constT&><};template<T> { } ; "
"struct Unconst<constT><};template<T> { } ;";
ASSERT_EQUALS(expected1, tok(code1)); ASSERT_EQUALS(expected1, tok(code1));
} }

View File

@ -2872,19 +2872,21 @@ private:
void enum1() { void enum1() {
const char code[] = "enum A { a, b, c }; A c1 = c;"; 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)); ASSERT_EQUALS(expected, tok(code, false));
} }
void enum2() { void enum2() {
const char code[] = "enum A { a, }; int array[a];"; 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)); ASSERT_EQUALS(expected, tok(code, false));
} }
void enum3() { void enum3() {
const char code[] = "enum { a, }; int array[a];"; 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)); ASSERT_EQUALS(expected, tok(code, false));
} }
@ -2904,14 +2906,14 @@ private:
const char expected[] = "class A { " const char expected[] = "class A { "
"public: " "public: "
"enum EA { a1 = 0 , a2 = 1 , a3 = 2 } ; " ""
"EA get ( ) const ; " "int get ( ) const ; "
"void put ( EA a ) { ea = a ; ea = 0 ; } " "void put ( int a ) { ea = a ; ea = 0 ; } "
"private: " "private: "
"EA ea ; " "int ea ; "
"} ; " "} ; "
"A :: EA A :: get ( ) const { return ea ; } " "int A :: get ( ) const { return ea ; } "
"A :: EA e ; e = 0 ;"; "int e ; e = 0 ;";
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
} }
@ -2927,13 +2929,13 @@ private:
"A::EA e = A::a1;"; "A::EA e = A::a1;";
const char expected[] = "struct A { " const char expected[] = "struct A { "
"enum EA { a1 = 0 , a2 = 1 , a3 = 2 } ; " ""
"EA get ( ) const ; " "int get ( ) const ; "
"void put ( EA a ) { ea = a ; ea = 0 ; } " "void put ( int a ) { ea = a ; ea = 0 ; } "
"EA ea ; " "int ea ; "
"} ; " "} ; "
"A :: EA A :: get ( ) const { return ea ; } " "int A :: get ( ) const { return ea ; } "
"A :: EA e ; e = 0 ;"; "int e ; e = 0 ;";
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
} }
@ -2950,8 +2952,7 @@ private:
" g\n" " g\n"
"};\n" "};\n"
"int sum = a + b + c + d + e + f + g;"; "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 } ; " const char expected[] = "int sum ; sum = "
"int sum ; sum = "
"sizeof ( int ) + " "sizeof ( int ) + "
"( 1 + sizeof ( int ) ) + " "( 1 + sizeof ( int ) ) + "
"( 1 + sizeof ( int ) + 100 ) + " // 101 = 100 + 1 "( 1 + sizeof ( int ) + 100 ) + " // 101 = 100 + 1
@ -2960,12 +2961,12 @@ private:
";"; ";";
ASSERT_EQUALS(expected, tok(code, false)); 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() { void enum6() {
const char code[] = "enum { a = MAC(A, B, C) }; void f(a) { }"; 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)); ASSERT_EQUALS(expected, tok(code, false));
} }
@ -2978,8 +2979,7 @@ private:
" int A = B;\n" " int A = B;\n"
" { float A = C; }\n" " { float A = C; }\n"
"}"; "}";
const char expected[] = "enum FOO { A = 0 , B = 1 , C = 2 } ; " const char expected[] = "int main ( ) "
"int main ( ) "
"{ " "{ "
"int A ; A = 1 ; " "int A ; A = 1 ; "
"{ float A ; A = 2 ; } " "{ float A ; A = 2 ; } "
@ -2990,8 +2990,7 @@ private:
{ {
const char code[] = "enum FOO {A,B,C};\n" const char code[] = "enum FOO {A,B,C};\n"
"void f(int A, float B, char C) { }"; "void f(int A, float B, char C) { }";
const char expected[] = "enum FOO { A = 0 , B = 1 , C = 2 } ; " const char expected[] = "void f ( int A , float B , char C ) { }";
"void f ( int A , float B , char C ) { }";
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
} }
} }
@ -3069,8 +3068,7 @@ private:
const char code[] = "enum {\n" const char code[] = "enum {\n"
"SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1,\n" "SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1,\n"
"} e = SHELL_SIZE;"; "} e = SHELL_SIZE;";
const char expected[] = "enum { SHELL_SIZE = sizeof ( union { int i ; char * cp ; double d ; } ) - 1 , } ; " const char expected[] = "int e ; e = sizeof ( union { int i ; char * cp ; double d ; } ) - 1 ;";
"int e ; e = sizeof ( union { int i ; char * cp ; double d ; } ) - 1 ;";
ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
@ -3084,7 +3082,7 @@ private:
"}"; "}";
const char expected[] = "int main ( ) " const char expected[] = "int main ( ) "
"{ " "{ "
"enum { u = 0 , v = 1 } ; " ""
"A u ; u = 1 ; A v ; v = 2 ; " "A u ; u = 1 ; A v ; v = 2 ; "
"}"; "}";
ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
@ -3099,7 +3097,7 @@ private:
"{\n" "{\n"
" unsigned int fred = 0;\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)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
} }
@ -3109,15 +3107,14 @@ private:
"{\n" "{\n"
" unsigned int fred = a;\n" " unsigned int fred = a;\n"
"}"; "}";
const char expected[] = "enum ab { ENTRY ( 1 , a = 0 ) , ENTRY ( 2 , b ) } ; " const char expected[] = "void foo ( ) { unsigned int fred ; fred = a ; }";
"void foo ( ) { unsigned int fred ; fred = a ; }";
ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
} }
void enum14() { void enum14() {
const char code[] = "enum ab { a };\n" const char code[] = "enum ab { a };\n"
"ab"; "ab";
const char expected[] = "enum ab { a = 0 } ; ab"; const char expected[] = "ab";
ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
} }
@ -3125,63 +3122,56 @@ private:
{ {
const char code[] = "enum : char { a = 99 };\n" const char code[] = "enum : char { a = 99 };\n"
"char c1 = a;"; "char c1 = a;";
const char expected[] = "enum : char { a = 99 } ; " const char expected[] = "char c1 ; c1 = 99 ;";
"char c1 ; c1 = 99 ;";
ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
} }
{ {
const char code[] = "enum class Enum1 { a };\n" const char code[] = "enum class Enum1 { a };\n"
"Enum1 e1 = Enum1::a;"; "Enum1 e1 = Enum1::a;";
const char expected[] = "enum Enum1 { a = 0 } ; " const char expected[] = "int e1 ; e1 = 0 ;";
"Enum1 e1 ; e1 = 0 ;";
ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
} }
{ {
const char code[] = "enum class Enum1 { a };\n" const char code[] = "enum class Enum1 { a };\n"
"Enum1 e1 = a;"; "Enum1 e1 = a;";
const char expected[] = "enum Enum1 { a = 0 } ; " const char expected[] = "int e1 ; e1 = a ;";
"Enum1 e1 ; e1 = a ;";
ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
} }
{ {
const char code[] = "enum Enum1 : char { a };\n" const char code[] = "enum Enum1 : char { a };\n"
"Enum1 e1 = a;"; "Enum1 e1 = a;";
const char expected[] = "enum Enum1 : char { a = 0 } ; " const char expected[] = "char e1 ; e1 = 0 ;";
"Enum1 e1 ; e1 = 0 ;";
ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
} }
{ {
const char code[] = "enum class Enum1 : unsigned char { a };\n" const char code[] = "enum class Enum1 : unsigned char { a };\n"
"Enum1 e1 = Enum1::a;"; "Enum1 e1 = Enum1::a;";
const char expected[] = "enum Enum1 : unsigned char { a = 0 } ; " const char expected[] = "unsigned char e1 ; e1 = 0 ;";
"Enum1 e1 ; e1 = 0 ;";
ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
} }
{ {
const char code[] = "enum class Enum1 : unsigned int { a };\n" const char code[] = "enum class Enum1 : unsigned int { a };\n"
"Enum1 e1 = Enum1::a;"; "Enum1 e1 = Enum1::a;";
const char expected[] = "enum Enum1 : unsigned int { a = 0 } ; " const char expected[] = "unsigned int e1 ; e1 = 0 ;";
"Enum1 e1 ; e1 = 0 ;";
ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
} }
{ {
const char code[] = "enum class Enum1 : unsigned long long int { a };\n" const char code[] = "enum class Enum1 : unsigned long long int { a };\n"
"Enum1 e1 = Enum1::a;"; "Enum1 e1 = Enum1::a;";
const char expected[] = "enum Enum1 : unsigned long long { a = 0 } ; " const char expected[] = "unsigned long long e1 ; e1 = 0 ;";
"Enum1 e1 ; e1 = 0 ;";
ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
} }
{ {
const char code[] = "enum class { A };\n" const char code[] = "enum class { A };\n"
"int i = A;"; "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' ASSERT_EQUALS(expected, checkSimplifyEnum(code, false)); // Compile as C code: enum has name 'class'
checkSimplifyEnum(code, true); // Compile as C++ code: Don't crash 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 // if header is included twice its enums will be duplicated
const char code[] = "enum ab { a=0, b };" const char code[] = "enum ab { a=0, b };"
"enum ab { a=0, b };\n"; "enum ab { a=0, b };\n";
ASSERT_EQUALS("enum ab { a = 0 , b = 1 } ;", checkSimplifyEnum(code)); ASSERT_EQUALS(";", checkSimplifyEnum(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void enum18() { // ticket #2466 - array with same name as enum constant void enum18() { // ticket #2466 - array with same name as enum constant
const char code[] = "enum ab { a=0, b };\n" const char code[] = "enum ab { a=0, b };\n"
"void f() { a[0]; }\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 void enum19() { // ticket #2536
@ -3221,12 +3211,12 @@ private:
void enum20() { // ticket #2600 segmentation fault void enum20() { // ticket #2600 segmentation fault
const char code[] = "enum { const }\n"; const char code[] = "enum { const }\n";
ASSERT_EQUALS("enum { const = 0 }", checkSimplifyEnum(code)); ASSERT_EQUALS("", checkSimplifyEnum(code));
} }
void enum21() { // ticket #2720 syntax error void enum21() { // ticket #2720 syntax error
const char code[] = "enum E2 : signed const short { };\n"; 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()); ASSERT_EQUALS("", errout.str());
} }
@ -3271,8 +3261,7 @@ private:
void enum23() { // ticket #2804 void enum23() { // ticket #2804
const char code[] = "enum Enumerator : std::uint8_t { ITEM1, ITEM2, ITEM3 };\n" const char code[] = "enum Enumerator : std::uint8_t { ITEM1, ITEM2, ITEM3 };\n"
"Enumerator e = ITEM3;\n"; "Enumerator e = ITEM3;\n";
const char expected[] = "enum Enumerator : std :: uint8_t { ITEM1 = 0 , ITEM2 = 1 , ITEM3 = 2 } ; " const char expected[] = "std :: uint8_t e ; e = 2 ;";
"Enumerator e ; e = 2 ;";
ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -3282,10 +3271,7 @@ private:
"void f(long style) {\n" "void f(long style) {\n"
" if (style & STYLE) { }\n" " if (style & STYLE) { }\n"
"}\n"; "}\n";
const char expected[] = "enum EnumName { STYLE = 1 } ; " const char expected[] = "void f ( long style ) { if ( style & 1 ) { } }";
"void f ( long style ) { "
"if ( style & 1 ) { } "
"}";
ASSERT_EQUALS(expected, checkSimplifyEnum(code)); ASSERT_EQUALS(expected, checkSimplifyEnum(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -3310,12 +3296,12 @@ private:
"void f() { char x[4]; memset(x, 0, 4);\n" "void f() { char x[4]; memset(x, 0, 4);\n"
"{ x } };\n" "{ x } };\n"
"void g() { x; }"; "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 void enum29() { // #3747 - bitwise or value
const char code[] = "enum { x=1, y=x|2 }; i = (3==y);"; 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 void enum30() { // #3852 - false positive
@ -3336,57 +3322,52 @@ private:
"int main() {" "int main() {"
" return TestIf::Bar::two;\n" " return TestIf::Bar::two;\n"
"}"; "}";
ASSERT_EQUALS("class TestIf { " ASSERT_EQUALS("class TestIf { public: } ; int main ( ) { return 1 ; }", checkSimplifyEnum(code));
"public: "
"enum Foo { one = 0 , two = 1 } ; "
"enum Bar { one = 0 , two = 1 } ; "
"} ; "
"int main ( ) { return 1 ; }", checkSimplifyEnum(code));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void enum31() { // #3934 - calculation in first item void enum31() { // #3934 - calculation in first item
const char code[] = "enum { x=2*32, y }; i = y;"; 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 void enum32() { // #3998 - wrong enum simplification => access violation
const char code[] = "enum { x=(32), y=x, z }; { a, z }"; 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 void enum33() { // #4015 - segmentation fault
const char code[] = "enum { A=SOME_VALUE, B=A };"; 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 void enum34() { // #4141 - division by zero
const char code[] = "enum { A=1/0 };"; 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 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("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) { }"));
} }
void enum36() { // #4378 void enum36() { // #4378
const char code[] = "struct X { enum Y { a, b }; X(Y) { Y y = (Y)1; } };"; 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 void enum37() { // #4280 - shadow variables
const char code1[] = "enum { a, b }; void f(int a) { return a + 1; }"; 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; }"; 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(); }"; 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; }"; 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 // #4857 - not shadow variable
checkSimplifyEnum("enum { a,b }; void f() { if (x) { } else if ( x & a ) {} }"); checkSimplifyEnum("enum { a,b }; void f() { if (x) { } else if ( x & a ) {} }");
@ -3407,7 +3388,7 @@ private:
void enum40() { void enum40() {
const char code[] = "enum { A=(1<<0)|(1<<1) }; void f() { x = y + A; }"; 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) void enum41() { // ticket #5212 (valgrind errors during enum simplification)
@ -3419,38 +3400,35 @@ private:
" };\n" " };\n"
"}\n" "}\n"
"int x = Foo::eAll;"; "int x = Foo::eAll;";
ASSERT_EQUALS("namespace Foo { enum BarConfig { eBitOne = 1 , eBitTwo = 2 , eAll = 1 | ( 2 ) } ; } " ASSERT_EQUALS("int x ; x = ( 1 ) | 2 ;", checkSimplifyEnum(code));
"int x ; x = ( 1 ) | 2 ;", checkSimplifyEnum(code));
} }
void enum42() { // ticket #5182 (template function call in template value) void enum42() { // ticket #5182 (template function call in template value)
const char code[] = "enum { A = f<int,2>() };\n" const char code[] = "enum { A = f<int,2>() };\n"
"a = A;"; "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 void enum43() { // lhs in assignment
const char code[] = "enum { A, B };\n" const char code[] = "enum { A, B };\n"
"A = 1;"; "A = 1;";
ASSERT_EQUALS("enum { A = 0 , B = 1 } ; A = 1 ;", checkSimplifyEnum(code)); ASSERT_EQUALS("A = 1 ;", checkSimplifyEnum(code));
} }
void enum44() { void enum44() {
const char code1[] = "enum format_t { YYYYMMDD = datemask_traits< datemask<'Y', 'Y', 'Y', 'Y', '/', 'M', 'M', '/', 'D', 'D'> >::value, };\n" const char code1[] = "enum format_t { YYYYMMDD = datemask_traits< datemask<'Y', 'Y', 'Y', 'Y', '/', 'M', 'M', '/', 'D', 'D'> >::value, };\n"
"YYYYMMDD;"; "YYYYMMDD;";
ASSERT_EQUALS("enum format_t { YYYYMMDD = datemask_traits < datemask < 'Y' , 'Y' , 'Y' , 'Y' , '/' , 'M' , 'M' , '/' , 'D' , 'D' > > :: value , } ; " ASSERT_EQUALS("( datemask_traits < datemask < 'Y' , 'Y' , 'Y' , 'Y' , '/' , 'M' , 'M' , '/' , 'D' , 'D' > > :: value ) ;", checkSimplifyEnum(code1));
"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" const char code2[] = "enum format_t { YYYYMMDD = datemask_traits< datemask<'Y', 'Y', 'Y', 'Y', '/', 'M', 'M', '/', 'D', 'D'>>::value, };\n"
"YYYYMMDD;"; "YYYYMMDD;";
ASSERT_EQUALS("enum format_t { YYYYMMDD = datemask_traits < datemask < 'Y' , 'Y' , 'Y' , 'Y' , '/' , 'M' , 'M' , '/' , 'D' , 'D' > > :: value , } ; " ASSERT_EQUALS("( datemask_traits < datemask < 'Y' , 'Y' , 'Y' , 'Y' , '/' , 'M' , 'M' , '/' , 'D' , 'D' > > :: value ) ;", checkSimplifyEnum(code2));
"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 void enumscope1() { // #3949 - don't simplify enum from one function in another function
const char code[] = "void foo() { enum { A = 0, B = 1 }; }\n" const char code[] = "void foo() { enum { A = 0, B = 1 }; }\n"
"void bar() { int a = A; }"; "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 void duplicateDefinition() { // #3565 - wrongly detects duplicate definition

View File

@ -425,10 +425,8 @@ private:
"abc e1;\n" "abc e1;\n"
"XYZ e2;"; "XYZ e2;";
const char expected[] = "enum abc { a = 0 , b = 1 , c = 2 } ; " const char expected[] = "int e1 ; "
"enum xyz { x = 0 , y = 1 , z = 2 } ; " "int e2 ;";
"abc e1 ; "
"enum xyz e2 ;";
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
} }
@ -1624,16 +1622,13 @@ private:
void simplifyTypedef60() { // ticket #2035 void simplifyTypedef60() { // ticket #2035
const char code[] = "typedef enum {qfalse, qtrue} qboolean;\n" const char code[] = "typedef enum {qfalse, qtrue} qboolean;\n"
"typedef qboolean (*localEntitiyAddFunc_t) (struct le_s * le, entity_t * ent);\n" "typedef qboolean (*localEntitiyAddFunc_t) (struct le_s * le, entity_t * ent);\n"
"void f() {\n" "void f()\n"
"{\n"
" qboolean b;\n" " qboolean b;\n"
" localEntitiyAddFunc_t f;\n" " localEntitiyAddFunc_t f;\n"
"}"; "}";
// The expected result.. // The expected result..
const char expected[] = "enum qboolean { qfalse = 0 , qtrue = 1 } ; " const char expected[] = "void f ( ) { int b ; int * f ; }";
"void f ( ) { "
"qboolean b ; "
"qboolean * f ; "
"}";
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -2427,8 +2422,10 @@ private:
" domain_dim = TrafoEvaluator::domain_dim,\n" " domain_dim = TrafoEvaluator::domain_dim,\n"
" };\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() { void simplifyTypedefFunction1() {
@ -3010,12 +3007,13 @@ private:
"typedef MySpace::Format_E2 (**PtrToFunPtr_Type2)();\n" "typedef MySpace::Format_E2 (**PtrToFunPtr_Type2)();\n"
"PtrToFunPtr_Type1 t1;\n" "PtrToFunPtr_Type1 t1;\n"
"PtrToFunPtr_Type2 t2;"; "PtrToFunPtr_Type2 t2;";
ASSERT_EQUALS("enum Format_E1 { FORMAT11 FORMAT12 } ; Format_E1 Format_T1 ; " ASSERT_EQUALS("int Format_T1 ; "
"namespace MySpace { " "namespace MySpace "
"enum Format_E2 { FORMAT21 FORMAT22 } ; Format_E2 Format_T2 ; " "{ "
"int Format_T2 ; "
"} " "} "
"Format_E1 ( * * t1 ) ( ) ; " "int ( * * t1 ) ( ) ; "
"MySpace :: Format_E2 ( * * t2 ) ( ) ;", "int ( * * t2 ) ( ) ;",
tok(code,false)); tok(code,false));
} }

View File

@ -2672,8 +2672,7 @@ private:
"int baz() { " "int baz() { "
" return sizeof(arr_t); " " return sizeof(arr_t); "
"}"; "}";
ASSERT_EQUALS("enum e { VAL1 = 1 ; VAL2 = 2 } ; " ASSERT_EQUALS("int foo ( int ) ; "
"int foo ( int ) ; "
"void bar ( ) { throw foo ( 1 ) ; } " "void bar ( ) { throw foo ( 1 ) ; } "
"int baz ( ) { return 2 ; }", tokenizeAndStringify(code, true)); "int baz ( ) { return 2 ; }", tokenizeAndStringify(code, true));
} }

View File

@ -1702,7 +1702,7 @@ private:
" AB ab;\n" " AB ab;\n"
" if (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.. // references..