CBMC
cover_instrument_branch.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Coverage Instrumentation
4 
5 Author: Daniel Kroening
6 
7 \*******************************************************************/
8 
11 
12 #include "cover_basic_blocks.h"
13 #include "cover_filter.h"
14 #include "cover_instrument.h"
15 
17  const irep_idt &function_id,
18  goto_programt &goto_program,
20  const cover_blocks_baset &basic_blocks,
21  const assertion_factoryt &make_assertion) const
22 {
23  if(is_non_cover_assertion(i_it))
24  i_it->turn_into_skip();
25 
26  const bool is_function_entry_point =
27  i_it == goto_program.instructions.begin();
28  const bool is_conditional_goto =
29  i_it->is_goto() && !i_it->condition().is_true();
30  if(!is_function_entry_point && !is_conditional_goto)
31  return;
32 
33  if(!goal_filters(i_it->source_location()))
34  return;
35 
36  if(is_function_entry_point)
37  {
38  // we want branch coverage to imply 'entry point of function'
39  // coverage
40  std::string comment = "entry point";
41 
42  source_locationt source_location = i_it->source_location();
43  initialize_source_location(source_location, comment, function_id);
44  goto_program.insert_before(
45  i_it, make_assertion(false_exprt(), source_location));
46  }
47 
48  if(is_conditional_goto)
49  {
50  std::string b =
51  std::to_string(basic_blocks.block_of(i_it) + 1); // start with 1
52  std::string true_comment = "block " + b + " branch true";
53  std::string false_comment = "block " + b + " branch false";
54 
55  exprt guard = i_it->condition();
56  source_locationt source_location = i_it->source_location();
57 
58  goto_program.insert_before_swap(i_it);
59  initialize_source_location(source_location, true_comment, function_id);
60  *i_it = make_assertion(not_exprt(guard), source_location);
61 
62  goto_program.insert_before_swap(i_it);
63  initialize_source_location(source_location, false_comment, function_id);
64  *i_it = make_assertion(guard, source_location);
65 
66  std::advance(i_it, 2);
67  }
68 }
static exprt guard(const exprt::operandst &guards, exprt cond)
virtual std::size_t block_of(goto_programt::const_targett t) const =0
void instrument(const irep_idt &function_id, goto_programt &, goto_programt::targett &, const cover_blocks_baset &, const assertion_factoryt &) const override
Override this method to implement an instrumenter.
const goal_filterst & goal_filters
bool is_non_cover_assertion(goto_programt::const_targett t) const
void initialize_source_location(source_locationt &source_location, const std::string &comment, const irep_idt &function_id) const
std::function< goto_programt::instructiont(const exprt &, const source_locationt &)> assertion_factoryt
The type of function used to make goto_program assertions.
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
The Boolean constant false.
Definition: std_expr.h:3072
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:73
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:622
void insert_before_swap(targett target)
Insertion that preserves jumps to "target".
Definition: goto_program.h:643
instructionst::iterator targett
Definition: goto_program.h:614
targett insert_before(const_targett target)
Insertion before the instruction pointed-to by the given instruction iterator target.
Definition: goto_program.h:692
Boolean negation.
Definition: std_expr.h:2327
Basic blocks detection for Coverage Instrumentation.
Filters for the Coverage Instrumentation.
Coverage Instrumentation.
static std::string comment(const rw_set_baset::entryt &entry, bool write)
Definition: race_check.cpp:108
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.