Skip to content

Commit 3b5ddb5

Browse files
committed
Adapt c nondet symbol factory for use in function body generation
1 parent 9dd28e5 commit 3b5ddb5

File tree

3 files changed

+72
-39
lines changed

3 files changed

+72
-39
lines changed

src/ansi-c/ansi_c_entry_point.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ exprt::operandst build_function_environment(
4242
base_name,
4343
p.type(),
4444
p.source_location(),
45-
object_factory_parameters);
45+
object_factory_parameters,
46+
allocation_typet::LOCAL);
4647
}
4748

4849
return main_arguments;

src/ansi-c/c_nondet_symbol_factory.cpp

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,51 +25,29 @@ Author: Diffblue Ltd.
2525

2626
#include <goto-programs/goto_functions.h>
2727

28-
class symbol_factoryt
29-
{
30-
std::vector<const symbolt *> &symbols_created;
31-
symbol_tablet &symbol_table;
32-
const source_locationt &loc;
33-
namespacet ns;
34-
const c_object_factory_parameterst &object_factory_params;
35-
36-
typedef std::set<irep_idt> recursion_sett;
37-
38-
public:
39-
symbol_factoryt(
40-
std::vector<const symbolt *> &_symbols_created,
41-
symbol_tablet &_symbol_table,
42-
const source_locationt &loc,
43-
const c_object_factory_parameterst &object_factory_params)
44-
: symbols_created(_symbols_created),
45-
symbol_table(_symbol_table),
46-
loc(loc),
47-
ns(_symbol_table),
48-
object_factory_params(object_factory_params)
49-
{}
50-
51-
void gen_nondet_init(
52-
code_blockt &assignments,
53-
const exprt &expr,
54-
const std::size_t depth = 0,
55-
recursion_sett recursion_set = recursion_sett());
56-
};
57-
5828
/// Creates a nondet for expr, including calling itself recursively to make
5929
/// appropriate symbols to point to if expr is a pointer.
6030
/// \param assignments: The code block to add code to
6131
/// \param expr: The expression which we are generating a non-determinate value
6232
/// for
6333
/// \param depth number of pointers followed so far during initialisation
6434
/// \param recursion_set names of structs seen so far on current pointer chain
35+
/// \param assign_const Indicates whether const objects should be nondet
36+
/// initialized
6537
void symbol_factoryt::gen_nondet_init(
6638
code_blockt &assignments,
6739
const exprt &expr,
6840
const std::size_t depth,
69-
recursion_sett recursion_set)
41+
recursion_sett recursion_set,
42+
const bool assign_const)
7043
{
7144
const typet &type=ns.follow(expr.type());
7245

46+
if(!assign_const && expr.type().get_bool(ID_C_constant))
47+
{
48+
return;
49+
}
50+
7351
if(type.id()==ID_pointer)
7452
{
7553
// dereferenced type
@@ -97,8 +75,8 @@ void symbol_factoryt::gen_nondet_init(
9775
allocate_objectst allocate_objects(
9876
ID_C, loc, loc.get_function(), symbol_table);
9977

100-
exprt allocated = allocate_objects.allocate_non_dynamic_object(
101-
non_null_inst, expr, subtype, false, symbols_created);
78+
exprt allocated = allocate_objects.allocate_object(
79+
non_null_inst, expr, subtype, allocation_type, symbols_created);
10280

10381
exprt init_expr;
10482
if(allocated.id()==ID_address_of)
@@ -109,7 +87,7 @@ void symbol_factoryt::gen_nondet_init(
10987
{
11088
init_expr=dereference_exprt(allocated, allocated.type().subtype());
11189
}
112-
gen_nondet_init(non_null_inst, init_expr, depth + 1, recursion_set);
90+
gen_nondet_init(non_null_inst, init_expr, depth + 1, recursion_set, true);
11391

11492
if(depth < object_factory_params.min_null_tree_depth)
11593
{
@@ -149,12 +127,18 @@ void symbol_factoryt::gen_nondet_init(
149127
for(const auto &component : struct_type.components())
150128
{
151129
const typet &component_type = component.type();
130+
131+
if(!assign_const && component_type.get_bool(ID_C_constant))
132+
{
133+
continue;
134+
}
135+
152136
const irep_idt name = component.get_name();
153137

154138
member_exprt me(expr, name, component_type);
155139
me.add_source_location() = loc;
156140

157-
gen_nondet_init(assignments, me, depth, recursion_set);
141+
gen_nondet_init(assignments, me, depth, recursion_set, assign_const);
158142
}
159143
}
160144
else
@@ -182,14 +166,17 @@ void symbol_factoryt::gen_nondet_init(
182166
/// \param loc: The location to assign to generated code
183167
/// \param object_factory_parameters configuration parameters for the object
184168
/// factory
169+
/// \param allocation_type Indicates whether the new objects should be allocated
170+
/// locally, globally, or dynamically
185171
/// \return Returns the symbol_exprt for the symbol created
186172
symbol_exprt c_nondet_symbol_factory(
187173
code_blockt &init_code,
188174
symbol_tablet &symbol_table,
189175
const irep_idt base_name,
190176
const typet &type,
191177
const source_locationt &loc,
192-
const c_object_factory_parameterst &object_factory_parameters)
178+
const c_object_factory_parameterst &object_factory_parameters,
179+
const allocation_typet allocation_type)
193180
{
194181
irep_idt identifier=id2string(goto_functionst::entry_point())+
195182
"::"+id2string(base_name);
@@ -212,7 +199,12 @@ symbol_exprt c_nondet_symbol_factory(
212199
symbols_created.push_back(main_symbol_ptr);
213200

214201
symbol_factoryt state(
215-
symbols_created, symbol_table, loc, object_factory_parameters);
202+
symbols_created,
203+
symbol_table,
204+
loc,
205+
object_factory_parameters,
206+
allocation_type);
207+
216208
code_blockt assignments;
217209
state.gen_nondet_init(assignments, main_symbol_expr);
218210

src/ansi-c/c_nondet_symbol_factory.h

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,55 @@ Author: Diffblue Ltd.
1414

1515
#include "c_object_factory_parameters.h"
1616

17+
#include <set>
18+
#include <vector>
19+
20+
#include <util/allocate_objects.h>
1721
#include <util/std_code.h>
1822
#include <util/symbol_table.h>
1923

24+
class symbol_factoryt
25+
{
26+
std::vector<const symbolt *> &symbols_created;
27+
symbol_tablet &symbol_table;
28+
const source_locationt &loc;
29+
namespacet ns;
30+
const c_object_factory_parameterst &object_factory_params;
31+
const allocation_typet allocation_type;
32+
33+
public:
34+
typedef std::set<irep_idt> recursion_sett;
35+
36+
symbol_factoryt(
37+
std::vector<const symbolt *> &symbols_created,
38+
symbol_tablet &symbol_table,
39+
const source_locationt &loc,
40+
const c_object_factory_parameterst &object_factory_params,
41+
const allocation_typet allocation_type)
42+
: symbols_created(symbols_created),
43+
symbol_table(symbol_table),
44+
loc(loc),
45+
ns(symbol_table),
46+
object_factory_params(object_factory_params),
47+
allocation_type(allocation_type)
48+
{
49+
}
50+
51+
void gen_nondet_init(
52+
code_blockt &assignments,
53+
const exprt &expr,
54+
const std::size_t depth = 0,
55+
recursion_sett recursion_set = recursion_sett(),
56+
const bool assign_const = true);
57+
};
58+
2059
symbol_exprt c_nondet_symbol_factory(
2160
code_blockt &init_code,
2261
symbol_tablet &symbol_table,
2362
const irep_idt base_name,
2463
const typet &type,
2564
const source_locationt &,
26-
const c_object_factory_parameterst &object_factory_parameters);
65+
const c_object_factory_parameterst &object_factory_parameters,
66+
const allocation_typet allocation_type);
2767

2868
#endif // CPROVER_ANSI_C_C_NONDET_SYMBOL_FACTORY_H

0 commit comments

Comments
 (0)