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, id2string(i_it->source_location().get_comment()), function_id);
46  }
47 }
48 
50  const irep_idt &function_id,
51  goto_programt &,
53  const cover_blocks_baset &,
54  const assertion_factoryt &make_assertion) const
55 {
56  // turn __CPROVER_cover(x) into 'assert(!x)'
57  if(i_it->is_function_call())
58  {
59  const auto &function = i_it->call_function();
60  if(
61  function.id() == ID_symbol &&
62  to_symbol_expr(function).get_identifier() == CPROVER_PREFIX "cover" &&
63  i_it->call_arguments().size() == 1)
64  {
65  const exprt c = i_it->call_arguments()[0];
66  *i_it = make_assertion(not_exprt(c), i_it->source_location());
67  std::string comment = "condition '" + from_expr(ns, function_id, c) + "'";
68  initialize_source_location(i_it, comment, function_id);
69  }
70  }
71  else if(is_non_cover_assertion(i_it))
72  i_it->turn_into_skip();
73 }
74 
76  const irep_idt &function_id,
77  goto_programt &goto_program,
79 {
80  const auto last_function_call = std::find_if(
81  goto_program.instructions.rbegin(),
82  goto_program.instructions.rend(),
83  [](const goto_programt::instructiont &instruction) {
84  return instruction.is_function_call();
85  });
86  INVARIANT(
87  last_function_call != goto_program.instructions.rend(),
88  "Goto program should have at least one function call");
89  INVARIANT(
90  last_function_call != goto_program.instructions.rbegin(),
91  "Goto program shouldn't end with a function call");
92  const auto if_it = last_function_call.base();
93  const auto location = if_it->source_location();
94  const std::string &comment =
95  "additional goal to ensure reachability of end of function";
96  goto_program.insert_before_swap(if_it);
97  *if_it = make_assertion(false_exprt(), location);
98  if_it->source_location_nonconst().set_comment(comment);
99  if_it->source_location_nonconst().set_property_class(
100  "reachability_constraint");
101  if_it->source_location_nonconst().set_function(function_id);
102 }
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
cover_instrumenter_baset::is_non_cover_assertion
bool is_non_cover_assertion(goto_programt::const_targett t) const
Definition: cover_instrument.h:95
cover_instrumenter_baset::initialize_source_location
void initialize_source_location(goto_programt::targett t, const std::string &comment, const irep_idt &function_id) const
Definition: cover_instrument.h:83
cover_instrument_end_of_function
void cover_instrument_end_of_function(const irep_idt &function_id, goto_programt &goto_program, const cover_instrumenter_baset::assertion_factoryt &make_assertion)
Definition: cover_instrument_other.cpp:75
cover_instrumenter_baset::ns
const namespacet ns
Definition: cover_instrument.h:72
exprt
Base class for all expressions.
Definition: expr.h:55
cover_cover_instrumentert::instrument
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.
Definition: cover_instrument_other.cpp:49
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
get_identifier
static optionalt< smt_termt > get_identifier(const exprt &expr, const std::unordered_map< exprt, smt_identifier_termt, irep_hash > &expression_handle_identifiers, const std::unordered_map< exprt, smt_identifier_termt, irep_hash > &expression_identifiers)
Definition: smt2_incremental_decision_procedure.cpp:328
language_util.h
cover_assertion_instrumentert::instrument
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.
Definition: cover_instrument_other.cpp:33
cover_instrument.h
cover_blocks_baset
Definition: cover_basic_blocks.h:25
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:222
false_exprt
The Boolean constant false.
Definition: std_expr.h:3016
cprover_prefix.h
cover_instrumenter_baset::assertion_factoryt
std::function< goto_programt::instructiont(const exprt &, const source_locationt &)> assertion_factoryt
The type of function used to make goto_program assertions.
Definition: cover_instrument.h:42
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:592
CPROVER_PREFIX
#define CPROVER_PREFIX
Definition: cprover_prefix.h:14
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:72
cover_path_instrumentert::instrument
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.
Definition: cover_instrument_other.cpp:20
INVARIANT
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
goto_programt::insert_before_swap
void insert_before_swap(targett target)
Insertion that preserves jumps to "target".
Definition: goto_program.h:613
comment
static std::string comment(const rw_set_baset::entryt &entry, bool write)
Definition: race_check.cpp:109
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:180
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:211
from_expr
std::string from_expr(const namespacet &ns, const irep_idt &identifier, const exprt &expr)
Definition: language_util.cpp:38
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:586
not_exprt
Boolean negation.
Definition: std_expr.h:2277