cprover
cover.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Coverage Instrumentation
4 
5 Author: Daniel Kroening
6 
7 Date: May 2016
8 
9 \*******************************************************************/
10 
13 
14 #include "cover.h"
15 
16 #include <util/config.h>
17 #include <util/message.h>
18 #include <util/make_unique.h>
19 #include <util/cmdline.h>
20 #include <util/options.h>
21 
23 
24 #include "cover_basic_blocks.h"
25 
38  const irep_idt &function_id,
39  goto_programt &goto_program,
40  const cover_instrumenterst &instrumenters,
41  const irep_idt &mode,
42  message_handlert &message_handler,
44 {
45  const std::unique_ptr<cover_blocks_baset> basic_blocks =
46  mode == ID_java ? std::unique_ptr<cover_blocks_baset>(
47  new cover_basic_blocks_javat(goto_program))
48  : std::unique_ptr<cover_blocks_baset>(
49  new cover_basic_blockst(goto_program));
50 
51  basic_blocks->report_block_anomalies(
52  function_id, goto_program, message_handler);
53  instrumenters(function_id, goto_program, *basic_blocks, make_assertion);
54 }
55 
61  coverage_criteriont criterion,
63  const goal_filterst &goal_filters)
64 {
65  switch(criterion)
66  {
68  instrumenters.push_back(
69  util_make_unique<cover_location_instrumentert>(
70  symbol_table, goal_filters));
71  break;
72  case coverage_criteriont::BRANCH:
73  instrumenters.push_back(
74  util_make_unique<cover_branch_instrumentert>(symbol_table, goal_filters));
75  break;
76  case coverage_criteriont::DECISION:
77  instrumenters.push_back(
78  util_make_unique<cover_decision_instrumentert>(
79  symbol_table, goal_filters));
80  break;
81  case coverage_criteriont::CONDITION:
82  instrumenters.push_back(
83  util_make_unique<cover_condition_instrumentert>(
84  symbol_table, goal_filters));
85  break;
86  case coverage_criteriont::PATH:
87  instrumenters.push_back(
88  util_make_unique<cover_path_instrumentert>(symbol_table, goal_filters));
89  break;
90  case coverage_criteriont::MCDC:
91  instrumenters.push_back(
92  util_make_unique<cover_mcdc_instrumentert>(symbol_table, goal_filters));
93  break;
94  case coverage_criteriont::ASSERTION:
95  instrumenters.push_back(
96  util_make_unique<cover_assertion_instrumentert>(
97  symbol_table, goal_filters));
98  break;
100  instrumenters.push_back(
101  util_make_unique<cover_cover_instrumentert>(symbol_table, goal_filters));
102  }
103 }
104 
109 parse_coverage_criterion(const std::string &criterion_string)
110 {
112 
113  if(criterion_string == "assertion" || criterion_string == "assertions")
114  c = coverage_criteriont::ASSERTION;
115  else if(criterion_string == "path" || criterion_string == "paths")
116  c = coverage_criteriont::PATH;
117  else if(criterion_string == "branch" || criterion_string == "branches")
118  c = coverage_criteriont::BRANCH;
119  else if(criterion_string == "location" || criterion_string == "locations")
121  else if(criterion_string == "decision" || criterion_string == "decisions")
122  c = coverage_criteriont::DECISION;
123  else if(criterion_string == "condition" || criterion_string == "conditions")
124  c = coverage_criteriont::CONDITION;
125  else if(criterion_string == "mcdc")
126  c = coverage_criteriont::MCDC;
127  else if(criterion_string == "cover")
129  else
130  {
131  std::stringstream s;
132  s << "unknown coverage criterion " << '\'' << criterion_string << '\'';
133  throw invalid_command_line_argument_exceptiont(s.str(), "--cover");
134  }
135 
136  return c;
137 }
138 
142 void parse_cover_options(const cmdlinet &cmdline, optionst &options)
143 {
144  options.set_option("cover", cmdline.get_values("cover"));
145 
146  // allow retrieving full traces
147  options.set_option("simple-slice", false);
148 
149  options.set_option(
150  "cover-include-pattern", cmdline.get_value("cover-include-pattern"));
151  options.set_option("no-trivial-tests", cmdline.isset("no-trivial-tests"));
152 
153  std::string cover_only = cmdline.get_value("cover-only");
154 
155  if(!cover_only.empty() && cmdline.isset("cover-function-only"))
157  "at most one of --cover-only and --cover-function-only can be used",
158  "--cover-only");
159 
160  options.set_option("cover-only", cmdline.get_value("cover-only"));
161  if(cmdline.isset("cover-function-only"))
162  options.set_option("cover-only", "function");
163 
164  options.set_option(
165  "cover-traces-must-terminate",
166  cmdline.isset("cover-traces-must-terminate"));
167  options.set_option(
168  "cover-failed-assertions", cmdline.isset("cover-failed-assertions"));
169 
170  options.set_option("show-test-suite", cmdline.isset("show-test-suite"));
171 }
172 
181  const optionst &options,
183  message_handlert &message_handler)
184 {
185  cover_configt cover_config;
186  function_filterst &function_filters =
187  cover_config.cover_configt::function_filters;
188  std::unique_ptr<goal_filterst> &goal_filters = cover_config.goal_filters;
189  cover_instrumenterst &instrumenters = cover_config.cover_instrumenters;
190 
191  function_filters.add(util_make_unique<internal_functions_filtert>());
192 
193  goal_filters->add(util_make_unique<internal_goals_filtert>());
194 
195  optionst::value_listt criteria_strings = options.get_list_option("cover");
196 
197  cover_config.keep_assertions = false;
198  for(const auto &criterion_string : criteria_strings)
199  {
200  coverage_criteriont c = parse_coverage_criterion(criterion_string);
201 
202  if(c == coverage_criteriont::ASSERTION)
203  cover_config.keep_assertions = true;
204 
205  instrumenters.add_from_criterion(c, symbol_table, *goal_filters);
206  }
207 
208  if(cover_config.keep_assertions && criteria_strings.size() > 1)
209  {
210  std::stringstream s;
211  s << "assertion coverage cannot currently be used together with other"
212  << "coverage criteria";
213  throw invalid_command_line_argument_exceptiont(s.str(), "--cover");
214  }
215 
216  std::string cover_include_pattern =
217  options.get_option("cover-include-pattern");
218  if(!cover_include_pattern.empty())
219  {
220  function_filters.add(
221  util_make_unique<include_pattern_filtert>(cover_include_pattern));
222  }
223 
224  if(options.get_bool_option("no-trivial-tests"))
225  function_filters.add(util_make_unique<trivial_functions_filtert>());
226 
227  cover_config.traces_must_terminate =
228  options.get_bool_option("cover-traces-must-terminate");
229 
230  cover_config.cover_failed_assertions =
231  options.get_bool_option("cover-failed-assertions");
232 
233  return cover_config;
234 }
235 
244  const optionst &options,
245  const irep_idt &main_function_id,
247  message_handlert &message_handler)
248 {
249  cover_configt cover_config =
250  get_cover_config(options, symbol_table, message_handler);
251 
252  std::string cover_only = options.get_option("cover-only");
253 
254  // cover entry point function only
255  if(cover_only == "function")
256  {
257  const symbolt &main_symbol = symbol_table.lookup_ref(main_function_id);
258  cover_config.function_filters.add(
259  util_make_unique<single_function_filtert>(main_symbol.name));
260  }
261  else if(cover_only == "file")
262  {
263  const symbolt &main_symbol = symbol_table.lookup_ref(main_function_id);
264  cover_config.function_filters.add(
265  util_make_unique<file_filtert>(main_symbol.location.get_file()));
266  }
267  else if(!cover_only.empty())
268  {
269  std::stringstream s;
270  s << "Argument to --cover-only not recognized: " << cover_only;
271  throw invalid_command_line_argument_exceptiont(s.str(), "--cover-only");
272  }
273 
274  return cover_config;
275 }
276 
283  const cover_configt &cover_config,
284  const symbolt &function_symbol,
286  message_handlert &message_handler)
287 {
288  if(!cover_config.keep_assertions)
289  {
290  Forall_goto_program_instructions(i_it, function.body)
291  {
292  // Simplify the common case where we have ASSERT(x); ASSUME(x):
293  if(i_it->is_assert())
294  {
295  if(!cover_config.cover_failed_assertions)
296  {
297  auto successor = std::next(i_it);
298  if(
299  successor != function.body.instructions.end() &&
300  successor->is_assume() &&
301  successor->get_condition() == i_it->get_condition())
302  {
303  successor->turn_into_skip();
304  }
306  }
307  else
308  {
309  i_it->turn_into_skip();
310  }
311  }
312  }
313  }
314 
315  bool changed = false;
316 
317  if(cover_config.function_filters(function_symbol, function))
318  {
319  messaget msg(message_handler);
320  msg.debug() << "Instrumenting coverage for function "
321  << id2string(function_symbol.name) << messaget::eom;
323  function_symbol.name,
324  function.body,
325  cover_config.cover_instrumenters,
326  function_symbol.mode,
327  message_handler,
328  cover_config.make_assertion);
329  changed = true;
330  }
331 
332  if(
333  cover_config.traces_must_terminate &&
334  function_symbol.name == goto_functionst::entry_point())
335  {
337  function_symbol.name, function.body, cover_config.make_assertion);
338  changed = true;
339  }
340 
341  if(changed)
342  remove_skip(function.body);
343 }
344 
350  const cover_configt &cover_config,
351  goto_model_functiont &function,
352  message_handlert &message_handler)
353 {
354  const symbolt function_symbol =
355  function.get_symbol_table().lookup_ref(function.get_function_id());
357  cover_config,
358  function_symbol,
359  function.get_goto_function(),
360  message_handler);
361 
362  function.compute_location_numbers();
363 }
364 
371  const cover_configt &cover_config,
374  message_handlert &message_handler)
375 {
376  messaget msg(message_handler);
377  msg.status() << "Rewriting existing assertions as assumptions"
378  << messaget::eom;
379 
380  if(
381  cover_config.traces_must_terminate &&
383  {
384  msg.error() << "cover-traces-must-terminate: invalid entry point ["
386  return true;
387  }
388 
389  for(auto &gf_entry : goto_functions.function_map)
390  {
391  const symbolt function_symbol = symbol_table.lookup_ref(gf_entry.first);
393  cover_config, function_symbol, gf_entry.second, message_handler);
394  }
396 
397  cover_config.function_filters.report_anomalies();
398  cover_config.goal_filters->report_anomalies();
399 
400  return false;
401 }
402 
408  const cover_configt &cover_config,
409  goto_modelt &goto_model,
410  message_handlert &message_handler)
411 {
412  return instrument_cover_goals(
413  cover_config,
414  goto_model.symbol_table,
415  goto_model.goto_functions,
416  message_handler);
417 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
Forall_goto_program_instructions
#define Forall_goto_program_instructions(it, program)
Definition: goto_program.h:1172
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
cover.h
Coverage Instrumentation.
symbol_tablet
The symbol table.
Definition: symbol_table.h:20
symbol_table_baset::lookup_ref
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Definition: symbol_table_base.h:104
function_filterst::report_anomalies
void report_anomalies() const
Can be called after final filter application to report on unexpected situations encountered.
Definition: cover_filter.h:90
cmdlinet::isset
virtual bool isset(char option) const
Definition: cmdline.cpp:29
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 &)
Definition: cover_instrument_other.cpp:75
cover_configt::goal_filters
std::unique_ptr< goal_filterst > goal_filters
Definition: cover.h:60
optionst
Definition: options.h:23
optionst::get_option
const std::string get_option(const std::string &option) const
Definition: options.cpp:67
messaget::status
mstreamt & status() const
Definition: message.h:414
remove_skip
void remove_skip(goto_programt &goto_program, goto_programt::targett begin, goto_programt::targett end)
remove unnecessary skip statements
Definition: remove_skip.cpp:85
goal_filterst::add
void add(std::unique_ptr< goal_filter_baset > filter)
Adds a function filter.
Definition: cover_filter.h:106
goto_functionst::compute_location_numbers
void compute_location_numbers()
Definition: goto_functions.cpp:18
cover_configt::cover_failed_assertions
bool cover_failed_assertions
Definition: cover.h:55
goto_modelt
Definition: goto_model.h:26
parse_coverage_criterion
coverage_criteriont parse_coverage_criterion(const std::string &criterion_string)
Parses a coverage criterion.
Definition: cover.cpp:109
options.h
Options.
cover_instrumenterst::add_from_criterion
void add_from_criterion(coverage_criteriont, const symbol_tablet &, const goal_filterst &)
Create and add an instrumenter based on the given criterion.
Definition: cover.cpp:60
optionst::set_option
void set_option(const std::string &option, const bool value)
Definition: options.cpp:28
messaget::eom
static eomt eom
Definition: message.h:297
optionst::value_listt
std::list< std::string > value_listt
Definition: options.h:25
instrument_cover_goals
static void instrument_cover_goals(const irep_idt &function_id, goto_programt &goto_program, const cover_instrumenterst &instrumenters, const irep_idt &mode, message_handlert &message_handler, const cover_instrumenter_baset::assertion_factoryt &make_assertion)
Applies instrumenters to given goto program.
Definition: cover.cpp:37
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:27
coverage_criteriont
coverage_criteriont
Definition: cover.h:41
coverage_criteriont::LOCATION
@ LOCATION
get_cover_config
cover_configt get_cover_config(const optionst &options, const symbol_tablet &symbol_table, message_handlert &message_handler)
Build data structures controlling coverage from command-line options.
Definition: cover.cpp:180
cover_basic_blocks_javat
Definition: cover_basic_blocks.h:143
cover_instrumenterst::instrumenters
std::vector< std::unique_ptr< cover_instrumenter_baset > > instrumenters
Definition: cover_instrument.h:126
cover_configt::cover_instrumenters
cover_instrumenterst cover_instrumenters
Definition: cover.h:62
cmdlinet
Definition: cmdline.h:21
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
make_unique.h
messaget::error
mstreamt & error() const
Definition: message.h:399
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
cover_configt::traces_must_terminate
bool traces_must_terminate
Definition: cover.h:56
cover_configt::make_assertion
cover_instrumenter_baset::assertion_factoryt make_assertion
Definition: cover.h:63
cmdlinet::get_value
std::string get_value(char option) const
Definition: cmdline.cpp:47
message_handlert
Definition: message.h:28
parse_cover_options
void parse_cover_options(const cmdlinet &cmdline, optionst &options)
Parses coverage-related command line options.
Definition: cover.cpp:142
goal_filterst
A collection of goal filters to be applied in conjunction.
Definition: cover_filter.h:102
cover_blocks_baset::report_block_anomalies
virtual void report_block_anomalies(const irep_idt &function_id, const goto_programt &goto_program, message_handlert &message_handler)
Output warnings about ignored blocks.
Definition: cover_basic_blocks.h:51
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:41
goto_modelt::get_goto_function
const goto_functionst::goto_functiont & get_goto_function(const irep_idt &id) override
Get a GOTO function by name, or throw if no such function exists.
Definition: goto_model.h:82
goto_functionst::goto_functiont
::goto_functiont goto_functiont
Definition: goto_functions.h:25
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:23
cover_basic_blockst
Definition: cover_basic_blocks.h:64
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
cover_configt::function_filters
function_filterst function_filters
Definition: cover.h:58
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
cmdline.h
symbolt
Symbol table entry.
Definition: symbol.h:28
ASSUME
@ ASSUME
Definition: goto_program.h:36
optionst::get_bool_option
bool get_bool_option(const std::string &option) const
Definition: options.cpp:44
cover_basic_blocks.h
Basic blocks detection for Coverage Instrumentation.
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:74
config.h
cover_configt
Definition: cover.h:53
source_locationt::get_file
const irep_idt & get_file() const
Definition: source_location.h:36
messaget::debug
mstreamt & debug() const
Definition: message.h:429
goto_functionst::entry_point
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
Definition: goto_functions.h:90
remove_skip.h
Program Transformation.
message.h
function_filterst
A collection of function filters to be applied in conjunction.
Definition: cover_filter.h:65
invalid_command_line_argument_exceptiont
Thrown when users pass incorrect command line arguments, for example passing no files to analysis or ...
Definition: exception_utils.h:38
cmdlinet::get_values
const std::list< std::string > & get_values(const std::string &option) const
Definition: cmdline.cpp:108
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
function_filterst::add
void add(std::unique_ptr< function_filter_baset > filter)
Adds a function filter.
Definition: cover_filter.h:69
optionst::get_list_option
const value_listt & get_list_option(const std::string &option) const
Definition: options.cpp:80
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
goto_model_functiont
Interface providing access to a single function in a GOTO model, plus its associated symbol table.
Definition: goto_model.h:183
cover_configt::keep_assertions
bool keep_assertions
Definition: cover.h:54
cover_instrumenterst
A collection of instrumenters to be run.
Definition: cover_instrument.h:102