CBMC
cover_goals_report_util.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Cover Goals Reporting Utilities
4 
5 Author: Daniel Kroening, Peter Schrammel
6 
7 \*******************************************************************/
8 
11 
13 
14 #include <iomanip>
15 
16 #include <util/json.h>
17 #include <util/json_irep.h>
18 #include <util/json_stream.h>
19 #include <util/ui_message.h>
20 #include <util/xml.h>
21 #include <util/xml_irep.h>
22 
24  const propertiest &properties,
25  unsigned iterations,
26  messaget &log)
27 {
28  const std::size_t goals_covered =
30  log.status() << "** " << goals_covered << " of " << properties.size()
31  << " covered (" << std::fixed << std::setw(1)
32  << std::setprecision(1)
33  << (properties.empty()
34  ? 100.0
35  : 100.0 * goals_covered / properties.size())
36  << "%)" << messaget::eom;
37  log.statistics() << "** Used " << iterations << " iteration"
38  << (iterations == 1 ? "" : "s") << messaget::eom;
39 }
40 
41 static void output_goals_plain(const propertiest &properties, messaget &log)
42 {
43  log.result() << "\n** coverage results:" << messaget::eom;
44 
45  for(const auto &property_pair : properties)
46  {
47  log.result() << "[" << property_pair.first << "]";
48 
49  if(property_pair.second.pc->source_location().is_not_nil())
50  log.result() << ' ' << property_pair.second.pc->source_location();
51 
52  if(!property_pair.second.description.empty())
53  log.result() << ' ' << property_pair.second.description;
54 
55  log.result() << ": "
56  << (property_pair.second.status == property_statust::FAIL
57  ? "SATISFIED"
58  : "FAILED")
59  << '\n';
60  }
61 
62  log.result() << messaget::eom;
63 }
64 
65 static void output_goals_xml(const propertiest &properties, messaget &log)
66 {
67  for(const auto &property_pair : properties)
68  {
69  xmlt xml_result(
70  "goal",
71  {{"id", id2string(property_pair.first)},
72  {"description", property_pair.second.description},
73  {"status",
74  property_pair.second.status == property_statust::FAIL ? "SATISFIED"
75  : "FAILED"}},
76  {});
77 
78  const auto &source_location = property_pair.second.pc->source_location();
79  if(source_location.is_not_nil())
80  {
81  xml_result.new_element() = xml(source_location);
82 
83  const irept &basic_block_lines =
84  source_location.get_basic_block_source_lines();
85  if(basic_block_lines.is_not_nil())
86  {
87  xmlt basic_block_lines_xml{"basic_block_lines"};
88  for(const auto &file_entry : basic_block_lines.get_named_sub())
89  {
90  for(const auto &lines_entry : file_entry.second.get_named_sub())
91  {
92  xmlt line{"line"};
93  line.set_attribute("file", id2string(file_entry.first));
94  line.set_attribute("function", id2string(lines_entry.first));
95  line.data = id2string(lines_entry.second.id());
96  basic_block_lines_xml.new_element(line);
97  }
98  }
99  xml_result.new_element(basic_block_lines_xml);
100  }
101  }
102 
103  log.result() << xml_result;
104  }
105 }
106 
107 static void output_goals_json(
108  const propertiest &properties,
109  messaget &log,
110  ui_message_handlert &ui_message_handler)
111 {
112  if(log.status().tellp() > 0)
113  log.status() << messaget::eom; // force end of previous message
114  json_stream_objectt &json_result =
115  ui_message_handler.get_json_stream().push_back_stream_object();
116  json_stream_arrayt &goals_array = json_result.push_back_stream_array("goals");
117  for(const auto &property_pair : properties)
118  {
119  const property_infot &property_info = property_pair.second;
120 
121  json_objectt json_goal;
122  json_goal["status"] = json_stringt(
123  property_info.status == property_statust::FAIL ? "satisfied" : "failed");
124  json_goal["goal"] = json_stringt(property_pair.first);
125  json_goal["description"] = json_stringt(property_info.description);
126 
127  const auto &source_location = property_info.pc->source_location();
128  if(source_location.is_not_nil())
129  {
130  json_goal["sourceLocation"] = json(source_location);
131 
132  const irept &basic_block_lines =
133  source_location.get_basic_block_source_lines();
134  if(basic_block_lines.is_not_nil())
135  {
136  json_objectt basic_block_lines_json;
137  for(const auto &file_entry : basic_block_lines.get_named_sub())
138  {
139  json_objectt file_lines_json;
140  for(const auto &lines_entry : file_entry.second.get_named_sub())
141  {
142  file_lines_json[id2string(lines_entry.first)] =
143  json_stringt{lines_entry.second.id()};
144  }
145  basic_block_lines_json[id2string(file_entry.first)] = file_lines_json;
146  }
147  json_goal["basicBlockLines"] = basic_block_lines_json;
148  }
149  }
150 
151  goals_array.push_back(std::move(json_goal));
152  }
153  const std::size_t goals_covered =
155  json_result.push_back(
156  "goalsCovered", json_numbert(std::to_string(goals_covered)));
157  json_result.push_back(
158  "totalGoals", json_numbert(std::to_string(properties.size())));
159 }
160 
162  const propertiest &properties,
163  unsigned iterations,
164  ui_message_handlert &ui_message_handler)
165 {
166  messaget log(ui_message_handler);
167  switch(ui_message_handler.get_ui())
168  {
170  {
171  output_goals_plain(properties, log);
172  break;
173  }
175  {
176  output_goals_xml(properties, log);
177  break;
178  }
180  {
181  output_goals_json(properties, log, ui_message_handler);
182  break;
183  }
184  }
185  output_goals_iterations(properties, iterations, log);
186 }
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:360
named_subt & get_named_sub()
Definition: irep.h:446
bool is_not_nil() const
Definition: irep.h:368
Provides methods for streaming JSON arrays.
Definition: json_stream.h:93
void push_back(const jsont &json)
Push back a JSON element into the current array stream.
Definition: json_stream.h:106
json_stream_objectt & push_back_stream_object()
Add a JSON object child stream.
Definition: json_stream.cpp:82
Provides methods for streaming JSON objects.
Definition: json_stream.h:140
void push_back(const std::string &key, const jsont &json)
Push back a JSON element into the current object stream.
Definition: json_stream.h:178
json_stream_arrayt & push_back_stream_array(const std::string &key)
Add a JSON array stream for a specific key.
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
static eomt eom
Definition: message.h:297
virtual uit get_ui() const
Definition: ui_message.h:33
virtual json_stream_arrayt & get_json_stream()
Definition: ui_message.h:40
Definition: xml.h:21
void set_attribute(const std::string &attribute, unsigned value)
Definition: xml.cpp:198
xmlt & new_element(const std::string &key)
Definition: xml.h:95
static void output_goals_iterations(const propertiest &properties, unsigned iterations, messaget &log)
static void output_goals_xml(const propertiest &properties, messaget &log)
static void output_goals_json(const propertiest &properties, messaget &log, ui_message_handlert &ui_message_handler)
void output_goals(const propertiest &properties, unsigned iterations, ui_message_handlert &ui_message_handler)
Outputs the properties interpreted as 'coverage goals' and the number of goto verifier iterations tha...
static void output_goals_plain(const propertiest &properties, messaget &log)
Cover Goals Reporting Utilities.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:40
double log(double x)
Definition: math.c:2776
static void json(json_objectT &result, const irep_idt &property_id, const property_infot &property_info)
Definition: properties.cpp:120
std::size_t count_properties(const propertiest &properties, property_statust status)
Return the number of properties with given status.
Definition: properties.cpp:164
xmlt xml(const irep_idt &property_id, const property_infot &property_info)
Definition: properties.cpp:110
@ FAIL
The property was violated.
std::map< irep_idt, property_infot > propertiest
A map of property IDs to property infos.
Definition: properties.h:76
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
property_statust status
The status of the property.
Definition: properties.h:72
std::string description
A description (usually derived from the assertion's comment)
Definition: properties.h:69
goto_programt::const_targett pc
A pointer to the corresponding goto instruction.
Definition: properties.h:66