Skip to content

Commit ae79a9a

Browse files
committed
Fix bit-field and __CPROVER_bool padding for Visual Studio
Bit fields that sum up to the underlying type need to update the offset to ensure padding suitable for the alignment is inserted.
1 parent 3120f0f commit ae79a9a

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

regression/ansi-c/bitfields1/main.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <limits.h>
2+
3+
#define STATIC_ASSERT(condition) int some_array##__LINE__[(condition) ? 1 : -1]
4+
5+
#if CHAR_BIT == 8
6+
struct bits
7+
{
8+
char a : 4;
9+
char b : 4;
10+
char c : 4;
11+
char d : 4;
12+
int i;
13+
};
14+
15+
STATIC_ASSERT(sizeof(struct bits) == 2 * sizeof(int));
16+
17+
#pragma pack(1)
18+
struct packed_bits
19+
{
20+
char a : 4;
21+
char b : 4;
22+
char c : 4;
23+
char d : 4;
24+
int i;
25+
};
26+
#pragma pack()
27+
28+
STATIC_ASSERT(sizeof(struct packed_bits) == sizeof(int) + 2);
29+
#endif
30+
31+
int main()
32+
{
33+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
8+
^CONVERSION ERROR$

regression/ansi-c/cprover_bool1/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
KNOWNBUG
1+
CORE
22
main.c
33

44
^EXIT=0$

src/ansi-c/padding.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ static void add_padding_msvc(struct_typet &type, const namespacet &ns)
190190
const auto width = to_c_bit_field_type(it->type()).get_width();
191191
bit_field_bits += width;
192192
}
193+
else if(
194+
it->type().id() == ID_bool && underlying_bits == config.ansi_c.char_width)
195+
{
196+
++bit_field_bits;
197+
}
193198
else
194199
{
195200
// pad up any remaining bit field
@@ -201,6 +206,11 @@ static void add_padding_msvc(struct_typet &type, const namespacet &ns)
201206
offset += (bit_field_bits + pad_bits) / config.ansi_c.char_width;
202207
underlying_bits = bit_field_bits = 0;
203208
}
209+
else
210+
{
211+
offset += bit_field_bits / config.ansi_c.char_width;
212+
underlying_bits = bit_field_bits = 0;
213+
}
204214

205215
// pad up to underlying type unless the struct is packed
206216
if(!is_packed)
@@ -231,6 +241,7 @@ static void add_padding_msvc(struct_typet &type, const namespacet &ns)
231241
}
232242
else if(it->type().id() == ID_bool)
233243
{
244+
underlying_bits = config.ansi_c.char_width;
234245
++bit_field_bits;
235246
}
236247
else
@@ -252,6 +263,8 @@ static void add_padding_msvc(struct_typet &type, const namespacet &ns)
252263
pad_bit_field(components, components.end(), pad);
253264
offset += (bit_field_bits + pad) / config.ansi_c.char_width;
254265
}
266+
else
267+
offset += bit_field_bits / config.ansi_c.char_width;
255268

256269
// alignment of the struct
257270
// Note that this is done even if the struct is packed.

0 commit comments

Comments
 (0)