cprover
auto_objects.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Symbolic Execution of ANSI-C
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "goto_symex.h"
13 
14 #include <util/prefix.h>
15 #include <util/cprover_prefix.h>
16 #include <util/symbol_table.h>
17 #include <util/std_expr.h>
18 
20 {
22 
23  // produce auto-object symbol
24  symbolt symbol;
25 
26  symbol.base_name="auto_object"+std::to_string(dynamic_counter);
27  symbol.name="symex::"+id2string(symbol.base_name);
28  symbol.is_lvalue=true;
29  symbol.type=type;
30  symbol.mode=ID_C;
31 
32  new_symbol_table.add(symbol);
33 
34  return symbol_exprt(symbol.name, symbol.type);
35 }
36 
38  const exprt &expr,
39  statet &state)
40 {
41  const typet &type=ns.follow(expr.type());
42 
43  if(type.id()==ID_struct)
44  {
45  const struct_typet &struct_type=to_struct_type(type);
46 
47  for(const auto &comp : struct_type.components())
48  {
49  member_exprt member_expr;
50  member_expr.struct_op()=expr;
51  member_expr.set_component_name(comp.get_name());
52  member_expr.type()=comp.type();
53 
54  initialize_auto_object(member_expr, state);
55  }
56  }
57  else if(type.id()==ID_pointer)
58  {
60  const typet &subtype=ns.follow(type.subtype());
61 
62  // we don't like function pointers and
63  // we don't like void *
64  if(subtype.id()!=ID_code &&
65  subtype.id()!=ID_empty)
66  {
67  // could be NULL nondeterministically
68 
69  address_of_exprt address_of_expr(
70  make_auto_object(type.subtype()),
71  pointer_type);
72 
73  if_exprt rhs(
76  address_of_expr);
77 
78  code_assignt assignment(expr, rhs);
79  symex_assign_rec(state, assignment);
80  }
81  }
82 }
83 
85  const exprt &expr,
86  statet &state)
87 {
88  if(expr.id()==ID_symbol &&
89  expr.get_bool(ID_C_SSA_symbol))
90  {
91  const ssa_exprt &ssa_expr=to_ssa_expr(expr);
92  const irep_idt &obj_identifier=ssa_expr.get_object_name();
93 
94  if(obj_identifier!="goto_symex::\\guard")
95  {
96  const symbolt &symbol=
97  ns.lookup(obj_identifier);
98 
99  if(has_prefix(id2string(symbol.base_name), "auto_object"))
100  {
101  // done already?
102  if(state.level2.current_names.find(ssa_expr.get_identifier())==
103  state.level2.current_names.end())
104  {
105  initialize_auto_object(expr, state);
106  }
107  }
108  }
109  }
110 
111  // recursive call
112  forall_operands(it, expr)
113  trigger_auto_object(*it, state);
114 }
The type of an expression.
Definition: type.h:20
irep_idt name
The unique identifier.
Definition: symbol.h:46
const typet & follow(const typet &src) const
Definition: namespace.cpp:66
virtual bool lookup(const irep_idt &name, const symbolt *&symbol) const
Definition: namespace.cpp:139
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:296
irep_idt mode
Language mode.
Definition: symbol.h:55
const irep_idt & get_identifier() const
Definition: std_expr.h:120
The null pointer constant.
Definition: std_expr.h:3774
const componentst & components() const
Definition: std_types.h:242
exprt make_auto_object(const typet &type)
The trinary if-then-else operator.
Definition: std_expr.h:2771
typet & type()
Definition: expr.h:60
The proper Booleans.
Definition: std_types.h:33
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:33
Structure type.
Definition: std_types.h:296
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:240
Extract member of struct or union.
Definition: std_expr.h:3214
goto_symex_statet::level2t level2
void set_component_name(const irep_idt &component_name)
Definition: std_expr.h:3254
const irep_idt & id() const
Definition: irep.h:189
const ssa_exprt & to_ssa_expr(const exprt &expr)
Cast a generic exprt to an ssa_exprt.
Definition: ssa_expr.h:150
static unsigned dynamic_counter
Definition: goto_symex.h:334
The pointer type.
Definition: std_types.h:1343
Symbolic Execution.
void trigger_auto_object(const exprt &expr, statet &state)
API to expression classes.
A side effect that returns a non-deterministically chosen value.
Definition: std_code.h:1037
#define forall_operands(it, expr)
Definition: expr.h:17
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
Symbol table.
Operator to return the address of an object.
Definition: std_expr.h:2593
void initialize_auto_object(const exprt &expr, statet &state)
void symex_assign_rec(statet &state, const code_assignt &code)
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
const struct_typet & to_struct_type(const typet &type)
Cast a generic typet to a struct_typet.
Definition: std_types.h:317
symbol_tablet & new_symbol_table
Definition: goto_symex.h:97
const pointer_typet & to_pointer_type(const typet &type)
Cast a generic typet to a pointer_typet.
Definition: std_types.h:1377
typet type
Type of symbol.
Definition: symbol.h:37
Base class for all expressions.
Definition: expr.h:46
const exprt & struct_op() const
Definition: std_expr.h:3270
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:52
irep_idt get_object_name() const
Definition: ssa_expr.h:46
Expression to hold a symbol (variable)
Definition: std_expr.h:82
const typet & subtype() const
Definition: type.h:31
const namespacet & ns
Definition: goto_symex.h:104
Expression providing an SSA-renamed symbol of expressions.
Definition: ssa_expr.h:17
Assignment.
Definition: std_code.h:144
bool is_lvalue
Definition: symbol.h:71