Skip to content

Commit a4f9ebf

Browse files
authored
unnamed bit fields fix (#435)
* unnamed bit fields tests generation fix * unnamed bit fields unit tests added * docs fix
1 parent ff1aa07 commit a4f9ebf

File tree

11 files changed

+40
-22
lines changed

11 files changed

+40
-22
lines changed

integration-tests/c-example/lib/structures/bitfields.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ typedef struct {
7575
// 7 bits: value of b1
7676
// 25 bits: unused
7777
// 6 bits: value of b2
78+
// 3 bits: unused
7879
// 15 bits: value of b3
79-
// 11 bits: unused
80+
// 8 bits: unused
8081
unsigned b1 : 7;
8182
unsigned : 0; // start a new allocation unit
8283
unsigned b2 : 6;
84+
unsigned : 3;
8385
unsigned b3 : 15;
8486
} StrWithUnnamedZeroBitfield;
8587

server/src/Tests.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,6 @@ std::shared_ptr<StructValueView> KTestObjectParser::structView(const std::vector
266266
std::vector<std::shared_ptr<AbstractValueView>> subViews;
267267

268268
for (const auto &field: curStruct.fields) {
269-
if (field.isUnnamedBitfield()) {
270-
continue;
271-
}
272269
size_t fieldLen = typesHandler.typeSize(field.type);
273270
size_t fieldOffset = offsetInBits + field.offset;
274271

server/src/printers/KleeConstraintsPrinter.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ void KleeConstraintsPrinter::genConstraintsForStruct(const ConstraintsState &sta
119119
StructInfo curStruct = typesHandler->getStructInfo(state.curType);
120120
bool isStruct = curStruct.subType == SubType::Struct;
121121
for (const auto &field : curStruct.fields) {
122-
if (field.isUnnamedBitfield()) {
123-
noConstraints("unnamed bit fields");
124-
continue;
125-
}
126122
auto access = PrinterUtils::getFieldAccess(state.curElement, field);
127123
ConstraintsState newState = { state.paramName,
128124
access,

server/src/printers/TestsPrinter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,6 @@ std::string printer::MultiLinePrinter::print(TestsPrinter *printer,
720720
const size_t longestFieldIndexForUnionInit = structInfo.longestFieldIndexForUnionInit;
721721
const bool isStruct = structInfo.subType == types::SubType::Struct;
722722

723-
bool firstField = true;
724723
size_t i = 0;
725724
for (const auto &sview : subViews) {
726725
if (i != 0) {

server/src/types/Types.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,6 @@ bool types::TypesHandler::isStructLike(uint64_t id) const {
488488
return typeIsInMap(id, typeMaps.structs);
489489
}
490490

491-
bool types::Field::isUnnamedBitfield() const {
492-
return name.empty() && TypesHandler::isPrimitiveType(type);
493-
}
494-
495491
/*
496492
* Enum types
497493
*/

server/src/types/Types.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ namespace types {
270270
AS_none
271271
};
272272
AccessSpecifier accessSpecifier = AS_pubic;
273-
[[nodiscard]] bool isUnnamedBitfield() const;
274273
};
275274

276275
struct TypeInfo {
@@ -350,8 +349,7 @@ namespace types {
350349
: typeMaps(types), sizeContext(sizeContext){};
351350

352351
/**
353-
* This functions calculates size of a given type. For structs in it calculates sum of sizes of its fields,
354-
* ignoring alignment.
352+
* Calculates size of a given type.
355353
* @return size of given type in @b bits.
356354
*/
357355
size_t typeSize(const types::Type &type) const;

server/src/types/TypesResolver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ void TypesResolver::resolveStructEx(const clang::RecordDecl *D, const std::strin
125125
size_t i = 0;
126126
size_t maxFieldSize = 0;
127127
for (const clang::FieldDecl *F : D->fields()) {
128+
if (F->isUnnamedBitfield()) {
129+
continue;
130+
}
128131
structInfo.hasAnonymousStructOrUnion |= F->isAnonymousStructOrUnion();
129132
types::Field field;
130133
field.name = F->getNameAsString();

server/src/visitors/AbstractValueViewVisitor.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ namespace visitor {
9696
inUnion = structInfo.subType == types::SubType::Union;
9797
for (int i = 0; i < structInfo.fields.size(); ++i) {
9898
auto const &field = structInfo.fields[i];
99-
if (field.isUnnamedBitfield()) {
100-
continue;
101-
}
10299
auto newName = PrinterUtils::getFieldAccess(name, field);
103100
auto const *newView = (subViews && i < subViews->size()) ? (*subViews)[i].get() : nullptr;
104101
auto newAccess = PrinterUtils::getFieldAccess(access, field);

server/src/visitors/FunctionPointerForStubsVisitor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ namespace visitor {
4444
}
4545
for (auto &field : structInfo.fields) {
4646
if (!types::TypesHandler::isPointerToFunction(field.type) &&
47-
!types::TypesHandler::isArrayOfPointersToFunction(field.type) &&
48-
!field.isUnnamedBitfield()) {
47+
!types::TypesHandler::isArrayOfPointersToFunction(field.type)) {
4948
visitAny(field.type, name, nullptr, access, depth + 1);
5049
}
5150
}

server/test/framework/Syntax_Tests.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,4 +3172,33 @@ namespace {
31723172
})
31733173
);
31743174
}
3175+
3176+
TEST_F(Syntax_Test, bitfields_check_unnamed) {
3177+
auto [testGen, status] = createTestForFunction(bitfields_c, 99);
3178+
3179+
ASSERT_TRUE(status.ok()) << status.error_message();
3180+
3181+
checkTestCasePredicates(
3182+
testGen.tests.at(bitfields_c).methods.begin().value().testCases,
3183+
std::vector<TestCasePredicate>(
3184+
{
3185+
[](const tests::Tests::MethodTestCase &testCase) {
3186+
auto &subViews = testCase.paramValues.front().view->getSubViews();
3187+
return subViews.size() == 3 &&
3188+
checkBitfieldFit<unsigned>(subViews[0], 7) &&
3189+
checkBitfieldFit<unsigned>(subViews[1], 6) &&
3190+
checkBitfieldFit<unsigned>(subViews[2], 15) &&
3191+
testCase.returnValue.view->getEntryValue(nullptr) == "0";
3192+
},
3193+
[](const tests::Tests::MethodTestCase &testCase) {
3194+
auto &subViews = testCase.paramValues.front().view->getSubViews();
3195+
return subViews.size() == 3 &&
3196+
checkBitfieldFit<unsigned>(subViews[0], 7) &&
3197+
checkBitfieldFit<unsigned>(subViews[1], 6) &&
3198+
checkBitfieldFit<unsigned>(subViews[2], 15) &&
3199+
testCase.returnValue.view->getEntryValue(nullptr) == "13";
3200+
}
3201+
})
3202+
);
3203+
}
31753204
}

server/test/suites/syntax/bitfields.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ typedef struct {
7575
// 7 bits: value of b1
7676
// 25 bits: unused
7777
// 6 bits: value of b2
78+
// 3 bits: unused
7879
// 15 bits: value of b3
79-
// 11 bits: unused
80+
// 8 bits: unused
8081
unsigned b1 : 7;
8182
unsigned : 0; // start a new allocation unit
8283
unsigned b2 : 6;
84+
unsigned : 3;
8385
unsigned b3 : 15;
8486
} StrWithUnnamedZeroBitfield;
8587

0 commit comments

Comments
 (0)