Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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));
Expand Down
10 changes: 10 additions & 0 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down
Loading