Skip to content

Commit 04995af

Browse files
committed
fix #13954
1 parent fa1d218 commit 04995af

4 files changed

Lines changed: 24 additions & 7 deletions

File tree

lib/checkclass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ void CheckClass::constructors()
231231
if (usage.assign || usage.init || var.isStatic())
232232
continue;
233233

234+
if (var.nameToken() && var.nameToken()->isAnonymousBitfield())
235+
continue;
236+
234237
if (var.valueType() && var.valueType()->pointer == 0 && var.type() && var.type()->needInitialization == Type::NeedInitialization::False && var.type()->derivedFrom.empty())
235238
continue;
236239

lib/checkunusedvar.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,9 @@ void CheckUnusedVar::checkStructMemberUsage()
15751575
if (isInherited && !var.isPrivate())
15761576
continue;
15771577

1578+
if (var.nameToken() && (var.nameToken()->isAttributeUnused() || var.nameToken()->isAnonymousBitfield()))
1579+
continue;
1580+
15781581
if (mTokenizer->isVarUsedInTemplate(var.declarationId()))
15791582
continue;
15801583

lib/token.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,13 @@ class CPPCHECKLIB Token {
742742
setFlag(fIsInitBracket, b);
743743
}
744744

745+
bool isAnonymousBitfield() const {
746+
return getFlag(fIsAnonymousBitfield);
747+
}
748+
void isAnonymousBitfield(bool b) {
749+
setFlag(fIsAnonymousBitfield, b);
750+
}
751+
745752
// cppcheck-suppress unusedFunction
746753
bool isBitfield() const {
747754
return mImpl->mBits > 0;
@@ -1426,6 +1433,7 @@ class CPPCHECKLIB Token {
14261433
fIsFinalType = (1ULL << 42), // Is this a type with final specifier
14271434
fIsInitComma = (1ULL << 43), // Is this comma located inside some {..}. i.e: {1,2,3,4}
14281435
fIsInitBracket = (1ULL << 44), // Is this bracket used as a part of variable initialization i.e: int a{5}, b(2);
1436+
fIsAnonymousBitfield = (1ULL << 45), // Is this a token added for an unnamed bit-field
14291437
};
14301438

14311439
enum : std::uint8_t {

lib/tokenize.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9985,12 +9985,8 @@ void Tokenizer::simplifyAt()
99859985
// Simplify bitfields
99869986
void Tokenizer::simplifyBitfields()
99879987
{
9988-
bool goback = false;
9988+
std::size_t anonymousBitfieldCounter = 0;
99899989
for (Token *tok = list.front(); tok; tok = tok->next()) {
9990-
if (goback) {
9991-
goback = false;
9992-
tok = tok->previous();
9993-
}
99949990
Token *last = nullptr;
99959991

99969992
if (Token::simpleMatch(tok, "for ("))
@@ -10040,8 +10036,15 @@ void Tokenizer::simplifyBitfields()
1004010036
}
1004110037
} else if (Token::Match(typeTok, "%type% : %num%|%bool% ;") &&
1004210038
typeTok->str() != "default") {
10043-
tok->deleteNext(4 + tokDistance(tok, typeTok) - 1);
10044-
goback = true;
10039+
const std::size_t id = anonymousBitfieldCounter++;
10040+
const std::string name = "__cppcheck_anon_bit_field_" + std::to_string(id) + "__";
10041+
Token *newTok = typeTok->insertToken(name);
10042+
newTok->isAnonymousBitfield(true);
10043+
if (newTok->tokAt(2)->isBoolean())
10044+
newTok->setBits(static_cast<unsigned char>(newTok->strAt(2) == "true"));
10045+
else
10046+
newTok->setBits(static_cast<unsigned char>(MathLib::toBigNumber(newTok->tokAt(2))));
10047+
newTok->deleteNext(2);
1004510048
}
1004610049

1004710050
if (last && last->str() == ",") {

0 commit comments

Comments
 (0)