diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d72ccafb0..38e7adef2 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6925,15 +6925,23 @@ void Tokenizer::simplifyStructDecl() tok = tok->next(); tok->insertToken(name.c_str()); } + + // unnamed anonymous struct/union so remove it else if (tok->next()->str() == ";") { - Token *previous = tok1->previous(); - previous->deleteNext(); - previous->deleteNext(); - tok1 = previous->next(); - previous = tok->previous(); - previous->deleteNext(); - previous->deleteNext(); + tok1->deleteThis(); + if (tok1->next() == tok) + { + tok1->deleteThis(); + tok = tok1; + } + else + tok1->deleteThis(); + tok->deleteThis(); + if (tok->next()) + tok->deleteThis(); + if (!tok->next()) + return; } tok = tok1->next(); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index b75c70394..9ca745eb8 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -4357,6 +4357,24 @@ private: const char expected[] = "struct ABC : public XYZ { struct Anonymous0 { } ; Anonymous0 def ; } ;"; ASSERT_EQUALS(expected, tok(code, false)); } + + { + const char code[] = "struct { int x; }; int y;"; + const char expected[] = "int x ; int y ;"; + ASSERT_EQUALS(expected, tok(code, false)); + } + + { + const char code[] = "struct { int x; };"; + const char expected[] = "int x ; ;"; + ASSERT_EQUALS(expected, tok(code, false)); + } + + { + const char code[] = "struct { };"; + const char expected[] = ";"; + ASSERT_EQUALS(expected, tok(code, false)); + } } };