From bcbc8ef017ea98e8c8d7d5096bc793b3ed425299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 4 Oct 2016 15:57:43 +0200 Subject: [PATCH] Fixed #7747 (Syntax error when setting the bitcount of an enum defined inside a struct) --- lib/tokenize.cpp | 20 +++++++++++++++++++- test/testtokenize.cpp | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index cee13f58f..9f7732231 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8909,6 +8909,18 @@ void Tokenizer::simplifyBitfields() if (!Token::Match(tok, ";|{|}|public:|protected:|private:")) 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% :") && !Token::Match(tok->next(), "case|public|protected|private|class|struct") && !Token::simpleMatch(tok->tokAt(2), "default :")) { @@ -8924,10 +8936,16 @@ void Tokenizer::simplifyBitfields() 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% ;") && tok->next()->str() != "default") { const int offset = (tok->next()->str() == "const") ? 1 : 0; - if (!Token::Match(tok->tokAt(3 + offset), "[{};()]")) { tok->deleteNext(4 + offset); goback = true; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 45c66b5ab..ffdcebb2a 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -345,6 +345,7 @@ private: TEST_CASE(bitfields12); // ticket #3485 (segmentation fault) TEST_CASE(bitfields13); // ticket #3502 (segmentation fault) TEST_CASE(bitfields14); // ticket #4561 (segfault for 'class a { signals: };') + TEST_CASE(bitfields15); // ticket #7747 (enum Foo {A,B}:4;) TEST_CASE(simplifyNamespaceStd); @@ -5390,6 +5391,21 @@ private: 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() { static const char code1[] = "map m;"; // namespace std is not used