-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathunreachable_instructions.cpp
460 lines (383 loc) · 12.3 KB
/
unreachable_instructions.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*******************************************************************\
Module: List all unreachable instructions
Author: Michael Tautschnig
Date: April 2016
\*******************************************************************/
/// \file
/// List all unreachable instructions
#include "unreachable_instructions.h"
#include <util/file_util.h>
#include <util/json_irep.h>
#include <util/options.h>
#include <util/xml.h>
#include <goto-programs/compute_called_functions.h>
#include <analyses/ai.h>
#include <analyses/cfg_dominators.h>
typedef std::map<unsigned, goto_programt::const_targett> dead_mapt;
static void unreachable_instructions(
const goto_programt &goto_program,
dead_mapt &dest)
{
cfg_dominatorst dominators;
dominators(goto_program);
for(cfg_dominatorst::cfgt::entry_mapt::const_iterator
it=dominators.cfg.entry_map.begin();
it!=dominators.cfg.entry_map.end();
++it)
{
const cfg_dominatorst::cfgt::nodet &n=dominators.cfg[it->second];
if(n.dominators.empty())
dest.insert(std::make_pair(it->first->location_number,
it->first));
}
}
static void all_unreachable(
const goto_programt &goto_program,
dead_mapt &dest)
{
forall_goto_program_instructions(it, goto_program)
if(!it->is_end_function())
dest.insert(std::make_pair(it->location_number, it));
}
static void build_dead_map_from_ai(
const goto_programt &goto_program,
const ai_baset &ai,
dead_mapt &dest)
{
forall_goto_program_instructions(it, goto_program)
if(ai.abstract_state_before(it)->is_bottom())
dest.insert(std::make_pair(it->location_number, it));
}
static void output_dead_plain(
const namespacet &ns,
const irep_idt &function_identifier,
const goto_programt &goto_program,
const dead_mapt &dead_map,
std::ostream &os)
{
os << "\n*** " << function_identifier << " ***\n";
for(dead_mapt::const_iterator it=dead_map.begin();
it!=dead_map.end();
++it)
goto_program.output_instruction(ns, function_identifier, os, *it->second);
}
static void add_to_xml(
const irep_idt &function_identifier,
const goto_programt &goto_program,
const dead_mapt &dead_map,
xmlt &dest)
{
xmlt &x = dest.new_element("function");
x.set_attribute("name", id2string(function_identifier));
for(dead_mapt::const_iterator it=dead_map.begin();
it!=dead_map.end();
++it)
{
xmlt &inst = x.new_element("instruction");
inst.set_attribute("location_number",
std::to_string(it->second->location_number));
inst.set_attribute("source_location",
it->second->source_location.as_string());
}
return;
}
static void add_to_json(
const namespacet &ns,
const irep_idt &function_identifier,
const goto_programt &goto_program,
const dead_mapt &dead_map,
json_arrayt &dest)
{
PRECONDITION(!goto_program.instructions.empty());
goto_programt::const_targett end_function=
goto_program.instructions.end();
--end_function;
DATA_INVARIANT(end_function->is_end_function(),
"The last instruction in a goto-program must be END_FUNCTION");
json_objectt entry{
{"function", json_stringt(function_identifier)},
{"fileName",
json_stringt(concat_dir_file(
id2string(end_function->source_location.get_working_directory()),
id2string(end_function->source_location.get_file())))}};
json_arrayt &dead_ins=entry["unreachableInstructions"].make_array();
for(dead_mapt::const_iterator it=dead_map.begin();
it!=dead_map.end();
++it)
{
std::ostringstream oss;
goto_program.output_instruction(ns, function_identifier, oss, *it->second);
std::string s=oss.str();
std::string::size_type n=s.find('\n');
assert(n!=std::string::npos);
s.erase(0, n+1);
n=s.find_first_not_of(' ');
assert(n!=std::string::npos);
s.erase(0, n);
assert(!s.empty());
s.erase(s.size()-1);
// print info for file actually with full path
const source_locationt &l=it->second->source_location;
json_objectt i_entry{{"sourceLocation", json(l)},
{"statement", json_stringt(s)}};
dead_ins.push_back(std::move(i_entry));
}
dest.push_back(std::move(entry));
}
void unreachable_instructions(
const goto_modelt &goto_model,
const bool json,
std::ostream &os)
{
json_arrayt json_result;
std::unordered_set<irep_idt> called = compute_called_functions(goto_model);
const namespacet ns(goto_model.symbol_table);
forall_goto_functions(f_it, goto_model.goto_functions)
{
if(!f_it->second.body_available())
continue;
const goto_programt &goto_program=f_it->second.body;
dead_mapt dead_map;
const symbolt &decl=ns.lookup(f_it->first);
// f_it->first may be a link-time renamed version, use the
// base_name instead; do not list inlined functions
if(
called.find(decl.base_name) != called.end() ||
to_code_type(decl.type).get_inlined())
{
unreachable_instructions(goto_program, dead_map);
}
else
all_unreachable(goto_program, dead_map);
if(!dead_map.empty())
{
if(!json)
output_dead_plain(ns, f_it->first, goto_program, dead_map, os);
else
add_to_json(ns, f_it->first, goto_program, dead_map, json_result);
}
}
if(json && !json_result.empty())
os << json_result << '\n';
}
bool static_unreachable_instructions(
const goto_modelt &goto_model,
const ai_baset &ai,
const optionst &options,
std::ostream &out)
{
json_arrayt json_result;
xmlt xml_result("unreachable-instructions");
const namespacet ns(goto_model.symbol_table);
forall_goto_functions(f_it, goto_model.goto_functions)
{
if(!f_it->second.body_available())
continue;
const goto_programt &goto_program=f_it->second.body;
dead_mapt dead_map;
build_dead_map_from_ai(goto_program, ai, dead_map);
if(!dead_map.empty())
{
if(options.get_bool_option("json"))
{
add_to_json(ns, f_it->first, f_it->second.body, dead_map, json_result);
}
else if(options.get_bool_option("xml"))
{
add_to_xml(f_it->first, f_it->second.body, dead_map, xml_result);
}
else
{
// text or console
output_dead_plain(ns, f_it->first, f_it->second.body, dead_map, out);
}
}
}
if(options.get_bool_option("json") && !json_result.empty())
out << json_result << '\n';
else if(options.get_bool_option("xml"))
out << xml_result << '\n';
return false;
}
static void json_output_function(
const irep_idt &function,
const source_locationt &first_location,
const source_locationt &last_location,
json_arrayt &dest)
{
// source locations occassionally omit line numbers
// an empty string makes json unparsable
std::string first_line = id2string(first_location.get_line());
std::string last_line = id2string(last_location.get_line());
first_line = first_line.empty() ? "0" : first_line;
last_line = last_line.empty() ? "0" : last_line;
json_objectt entry{{"function", json_stringt(function)},
{"file name",
json_stringt(concat_dir_file(
id2string(first_location.get_working_directory()),
id2string(first_location.get_file())))},
{"first line", json_numbert(first_line)},
{"last line", json_numbert(last_line)}};
dest.push_back(std::move(entry));
}
static void xml_output_function(
const irep_idt &function,
const source_locationt &first_location,
const source_locationt &last_location,
xmlt &dest)
{
xmlt &x=dest.new_element("function");
// source locations occassionally omit line numbers
// an empty string makes xml unparsable
std::string first_line = id2string(first_location.get_line());
std::string last_line = id2string(last_location.get_line());
first_line = first_line.empty() ? "0" : first_line;
last_line = last_line.empty() ? "0" : last_line;
x.set_attribute("name", id2string(function));
x.set_attribute("file name",
concat_dir_file(
id2string(first_location.get_working_directory()),
id2string(first_location.get_file())));
x.set_attribute("first line", first_line);
x.set_attribute("last line", last_line);
}
static void list_functions(
const goto_modelt &goto_model,
const std::unordered_set<irep_idt> &called,
const optionst &options,
std::ostream &os,
bool unreachable)
{
json_arrayt json_result;
xmlt xml_result(unreachable ?
"unreachable-functions" :
"reachable-functions");
const namespacet ns(goto_model.symbol_table);
forall_goto_functions(f_it, goto_model.goto_functions)
{
const symbolt &decl=ns.lookup(f_it->first);
// f_it->first may be a link-time renamed version, use the
// base_name instead; do not list inlined functions
if(
unreachable == (called.find(decl.base_name) != called.end() ||
to_code_type(decl.type).get_inlined()))
{
continue;
}
source_locationt first_location=decl.location;
source_locationt last_location;
if(f_it->second.body_available())
{
const goto_programt &goto_program=f_it->second.body;
goto_programt::const_targett end_function=
goto_program.instructions.end();
// find the last instruction with a line number
// TODO(tautschnig): #918 will eventually ensure that every instruction
// has such
do
{
--end_function;
last_location = end_function->source_location;
}
while(
end_function != goto_program.instructions.begin() &&
last_location.get_line().empty());
if(last_location.get_line().empty())
last_location = decl.location;
}
else
// completely ignore functions without a body, both for
// reachable and unreachable functions; we could also restrict
// this to macros/asm renaming
continue;
if(options.get_bool_option("json"))
{
json_output_function(
decl.base_name,
first_location,
last_location,
json_result);
}
else if(options.get_bool_option("xml"))
{
xml_output_function(
decl.base_name,
first_location,
last_location,
xml_result);
}
else
{
// text or console
os << concat_dir_file(
id2string(first_location.get_working_directory()),
id2string(first_location.get_file())) << " "
<< decl.base_name << " "
<< first_location.get_line() << " "
<< last_location.get_line() << "\n";
}
}
if(options.get_bool_option("json") && !json_result.empty())
os << json_result << '\n';
else if(options.get_bool_option("xml"))
os << xml_result << '\n';
}
void unreachable_functions(
const goto_modelt &goto_model,
const bool json,
std::ostream &os)
{
optionst options;
if(json)
options.set_option("json", true);
std::unordered_set<irep_idt> called = compute_called_functions(goto_model);
list_functions(goto_model, called, options, os, true);
}
void reachable_functions(
const goto_modelt &goto_model,
const bool json,
std::ostream &os)
{
optionst options;
if(json)
options.set_option("json", true);
std::unordered_set<irep_idt> called = compute_called_functions(goto_model);
list_functions(goto_model, called, options, os, false);
}
std::unordered_set<irep_idt> compute_called_functions_from_ai(
const goto_modelt &goto_model,
const ai_baset &ai)
{
std::unordered_set<irep_idt> called;
forall_goto_functions(f_it, goto_model.goto_functions)
{
if(!f_it->second.body_available())
continue;
const goto_programt &p = f_it->second.body;
if(!ai.abstract_state_before(p.instructions.begin())->is_bottom())
called.insert(f_it->first);
}
return called;
}
bool static_unreachable_functions(
const goto_modelt &goto_model,
const ai_baset &ai,
const optionst &options,
std::ostream &out)
{
std::unordered_set<irep_idt> called =
compute_called_functions_from_ai(goto_model, ai);
list_functions(goto_model, called, options, out, true);
return false;
}
bool static_reachable_functions(
const goto_modelt &goto_model,
const ai_baset &ai,
const optionst &options,
std::ostream &out)
{
std::unordered_set<irep_idt> called =
compute_called_functions_from_ai(goto_model, ai);
list_functions(goto_model, called, options, out, false);
return false;
}