Skip to content

Commit 075f70f

Browse files
committed
fix #13954
1 parent f1138a8 commit 075f70f

4 files changed

Lines changed: 25 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())
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: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9970,12 +9970,8 @@ void Tokenizer::simplifyAt()
99709970
// Simplify bitfields
99719971
void Tokenizer::simplifyBitfields()
99729972
{
9973-
bool goback = false;
9973+
std::size_t anonymousBitfieldCounter = 0;
99749974
for (Token *tok = list.front(); tok; tok = tok->next()) {
9975-
if (goback) {
9976-
goback = false;
9977-
tok = tok->previous();
9978-
}
99799975
Token *last = nullptr;
99809976

99819977
if (Token::simpleMatch(tok, "for ("))
@@ -10025,8 +10021,16 @@ void Tokenizer::simplifyBitfields()
1002510021
}
1002610022
} else if (Token::Match(typeTok, "%type% : %num%|%bool% ;") &&
1002710023
typeTok->str() != "default") {
10028-
tok->deleteNext(4 + tokDistance(tok, typeTok) - 1);
10029-
goback = true;
10024+
const std::size_t id = anonymousBitfieldCounter++;
10025+
const std::string name = "__cppcheck_anon_bit_field_" + std::to_string(id) + "__";
10026+
Token *newTok = typeTok->insertToken(name);
10027+
newTok->isAttributeUnused(true);
10028+
newTok->isAnonymousBitfield(true);
10029+
if (newTok->tokAt(2)->isBoolean())
10030+
newTok->setBits(static_cast<unsigned char>(newTok->tokAt(2)->str() == "true"));
10031+
else
10032+
newTok->setBits(static_cast<unsigned char>(MathLib::toBigNumber(newTok->tokAt(2))));
10033+
newTok->deleteNext(2);
1003010034
}
1003110035

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

0 commit comments

Comments
 (0)