Skip to content

Commit bcbc8ef

Browse files
committed
Fixed #7747 (Syntax error when setting the bitcount of an enum defined inside a struct)
1 parent 02402ee commit bcbc8ef

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8909,6 +8909,18 @@ void Tokenizer::simplifyBitfields()
89098909
if (!Token::Match(tok, ";|{|}|public:|protected:|private:"))
89108910
continue;
89118911

8912+
bool isEnum = false;
8913+
if (tok->str() == "}") {
8914+
const Token *type = tok->link()->previous();
8915+
while (type && type->isName()) {
8916+
if (type->str() == "enum") {
8917+
isEnum = true;
8918+
break;
8919+
}
8920+
type = type->previous();
8921+
}
8922+
}
8923+
89128924
if (Token::Match(tok->next(), "const| %type% %name% :") &&
89138925
!Token::Match(tok->next(), "case|public|protected|private|class|struct") &&
89148926
!Token::simpleMatch(tok->tokAt(2), "default :")) {
@@ -8924,10 +8936,16 @@ void Tokenizer::simplifyBitfields()
89248936

89258937
last = tok1->next();
89268938
}
8939+
} else if (isEnum && Token::Match(tok, "} %name%| : %num% ;")) {
8940+
if (tok->next()->str() == ":") {
8941+
tok->deleteNext(2);
8942+
tok->insertToken("Anonymous");
8943+
} else {
8944+
tok->next()->deleteNext(2);
8945+
}
89278946
} else if (Token::Match(tok->next(), "const| %type% : %num%|%bool% ;") &&
89288947
tok->next()->str() != "default") {
89298948
const int offset = (tok->next()->str() == "const") ? 1 : 0;
8930-
89318949
if (!Token::Match(tok->tokAt(3 + offset), "[{};()]")) {
89328950
tok->deleteNext(4 + offset);
89338951
goback = true;

test/testtokenize.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ class TestTokenizer : public TestFixture {
345345
TEST_CASE(bitfields12); // ticket #3485 (segmentation fault)
346346
TEST_CASE(bitfields13); // ticket #3502 (segmentation fault)
347347
TEST_CASE(bitfields14); // ticket #4561 (segfault for 'class a { signals: };')
348+
TEST_CASE(bitfields15); // ticket #7747 (enum Foo {A,B}:4;)
348349

349350
TEST_CASE(simplifyNamespaceStd);
350351

@@ -5390,6 +5391,21 @@ class TestTokenizer : public TestFixture {
53905391
ASSERT_EQUALS("class x { signals : } ;", tokenizeAndStringify("class x { signals: };\n",false));
53915392
}
53925393

5394+
void bitfields15() { // #7747 - enum Foo {A,B}:4;
5395+
ASSERT_EQUALS("struct AB {\n"
5396+
"enum Foo { A , B } ; enum Foo Anonymous ;\n"
5397+
"} ;",
5398+
tokenizeAndStringify("struct AB {\n"
5399+
" enum Foo {A,B} : 4;\n"
5400+
"};"));
5401+
ASSERT_EQUALS("struct AB {\n"
5402+
"enum Foo { A , B } ; enum Foo foo ;\n"
5403+
"} ;",
5404+
tokenizeAndStringify("struct AB {\n"
5405+
" enum Foo {A,B} foo : 4;\n"
5406+
"};"));
5407+
}
5408+
53935409

53945410
void simplifyNamespaceStd() {
53955411
static const char code1[] = "map<foo, bar> m;"; // namespace std is not used

0 commit comments

Comments
 (0)