Fixed #7747 (Syntax error when setting the bitcount of an enum defined inside a struct)

This commit is contained in:
Daniel Marjamäki 2016-10-04 15:57:43 +02:00
parent 02402eeea4
commit bcbc8ef017
2 changed files with 35 additions and 1 deletions

View File

@ -8909,6 +8909,18 @@ void Tokenizer::simplifyBitfields()
if (!Token::Match(tok, ";|{|}|public:|protected:|private:")) if (!Token::Match(tok, ";|{|}|public:|protected:|private:"))
continue; continue;
bool isEnum = false;
if (tok->str() == "}") {
const Token *type = tok->link()->previous();
while (type && type->isName()) {
if (type->str() == "enum") {
isEnum = true;
break;
}
type = type->previous();
}
}
if (Token::Match(tok->next(), "const| %type% %name% :") && if (Token::Match(tok->next(), "const| %type% %name% :") &&
!Token::Match(tok->next(), "case|public|protected|private|class|struct") && !Token::Match(tok->next(), "case|public|protected|private|class|struct") &&
!Token::simpleMatch(tok->tokAt(2), "default :")) { !Token::simpleMatch(tok->tokAt(2), "default :")) {
@ -8924,10 +8936,16 @@ void Tokenizer::simplifyBitfields()
last = tok1->next(); last = tok1->next();
} }
} else if (isEnum && Token::Match(tok, "} %name%| : %num% ;")) {
if (tok->next()->str() == ":") {
tok->deleteNext(2);
tok->insertToken("Anonymous");
} else {
tok->next()->deleteNext(2);
}
} else if (Token::Match(tok->next(), "const| %type% : %num%|%bool% ;") && } else if (Token::Match(tok->next(), "const| %type% : %num%|%bool% ;") &&
tok->next()->str() != "default") { tok->next()->str() != "default") {
const int offset = (tok->next()->str() == "const") ? 1 : 0; const int offset = (tok->next()->str() == "const") ? 1 : 0;
if (!Token::Match(tok->tokAt(3 + offset), "[{};()]")) { if (!Token::Match(tok->tokAt(3 + offset), "[{};()]")) {
tok->deleteNext(4 + offset); tok->deleteNext(4 + offset);
goback = true; goback = true;

View File

@ -345,6 +345,7 @@ private:
TEST_CASE(bitfields12); // ticket #3485 (segmentation fault) TEST_CASE(bitfields12); // ticket #3485 (segmentation fault)
TEST_CASE(bitfields13); // ticket #3502 (segmentation fault) TEST_CASE(bitfields13); // ticket #3502 (segmentation fault)
TEST_CASE(bitfields14); // ticket #4561 (segfault for 'class a { signals: };') TEST_CASE(bitfields14); // ticket #4561 (segfault for 'class a { signals: };')
TEST_CASE(bitfields15); // ticket #7747 (enum Foo {A,B}:4;)
TEST_CASE(simplifyNamespaceStd); TEST_CASE(simplifyNamespaceStd);
@ -5390,6 +5391,21 @@ private:
ASSERT_EQUALS("class x { signals : } ;", tokenizeAndStringify("class x { signals: };\n",false)); ASSERT_EQUALS("class x { signals : } ;", tokenizeAndStringify("class x { signals: };\n",false));
} }
void bitfields15() { // #7747 - enum Foo {A,B}:4;
ASSERT_EQUALS("struct AB {\n"
"enum Foo { A , B } ; enum Foo Anonymous ;\n"
"} ;",
tokenizeAndStringify("struct AB {\n"
" enum Foo {A,B} : 4;\n"
"};"));
ASSERT_EQUALS("struct AB {\n"
"enum Foo { A , B } ; enum Foo foo ;\n"
"} ;",
tokenizeAndStringify("struct AB {\n"
" enum Foo {A,B} foo : 4;\n"
"};"));
}
void simplifyNamespaceStd() { void simplifyNamespaceStd() {
static const char code1[] = "map<foo, bar> m;"; // namespace std is not used static const char code1[] = "map<foo, bar> m;"; // namespace std is not used