cprover
label_function_pointer_call_sites.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 Module: Label function pointer call sites
3 Author: Diffblue Ltd.
4 \*******************************************************************/
5 
8 
10 
11 #include <util/fresh_symbol.h>
12 
14 {
15  for(auto &goto_function : goto_model.goto_functions.function_map)
16  {
17  std::size_t function_pointer_call_counter = 0;
18 
20  goto_function.second,
21  [](const goto_programt::targett it) {
22  return it->is_function_call() && can_cast_expr<dereference_exprt>(
23  it->get_function_call().function());
24  },
25  [&](goto_programt::targett &it) {
26  auto const &function_call = it->get_function_call();
27  auto const &function_pointer_dereference =
28  to_dereference_expr(function_call.function());
29  auto const &source_location = function_call.source_location();
30  auto const &goto_function_symbol_mode =
31  goto_model.symbol_table.lookup_ref(goto_function.first).mode;
32 
33  auto const call_site_symbol_name =
34  irep_idt{id2string(goto_function.first) + ".function_pointer_call." +
35  std::to_string(++function_pointer_call_counter)};
36 
37  // insert new function pointer variable into the symbol table
38  goto_model.symbol_table.insert([&] {
39  symbolt function_call_site_symbol{};
40  function_call_site_symbol.name = function_call_site_symbol.base_name =
41  function_call_site_symbol.pretty_name = call_site_symbol_name;
42  function_call_site_symbol.type =
43  function_pointer_dereference.pointer().type();
44  function_call_site_symbol.location = function_call.source_location();
45  function_call_site_symbol.is_lvalue = true;
46  function_call_site_symbol.mode = goto_function_symbol_mode;
47  return function_call_site_symbol;
48  }());
49 
50  auto const new_function_pointer =
51  goto_model.symbol_table.lookup_ref(call_site_symbol_name)
52  .symbol_expr();
53 
54  // add assignment to the new function pointer variable, followed by a
55  // call of the new variable
56  auto assign_instruction = goto_programt::make_assignment(
57  code_assignt{new_function_pointer,
58  function_pointer_dereference.pointer()},
59  source_location);
60 
61  goto_function.second.body.insert_before_swap(it, assign_instruction);
62  const auto next = std::next(it);
63  to_code_function_call(next->code).function() =
64  dereference_exprt{new_function_pointer};
65  // we need to increment the iterator once more (in addition to the
66  // increment already done by for_each_goto_function_if()). This is
67  // because insert_before_swap() inserts a new instruction after the
68  // instruction pointed to by it (and then swaps the contents with the
69  // previous instruction). We need to increment the iterator as we also
70  // need to skip over this newly inserted instruction.
71  it++;
72  });
73  }
74 }
symbol_table_baset::lookup_ref
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Definition: symbol_table_base.h:104
fresh_symbol.h
Fresh auxiliary symbol creation.
dereference_exprt
Operator to dereference a pointer.
Definition: std_expr.h:2888
for_each_instruction_if
void for_each_instruction_if(GotoFunctionT &&goto_function, PredicateT predicate, HandlerT handler)
Definition: goto_program.h:1173
label_function_pointer_call_sites
void label_function_pointer_call_sites(goto_modelt &goto_model)
This ensures that call instructions can be only one of two things:
Definition: label_function_pointer_call_sites.cpp:13
goto_modelt
Definition: goto_model.h:26
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:27
goto_programt::make_assignment
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
Definition: goto_program.h:1024
symbolt::symbol_expr
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:122
symbol_tablet::insert
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
Definition: symbol_table.cpp:19
to_code_function_call
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:1294
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:295
label_function_pointer_call_sites.h
Label function pointer call sites across a goto model.
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:579
code_function_callt::function
exprt & function()
Definition: std_code.h:1218