Skip to content

Commit 98dd5ec

Browse files
committed
Use allocate_objectst in the c nondet object factory
1 parent 5ce5cb4 commit 98dd5ec

File tree

1 file changed

+27
-91
lines changed

1 file changed

+27
-91
lines changed

src/ansi-c/c_nondet_symbol_factory.cpp

Lines changed: 27 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Author: Diffblue Ltd.
1313

1414
#include <ansi-c/c_object_factory_parameters.h>
1515

16+
#include <util/allocate_objects.h>
1617
#include <util/arith_tools.h>
1718
#include <util/c_types.h>
1819
#include <util/fresh_symbol.h>
@@ -26,39 +27,48 @@ Author: Diffblue Ltd.
2627

2728
class symbol_factoryt
2829
{
29-
std::vector<const symbolt *> &symbols_created;
3030
symbol_tablet &symbol_table;
3131
const source_locationt &loc;
3232
namespacet ns;
3333
const c_object_factory_parameterst &object_factory_params;
3434

35+
allocate_objectst allocate_objects;
36+
3537
typedef std::set<irep_idt> recursion_sett;
3638

3739
public:
3840
symbol_factoryt(
39-
std::vector<const symbolt *> &_symbols_created,
4041
symbol_tablet &_symbol_table,
4142
const source_locationt &loc,
4243
const c_object_factory_parameterst &object_factory_params)
43-
: symbols_created(_symbols_created),
44-
symbol_table(_symbol_table),
44+
: symbol_table(_symbol_table),
4545
loc(loc),
4646
ns(_symbol_table),
47-
object_factory_params(object_factory_params)
47+
object_factory_params(object_factory_params),
48+
allocate_objects(ID_C, loc, loc.get_function(), symbol_table)
4849
{}
4950

50-
exprt allocate_object(
51-
code_blockt &assignments,
52-
const exprt &target_expr,
53-
const typet &allocate_type,
54-
const bool static_lifetime);
55-
5651
void gen_nondet_init(
5752
code_blockt &assignments,
5853
const exprt &expr,
5954
const std::size_t depth = 0,
6055
recursion_sett recursion_set = recursion_sett());
6156

57+
void add_created_symbol(const symbolt *symbol_ptr)
58+
{
59+
allocate_objects.add_created_symbol(symbol_ptr);
60+
}
61+
62+
void declare_created_symbols(code_blockt &init_code)
63+
{
64+
allocate_objects.declare_created_symbols(init_code);
65+
}
66+
67+
void mark_created_symbols_as_input(code_blockt &init_code)
68+
{
69+
allocate_objects.mark_created_symbols_as_input(init_code);
70+
}
71+
6272
private:
6373
/// Generate initialisation code for each array element
6474
/// \param assignments: The code block to add code to
@@ -73,49 +83,6 @@ class symbol_factoryt
7383
const recursion_sett &recursion_set);
7484
};
7585

76-
/// Create a symbol for a pointer to point to
77-
/// \param assignments: The code block to add code to
78-
/// \param target_expr: The expression which we are allocating a symbol for
79-
/// \param allocate_type: The type to use for the symbol. If this doesn't match
80-
/// target_expr then a cast will be used for the assignment
81-
/// \param static_lifetime: Whether the symbol created should have static
82-
/// lifetime
83-
/// \return Returns the address of the allocated symbol
84-
exprt symbol_factoryt::allocate_object(
85-
code_blockt &assignments,
86-
const exprt &target_expr,
87-
const typet &allocate_type,
88-
const bool static_lifetime)
89-
{
90-
symbolt &aux_symbol = get_fresh_aux_symbol(
91-
allocate_type,
92-
id2string(loc.get_function()),
93-
"tmp",
94-
loc,
95-
ID_C,
96-
symbol_table);
97-
aux_symbol.is_static_lifetime = static_lifetime;
98-
symbols_created.push_back(&aux_symbol);
99-
100-
const typet &allocate_type_resolved=ns.follow(allocate_type);
101-
const typet &target_type=ns.follow(target_expr.type().subtype());
102-
bool cast_needed=allocate_type_resolved!=target_type;
103-
104-
exprt aoe=address_of_exprt(aux_symbol.symbol_expr());
105-
if(cast_needed)
106-
{
107-
aoe=typecast_exprt(aoe, target_expr.type());
108-
}
109-
110-
// Add the following code to assignments:
111-
// <target_expr> = &tmp$<temporary_counter>
112-
code_assignt assign(target_expr, aoe);
113-
assign.add_source_location()=loc;
114-
assignments.add(std::move(assign));
115-
116-
return aoe;
117-
}
118-
11986
/// Creates a nondet for expr, including calling itself recursively to make
12087
/// appropriate symbols to point to if expr is a pointer.
12188
/// \param assignments: The code block to add code to
@@ -155,17 +122,9 @@ void symbol_factoryt::gen_nondet_init(
155122

156123
code_blockt non_null_inst;
157124

158-
exprt allocated=allocate_object(non_null_inst, expr, subtype, false);
125+
exprt init_expr = allocate_objects.allocate_automatic_local_object(
126+
non_null_inst, expr, subtype);
159127

160-
exprt init_expr;
161-
if(allocated.id()==ID_address_of)
162-
{
163-
init_expr=allocated.op0();
164-
}
165-
else
166-
{
167-
init_expr=dereference_exprt(allocated, allocated.type().subtype());
168-
}
169128
gen_nondet_init(non_null_inst, init_expr, depth + 1, recursion_set);
170129

171130
if(depth < object_factory_params.min_null_tree_depth)
@@ -288,39 +247,16 @@ symbol_exprt c_nondet_symbol_factory(
288247
bool moving_symbol_failed=symbol_table.move(main_symbol, main_symbol_ptr);
289248
CHECK_RETURN(!moving_symbol_failed);
290249

291-
std::vector<const symbolt *> symbols_created;
292-
symbols_created.push_back(main_symbol_ptr);
293-
294-
symbol_factoryt state(
295-
symbols_created, symbol_table, loc, object_factory_parameters);
250+
symbol_factoryt state(symbol_table, loc, object_factory_parameters);
296251
code_blockt assignments;
297252
state.gen_nondet_init(assignments, main_symbol_expr);
298253

299-
// Add the following code to init_code for each symbol that's been created:
300-
// <type> <identifier>;
301-
for(const symbolt * const symbol_ptr : symbols_created)
302-
{
303-
code_declt decl(symbol_ptr->symbol_expr());
304-
decl.add_source_location()=loc;
305-
init_code.add(std::move(decl));
306-
}
254+
state.add_created_symbol(main_symbol_ptr);
255+
state.declare_created_symbols(init_code);
307256

308257
init_code.append(assignments);
309258

310-
// Add the following code to init_code for each symbol that's been created:
311-
// INPUT("<identifier>", <identifier>);
312-
for(symbolt const *symbol_ptr : symbols_created)
313-
{
314-
codet input_code(ID_input);
315-
input_code.operands().resize(2);
316-
input_code.op0()=
317-
address_of_exprt(index_exprt(
318-
string_constantt(symbol_ptr->base_name),
319-
from_integer(0, index_type())));
320-
input_code.op1()=symbol_ptr->symbol_expr();
321-
input_code.add_source_location()=loc;
322-
init_code.add(std::move(input_code));
323-
}
259+
state.mark_created_symbols_as_input(init_code);
324260

325261
return main_symbol_expr;
326262
}

0 commit comments

Comments
 (0)