Skip to content

Commit 391adab

Browse files
committed
Evaluating sizeof over __CPROVER_bool requires special cases
__CPROVER_bool is just a single bit, and not part of any language standard describing the semantics of sizeof. We can declare arrays of __CPROVER_bool, which will thus have elements that are not aligned on byte boundaries. Using sizeof with such an array thus requires specific handling.
1 parent 029acc1 commit 391adab

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ void c_typecheck_baset::typecheck_expr_sizeof(exprt &expr)
979979

980980
exprt new_expr;
981981

982-
if(type.id()==ID_c_bit_field)
982+
if(type.id() == ID_c_bit_field || type.id() == ID_bool)
983983
{
984984
err_location(expr);
985985
error() << "sizeof cannot be applied to bit fields" << eom;
@@ -1739,7 +1739,7 @@ void c_typecheck_baset::typecheck_expr_address_of(exprt &expr)
17391739

17401740
exprt &op=expr.op0();
17411741

1742-
if(op.type().id()==ID_c_bit_field)
1742+
if(op.type().id() == ID_c_bit_field || op.type().id() == ID_bool)
17431743
{
17441744
err_location(expr);
17451745
error() << "cannot take address of a bit field" << eom;

src/util/pointer_offset_size.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,13 @@ exprt member_offset_expr(
305305
if(bytes > 0)
306306
result = plus_exprt(result, from_integer(bytes, result.type()));
307307
}
308+
else if(c.type().id() == ID_bool)
309+
{
310+
++bit_field_bits;
311+
const std::size_t bytes = bit_field_bits / 8;
312+
bit_field_bits %= 8;
313+
result = plus_exprt(result, from_integer(bytes, result.type()));
314+
}
308315
else
309316
{
310317
DATA_INVARIANT(
@@ -427,6 +434,13 @@ exprt size_of_expr(
427434
if(bytes > 0)
428435
result = plus_exprt(result, from_integer(bytes, result.type()));
429436
}
437+
else if(c.type().id() == ID_bool)
438+
{
439+
++bit_field_bits;
440+
const std::size_t bytes = bit_field_bits / 8;
441+
bit_field_bits %= 8;
442+
result = plus_exprt(result, from_integer(bytes, result.type()));
443+
}
430444
else
431445
{
432446
DATA_INVARIANT(

0 commit comments

Comments
 (0)