diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 650420b87..76db9bdff 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8299,13 +8299,13 @@ void Tokenizer::simplifyStructDecl() continue; // check for anonymous struct/union if (Token::Match(tok, "struct|union {")) { - if (Token::Match(tok->next()->link(), "} *|&| %type% ,|;|[")) { + if (Token::Match(tok->next()->link(), "} *|&| %type% ,|;|[|(|{")) { tok->insertToken("Anonymous" + MathLib::toString(count++)); } } // check for anonymous enum - else if ((Token::simpleMatch(tok, "enum {") && Token::Match(tok->next()->link(), "} %type%| ,|;|[")) || - (Token::Match(tok, "enum : %type% {") && Token::Match(tok->linkAt(3), "} %type%| ,|;|["))) { + else if ((Token::simpleMatch(tok, "enum {") && Token::Match(tok->next()->link(), "} %type%| ,|;|[|(|{")) || + (Token::Match(tok, "enum : %type% {") && Token::Match(tok->linkAt(3), "} %type%| ,|;|[|(|{"))) { tok->insertToken("Anonymous" + MathLib::toString(count++)); } } @@ -8339,7 +8339,7 @@ void Tokenizer::simplifyStructDecl() Token *restart = next; // check for named type - if (Token::Match(tok->next(), "*|&| %type% ,|;|[|=")) { + if (Token::Match(tok->next(), "*|&| %type% ,|;|[|=|(|{")) { tok->insertToken(";"); tok = tok->next(); while (!Token::Match(start, "struct|class|union|enum")) { @@ -8350,8 +8350,25 @@ void Tokenizer::simplifyStructDecl() if (!tok) break; // see #4869 segmentation fault in Tokenizer::simplifyStructDecl (invalid code) tok->insertToken(type->str()); - if (start->str() != "class") + if (start->str() != "class") { tok->insertToken(start->str()); + tok = tok->next(); + } + + tok = tok->tokAt(2); + + // check for initialization + if (tok && (tok->next()->str() == "(" || tok->next()->str() == "{")) { + tok->insertToken("="); + tok = tok->next(); + + if (start->str() == "enum") { + if (tok->next()->str() == "{") { + tok->next()->str("("); + tok->linkAt(1)->str(")"); + } + } + } } tok = restart; diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 5878f4cbb..fb376d214 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -186,6 +186,7 @@ private: TEST_CASE(simplifyStructDecl4); TEST_CASE(simplifyStructDecl6); // ticket #3732 TEST_CASE(simplifyStructDecl7); // ticket #476 (static anonymous struct array) + TEST_CASE(simplifyStructDecl8); // ticket #7698 // register int var; => int var; // inline int foo() {} => int foo() {} @@ -3217,6 +3218,19 @@ private: tok("static struct { char x; } a[2];", false)); } + void simplifyStructDecl8() { + ASSERT_EQUALS("enum A { x , y , z } ; enum A a ; a = x ;", tok("enum A { x, y, z } a(x);", false)); + ASSERT_EQUALS("enum B { x , y , z } ; enum B b ; b = x ;", tok("enum B { x , y, z } b{x};", false)); + ASSERT_EQUALS("struct C { int i ; } ; struct C c ; c = { 0 } ;", tok("struct C { int i; } c{0};", false)); + ASSERT_EQUALS("enum Anonymous0 { x , y , z } ; enum Anonymous0 d ; d = x ;", tok("enum { x, y, z } d(x);", false)); + ASSERT_EQUALS("enum Anonymous0 { x , y , z } ; enum Anonymous0 e ; e = x ;", tok("enum { x, y, z } e{x};", false)); + ASSERT_EQUALS("struct Anonymous0 { int i ; } ; struct Anonymous0 f ; f = { 0 } ;", tok("struct { int i; } f{0};", false)); + ASSERT_EQUALS("enum G : short { x , y , z } ; enum G g ; g = x ;", tok("enum G : short { x, y, z } g(x);", false)); + ASSERT_EQUALS("enum H : short { x , y , z } ; enum H h ; h = x ;", tok("enum H : short { x, y, z } h{x};", false)); + ASSERT_EQUALS("enum class I : short { x , y , z } ; enum I i ; i = x ;", tok("enum class I : short { x, y, z } i(x);", false)); + ASSERT_EQUALS("enum class J : short { x , y , z } ; enum J j ; j = x ;", tok("enum class J : short { x, y, z } j{x};", false)); + } + void removeUnwantedKeywords() { ASSERT_EQUALS("int var ;", tok("register int var ;", true)); ASSERT_EQUALS("short var ;", tok("register short int var ;", true));