diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d67356ad086..2279f16a80e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -10414,6 +10414,7 @@ void Tokenizer::simplifyBitfields() while (Token::Match(typeTok, "%name% :: %name%")) typeTok = typeTok->tokAt(2); if (Token::Match(typeTok, "%type% %name% :") && + typeTok->str() != "enum" && !Token::Match(tok->next(), "case|public|protected|private|class|struct") && !Token::simpleMatch(tok->tokAt(2), "default :")) { Token *tok1 = typeTok->next(); @@ -10422,7 +10423,7 @@ void Tokenizer::simplifyBitfields() tooLargeError(tok1->tokAt(2)); if (tok1 && tok1->tokAt(2) && (Token::Match(tok1->tokAt(2), "%bool%|%num%") || - !Token::Match(tok1->tokAt(2), "public|protected|private| %type% ::|<|,|{|;"))) { + !Token::Match(tok1->tokAt(2), "public|protected|private| %type% ::|<|,|{"))) { while (tok1->next() && !Token::Match(tok1->next(), "[;,)]{}=]")) { if (Token::Match(tok1->next(), "[([]")) Token::eraseTokens(tok1, tok1->linkAt(1)); diff --git a/test/testclass.cpp b/test/testclass.cpp index 451502c2164..37dd28647a0 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -199,6 +199,7 @@ class TestClass : public TestFixture { TEST_CASE(const100); TEST_CASE(const101); TEST_CASE(const102); + TEST_CASE(const103); TEST_CASE(const_handleDefaultParameters); TEST_CASE(const_passThisToMemberOfOtherClass); @@ -7045,6 +7046,15 @@ class TestClass : public TestFixture { errout_str()); } + void const103() { + checkConst("constexpr int BITS{4};\n" + "struct Struct {\n" + " unsigned int data() const { return m_data; }\n" + " unsigned int m_data : BITS;\n" + "};\n"); + ASSERT_EQUALS("", errout_str()); + } + void const_handleDefaultParameters() { checkConst("struct Foo {\n" " void foo1(int i, int j = 0) {\n" diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index a25ea9c4bd6..ddccaa62584 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -342,6 +342,7 @@ class TestTokenizer : public TestFixture { TEST_CASE(bitfields19); // ticket #13733 TEST_CASE(bitfields20); TEST_CASE(bitfields21); + TEST_CASE(bitfields22); TEST_CASE(simplifyNamespaceStd); @@ -5371,6 +5372,14 @@ class TestTokenizer : public TestFixture { ASSERT_EQUALS(1, b->bits()); } + void bitfields22() { + const char code[] = "constexpr int BITS{4};\n" + "struct Struct { unsigned int m_data : BITS; };\n"; + const char expected[] = "constexpr int BITS { 4 } ;\n" + "struct Struct { unsigned int m_data ; } ;"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); + } + void simplifyNamespaceStd() { const char *expected;