42 int param_counter = 0;
43 for(
auto ¶meter : parameters)
45 if(parameter.get_identifier().empty())
47 const std::string param_base_name =
48 parameter.get_base_name().empty()
52 id2string(function_name) +
"::" + param_base_name;
53 parameter.set_base_name(param_base_name);
54 parameter.set_identifier(new_param_identifier);
56 new_param_sym.
name = new_param_identifier;
57 new_param_sym.
type = parameter.type();
58 new_param_sym.
base_name = param_base_name;
59 new_param_sym.
mode = function_symbol.mode;
60 new_param_sym.
module = function_symbol.module;
61 new_param_sym.
location = function_symbol.location;
62 symbol_table.
add(new_param_sym);
65 function.set_parameter_identifiers(
to_code_type(function_symbol.type));
74 const irep_idt &function_name)
const override
76 auto const &function_symbol = symbol_table.
lookup_ref(function_name);
91 const irep_idt &function_name)
const override
93 auto const &function_symbol = symbol_table.
lookup_ref(function_name);
97 annotated_location.
set_comment(
"undefined function should be unreachable");
115 const irep_idt &function_name)
const override
117 auto const &function_symbol = symbol_table.
lookup_ref(function_name);
121 annotated_location.
set_comment(
"undefined function should be unreachable");
165 const std::size_t initial_depth,
190 init_code.
append(assignments);
200 const std::string ¶m_name,
201 std::size_t param_number)
const
210 return std::binary_search(
221 const irep_idt &function_name)
const override
227 auto max_param_number = std::max_element(
229 if(*max_param_number >=
function.parameter_identifiers.size())
232 "function " +
id2string(function_name) +
" does not take " +
234 "--generate-havocing-body"};
238 const auto ¶meter =
function.parameter_identifiers[number];
240 if(parameter_symbol.
type.
id() != ID_pointer)
244 id2string(function_name) +
" is not a pointer",
245 "--generate-havocing-body"};
250 auto const &function_symbol = symbol_table.
lookup_ref(function_name);
254 for(std::size_t i = 0; i <
function.parameter_identifiers.size(); ++i)
256 const auto ¶meter =
function.parameter_identifiers[i];
259 parameter_symbol.
type.
id() == ID_pointer &&
265 auto goto_instruction =
285 function.body.destructive_append(dest);
287 auto label_instruction =
289 goto_instruction->complete_goto(label_instruction);
295 auto const &global_sym = symbol_table.
lookup_ref(global_id);
307 function.body.destructive_append(dest);
313 typet type(return_type);
314 type.
remove(ID_C_constant);
339 function.body.destructive_append(dest);
368 : runtime_error(reason)
376 const std::string &options,
381 if(options.empty() || options ==
"nondet-return")
383 return std::make_unique<havoc_generate_function_bodiest>(
384 std::vector<irep_idt>{},
386 object_factory_parameters,
390 if(options ==
"assume-false")
392 return std::make_unique<assume_false_generate_function_bodiest>();
395 if(options ==
"assert-false")
397 return std::make_unique<assert_false_generate_function_bodiest>();
400 if(options ==
"assert-false-assume-false")
402 return std::make_unique<
406 const std::vector<std::string> option_components =
split_string(options,
',');
407 if(!option_components.empty() && option_components[0] ==
"havoc")
409 std::regex globals_regex;
410 std::regex params_regex;
411 std::vector<std::size_t> param_numbers;
412 for(std::size_t i = 1; i < option_components.size(); ++i)
414 const std::vector<std::string> key_value_pair =
416 if(key_value_pair.size() != 2)
419 "Expected key_value_pair of form argument:value");
421 if(key_value_pair[0] ==
"globals")
423 globals_regex = key_value_pair[1];
425 else if(key_value_pair[0] ==
"params")
427 auto param_identifiers =
split_string(key_value_pair[1],
';');
428 if(param_identifiers.size() == 1)
430 auto maybe_nondet_param_number =
432 if(!maybe_nondet_param_number.has_value())
434 params_regex = key_value_pair[1];
439 param_identifiers.begin(),
440 param_identifiers.end(),
441 std::back_inserter(param_numbers),
442 [](
const std::string ¶m_id) {
443 auto maybe_nondet_param_number = string2optional_size_t(param_id);
445 maybe_nondet_param_number.has_value(),
446 param_id +
" not a number");
447 return *maybe_nondet_param_number;
449 std::sort(param_numbers.begin(), param_numbers.end());
454 "Unknown option \"" + key_value_pair[0] +
"\"");
457 std::vector<irep_idt> globals_to_havoc;
460 const std::regex cprover_prefix = std::regex(
"__CPROVER.*");
461 for(
auto const &symbol : symbol_table.
symbols)
464 symbol.second.is_lvalue && symbol.second.is_static_lifetime &&
465 std::regex_match(
id2string(symbol.first), globals_regex))
467 if(std::regex_match(
id2string(symbol.first), cprover_prefix))
469 messages.
warning() <<
"generate function bodies: "
470 <<
"matched global '" <<
id2string(symbol.first)
471 <<
"' begins with __CPROVER, "
472 <<
"havoc-ing this global may interfere"
475 globals_to_havoc.push_back(symbol.first);
478 if(param_numbers.empty())
479 return std::make_unique<havoc_generate_function_bodiest>(
480 std::move(globals_to_havoc),
481 std::move(params_regex),
482 object_factory_parameters,
485 return std::make_unique<havoc_generate_function_bodiest>(
486 std::move(globals_to_havoc),
487 std::move(param_numbers),
488 object_factory_parameters,
502 const std::regex &functions_regex,
508 const std::regex cprover_prefix = std::regex(
"__CPROVER.*");
509 bool did_generate_body =
false;
513 !
function.second.body_available() &&
514 std::regex_match(
id2string(
function.first), functions_regex))
516 if(std::regex_match(
id2string(
function.first), cprover_prefix))
518 messages.
warning() <<
"generate function bodies: matched function '"
520 <<
"' begins with __CPROVER "
521 <<
"the generated body for this function "
524 did_generate_body =
true;
529 if(!did_generate_body)
532 <<
"generate function bodies: No function name matched regex"
538 const std::string &function_name,
539 const std::string &call_site_id,
557 auto const symbol = symbol_pair.second;
558 if(symbol.type.id() == ID_code && symbol.name == function_name)
560 function_mode = symbol.mode;
561 function_type = symbol.type;
569 symbolt havoc_function_symbol{};
570 havoc_function_symbol.
name = havoc_function_symbol.base_name =
571 havoc_function_symbol.pretty_name = function_name +
"." + call_site_id;
573 havoc_function_symbol.is_lvalue =
true;
574 havoc_function_symbol.mode = function_mode;
575 havoc_function_symbol.type = function_type;
579 auto const &generated_havoc =
589 !
function.second.body_available() &&
590 havoc_function_symbol.name ==
id2string(
function.first))
597 auto is_havoc_function_call = [&function_name](
const exprt &expr) {
598 if(expr.id() != ID_symbol)
600 std::string called_function_name =
602 if(called_function_name == function_name)
605 return (
has_prefix(called_function_name, function_name +
"."));
609 std::size_t counter = 0;
612 for(
auto &instruction :
function.second.body.instructions)
614 if(instruction.is_function_call())
616 auto &called_function = instruction.call_function();
617 if(is_havoc_function_call(called_function))
619 if(++counter == *call_site_number)
621 called_function = generated_havoc.symbol_expr();
static abstract_object_pointert transform(const exprt &expr, const std::vector< abstract_object_pointert > &operands, const abstract_environmentt &environment, const namespacet &ns)
@ DYNAMIC
Allocate dynamic objects (using ALLOCATE)
void generate_function_body_impl(goto_functiont &function, symbol_tablet &symbol_table, const irep_idt &function_name) const override
Produce a body for the passed function At this point the body of function is always empty,...
void generate_function_body_impl(goto_functiont &function, symbol_tablet &symbol_table, const irep_idt &function_name) const override
Produce a body for the passed function At this point the body of function is always empty,...
void generate_function_body_impl(goto_functiont &function, symbol_tablet &symbol_table, const irep_idt &function_name) const override
Produce a body for the passed function At this point the body of function is always empty,...
A codet representing sequential composition of program statements.
void append(const code_blockt &extra_block)
Add all the codets from extra_block to the current code_blockt.
const typet & return_type() const
const parameterst & parameters() const
Operator to dereference a pointer.
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Base class for all expressions.
The Boolean constant false.
generate_function_bodies_errort(const std::string &reason)
Base class for replace_function_body implementations.
void generate_function_body(goto_functiont &function, symbol_tablet &symbol_table, const irep_idt &function_name) const
Replace the function body with one based on the replace_function_body class being used.
void generate_parameter_names(goto_functiont &function, symbol_tablet &symbol_table, const irep_idt &function_name) const
Generate parameter names for unnamed parameters.
virtual void generate_function_body_impl(goto_functiont &function, symbol_tablet &symbol_table, const irep_idt &function_name) const =0
Produce a body for the passed function At this point the body of function is always empty,...
function_mapt function_map
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
symbol_tablet symbol_table
Symbol table.
goto_functionst goto_functions
GOTO functions.
A generic container class for the GOTO intermediate representation of one function.
static instructiont make_assumption(const exprt &g, const source_locationt &l=source_locationt::nil())
static instructiont make_set_return_value(exprt return_value, const source_locationt &l=source_locationt::nil())
static instructiont make_dead(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
static instructiont make_end_function(const source_locationt &l=source_locationt::nil())
void destructive_append(goto_programt &p)
Appends the given program p to *this. p is destroyed.
static instructiont make_skip(const source_locationt &l=source_locationt::nil())
static instructiont make_decl(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
static instructiont make_assertion(const exprt &g, const source_locationt &l=source_locationt::nil())
static instructiont make_incomplete_goto(const exprt &_cond, const source_locationt &l=source_locationt::nil())
havoc_generate_function_bodiest(std::vector< irep_idt > globals_to_havoc, std::vector< std::size_t > param_numbers_to_havoc, const c_object_factory_parameterst &object_factory_parameters, message_handlert &message_handler)
std::optional< std::vector< std::size_t > > param_numbers_to_havoc
const std::vector< irep_idt > globals_to_havoc
havoc_generate_function_bodiest(std::vector< irep_idt > globals_to_havoc, std::regex parameters_to_havoc, const c_object_factory_parameterst &object_factory_parameters, message_handlert &message_handler)
bool should_havoc_param(const std::string ¶m_name, std::size_t param_number) const
std::optional< std::regex > parameters_to_havoc
void generate_function_body_impl(goto_functiont &function, symbol_tablet &symbol_table, const irep_idt &function_name) const override
Produce a body for the passed function At this point the body of function is always empty,...
const c_object_factory_parameterst & object_factory_parameters
void havoc_expr_rec(const exprt &lhs, const std::size_t initial_depth, const source_locationt &source_location, const irep_idt &function_id, symbol_tablet &symbol_table, goto_programt &dest) const
Thrown when users pass incorrect command line arguments, for example passing no files to analysis or ...
bool get_bool(const irep_idt &name) const
void remove(const irep_idt &name)
const irep_idt & id() const
Class that provides messages with a built-in verbosity 'level'.
mstreamt & warning() const
message_handlert & get_message_handler()
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
The null pointer constant.
Symbol table entry of function parameterThis is a symbol generated as part of type checking.
const typet & base_type() const
The type of the data what we point to.
void set_comment(const irep_idt &comment)
void set_property_class(const irep_idt &property_class)
void set_function(const irep_idt &function)
Expression to hold a symbol (variable)
void gen_nondet_init(code_blockt &assignments, const exprt &expr, const std::size_t depth=0, recursion_sett recursion_set=recursion_sett(), const bool assign_const=true)
Creates a nondet for expr, including calling itself recursively to make appropriate symbols to point ...
std::set< irep_idt > recursion_sett
void declare_created_symbols(code_blockt &init_code)
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
const symbolst & symbols
Read-only field, used to look up symbols given their names.
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
irep_idt base_name
Base (non-scoped) name.
irep_idt module
Name of module the symbol belongs to.
source_locationt location
Source code location of definition of symbol.
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
typet type
Type of symbol.
irep_idt name
The unique identifier.
irep_idt mode
Language mode.
static exprt conditional_cast(const exprt &expr, const typet &type)
The type of an expression, extends irept.
bool has_prefix(const std::string &s, const std::string &prefix)
symbolt & get_fresh_aux_symbol(const typet &type, const std::string &name_prefix, const std::string &basename_prefix, const source_locationt &source_location, const irep_idt &symbol_mode, const namespacet &ns, symbol_table_baset &symbol_table)
Installs a fresh-named symbol with respect to the given namespace ns with the requested name pattern ...
Fresh auxiliary symbol creation.
std::unique_ptr< generate_function_bodiest > generate_function_bodies_factory(const std::string &options, const c_object_factory_parameterst &object_factory_parameters, const symbol_tablet &symbol_table, message_handlert &message_handler)
Create the type that actually generates the functions.
void generate_function_bodies(const std::regex &functions_regex, const generate_function_bodiest &generate_function_body, goto_modelt &model, message_handlert &message_handler)
Generate function bodies with some default behavior: assert-false, assume-false, assert-false-assume-...
void goto_convert(const codet &code, symbol_table_baset &symbol_table, goto_programt &dest, message_handlert &message_handler, const irep_idt &mode)
Goto Programs with Functions.
const std::string & id2string(const irep_idt &d)
API to expression classes for Pointers.
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
void remove_skip(goto_programt &goto_program, goto_programt::targett begin, goto_programt::targett end)
remove unnecessary skip statements
#define CHECK_RETURN(CONDITION)
#define PRECONDITION(CONDITION)
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
std::optional< std::size_t > string2optional_size_t(const std::string &str, int base)
Convert string to size_t similar to the stoul or stoull functions, return nullopt when the conversion...
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
void split_string(const std::string &s, char delim, std::vector< std::string > &result, bool strip, bool remove_empty)