Tokenizer: Don't remove enum declarations

This commit is contained in:
Daniel Marjamäki 2015-09-06 17:44:49 +02:00
parent 4cffff7bde
commit 00c54df07c
7 changed files with 118 additions and 169 deletions

View File

@ -7278,18 +7278,8 @@ 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")) {
@ -7652,7 +7642,7 @@ void Tokenizer::simplifyEnum()
tempTok->insertToken(";");
tempTok = tempTok->next();
if (typeTokenStart == 0)
tempTok->insertToken("int");
tempTok->insertToken(enumType ? enumType->str() : std::string("int"));
else {
Token *tempTok1 = typeTokenStart;
@ -7666,83 +7656,6 @@ 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"
"};");
ASSERT_EQUALS("[test.cpp:10]: (warning) Member variable 'Fred::_code' is not initialized in the constructor.\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:10]: (warning) Member variable 'Fred::_code' is not initialized in the constructor.\n", "", errout.str());
check("class A{};\n"

View File

@ -1017,7 +1017,18 @@ private:
"{\n"
" enum {value = !type_equal<T, typename Unconst<T>::type>::value };\n"
"};";
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> { } ;";
const char expected1[] = "template < class T > struct Unconst { } ; "
"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));
}

View File

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

View File

@ -425,8 +425,10 @@ private:
"abc e1;\n"
"XYZ e2;";
const char expected[] = "int e1 ; "
"int 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 ;";
ASSERT_EQUALS(expected, tok(code, false));
}
@ -1622,13 +1624,16 @@ 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"
"{\n"
"void f() {\n"
" qboolean b;\n"
" localEntitiyAddFunc_t f;\n"
"}";
// The expected result..
const char expected[] = "void f ( ) { int b ; int * f ; }";
const char expected[] = "enum qboolean { qfalse = 0 , qtrue = 1 } ; "
"void f ( ) { "
"qboolean b ; "
"qboolean * f ; "
"}";
ASSERT_EQUALS(expected, tok(code, false));
ASSERT_EQUALS("", errout.str());
}
@ -2422,10 +2427,8 @@ private:
" domain_dim = TrafoEvaluator::domain_dim,\n"
" };\n"
"};";
const char expected[] = "template < typename DataType , typename SpaceType , typename TrafoConfig > class AsmTraits1 { } ;";
ASSERT_EQUALS(expected, tok(code));
ASSERT_EQUALS("", errout.str());
(void)tok(code);
ASSERT_EQUALS("", errout.str()); // <- no syntax error
}
void simplifyTypedefFunction1() {
@ -3007,13 +3010,12 @@ private:
"typedef MySpace::Format_E2 (**PtrToFunPtr_Type2)();\n"
"PtrToFunPtr_Type1 t1;\n"
"PtrToFunPtr_Type2 t2;";
ASSERT_EQUALS("int Format_T1 ; "
"namespace MySpace "
"{ "
"int Format_T2 ; "
ASSERT_EQUALS("enum Format_E1 { FORMAT11 FORMAT12 } ; Format_E1 Format_T1 ; "
"namespace MySpace { "
"enum Format_E2 { FORMAT21 FORMAT22 } ; Format_E2 Format_T2 ; "
"} "
"int ( * * t1 ) ( ) ; "
"int ( * * t2 ) ( ) ;",
"Format_E1 ( * * t1 ) ( ) ; "
"MySpace :: Format_E2 ( * * t2 ) ( ) ;",
tok(code,false));
}

View File

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

View File

@ -1702,7 +1702,7 @@ private:
" AB ab;\n"
" if (ab);\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: ab\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: ab\n", "", errout.str());
}
// references..