cprover
Loading...
Searching...
No Matches
cover_basic_blocks.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Coverage Instrumentation
4
5Author: Peter Schrammel
6
7\*******************************************************************/
8
11
12#include "cover_basic_blocks.h"
13
14#include <util/message.h>
15
17 const goto_programt::const_targett &instruction,
19{
20 if(instruction->incoming_edges.size() != 1)
21 return {};
22
23 const goto_programt::targett in_t = *instruction->incoming_edges.cbegin();
24 if(
25 in_t->is_goto() && !in_t->is_backwards_goto() &&
26 in_t->condition().is_true())
27 return block_map[in_t];
28
29 return {};
30}
31
33{
34 bool next_is_target = true;
35 std::size_t current_block = 0;
36
37 forall_goto_program_instructions(it, goto_program)
38 {
39 // Is it a potential beginning of a block?
40 if(next_is_target || it->is_target())
41 {
43 {
45 }
46 else
47 {
48 block_infos.emplace_back();
49 block_infos.back().representative_inst = it;
50 block_infos.back().source_location = source_locationt::nil();
51 current_block = block_infos.size() - 1;
52 }
53 }
54
56 current_block < block_infos.size(), "current block number out of range");
58
60
62
63 // set representative program location to instrument
64 if(
65 !it->source_location().is_nil() &&
66 !it->source_location().get_file().empty() &&
67 !it->source_location().get_line().empty() &&
68 !it->source_location().is_built_in() &&
69 block_info.source_location.is_nil())
70 {
71 block_info.representative_inst = it; // update
72 block_info.source_location = it->source_location();
73 }
74
76#if 0
77 // Disabled for being too messy
78 it->is_goto() || it->is_function_call() || it->is_assume();
79#else
80 it->is_goto() || it->is_function_call();
81#endif
82 }
83}
84
86{
87 const auto it = block_map.find(t);
88 INVARIANT(it != block_map.end(), "instruction must be part of a block");
89 return it->second;
90}
91
94{
95 INVARIANT(block_nr < block_infos.size(), "block number out of range");
96 return block_infos[block_nr].representative_inst;
97}
98
99const source_locationt &
101{
102 INVARIANT(block_nr < block_infos.size(), "block number out of range");
103 return block_infos[block_nr].source_location;
104}
105
106const source_linest &
108{
109 INVARIANT(block_nr < block_infos.size(), "block number out of range");
110 return block_infos[block_nr].source_lines;
111}
112
114 const irep_idt &function_id,
115 const goto_programt &goto_program,
116 message_handlert &message_handler)
117{
118 messaget msg(message_handler);
119 std::set<std::size_t> blocks_seen;
120 forall_goto_program_instructions(it, goto_program)
121 {
122 const std::size_t block_nr = block_of(it);
124
125 if(
126 blocks_seen.insert(block_nr).second &&
127 block_info.representative_inst == goto_program.instructions.end())
128 {
129 msg.warning() << "Ignoring block " << (block_nr + 1) << " location "
130 << it->location_number << " " << it->source_location()
131 << " (bytecode-index already instrumented)"
132 << messaget::eom;
133 }
134 else if(
135 block_info.representative_inst == it &&
136 block_info.source_location.is_nil())
137 {
138 msg.warning() << "Ignoring block " << (block_nr + 1) << " location "
139 << it->location_number << " " << function_id
140 << " (missing source location)" << messaget::eom;
141 }
142 // The location numbers printed here are those
143 // before the coverage instrumentation.
144 }
145}
146
147void cover_basic_blockst::output(std::ostream &out) const
148{
149 for(const auto &block_pair : block_map)
150 out << block_pair.first->source_location() << " -> " << block_pair.second
151 << '\n';
152}
153
156 const goto_programt::instructiont &instruction)
157{
158 const auto &add_location = [&](const source_locationt &location) {
159 const irep_idt &line = location.get_line();
160 if(!line.empty())
161 {
162 block.source_lines.insert(location);
163 }
164 };
165 add_location(instruction.source_location());
166 instruction.code().visit_pre([&](const exprt &expr) {
167 const auto &location = expr.source_location();
168 if(!location.get_function().empty())
169 add_location(location);
170 });
171}
172
175{
177 {
178 const auto &location = it->source_location();
179 const auto &bytecode_index = location.get_java_bytecode_index();
180 auto entry = index_to_block.emplace(bytecode_index, block_infos.size());
181 if(entry.second)
182 {
183 block_infos.push_back(it);
184 block_locations.push_back(location);
185 block_source_lines.emplace_back(location);
186 }
187 else
188 {
189 block_source_lines[entry.first->second].insert(location);
190 }
191 }
192}
193
194std::size_t
196{
197 const auto &bytecode_index = t->source_location().get_java_bytecode_index();
198 const auto it = index_to_block.find(bytecode_index);
199 INVARIANT(it != index_to_block.end(), "instruction must be part of a block");
200 return it->second;
201}
202
205{
207 return block_infos[block_nr];
208}
209
210const source_locationt &
216
217const source_linest &
223
224void cover_basic_blocks_javat::output(std::ostream &out) const
225{
226 for(std::size_t i = 0; i < block_locations.size(); ++i)
227 out << block_locations[i] << " -> " << i << '\n';
228}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:563
cover_basic_blocks_javat(const goto_programt &_goto_program)
std::vector< goto_programt::const_targett > block_infos
const source_locationt & source_location_of(std::size_t block_number) const override
std::vector< source_locationt > block_locations
void output(std::ostream &out) const override
Outputs the list of blocks.
std::size_t block_of(goto_programt::const_targett t) const override
const source_linest & source_lines_of(std::size_t block_number) const override
std::vector< source_linest > block_source_lines
optionalt< goto_programt::const_targett > instruction_of(std::size_t block_number) const override
std::unordered_map< irep_idt, std::size_t > index_to_block
void output(std::ostream &out) const override
Outputs the list of blocks.
cover_basic_blockst(const goto_programt &goto_program)
static optionalt< std::size_t > continuation_of_block(const goto_programt::const_targett &instruction, block_mapt &block_map)
If this block is a continuation of a previous block through unconditional forward gotos,...
optionalt< goto_programt::const_targett > instruction_of(std::size_t block_nr) const override
std::map< goto_programt::const_targett, std::size_t, goto_programt::target_less_than > block_mapt
std::vector< block_infot > block_infos
map block numbers to block information
void report_block_anomalies(const irep_idt &function_id, const goto_programt &goto_program, message_handlert &message_handler) override
Output warnings about ignored blocks.
const source_linest & source_lines_of(std::size_t block_nr) const override
std::size_t block_of(goto_programt::const_targett t) const override
static void add_block_lines(cover_basic_blockst::block_infot &block, const goto_programt::instructiont &instruction)
Adds the lines which instruction spans to block.
const source_locationt & source_location_of(std::size_t block_nr) const override
block_mapt block_map
map program locations to block numbers
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:39
bool empty() const
Definition dstring.h:90
Base class for all expressions.
Definition expr.h:56
const source_locationt & source_location() const
Definition expr.h:223
This class represents an instruction in the GOTO intermediate representation.
const goto_instruction_codet & code() const
Get the code represented by this instruction.
const source_locationt & source_location() const
A generic container class for the GOTO intermediate representation of one function.
instructionst instructions
The list of instructions in the goto program.
instructionst::iterator targett
instructionst::const_iterator const_targett
source_locationt source_location
Definition message.h:247
Class that provides messages with a built-in verbosity 'level'.
Definition message.h:155
mstreamt & warning() const
Definition message.h:404
static eomt eom
Definition message.h:297
void insert(const source_locationt &loc)
Insert a line (a source location) into the set of lines.
static const source_locationt & nil()
Basic blocks detection for Coverage Instrumentation.
#define forall_goto_program_instructions(it, program)
#define PRECONDITION(CONDITION)
Definition invariant.h:463
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition invariant.h:423
source_linest source_lines
the set of source code lines belonging to this block