Fixed #9519 (Syntax error on valid C++ 'enum {} (a)')

This commit is contained in:
Daniel Marjamäki 2019-12-15 08:40:04 +01:00
parent bcfc5924fa
commit f614d32d6a
2 changed files with 18 additions and 3 deletions

View File

@ -9726,8 +9726,13 @@ void Tokenizer::simplifyStructDecl()
// check for anonymous enum
else if ((Token::simpleMatch(tok, "enum {") &&
!Token::Match(tok->tokAt(-3), "using %name% =") &&
Token::Match(tok->next()->link(), "} %type%| ,|;|[|(|{")) ||
(Token::Match(tok, "enum : %type% {") && Token::Match(tok->linkAt(3), "} %type%| ,|;|[|(|{"))) {
Token::Match(tok->next()->link(), "} (| %type%| )| ,|;|[|(|{")) ||
(Token::Match(tok, "enum : %type% {") && Token::Match(tok->linkAt(3), "} (| %type%| )| ,|;|[|(|{"))) {
Token *start = tok->strAt(1) == ":" ? tok->linkAt(3) : tok->linkAt(1);
if (start && Token::Match(start->next(), "( %type% )")) {
start->next()->link()->deleteThis();
start->next()->deleteThis();
}
tok->insertToken("Anonymous" + MathLib::toString(count++));
}
}
@ -9761,7 +9766,7 @@ void Tokenizer::simplifyStructDecl()
Token *restart = next;
// check for named type
if (Token::Match(tok->next(), "const| *|&| const| %type% ,|;|[|=|(|{")) {
if (Token::Match(tok->next(), "const| *|&| const| (| %type% )| ,|;|[|=|(|{")) {
tok->insertToken(";");
tok = tok->next();
while (!Token::Match(start, "struct|class|union|enum")) {
@ -9779,6 +9784,11 @@ void Tokenizer::simplifyStructDecl()
tok = tok->tokAt(2);
if (Token::Match(tok, "( %type% )")) {
tok->link()->deleteThis();
tok->deleteThis();
}
// check for initialization
if (tok && (tok->next()->str() == "(" || tok->next()->str() == "{")) {
tok->insertToken("=");

View File

@ -3581,6 +3581,11 @@ private:
void simplifyStructDecl() {
const char code[] = "const struct A { int a; int b; } a;";
ASSERT_EQUALS("struct A { int a ; int b ; } ; const struct A a ;", tokenizeAndStringify(code));
// #9519
const char code2[] = "enum A {} (a);";
const char expected2[] = "enum A { } ; enum A a ;";
ASSERT_EQUALS(expected2, tokenizeAndStringify(code2));
}
void vardecl1() {