Skip to content

Commit 8f1868c

Browse files
committed
expr2c: output ID_allocate in a way that goto-cc can compile
We can compile __CPROVER_allocate, but wouldn't be able to handle ALLOCATE(type, size, bool).
1 parent d148ae6 commit 8f1868c

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

jbmc/unit/java_bytecode/java_object_factory/gen_nondet_string_init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ SCENARIO(
9292
CPROVER_PREFIX "assume(tmp_object_factory <= 20);",
9393
"char (*nondet_infinite_array_pointer)[INFINITY()];",
9494
"nondet_infinite_array_pointer = "
95-
"ALLOCATE(char [INFINITY()], INFINITY(), false);",
95+
"__CPROVER_allocate(INFINITY(), false);",
9696
"*nondet_infinite_array_pointer = NONDET(char [INFINITY()]);",
9797
"int return_array;",
9898
"return_array = cprover_associate_array_to_pointer_func"

src/ansi-c/expr2c.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,24 +1156,30 @@ std::string expr2ct::convert_allocate(const exprt &src, unsigned &precedence)
11561156
if(src.operands().size() != 2)
11571157
return convert_norep(src, precedence);
11581158

1159-
unsigned p0;
1160-
std::string op0 = convert_with_precedence(to_binary_expr(src).op0(), p0);
1159+
const binary_exprt &binary_expr = to_binary_expr(src);
11611160

11621161
unsigned p1;
1163-
std::string op1 = convert_with_precedence(to_binary_expr(src).op1(), p1);
1162+
std::string op1 = convert_with_precedence(binary_expr.op1(), p1);
11641163

1165-
std::string dest = "ALLOCATE";
1164+
std::string dest = CPROVER_PREFIX "allocate";
11661165
dest += '(';
11671166

1167+
const typet &type =
1168+
static_cast<const typet &>(binary_expr.op0().find(ID_C_c_sizeof_type));
11681169
if(
11691170
src.type().id() == ID_pointer &&
1170-
to_pointer_type(src.type()).base_type().id() != ID_empty)
1171+
to_pointer_type(src.type()).base_type() == type)
11711172
{
1172-
dest += convert(to_pointer_type(src.type()).base_type());
1173+
dest += "sizeof(" + convert(to_pointer_type(src.type()).base_type()) + ')';
11731174
dest+=", ";
11741175
}
1176+
else
1177+
{
1178+
unsigned p0;
1179+
dest += convert_with_precedence(binary_expr.op0(), p0);
1180+
}
11751181

1176-
dest += op0 + ", " + op1;
1182+
dest += ", " + op1;
11771183
dest += ')';
11781184

11791185
return dest;

0 commit comments

Comments
 (0)