Skip to content

Commit 43425ee

Browse files
authored
Fix #15069: False positive: functionStatic for access to bitfields with non-literal no. of bits (#8896)
1 parent a54e660 commit 43425ee

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎lib/tokenize.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10414,6 +10414,7 @@ void Tokenizer::simplifyBitfields()
1041410414
while (Token::Match(typeTok, "%name% :: %name%"))
1041510415
typeTok = typeTok->tokAt(2);
1041610416
if (Token::Match(typeTok, "%type% %name% :") &&
10417+
typeTok->str() != "enum" &&
1041710418
!Token::Match(tok->next(), "case|public|protected|private|class|struct") &&
1041810419
!Token::simpleMatch(tok->tokAt(2), "default :")) {
1041910420
Token *tok1 = typeTok->next();
@@ -10422,7 +10423,7 @@ void Tokenizer::simplifyBitfields()
1042210423
tooLargeError(tok1->tokAt(2));
1042310424
if (tok1 && tok1->tokAt(2) &&
1042410425
(Token::Match(tok1->tokAt(2), "%bool%|%num%") ||
10425-
!Token::Match(tok1->tokAt(2), "public|protected|private| %type% ::|<|,|{|;"))) {
10426+
!Token::Match(tok1->tokAt(2), "public|protected|private| %type% ::|<|,|{"))) {
1042610427
while (tok1->next() && !Token::Match(tok1->next(), "[;,)]{}=]")) {
1042710428
if (Token::Match(tok1->next(), "[([]"))
1042810429
Token::eraseTokens(tok1, tok1->linkAt(1));

‎test/testclass.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ class TestClass : public TestFixture {
199199
TEST_CASE(const100);
200200
TEST_CASE(const101);
201201
TEST_CASE(const102);
202+
TEST_CASE(const103);
202203

203204
TEST_CASE(const_handleDefaultParameters);
204205
TEST_CASE(const_passThisToMemberOfOtherClass);
@@ -7045,6 +7046,15 @@ class TestClass : public TestFixture {
70457046
errout_str());
70467047
}
70477048

7049+
void const103() {
7050+
checkConst("constexpr int BITS{4};\n"
7051+
"struct Struct {\n"
7052+
" unsigned int data() const { return m_data; }\n"
7053+
" unsigned int m_data : BITS;\n"
7054+
"};\n");
7055+
ASSERT_EQUALS("", errout_str());
7056+
}
7057+
70487058
void const_handleDefaultParameters() {
70497059
checkConst("struct Foo {\n"
70507060
" void foo1(int i, int j = 0) {\n"

‎test/testtokenize.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ class TestTokenizer : public TestFixture {
342342
TEST_CASE(bitfields19); // ticket #13733
343343
TEST_CASE(bitfields20);
344344
TEST_CASE(bitfields21);
345+
TEST_CASE(bitfields22);
345346

346347
TEST_CASE(simplifyNamespaceStd);
347348

@@ -5371,6 +5372,14 @@ class TestTokenizer : public TestFixture {
53715372
ASSERT_EQUALS(1, b->bits());
53725373
}
53735374

5375+
void bitfields22() {
5376+
const char code[] = "constexpr int BITS{4};\n"
5377+
"struct Struct { unsigned int m_data : BITS; };\n";
5378+
const char expected[] = "constexpr int BITS { 4 } ;\n"
5379+
"struct Struct { unsigned int m_data ; } ;";
5380+
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
5381+
}
5382+
53745383
void simplifyNamespaceStd() {
53755384
const char *expected;
53765385

0 commit comments

Comments
 (0)