-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathstatic_analyzer.cpp
237 lines (187 loc) · 5.42 KB
/
static_analyzer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*******************************************************************\
Module:
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#define USE_DEPRECATED_STATIC_ANALYZER_H
#include "static_analyzer.h"
#undef USE_DEPRECATED_STATIC_ANALYZER_H
#include <fstream>
#include <util/threeval.h>
#include <util/json.h>
#include <util/xml.h>
#include <analyses/interval_domain.h>
class static_analyzert:public messaget
{
public:
static_analyzert(
const goto_modelt &_goto_model,
const optionst &_options,
message_handlert &_message_handler):
messaget(_message_handler),
goto_functions(_goto_model.goto_functions),
ns(_goto_model.symbol_table),
options(_options)
{
}
bool operator()();
protected:
const goto_functionst &goto_functions;
const namespacet ns;
const optionst &options;
// analyses
ait<interval_domaint> interval_analysis;
void plain_text_report();
void json_report(const std::string &);
void xml_report(const std::string &);
tvt eval(goto_programt::const_targett);
};
bool static_analyzert::operator()()
{
status() << "performing interval analysis" << eom;
interval_analysis(goto_functions, ns);
if(!options.get_option("json").empty())
json_report(options.get_option("json"));
else if(!options.get_option("xml").empty())
xml_report(options.get_option("xml"));
else
plain_text_report();
return false;
}
tvt static_analyzert::eval(goto_programt::const_targett t)
{
exprt guard=t->guard;
interval_domaint d=interval_analysis[t];
d.assume(not_exprt(guard), ns);
if(d.is_bottom())
return tvt(true);
return tvt::unknown();
}
void static_analyzert::plain_text_report()
{
unsigned pass=0, fail=0, unknown=0;
forall_goto_functions(f_it, goto_functions)
{
if(!f_it->second.body.has_assertion())
continue;
if(f_it->first=="__actual_thread_spawn")
continue;
status() << "******** Function " << f_it->first << eom;
forall_goto_program_instructions(i_it, f_it->second.body)
{
if(!i_it->is_assert())
continue;
tvt r=eval(i_it);
result() << '[' << i_it->source_location.get_property_id()
<< ']' << ' ';
result() << i_it->source_location;
if(!i_it->source_location.get_comment().empty())
result() << ", " << i_it->source_location.get_comment();
result() << ": ";
if(r.is_true())
result() << "SUCCESS";
else if(r.is_false())
result() << "FAILURE";
else
result() << "UNKNOWN";
result() << eom;
if(r.is_true())
pass++;
else if(r.is_false())
fail++;
else
unknown++;
}
status() << '\n';
}
status() << "SUMMARY: " << pass << " pass, " << fail << " fail, "
<< unknown << " unknown\n";
}
void static_analyzert::json_report(const std::string &file_name)
{
json_arrayt json_result;
forall_goto_functions(f_it, goto_functions)
{
if(!f_it->second.body.has_assertion())
continue;
if(f_it->first=="__actual_thread_spawn")
continue;
forall_goto_program_instructions(i_it, f_it->second.body)
{
if(!i_it->is_assert())
continue;
tvt r=eval(i_it);
json_objectt &j=json_result.push_back().make_object();
if(r.is_true())
j["status"]=json_stringt("SUCCESS");
else if(r.is_false())
j["status"]=json_stringt("FAILURE");
else
j["status"]=json_stringt("UNKNOWN");
j["file"] = json_stringt(i_it->source_location.get_file());
j["line"]=json_numbert(id2string(i_it->source_location.get_line()));
j["description"] = json_stringt(i_it->source_location.get_comment());
}
}
std::ofstream out(file_name);
if(!out)
{
error() << "failed to open JSON output file `"
<< file_name << "'" << eom;
return;
}
status() << "Writing report to `" << file_name << "'" << eom;
out << json_result;
}
void static_analyzert::xml_report(const std::string &file_name)
{
xmlt xml_result;
forall_goto_functions(f_it, goto_functions)
{
if(!f_it->second.body.has_assertion())
continue;
if(f_it->first=="__actual_thread_spawn")
continue;
forall_goto_program_instructions(i_it, f_it->second.body)
{
if(!i_it->is_assert())
continue;
tvt r=eval(i_it);
xmlt &x=xml_result.new_element("result");
if(r.is_true())
x.set_attribute("status", "SUCCESS");
else if(r.is_false())
x.set_attribute("status", "FAILURE");
else
x.set_attribute("status", "UNKNOWN");
x.set_attribute("file", id2string(i_it->source_location.get_file()));
x.set_attribute("line", id2string(i_it->source_location.get_line()));
x.set_attribute(
"description", id2string(i_it->source_location.get_comment()));
}
}
std::ofstream out(file_name);
if(!out)
{
error() << "failed to open XML output file `"
<< file_name << "'" << eom;
return;
}
status() << "Writing report to `" << file_name << "'" << eom;
out << xml_result;
}
bool static_analyzer(
const goto_modelt &goto_model,
const optionst &options,
message_handlert &message_handler)
{
return static_analyzert(
goto_model, options, message_handler)();
}
void show_intervals(
const goto_modelt &goto_model,
std::ostream &out)
{
ait<interval_domaint> interval_analysis;
interval_analysis(goto_model);
interval_analysis.output(goto_model, out);
}