Skip to content

Commit f614d32

Browse files
committed
Fixed #9519 (Syntax error on valid C++ 'enum {} (a)')
1 parent bcfc592 commit f614d32

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

lib/tokenize.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9726,8 +9726,13 @@ void Tokenizer::simplifyStructDecl()
97269726
// check for anonymous enum
97279727
else if ((Token::simpleMatch(tok, "enum {") &&
97289728
!Token::Match(tok->tokAt(-3), "using %name% =") &&
9729-
Token::Match(tok->next()->link(), "} %type%| ,|;|[|(|{")) ||
9730-
(Token::Match(tok, "enum : %type% {") && Token::Match(tok->linkAt(3), "} %type%| ,|;|[|(|{"))) {
9729+
Token::Match(tok->next()->link(), "} (| %type%| )| ,|;|[|(|{")) ||
9730+
(Token::Match(tok, "enum : %type% {") && Token::Match(tok->linkAt(3), "} (| %type%| )| ,|;|[|(|{"))) {
9731+
Token *start = tok->strAt(1) == ":" ? tok->linkAt(3) : tok->linkAt(1);
9732+
if (start && Token::Match(start->next(), "( %type% )")) {
9733+
start->next()->link()->deleteThis();
9734+
start->next()->deleteThis();
9735+
}
97319736
tok->insertToken("Anonymous" + MathLib::toString(count++));
97329737
}
97339738
}
@@ -9761,7 +9766,7 @@ void Tokenizer::simplifyStructDecl()
97619766
Token *restart = next;
97629767

97639768
// check for named type
9764-
if (Token::Match(tok->next(), "const| *|&| const| %type% ,|;|[|=|(|{")) {
9769+
if (Token::Match(tok->next(), "const| *|&| const| (| %type% )| ,|;|[|=|(|{")) {
97659770
tok->insertToken(";");
97669771
tok = tok->next();
97679772
while (!Token::Match(start, "struct|class|union|enum")) {
@@ -9779,6 +9784,11 @@ void Tokenizer::simplifyStructDecl()
97799784

97809785
tok = tok->tokAt(2);
97819786

9787+
if (Token::Match(tok, "( %type% )")) {
9788+
tok->link()->deleteThis();
9789+
tok->deleteThis();
9790+
}
9791+
97829792
// check for initialization
97839793
if (tok && (tok->next()->str() == "(" || tok->next()->str() == "{")) {
97849794
tok->insertToken("=");

test/testtokenize.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3581,6 +3581,11 @@ class TestTokenizer : public TestFixture {
35813581
void simplifyStructDecl() {
35823582
const char code[] = "const struct A { int a; int b; } a;";
35833583
ASSERT_EQUALS("struct A { int a ; int b ; } ; const struct A a ;", tokenizeAndStringify(code));
3584+
3585+
// #9519
3586+
const char code2[] = "enum A {} (a);";
3587+
const char expected2[] = "enum A { } ; enum A a ;";
3588+
ASSERT_EQUALS(expected2, tokenizeAndStringify(code2));
35843589
}
35853590

35863591
void vardecl1() {

0 commit comments

Comments
 (0)