CBMC
cover_instrument_other.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_instrument.h"
13 
14 #include <util/cprover_prefix.h>
15 
16 #include <langapi/language_util.h>
17 
18 #include <algorithm>
19 
21  const irep_idt &,
22  goto_programt &,
24  const cover_blocks_baset &,
25  const assertion_factoryt &) const
26 {
27  if(is_non_cover_assertion(i_it))
28  i_it->turn_into_skip();
29 
30  // TODO: implement
31 }
32 
34  const irep_idt &function_id,
35  goto_programt &,
37  const cover_blocks_baset &,
38  const assertion_factoryt &) const
39 {
40  // turn into 'assert(false)' to avoid simplification
41  if(is_non_cover_assertion(i_it))
42  {
43  i_it->condition_nonconst() = false_exprt();
45  i_it->source_location_nonconst(),
46  id2string(i_it->source_location().get_comment()),
47  function_id);
48  }
49 }
50 
52  const irep_idt &function_id,
53  goto_programt &,
55  const cover_blocks_baset &,
56  const assertion_factoryt &make_assertion) const
57 {
58  // turn __CPROVER_cover(x) into 'assert(!x)'
59  if(i_it->is_function_call())
60  {
61  const auto &function = i_it->call_function();
62  if(
63  function.id() == ID_symbol &&
64  to_symbol_expr(function).get_identifier() == CPROVER_PREFIX "cover" &&
65  i_it->call_arguments().size() == 1)
66  {
67  const exprt c = i_it->call_arguments()[0];
68  *i_it = make_assertion(not_exprt(c), i_it->source_location());
69  std::string comment = "condition '" + from_expr(ns, function_id, c) + "'";
71  i_it->source_location_nonconst(), comment, function_id);
72  }
73  }
74  else if(is_non_cover_assertion(i_it))
75  i_it->turn_into_skip();
76 }
77 
79  const irep_idt &function_id,
80  goto_programt &goto_program,
82 {
83  const auto last_function_call = std::find_if(
84  goto_program.instructions.rbegin(),
85  goto_program.instructions.rend(),
86  [](const goto_programt::instructiont &instruction) {
87  return instruction.is_function_call();
88  });
89  INVARIANT(
90  last_function_call != goto_program.instructions.rend(),
91  "Goto program should have at least one function call");
92  INVARIANT(
93  last_function_call != goto_program.instructions.rbegin(),
94  "Goto program shouldn't end with a function call");
95  const auto if_it = last_function_call.base();
96  const auto location = if_it->source_location();
97  const std::string &comment =
98  "additional goal to ensure reachability of end of function";
99  goto_program.insert_before_swap(if_it);
100  source_locationt annotated_location = location;
101  annotated_location.set_comment(comment);
102  annotated_location.set_property_class("reachability_constraint");
103  annotated_location.set_function(function_id);
104  *if_it = make_assertion(false_exprt(), annotated_location);
105 }
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.
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.
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.
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.
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
const source_locationt & source_location() const
Definition: expr.h:231
The Boolean constant false.
Definition: std_expr.h:3082
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:181
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
Boolean negation.
Definition: std_expr.h:2337
void set_comment(const irep_idt &comment)
void set_property_class(const irep_idt &property_class)
void set_function(const irep_idt &function)
Coverage Instrumentation.
void cover_instrument_end_of_function(const irep_idt &function_id, goto_programt &goto_program, const cover_instrumenter_baset::assertion_factoryt &make_assertion)
#define CPROVER_PREFIX
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
std::string from_expr(const namespacet &ns, const irep_idt &identifier, const exprt &expr)
static std::string comment(const rw_set_baset::entryt &entry, bool write)
Definition: race_check.cpp:108
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:272