cprover
static_lifetime_init.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "static_lifetime_init.h"
10 
11 #include <cassert>
12 #include <cstdlib>
13 
14 #include <util/arith_tools.h>
15 #include <util/c_types.h>
16 #include <util/config.h>
17 #include <util/expr_initializer.h>
18 #include <util/namespace.h>
19 #include <util/prefix.h>
20 #include <util/std_code.h>
21 #include <util/std_expr.h>
22 
24 
26  symbol_tablet &symbol_table,
27  const source_locationt &source_location)
28 {
30 
31  const namespacet ns(symbol_table);
32 
33  symbolt &init_symbol = symbol_table.get_writeable_ref(INITIALIZE_FUNCTION);
34 
35  init_symbol.value=code_blockt();
36  init_symbol.value.add_source_location()=source_location;
37 
38  code_blockt &dest=to_code_block(to_code(init_symbol.value));
39 
40  // add the magic label to hide
41  dest.add(code_labelt(CPROVER_PREFIX "HIDE", code_skipt()));
42 
43  // do assignments based on "value"
44 
45  // sort alphabetically for reproducible results
46  std::set<std::string> symbols;
47 
48  for(const auto &symbol_pair : symbol_table.symbols)
49  {
50  symbols.insert(id2string(symbol_pair.first));
51  }
52 
53  for(const std::string &id : symbols)
54  {
55  const symbolt &symbol=ns.lookup(id);
56 
57  const irep_idt &identifier=symbol.name;
58 
59  if(!symbol.is_static_lifetime)
60  continue;
61 
62  if(symbol.is_type || symbol.is_macro)
63  continue;
64 
65  // special values
66  if(identifier==CPROVER_PREFIX "constant_infinity_uint" ||
67  identifier==CPROVER_PREFIX "memory" ||
68  identifier=="__func__" ||
69  identifier=="__FUNCTION__" ||
70  identifier=="__PRETTY_FUNCTION__" ||
71  identifier=="argc'" ||
72  identifier=="argv'" ||
73  identifier=="envp'" ||
74  identifier=="envp_size'")
75  continue;
76 
77  // just for linking
78  if(has_prefix(id, CPROVER_PREFIX "architecture_"))
79  continue;
80 
81  const typet &type=ns.follow(symbol.type);
82 
83  // check type
84  if(type.id()==ID_code ||
85  type.id()==ID_empty)
86  continue;
87 
88  // We won't try to initialize any symbols that have
89  // remained incomplete.
90 
91  if(symbol.value.is_nil() &&
92  symbol.is_extern)
93  // Compilers would usually complain about these
94  // symbols being undefined.
95  continue;
96 
97  if(type.id()==ID_array &&
98  to_array_type(type).size().is_nil())
99  {
100  // C standard 6.9.2, paragraph 5
101  // adjust the type to an array of size 1
102  symbolt &writable_symbol = *symbol_table.get_writeable(identifier);
103  writable_symbol.type = type;
104  writable_symbol.type.set(ID_size, from_integer(1, size_type()));
105  }
106 
107  if(type.id()==ID_incomplete_struct ||
108  type.id()==ID_incomplete_union)
109  continue; // do not initialize
110 
111  if(symbol.value.id()==ID_nondet)
112  continue; // do not initialize
113 
114  exprt rhs;
115 
116  if(symbol.value.is_nil())
117  {
118  const auto zero = zero_initializer(symbol.type, symbol.location, ns);
119  CHECK_RETURN(zero.has_value());
120  rhs = *zero;
121  }
122  else
123  rhs=symbol.value;
124 
125  code_assignt code(symbol.symbol_expr(), rhs);
126  code.add_source_location()=symbol.location;
127 
128  dest.add(code);
129  }
130 
131  // call designated "initialization" functions
132 
133  for(const std::string &id : symbols)
134  {
135  const symbolt &symbol=ns.lookup(id);
136 
137  if(symbol.type.id() != ID_code)
138  continue;
139 
140  const code_typet &code_type = to_code_type(symbol.type);
141  if(
142  code_type.return_type().id() == ID_constructor &&
143  code_type.parameters().empty())
144  {
145  code_function_callt function_call(symbol.symbol_expr());
146  function_call.add_source_location()=source_location;
147  dest.add(function_call);
148  }
149  }
150 }
symbol_table_baset::has_symbol
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
Definition: symbol_table_base.h:78
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:35
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:150
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:438
symbol_tablet
The symbol table.
Definition: symbol_table.h:19
arith_tools.h
symbolt::is_macro
bool is_macro
Definition: symbol.h:61
typet
The type of an expression, extends irept.
Definition: type.h:27
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
prefix.h
exprt
Base class for all expressions.
Definition: expr.h:54
namespace.h
zero_initializer
optionalt< exprt > zero_initializer(const typet &type, const source_locationt &source_location, const namespacet &ns)
Create the equivalent of zero for type type.
Definition: expr_initializer.cpp:322
to_code
const codet & to_code(const exprt &expr)
Definition: std_code.h:136
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:93
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:166
code_function_callt
codet representation of a function call statement.
Definition: std_code.h:1036
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:982
expr_initializer.h
static_lifetime_init
void static_lifetime_init(symbol_tablet &symbol_table, const source_locationt &source_location)
Definition: static_lifetime_init.cpp:25
symbol_table_baset::get_writeable_ref
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
Definition: symbol_table_base.h:110
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
code_labelt
codet representation of a label for branch targets.
Definition: std_code.h:1256
symbolt::symbol_expr
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:121
INITIALIZE_FUNCTION
#define INITIALIZE_FUNCTION
Definition: static_lifetime_init.h:23
code_typet
Base type of functions.
Definition: std_types.h:751
irept::is_nil
bool is_nil() const
Definition: irep.h:172
irept::id
const irep_idt & id() const
Definition: irep.h:259
code_blockt::add
void add(const codet &code)
Definition: std_code.h:189
code_typet::parameters
const parameterst & parameters() const
Definition: std_types.h:893
std_code.h
source_locationt
Definition: source_location.h:20
symbol_tablet::get_writeable
virtual symbolt * get_writeable(const irep_idt &name) override
Find a symbol in the symbol table for read-write access.
Definition: symbol_table.h:93
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
code_skipt
A codet representing a skip statement.
Definition: std_code.h:237
symbolt::is_extern
bool is_extern
Definition: symbol.h:66
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:62
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
symbolt
Symbol table entry.
Definition: symbol.h:27
irept::set
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:286
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:123
symbol_table_baset::symbols
const symbolst & symbols
Definition: symbol_table_base.h:27
symbolt::is_type
bool is_type
Definition: symbol.h:61
CPROVER_PREFIX
#define CPROVER_PREFIX
Definition: cprover_prefix.h:14
to_array_type
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:1048
symbolt::is_static_lifetime
bool is_static_lifetime
Definition: symbol.h:65
goto_functions.h
config.h
code_typet::return_type
const typet & return_type() const
Definition: std_types.h:883
to_code_block
const code_blockt & to_code_block(const codet &code)
Definition: std_code.h:224
has_prefix
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
static_lifetime_init.h
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:233
size_type
unsignedbv_typet size_type()
Definition: c_types.cpp:58
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:256
std_expr.h
c_types.h
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:470