cprover
remove_calls_no_body.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Remove calls to functions without a body
4 
5 Author: Daniel Poetzl
6 
7 \*******************************************************************/
8 
11 
12 #include "remove_calls_no_body.h"
13 
14 #include <util/invariant.h>
15 
16 #include "goto_functions.h"
17 
26  const exprt &lhs,
27  const exprt::operandst &arguments)
28 {
29  PRECONDITION(target->is_function_call());
31 
32  goto_programt tmp;
33 
34  // evaluate function arguments -- they might have
35  // pointer dereferencing or the like
36  for(const exprt &argument : arguments)
37  {
39  t->make_other(code_expressiont(argument));
40  t->source_location = target->source_location;
41  t->function = target->function;
42  }
43 
44  // return value
45  if(lhs.is_not_nil())
46  {
47  side_effect_expr_nondett rhs(lhs.type());
48  rhs.add_source_location() = target->source_location;
49 
50  code_assignt code(lhs, rhs);
51  code.add_source_location() = target->source_location;
52 
53  goto_programt::targett t = tmp.add_instruction(ASSIGN);
54  t->source_location = target->source_location;
55  t->function = target->function;
56  t->code.swap(code);
57  }
58 
59  // kill call
60  target->type = LOCATION;
61  target->code.clear();
62  target++;
63 
64  goto_program.destructive_insert(target, tmp);
65 }
66 
72  const goto_programt::const_targett target,
73  const goto_functionst &goto_functions)
74 {
75  if(!target->is_function_call())
76  return false;
77 
78  const code_function_callt &cfc = to_code_function_call(target->code);
79  const exprt &f = cfc.function();
80 
81  if(f.id() != ID_symbol)
82  return false;
83 
84  const symbol_exprt &se = to_symbol_expr(f);
85  const irep_idt id = se.get_identifier();
86 
87  goto_functionst::function_mapt::const_iterator f_it =
88  goto_functions.function_map.find(id);
89 
90  if(f_it != goto_functions.function_map.end())
91  {
92  return !f_it->second.body_available();
93  }
94 
95  return false;
96 }
97 
106 {
108  it != goto_program.instructions.end();) // no it++
109  {
110  if(is_opaque_function_call(it, goto_functions))
111  {
112  const code_function_callt &cfc = to_code_function_call(it->code);
113  remove_call_no_body(goto_program, it, cfc.lhs(), cfc.arguments());
114  }
115  else
116  {
117  it++;
118  }
119  }
120 }
121 
127 {
128  Forall_goto_functions(f_it, goto_functions)
129  {
130  (*this)(f_it->second.body, goto_functions);
131  }
132 }
bool is_not_nil() const
Definition: irep.h:103
const irep_idt & get_identifier() const
Definition: std_expr.h:128
Goto Programs with Functions.
Remove calls to functions without a body.
function_mapt function_map
typet & type()
Definition: expr.h:56
void operator()(goto_programt &goto_program, const goto_functionst &goto_functions)
Remove calls to functions without a body and replace them with evaluations of the arguments of the ca...
An expression statement.
Definition: std_code.h:1188
bool is_opaque_function_call(const goto_programt::const_targett target, const goto_functionst &goto_functions)
Check if instruction is a call to an opaque function through an ordinary (non-function pointer) call...
const irep_idt & id() const
Definition: irep.h:189
argumentst & arguments()
Definition: std_code.h:860
instructionst::iterator targett
Definition: goto_program.h:397
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:403
instructionst::const_iterator const_targett
Definition: goto_program.h:398
#define PRECONDITION(CONDITION)
Definition: invariant.h:230
A side effect that returns a non-deterministically chosen value.
Definition: std_code.h:1301
void destructive_insert(const_targett target, goto_programt &p)
Inserts the given program at the given location.
Definition: goto_program.h:495
A function call.
Definition: std_code.h:828
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast a generic exprt to a symbol_exprt.
Definition: std_expr.h:210
std::vector< exprt > operandst
Definition: expr.h:45
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:70
exprt & function()
Definition: std_code.h:848
targett add_instruction()
Adds an instruction at the end.
Definition: goto_program.h:505
Base class for all expressions.
Definition: expr.h:42
void remove_call_no_body(goto_programt &dest, goto_programt::targett target, const exprt &lhs, const exprt::operandst &arguments)
Remove a single call.
#define Forall_goto_functions(it, functions)
bool empty() const
Is the program empty?
Definition: goto_program.h:590
void swap(irept &irep)
Definition: irep.h:231
goto_programt & goto_program
Definition: cover.cpp:63
source_locationt & add_source_location()
Definition: expr.h:130
Expression to hold a symbol (variable)
Definition: std_expr.h:90
Assignment.
Definition: std_code.h:195
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:879