cprover
nondet_static.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Nondeterministically initializes global scope variables, except for
4  constants (such as string literals, final fields) and internal variables
5  (such as CPROVER and symex variables, language specific internal
6  variables).
7 
8 Author: Daniel Kroening, Michael Tautschnig
9 
10 Date: November 2011
11 
12 \*******************************************************************/
13 
19 
20 #include "nondet_static.h"
21 
22 #include <util/prefix.h>
23 
25 
27 
34  const symbol_exprt &symbol_expr,
35  const namespacet &ns)
36 {
37  const irep_idt &id = symbol_expr.get_identifier();
38 
39  // is it a __CPROVER_* variable?
41  return false;
42 
43  // variable not in symbol table such as symex variable?
44  if(!ns.get_symbol_table().has_symbol(id))
45  return false;
46 
47  const symbolt &symbol = ns.lookup(id);
48 
49  // is the symbol explicitly marked as not to be nondet initialized?
50  if(symbol.value.get_bool(ID_C_no_nondet_initialization))
51  {
52  return false;
53  }
54 
55  // is the type explicitly marked as not to be nondet initialized?
56  if(symbol.type.get_bool(ID_C_no_nondet_initialization))
57  return false;
58 
59  // is the type explicitly marked as not to be initialized?
60  if(symbol.type.get_bool(ID_C_no_initialization_required))
61  return false;
62 
63  // static lifetime?
64  if(!symbol.is_static_lifetime)
65  return false;
66 
67  // constant?
68  return !is_constant_or_has_constant_components(symbol_expr.type(), ns) &&
70 }
71 
80  const namespacet &ns,
81  goto_functionst &goto_functions,
82  const irep_idt &fct_name)
83 {
84  goto_functionst::function_mapt::iterator fct_entry =
85  goto_functions.function_map.find(fct_name);
86  CHECK_RETURN(fct_entry != goto_functions.function_map.end());
87 
88  goto_programt &init = fct_entry->second.body;
89 
91  {
92  const goto_programt::instructiont &instruction=*i_it;
93 
94  if(instruction.is_assign())
95  {
96  const symbol_exprt &sym=to_symbol_expr(
97  to_code_assign(instruction.code).lhs());
98 
100  {
101  const goto_programt::instructiont original_instruction = instruction;
103  code_assignt(
104  sym,
106  sym.type(), original_instruction.source_location)),
107  original_instruction.source_location);
108  }
109  }
110  else if(instruction.is_function_call())
111  {
112  const code_function_callt &fct=to_code_function_call(instruction.code);
113  const symbol_exprt &fsym=to_symbol_expr(fct.function());
114 
115  if(has_prefix(id2string(fsym.get_identifier()), "#ini#"))
116  nondet_static(ns, goto_functions, fsym.get_identifier());
117  }
118  }
119 
120  // update counters etc.
121  goto_functions.update();
122 }
123 
128 void nondet_static(const namespacet &ns, goto_functionst &goto_functions)
129 {
130  nondet_static(ns, goto_functions, INITIALIZE_FUNCTION);
131 }
132 
138 void nondet_static(goto_modelt &goto_model)
139 {
140  const namespacet ns(goto_model.symbol_table);
141  nondet_static(ns, goto_model.goto_functions);
142 }
143 
151  goto_modelt &goto_model,
152  const optionst::value_listt &except_values)
153 {
154  const namespacet ns(goto_model.symbol_table);
155  std::set<std::string> to_exclude;
156 
157  for(auto const &except : except_values)
158  {
159  const bool file_prefix_found = except.find(":") != std::string::npos;
160 
161  if(file_prefix_found)
162  {
163  to_exclude.insert(except);
164  if(has_prefix(except, "./"))
165  {
166  to_exclude.insert(except.substr(2, except.length() - 2));
167  }
168  else
169  {
170  to_exclude.insert("./" + except);
171  }
172  }
173  else
174  {
175  irep_idt symbol_name(except);
176  symbolt lookup_results = ns.lookup(symbol_name);
177  to_exclude.insert(
178  id2string(lookup_results.location.get_file()) + ":" + except);
179  }
180  }
181 
182  symbol_tablet &symbol_table = goto_model.symbol_table;
183 
184  for(symbol_tablet::iteratort symbol_it = symbol_table.begin();
185  symbol_it != symbol_table.end();
186  symbol_it++)
187  {
188  symbolt &symbol = symbol_it.get_writeable_symbol();
189  std::string qualified_name = id2string(symbol.location.get_file()) + ":" +
190  id2string(symbol.display_name());
191  if(to_exclude.find(qualified_name) != to_exclude.end())
192  {
193  symbol.value.set(ID_C_no_nondet_initialization, 1);
194  }
195  }
196 
198 }
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
#define CPROVER_PREFIX
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
const irep_idt & get_identifier() const
Definition: std_expr.h:118
exprt value
Initial value of symbol.
Definition: symbol.h:34
function_mapt function_map
typet & type()
Return the type of the expression.
Definition: expr.h:81
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
Symbol table entry.
Definition: symbol.h:27
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:496
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:64
bool is_static_lifetime
Definition: symbol.h:65
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:178
const code_assignt & to_code_assign(const codet &code)
Definition: std_code.h:381
exprt & lhs()
Definition: std_code.h:310
Symbol Table + CFG.
#define INITIALIZE_FUNCTION
Nondeterministically initializes global scope variables, except for constants (such as string literal...
const symbol_table_baset & get_symbol_table() const
Return first symbol table registered with the namespace.
Definition: namespace.h:124
The symbol table.
Definition: symbol_table.h:19
virtual iteratort begin() override
Definition: symbol_table.h:111
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
std::list< std::string > value_listt
Definition: options.h:25
A side_effect_exprt that returns a non-deterministically chosen value.
Definition: std_code.h:1951
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
virtual iteratort end() override
Definition: symbol_table.h:115
codet representation of a function call statement.
Definition: std_code.h:1182
A collection of goto functions.
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:184
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
Definition: dstring.h:36
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:72
const irep_idt & display_name() const
Return language specific display name if present.
Definition: symbol.h:55
typet type
Type of symbol.
Definition: symbol.h:31
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
const irep_idt & get_file() const
exprt & function()
Definition: std_code.h:1218
source_locationt source_location
The location of the instruction in the source file.
Definition: goto_program.h:269
bool is_constant_or_has_constant_components(const typet &type, const namespacet &ns)
Identify whether a given type is constant itself or contains constant components. ...
Definition: std_types.cpp:200
#define Forall_goto_program_instructions(it, program)
Expression to hold a symbol (variable)
Definition: std_expr.h:88
codet code
Do not read or modify directly – use get_X() instead.
Definition: goto_program.h:182
bool is_nondet_initializable_static(const symbol_exprt &symbol_expr, const namespacet &ns)
See the return.
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:140
void nondet_static(const namespacet &ns, goto_functionst &goto_functions, const irep_idt &fct_name)
Nondeterministically initializes global scope variables in a goto-function.
A codet representing an assignment in the program.
Definition: std_code.h:292
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:442
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:1294