Skip to content

Commit daf1be4

Browse files
author
Daniel Kroening
committed
document why sizeof(void) works
1 parent e8d26ae commit daf1be4

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -935,26 +935,30 @@ void c_typecheck_baset::typecheck_expr_sizeof(exprt &expr)
935935
throw 0;
936936
}
937937

938+
exprt new_expr;
939+
938940
if(type.id()==ID_c_bit_field)
939941
{
940942
err_location(expr);
941943
error() << "sizeof cannot be applied to bit fields" << eom;
942944
throw 0;
943945
}
944-
945-
if(type.id()==ID_empty &&
946-
expr.operands().size()==1 &&
947-
expr.op0().id()==ID_dereference &&
948-
expr.op0().op0().type()==pointer_type(void_type()))
949-
type=char_type();
950-
951-
exprt new_expr=size_of_expr(type, *this);
952-
953-
if(new_expr.is_nil())
946+
else if(type.id() == ID_empty)
954947
{
955-
err_location(expr);
956-
error() << "type has no size: " << to_string(type) << eom;
957-
throw 0;
948+
// This is a gcc extension.
949+
// https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Pointer-Arith.html
950+
new_expr = size_of_expr(char_type(), *this);
951+
}
952+
else
953+
{
954+
new_expr = size_of_expr(type, *this);
955+
956+
if(new_expr.is_nil())
957+
{
958+
err_location(expr);
959+
error() << "type has no size: " << to_string(type) << eom;
960+
throw 0;
961+
}
958962
}
959963

960964
new_expr.swap(expr);

src/solvers/flattening/bv_pointers.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,18 @@ bvt bv_pointerst::convert_pointer_type(const exprt &expr)
348348
CHECK_RETURN(bv.size()==bits);
349349

350350
typet pointer_sub_type=it->type().subtype();
351+
351352
if(pointer_sub_type.id()==ID_empty)
352-
pointer_sub_type=char_type();
353-
size=pointer_offset_size(pointer_sub_type, ns);
354-
CHECK_RETURN(size>0);
353+
{
354+
// This is a gcc extension.
355+
// https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Pointer-Arith.html
356+
size = 1;
357+
}
358+
else
359+
{
360+
size = pointer_offset_size(pointer_sub_type, ns);
361+
CHECK_RETURN(size > 0);
362+
}
355363
}
356364
}
357365

0 commit comments

Comments
 (0)