Skip to content

Commit 44509e4

Browse files
committed
Apply miscellaneous refactoring and fixes to goto-harness.
Add error checking (check that the entry function is in the symbol table before the code generation, and make sure that it's present correctly) make sure that doc comments are capitalised, change the signatures of functions to make them stricter in the arguments they accept.
1 parent 9db8bd4 commit 44509e4

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

src/goto-harness/function_call_harness_generator.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ struct function_call_harness_generatort::implt
7272
void ensure_harness_does_not_already_exist();
7373
/// Update the goto-model with the new harness function.
7474
void add_harness_function_to_goto_model(code_blockt function_body);
75-
/// declare local variables for each of the parameters of the entry function
75+
/// Declare local variables for each of the parameters of the entry function
7676
/// and return them
7777
code_function_callt::argumentst declare_arguments(code_blockt &function_body);
78-
/// write initialisation code for each of the arguments into function_body,
78+
/// Write initialisation code for each of the arguments into function_body,
7979
/// then insert a call to the entry function with the arguments
8080
void call_function(
8181
const code_function_callt::argumentst &arguments,
@@ -260,7 +260,7 @@ void function_call_harness_generatort::implt::generate_initialisation_code_for(
260260
void function_call_harness_generatort::validate_options(
261261
const goto_modelt &goto_model)
262262
{
263-
if(p_impl->function == ID_empty)
263+
if(p_impl->function == ID_empty_string)
264264
throw invalid_command_line_argument_exceptiont{
265265
"required parameter entry function not set",
266266
"--" FUNCTION_HARNESS_GENERATOR_FUNCTION_OPT};
@@ -274,8 +274,17 @@ void function_call_harness_generatort::validate_options(
274274
" --" COMMON_HARNESS_GENERATOR_MAX_ARRAY_SIZE_OPT};
275275
}
276276

277-
auto function_to_call = goto_model.symbol_table.lookup_ref(p_impl->function);
278-
auto ftype = to_code_type(function_to_call.type);
277+
const auto function_to_call_pointer =
278+
goto_model.symbol_table.lookup(p_impl->function);
279+
if(function_to_call_pointer == nullptr)
280+
{
281+
throw invalid_command_line_argument_exceptiont{
282+
"entry function `" + id2string(p_impl->function) +
283+
"' does not exist in the symbol table",
284+
"--" FUNCTION_HARNESS_GENERATOR_FUNCTION_OPT};
285+
}
286+
const auto &function_to_call = *function_to_call_pointer;
287+
const auto &ftype = to_code_type(function_to_call.type);
279288
for(auto const &pointer_id : p_impl->function_parameters_to_treat_equal)
280289
{
281290
std::string decorated_pointer_id =

src/goto-harness/recursive_initialization.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void recursive_initializationt::initialize(
157157

158158
code_blockt recursive_initializationt::build_constructor_body(
159159
const exprt &depth_symbol,
160-
const exprt &result_symbol,
160+
const symbol_exprt &result_symbol,
161161
const optionalt<exprt> &size_symbol,
162162
const optionalt<irep_idt> &lhs_name)
163163
{
@@ -192,7 +192,7 @@ code_blockt recursive_initializationt::build_constructor_body(
192192
}
193193
}
194194

195-
const irep_idt &recursive_initializationt::build_constructor(const exprt &expr)
195+
irep_idt recursive_initializationt::build_constructor(const exprt &expr)
196196
{
197197
// for `expr` of type T builds a declaration of a function:
198198
//
@@ -207,7 +207,9 @@ const irep_idt &recursive_initializationt::build_constructor(const exprt &expr)
207207
{
208208
expr_name = to_symbol_expr(expr).get_identifier();
209209
if(should_be_treated_as_array(*expr_name))
210+
{
210211
size_var = get_associated_size_variable(*expr_name);
212+
}
211213
}
212214
const typet &type = expr.type();
213215
if(type_constructor_names.find(type) != type_constructor_names.end())
@@ -520,7 +522,7 @@ symbol_exprt recursive_initializationt::get_free_function()
520522

521523
code_blockt recursive_initializationt::build_pointer_constructor(
522524
const exprt &depth,
523-
const exprt &result)
525+
const symbol_exprt &result)
524526
{
525527
PRECONDITION(result.type().id() == ID_pointer);
526528
const typet &type = result.type().subtype();
@@ -605,7 +607,7 @@ code_blockt recursive_initializationt::build_pointer_constructor(
605607
}
606608

607609
code_blockt recursive_initializationt::build_array_string_constructor(
608-
const exprt &result) const
610+
const symbol_exprt &result) const
609611
{
610612
PRECONDITION(result.type().id() == ID_pointer);
611613
const typet &type = result.type().subtype();
@@ -634,7 +636,7 @@ code_blockt recursive_initializationt::build_array_string_constructor(
634636

635637
code_blockt recursive_initializationt::build_array_constructor(
636638
const exprt &depth,
637-
const exprt &result)
639+
const symbol_exprt &result)
638640
{
639641
PRECONDITION(result.type().id() == ID_pointer);
640642
const typet &type = result.type().subtype();
@@ -656,7 +658,7 @@ code_blockt recursive_initializationt::build_array_constructor(
656658

657659
code_blockt recursive_initializationt::build_struct_constructor(
658660
const exprt &depth,
659-
const exprt &result)
661+
const symbol_exprt &result)
660662
{
661663
PRECONDITION(result.type().id() == ID_pointer);
662664
const typet &struct_type = result.type().subtype();
@@ -671,8 +673,8 @@ code_blockt recursive_initializationt::build_struct_constructor(
671673
return body;
672674
}
673675

674-
code_blockt
675-
recursive_initializationt::build_nondet_constructor(const exprt &result) const
676+
code_blockt recursive_initializationt::build_nondet_constructor(
677+
const symbol_exprt &result) const
676678
{
677679
PRECONDITION(result.type().id() == ID_pointer);
678680
code_blockt body{};
@@ -684,7 +686,7 @@ recursive_initializationt::build_nondet_constructor(const exprt &result) const
684686

685687
code_blockt recursive_initializationt::build_dynamic_array_constructor(
686688
const exprt &depth,
687-
const exprt &result,
689+
const symbol_exprt &result,
688690
const exprt &size,
689691
const optionalt<irep_idt> &lhs_name)
690692
{
@@ -786,7 +788,7 @@ code_blockt recursive_initializationt::build_dynamic_array_constructor(
786788

787789
bool recursive_initializationt::needs_freeing(const exprt &expr) const
788790
{
789-
if(expr.type().id() != ID_pointer)
791+
if(expr.type().id() != ID_pointer || expr.type().subtype().id() == ID_code)
790792
return false;
791793
if(common_arguments_origin.has_value() && expr.id() == ID_symbol)
792794
{

src/goto-harness/recursive_initialization.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@ class recursive_initializationt
168168
/// \return the body of the constructor
169169
code_blockt build_constructor_body(
170170
const exprt &depth_symbol,
171-
const exprt &result_symbol,
171+
const symbol_exprt &result_symbol,
172172
const optionalt<exprt> &size_symbol,
173173
const optionalt<irep_idt> &lhs_name);
174174

175175
/// Check if a constructor for the type of \p expr already exists and create
176176
/// it if not.
177177
/// \param expr: the expression to be constructed
178178
/// \return name of the constructor function
179-
const irep_idt &build_constructor(const exprt &expr);
179+
irep_idt build_constructor(const exprt &expr);
180180

181181
/// Generic constructor for all pointers: only builds one pointee (not an
182182
/// array) but may recourse in case the pointee contains more pointers, e.g.
@@ -185,26 +185,28 @@ class recursive_initializationt
185185
/// \param result: symbol of the result parameter
186186
/// \return the body of the constructor
187187
code_blockt
188-
build_pointer_constructor(const exprt &depth, const exprt &result);
188+
build_pointer_constructor(const exprt &depth, const symbol_exprt &result);
189189

190190
/// Constructor for structures: simply iterates over members and initialise
191191
/// each one.
192192
/// \param depth: symbol of the depth parameter
193193
/// \param result: symbol of the result parameter
194194
/// \return the body of the constructor
195-
code_blockt build_struct_constructor(const exprt &depth, const exprt &result);
195+
code_blockt
196+
build_struct_constructor(const exprt &depth, const symbol_exprt &result);
196197

197198
/// Default constructor: assigns non-deterministic value of the right type.
198199
/// \param result: symbol of the result parameter
199200
/// \return the body of the constructor
200-
code_blockt build_nondet_constructor(const exprt &result) const;
201+
code_blockt build_nondet_constructor(const symbol_exprt &result) const;
201202

202203
/// Constructor for arrays: simply iterates over elements and initialise
203204
/// each one.
204205
/// \param depth: symbol of the depth parameter
205206
/// \param result: symbol of the result parameter
206207
/// \return the body of the constructor
207-
code_blockt build_array_constructor(const exprt &depth, const exprt &result);
208+
code_blockt
209+
build_array_constructor(const exprt &depth, const symbol_exprt &result);
208210

209211
/// Constructor for dynamic arrays: allocate memory for `n` elements (`n` is
210212
/// random but bounded) and initialise each one.
@@ -215,14 +217,14 @@ class recursive_initializationt
215217
/// \return the body of the constructor
216218
code_blockt build_dynamic_array_constructor(
217219
const exprt &depth,
218-
const exprt &result,
220+
const symbol_exprt &result,
219221
const exprt &size,
220222
const optionalt<irep_idt> &lhs_name);
221223

222224
/// Constructor for strings: as array but the last element is zero.
223225
/// \param result: symbol of the result parameter
224226
/// \return the body of the constructor
225-
code_blockt build_array_string_constructor(const exprt &result) const;
227+
code_blockt build_array_string_constructor(const symbol_exprt &result) const;
226228
};
227229

228230
#endif // CPROVER_GOTO_HARNESS_RECURSIVE_INITIALIZATION_H

0 commit comments

Comments
 (0)