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 type explicitly marked as not to be nondet initialized?
50  if(symbol.type.get_bool(ID_C_no_nondet_initialization))
51  return false;
52 
53  // is the type explicitly marked as not to be initialized?
54  if(symbol.type.get_bool(ID_C_no_initialization_required))
55  return false;
56 
57  // static lifetime?
58  if(!symbol.is_static_lifetime)
59  return false;
60 
61  // constant?
62  return !is_constant_or_has_constant_components(symbol_expr.type(), ns) &&
64 }
65 
74  const namespacet &ns,
75  goto_functionst &goto_functions,
76  const irep_idt &fct_name)
77 {
78  goto_functionst::function_mapt::iterator fct_entry =
79  goto_functions.function_map.find(fct_name);
80  CHECK_RETURN(fct_entry != goto_functions.function_map.end());
81 
82  goto_programt &init = fct_entry->second.body;
83 
85  {
86  const goto_programt::instructiont &instruction=*i_it;
87 
88  if(instruction.is_assign())
89  {
90  const symbol_exprt &sym=to_symbol_expr(
91  to_code_assign(instruction.code).lhs());
92 
94  {
95  const goto_programt::instructiont original_instruction = instruction;
98  sym,
100  sym.type(), original_instruction.source_location)),
101  original_instruction.source_location);
102  }
103  }
104  else if(instruction.is_function_call())
105  {
106  const code_function_callt &fct=to_code_function_call(instruction.code);
107  const symbol_exprt &fsym=to_symbol_expr(fct.function());
108 
109  if(has_prefix(id2string(fsym.get_identifier()), "#ini#"))
110  nondet_static(ns, goto_functions, fsym.get_identifier());
111  }
112  }
113 }
114 
120  const namespacet &ns,
121  goto_functionst &goto_functions)
122 {
123  nondet_static(ns, goto_functions, INITIALIZE_FUNCTION);
124 
125  // update counters etc.
126  goto_functions.update();
127 }
128 
134 void nondet_static(goto_modelt &goto_model)
135 {
136  const namespacet ns(goto_model.symbol_table);
137  nondet_static(ns, goto_model.goto_functions);
138 }
const std::string & id2string(const irep_idt &d)
Definition: irep.h:42
#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:144
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:470
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:102
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:386
exprt & lhs()
Definition: std_code.h:315
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
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
A side_effect_exprt that returns a non-deterministically chosen value.
Definition: std_code.h:1931
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
codet representation of a function call statement.
Definition: std_code.h:1129
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:232
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
typet type
Type of symbol.
Definition: symbol.h:31
exprt & function()
Definition: std_code.h:1173
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:102
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:297
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:1249