ESBMC
Efficient SMT-based Context-Bounded Model Checker
Loading...
Searching...
No Matches
static_analysis.h
Go to the documentation of this file.
1#ifndef CPROVER_GOTO_PROGRAMS_STATIC_ANALYSIS_H
2#define CPROVER_GOTO_PROGRAMS_STATIC_ANALYSIS_H
3
5
6#include <map>
7#include <irep2/irep2.h>
8
9// don't use me -- I am just a base class
10// please derive from me
12{
13public:
17
19
20 virtual void initialize(const namespacet &ns, locationt l) = 0;
21
22 // how function calls are treated:
23 // a) there is an edge from each call site to the function head
24 // b) there is an edge from each return to the last instruction (END_FUNCTION)
25 // of each function
26 // c) there is an edge from the last instruction of the function
27 // to the instruction following the call site
28 // (for setting the LHS)
29
30 virtual void
31 transform(const namespacet &ns, locationt from, locationt to) = 0;
32
33 virtual ~abstract_domain_baset() = default;
34
35 virtual void output(const namespacet &, std::ostream &) const
36 {
37 }
38
39 virtual void
40 get_reference_set(const namespacet &, const expr2tc &, std::list<expr2tc> &)
41 {
42 assert(0);
43 };
44
45 // also add
46 //
47 // bool merge(const T &b);
48 //
49 // returns true iff there is s.th. new
50
51protected:
52 bool seen;
53
55
56 // utilities
57
58 // get guard of a conditional edge
59 expr2tc get_guard(locationt from, locationt to) const;
60
61 // get lhs that return value is assigned to
62 // for an edge that returns from a function
64};
65
66// don't use me -- I am just a base class
67// use static_analysist instead
69{
70public:
73
75 {
76 }
77
78 virtual void initialize(const goto_programt &goto_program)
79 {
80 if (!initialized)
81 {
82 initialized = true;
83 generate_states(goto_program);
84 }
85 }
86
87 virtual void initialize(const goto_functionst &goto_functions)
88 {
89 if (!initialized)
90 {
91 initialized = true;
92 generate_states(goto_functions);
93 }
94 }
95
96 virtual void update(const goto_programt &goto_program);
97 virtual void update(const goto_functionst &goto_functions);
98
99 virtual void operator()(const goto_programt &goto_program);
100
101 virtual void operator()(const goto_functionst &goto_functions);
102
103 virtual ~static_analysis_baset() = default;
104
105 virtual void clear()
106 {
107 initialized = false;
108 }
109
110 virtual void
111 output(const goto_functionst &goto_functions, std::ostream &out) const;
112
113 void output(const goto_programt &goto_program, std::ostream &out) const
114 {
115 output(goto_program, "", out);
116 }
117
118 virtual bool has_location(locationt l) const = 0;
119
121 {
123 }
124
125protected:
127
128 virtual void output(
129 const goto_programt &goto_program,
130 const irep_idt &identifier,
131 std::ostream &out) const;
132
133 typedef std::map<unsigned, locationt> working_sett;
134
135 locationt get_next(working_sett &working_set);
136
138 {
139 working_set.insert(std::pair<unsigned, locationt>(l->location_number, l));
140 }
141
142 // true = found s.th. new
143 bool fixedpoint(
144 const goto_programt &goto_program,
145 const goto_functionst &goto_functions);
146
147 bool fixedpoint(
148 goto_functionst::function_mapt::const_iterator it,
149 const goto_functionst &goto_functions);
150
151 void fixedpoint(const goto_functionst &goto_functions);
152
153 // true = found s.th. new
154 bool visit(
155 locationt l,
156 working_sett &working_set,
157 const goto_programt &goto_program,
158 const goto_functionst &goto_functions);
159
161 {
162 l++;
163 return l;
164 }
165
166 virtual bool merge(statet &a, const statet &b, bool keepnew = false) = 0;
167
168 typedef std::set<irep_idt> functions_donet;
170
171 typedef std::set<irep_idt> recursion_sett;
173
174 void generate_states(const goto_functionst &goto_functions);
175
176 void generate_states(const goto_programt &goto_program);
177
179
180 // function calls
182 locationt l_call,
183 const expr2tc &function,
184 const std::vector<expr2tc> &arguments,
185 statet &new_state,
186 const goto_functionst &goto_functions);
187
188 void do_function_call(
189 locationt l_call,
190 const goto_functionst &goto_functions,
191 const goto_functionst::function_mapt::const_iterator f_it,
192 const std::vector<expr2tc> &arguments,
193 statet &new_state);
194
195 // abstract methods
196
197 virtual void generate_state(locationt l) = 0;
198 virtual statet &get_state(locationt l) = 0;
199 virtual const statet &get_state(locationt l) const = 0;
201
202 virtual void get_reference_set(
203 locationt l,
204 const expr2tc &expr,
205 std::list<expr2tc> &dest) = 0;
206};
207
208// T is expected to be derived from abstract_domain_baset
209template <typename T>
211{
212public:
213 // constructor
215 {
216 }
217
219
221 {
222 typename state_mapt::iterator it = state_map.find(l);
223 if (it == state_map.end())
224 throw "failed to find state";
225 return it->second;
226 }
227
228 inline const T &operator[](locationt l) const
229 {
230 typename state_mapt::const_iterator it = state_map.find(l);
231 if (it == state_map.end())
232 throw "failed to find state";
233 return it->second;
234 }
235
236 void clear() override
237 {
238 state_map.clear();
240 }
241
242 bool has_location(locationt l) const override
243 {
244 return state_map.count(l) != 0;
245 }
246
247protected:
248 typedef std::map<locationt, T> state_mapt;
250
252 {
253 typename state_mapt::iterator it = state_map.find(l);
254 if (it == state_map.end())
255 throw "failed to find state";
256 return it->second;
257 }
258
259 const statet &get_state(locationt l) const override
260 {
261 typename state_mapt::const_iterator it = state_map.find(l);
262 if (it == state_map.end())
263 throw "failed to find state";
264 return it->second;
265 }
266
267 bool merge(statet &a, const statet &b, bool keepnew = false) override
268 {
269 return static_cast<T &>(a).merge(static_cast<const T &>(b), keepnew);
270 }
271
273 {
274 return new T(static_cast<T &>(s));
275 }
276
277 void generate_state(locationt l) override
278 {
279 state_map[l].initialize(ns, l);
280 }
281
283 locationt l,
284 const expr2tc &expr,
285 std::list<expr2tc> &dest) override
286 {
287 state_map[l].get_reference_set(ns, expr, dest);
288 }
289};
290
291#endif
Definition static_analysis.h:12
virtual void initialize(const namespacet &ns, locationt l)=0
friend class static_analysis_baset
Definition static_analysis.h:54
goto_programt::const_targett locationt
Definition static_analysis.h:18
expr2tc get_guard(locationt from, locationt to) const
Definition static_analysis.cpp:9
virtual void transform(const namespacet &ns, locationt from, locationt to)=0
expr2tc get_return_lhs(locationt to) const
Definition static_analysis.cpp:26
bool seen
Definition static_analysis.h:52
virtual ~abstract_domain_baset()=default
virtual void get_reference_set(const namespacet &, const expr2tc &, std::list< expr2tc > &)
Definition static_analysis.h:40
abstract_domain_baset()
Definition static_analysis.h:14
virtual void output(const namespacet &, std::ostream &) const
Definition static_analysis.h:35
Definition goto_functions.h:56
A generic container class for a control flow graph for one function, in the form of a goto-program.
Definition goto_program.h:63
instructionst::const_iterator const_targett
Definition goto_program.h:591
Definition irep_idt.h:12
Definition location.h:7
Definition namespace.h:10
const namespacet & ns
Definition static_analysis.h:126
abstract_domain_baset statet
Definition static_analysis.h:71
void do_function_call(locationt l_call, const goto_functionst &goto_functions, const goto_functionst::function_mapt::const_iterator f_it, const std::vector< expr2tc > &arguments, statet &new_state)
Definition static_analysis.cpp:227
void generate_states(const goto_functionst &goto_functions)
Definition static_analysis.cpp:89
void insert(locationt l)
Definition static_analysis.h:120
void output(const goto_programt &goto_program, std::ostream &out) const
Definition static_analysis.h:113
locationt get_next(working_sett &working_set)
Definition static_analysis.cpp:130
virtual bool has_location(locationt l) const =0
virtual bool merge(statet &a, const statet &b, bool keepnew=false)=0
void put_in_working_set(working_sett &working_set, locationt l)
Definition static_analysis.h:137
virtual void get_reference_set(locationt l, const expr2tc &expr, std::list< expr2tc > &dest)=0
std::set< irep_idt > functions_donet
Definition static_analysis.h:168
std::set< irep_idt > recursion_sett
Definition static_analysis.h:171
virtual void generate_state(locationt l)=0
void do_function_call_rec(locationt l_call, const expr2tc &function, const std::vector< expr2tc > &arguments, statet &new_state, const goto_functionst &goto_functions)
Definition static_analysis.cpp:293
virtual statet * make_temporary_state(statet &s)=0
virtual void update(const goto_programt &goto_program)
Definition static_analysis.cpp:108
static locationt successor(locationt l)
Definition static_analysis.h:160
functions_donet functions_done
Definition static_analysis.h:169
virtual ~static_analysis_baset()=default
virtual statet & get_state(locationt l)=0
virtual void initialize(const goto_programt &goto_program)
Definition static_analysis.h:78
std::map< unsigned, locationt > working_sett
Definition static_analysis.h:133
virtual const statet & get_state(locationt l) const =0
static_analysis_baset(const namespacet &_ns)
Definition static_analysis.h:74
bool fixedpoint(const goto_programt &goto_program, const goto_functionst &goto_functions)
Definition static_analysis.cpp:141
bool initialized
Definition static_analysis.h:178
bool visit(locationt l, working_sett &working_set, const goto_programt &goto_program, const goto_functionst &goto_functions)
Definition static_analysis.cpp:165
virtual void initialize(const goto_functionst &goto_functions)
Definition static_analysis.h:87
goto_programt::const_targett locationt
Definition static_analysis.h:72
virtual void clear()
Definition static_analysis.h:105
virtual void output(const goto_functionst &goto_functions, std::ostream &out) const
Definition static_analysis.cpp:56
recursion_sett recursion_set
Definition static_analysis.h:172
virtual void operator()(const goto_programt &goto_program)
Definition static_analysis.cpp:49
static_analysist(const namespacet &_ns)
Definition static_analysis.h:214
T & operator[](locationt l)
Definition static_analysis.h:220
std::map< locationt, T > state_mapt
Definition static_analysis.h:248
void get_reference_set(locationt l, const expr2tc &expr, std::list< expr2tc > &dest) override
Definition static_analysis.h:282
void generate_state(locationt l) override
Definition static_analysis.h:277
state_mapt state_map
Definition static_analysis.h:249
goto_programt::const_targett locationt
Definition static_analysis.h:218
const T & operator[](locationt l) const
Definition static_analysis.h:228
const statet & get_state(locationt l) const override
Definition static_analysis.h:259
statet * make_temporary_state(statet &s) override
Definition static_analysis.h:272
statet & get_state(locationt l) override
Definition static_analysis.h:251
bool merge(statet &a, const statet &b, bool keepnew=false) override
Definition static_analysis.h:267
void clear() override
Definition static_analysis.h:236
bool has_location(locationt l) const override
Definition static_analysis.h:242
#define T
Definition exp2f.c:24
Classes and definitions for non-stringy internal representation.
irep_container< expr2t > expr2tc
Definition irep2.h:431
@ identifier
Definition nested_func_transform.cpp:62
#define false
Definition stdbool.h:10
Definition module.h:12