Skip to content

Commit 963d222

Browse files
committed
handle unnamed bitfields of size zero
1 parent f86bc46 commit 963d222

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

lib/token.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct TokenImpl {
8282
nonneg int mIndex{};
8383

8484
/** Bitfield bit count. */
85-
unsigned char mBits{};
85+
char mBits = -1;
8686

8787
// AST..
8888
Token* mAstOperand1{};

lib/tokenize.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10015,7 +10015,7 @@ void Tokenizer::simplifyBitfields()
1001510015
!Token::simpleMatch(tok->tokAt(2), "default :")) {
1001610016
Token *tok1 = typeTok->next();
1001710017
if (Token::Match(tok1, "%name% : %num% [;=]"))
10018-
tok1->setBits(static_cast<unsigned char>(MathLib::toBigNumber(tok1->tokAt(2))));
10018+
tok1->setBits(static_cast<char>(MathLib::toBigNumber(tok1->tokAt(2))));
1001910019
if (tok1 && tok1->tokAt(2) &&
1002010020
(Token::Match(tok1->tokAt(2), "%bool%|%num%") ||
1002110021
!Token::Match(tok1->tokAt(2), "public|protected|private| %type% ::|<|,|{|;"))) {
@@ -10041,9 +10041,9 @@ void Tokenizer::simplifyBitfields()
1004110041
Token *newTok = typeTok->insertToken(name);
1004210042
newTok->isAnonymousBitfield(true);
1004310043
if (newTok->tokAt(2)->isBoolean())
10044-
newTok->setBits(static_cast<unsigned char>(newTok->strAt(2) == "true"));
10044+
newTok->setBits(static_cast<char>(newTok->strAt(2) == "true"));
1004510045
else
10046-
newTok->setBits(static_cast<unsigned char>(MathLib::toBigNumber(newTok->tokAt(2))));
10046+
newTok->setBits(static_cast<char>(MathLib::toBigNumber(newTok->tokAt(2))));
1004710047
newTok->deleteNext(2);
1004810048
}
1004910049

lib/valueflow.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ static Result accumulateStructMembers(const Scope* scope, F f, ValueFlow::Accura
440440
for (const Variable& var : scope->varlist) {
441441
if (var.isStatic())
442442
continue;
443-
const size_t bits = var.nameToken() ? var.nameToken()->bits() : 0;
443+
const char bits = var.nameToken() ? var.nameToken()->bits() : -1;
444444
if (const ValueType* vt = var.valueType()) {
445445
if (vt->type == ValueType::Type::RECORD && vt->typeScope == scope)
446446
return {0, false};
@@ -455,7 +455,7 @@ static Result accumulateStructMembers(const Scope* scope, F f, ValueFlow::Accura
455455
else
456456
total = f(total, *vt, dim, bits);
457457
}
458-
if (accuracy == ValueFlow::Accuracy::ExactOrZero && total == 0 && bits == 0)
458+
if (accuracy == ValueFlow::Accuracy::ExactOrZero && total == 0 && bits == -1)
459459
return {0, false};
460460
}
461461
return {total, true};
@@ -537,10 +537,17 @@ size_t ValueFlow::getSizeOf(const ValueType &vt, const Settings &settings, Accur
537537
if (vt.type == ValueType::Type::RECORD && vt.typeScope) {
538538
size_t currentBitCount = 0;
539539
size_t currentBitfieldAlloc = 0;
540-
auto accHelper = [&](size_t total, const ValueType& vt2, size_t dim, size_t bits) -> size_t {
540+
auto accHelper = [&](size_t total, const ValueType& vt2, size_t dim, char bits) -> size_t {
541541
const size_t charBit = settings.platform.char_bit;
542542
size_t n = ValueFlow::getSizeOf(vt2, settings,accuracy, ++maxRecursion);
543543
size_t a = getAlignOf(vt2, settings, accuracy);
544+
if (bits == 0) {
545+
if (currentBitfieldAlloc == 0) {
546+
bits = n * charBit;
547+
} else {
548+
bits = currentBitfieldAlloc * charBit - currentBitCount;
549+
}
550+
}
544551
if (bits > 0) {
545552
size_t ret = total;
546553
if (currentBitfieldAlloc == 0) {

0 commit comments

Comments
 (0)