CBMC
show_properties.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Show Claims
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "show_properties.h"
13 
14 #include <util/json_irep.h>
15 #include <util/ui_message.h>
16 #include <util/xml_irep.h>
17 
18 #include <langapi/language_util.h>
19 
20 #include "goto_model.h"
21 
23  const irep_idt &property,
24  const goto_functionst & goto_functions)
25 {
26  for(const auto &fct : goto_functions.function_map)
27  {
28  const goto_programt &goto_program = fct.second.body;
29 
30  for(const auto &ins : goto_program.instructions)
31  {
32  if(ins.is_assert())
33  {
34  if(ins.source_location().get_property_id() == property)
35  {
36  return ins.source_location();
37  }
38  }
39  }
40  }
41  return { };
42 }
43 
45  const namespacet &ns,
46  const irep_idt &identifier,
47  message_handlert &message_handler,
49  const goto_programt &goto_program)
50 {
51  messaget msg(message_handler);
52  for(const auto &ins : goto_program.instructions)
53  {
54  if(!ins.is_assert())
55  continue;
56 
57  const source_locationt &source_location = ins.source_location();
58 
59  const irep_idt &comment=source_location.get_comment();
60  const irep_idt &property_class=source_location.get_property_class();
61  const irep_idt description = (comment.empty() ? "assertion" : comment);
62 
63  irep_idt property_id=source_location.get_property_id();
64 
65  switch(ui)
66  {
68  {
69  // use me instead
70  xmlt xml_property(
71  "property",
72  {{"name", id2string(property_id)},
73  {"class", id2string(property_class)}},
74  {});
75 
76  xmlt &property_l=xml_property.new_element();
77  property_l=xml(source_location);
78 
79  xml_property.new_element("description").data=id2string(description);
80  xml_property.new_element("expression").data =
81  from_expr(ns, identifier, ins.condition());
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_property.new_element(basic_block_lines_xml);
100  }
101 
102  msg.result() << xml_property;
103  }
104  break;
105 
107  UNREACHABLE;
108  break;
109 
111  msg.result() << "Property " << property_id << ":\n";
112 
113  msg.result() << " " << ins.source_location() << '\n'
114  << " " << description << '\n'
115  << " " << from_expr(ns, identifier, ins.condition())
116  << '\n';
117 
118  msg.result() << messaget::eom;
119  break;
120 
121  default:
122  UNREACHABLE;
123  }
124  }
125 }
126 
128  json_arrayt &json_properties,
129  const namespacet &ns,
130  const irep_idt &identifier,
131  const goto_programt &goto_program)
132 {
133  for(const auto &ins : goto_program.instructions)
134  {
135  if(!ins.is_assert())
136  continue;
137 
138  const source_locationt &source_location = ins.source_location();
139 
140  const irep_idt &comment=source_location.get_comment();
141  // const irep_idt &function=location.get_function();
142  const irep_idt &property_class=source_location.get_property_class();
143  const irep_idt description = (comment.empty() ? "assertion" : comment);
144 
145  irep_idt property_id=source_location.get_property_id();
146 
147  json_objectt json_property{
148  {"name", json_stringt(property_id)},
149  {"class", json_stringt(property_class)},
150  {"sourceLocation", json(source_location)},
151  {"description", json_stringt(description)},
152  {"expression", json_stringt(from_expr(ns, identifier, ins.condition()))}};
153 
154  const irept &basic_block_lines =
155  source_location.get_basic_block_source_lines();
156  if(basic_block_lines.is_not_nil())
157  {
158  json_objectt basic_block_lines_json;
159  for(const auto &file_entry : basic_block_lines.get_named_sub())
160  {
161  json_objectt file_lines_json;
162  for(const auto &lines_entry : file_entry.second.get_named_sub())
163  {
164  file_lines_json[id2string(lines_entry.first)] =
165  json_stringt{lines_entry.second.id()};
166  }
167  basic_block_lines_json[id2string(file_entry.first)] = file_lines_json;
168  }
169  json_property["basicBlockLines"] = basic_block_lines_json;
170  }
171 
172  json_properties.push_back(std::move(json_property));
173  }
174 }
175 
177  const namespacet &ns,
178  message_handlert &message_handler,
179  const goto_functionst &goto_functions)
180 {
181  messaget msg(message_handler);
182  json_arrayt json_properties;
183 
184  for(const auto &fct : goto_functions.function_map)
185  convert_properties_json(json_properties, ns, fct.first, fct.second.body);
186 
187  json_objectt json_result{{"properties", json_properties}};
188  msg.result() << json_result;
189 }
190 
192  const namespacet &ns,
193  ui_message_handlert &ui_message_handler,
194  const goto_functionst &goto_functions)
195 {
196  ui_message_handlert::uit ui = ui_message_handler.get_ui();
198  show_properties_json(ns, ui_message_handler, goto_functions);
199  else
200  for(const auto &fct : goto_functions.function_map)
201  show_properties(ns, fct.first, ui_message_handler, ui, fct.second.body);
202 }
203 
205  const goto_modelt &goto_model,
206  ui_message_handlert &ui_message_handler)
207 {
208  ui_message_handlert::uit ui = ui_message_handler.get_ui();
209  const namespacet ns(goto_model.symbol_table);
211  show_properties_json(ns, ui_message_handler, goto_model.goto_functions);
212  else
213  show_properties(ns, ui_message_handler, goto_model.goto_functions);
214 }
convert_properties_json
void convert_properties_json(json_arrayt &json_properties, const namespacet &ns, const irep_idt &identifier, const goto_programt &goto_program)
Collects the properties in the goto program into a json_arrayt
Definition: show_properties.cpp:127
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
UNREACHABLE
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:503
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
source_locationt::get_comment
const irep_idt & get_comment() const
Definition: source_location.h:70
source_locationt::get_property_id
const irep_idt & get_property_id() const
Definition: source_location.h:60
source_locationt::get_property_class
const irep_idt & get_property_class() const
Definition: source_location.h:65
ui_message_handlert
Definition: ui_message.h:21
show_properties_json
void show_properties_json(const namespacet &ns, message_handlert &message_handler, const goto_functionst &goto_functions)
Definition: show_properties.cpp:176
ui_message_handlert::uit::XML_UI
@ XML_UI
show_properties
void show_properties(const namespacet &ns, const irep_idt &identifier, message_handlert &message_handler, ui_message_handlert::uit ui, const goto_programt &goto_program)
Definition: show_properties.cpp:44
goto_model.h
xml_irep.h
goto_modelt
Definition: goto_model.h:25
messaget::eom
static eomt eom
Definition: message.h:297
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:29
json_irep.h
json_arrayt
Definition: json.h:164
json_objectt
Definition: json.h:299
ui_message_handlert::get_ui
virtual uit get_ui() const
Definition: ui_message.h:33
ui_message_handlert::uit
uit
Definition: ui_message.h:24
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
irept::is_not_nil
bool is_not_nil() const
Definition: irep.h:380
messaget::result
mstreamt & result() const
Definition: message.h:409
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
irept::get_named_sub
named_subt & get_named_sub()
Definition: irep.h:458
xml
xmlt xml(const irep_idt &property_id, const property_infot &property_info)
Definition: properties.cpp:110
ui_message_handlert::uit::JSON_UI
@ JSON_UI
messaget::mstreamt::source_location
source_locationt source_location
Definition: message.h:247
language_util.h
json
static void json(json_objectT &result, const irep_idt &property_id, const property_infot &property_info)
Definition: properties.cpp:120
show_properties.h
message_handlert
Definition: message.h:27
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
xmlt
Definition: xml.h:20
source_locationt
Definition: source_location.h:18
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:592
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:24
goto_modelt::goto_functions
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
ui_message_handlert::uit::PLAIN
@ PLAIN
xmlt::set_attribute
void set_attribute(const std::string &attribute, unsigned value)
Definition: xml.cpp:198
find_property
optionalt< source_locationt > find_property(const irep_idt &property, const goto_functionst &goto_functions)
Returns a source_locationt that corresponds to the property given by an irep_idt.
Definition: show_properties.cpp:22
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:72
irept
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:359
comment
static std::string comment(const rw_set_baset::entryt &entry, bool write)
Definition: race_check.cpp:109
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
from_expr
std::string from_expr(const namespacet &ns, const irep_idt &identifier, const exprt &expr)
Definition: language_util.cpp:38
source_locationt::get_basic_block_source_lines
const irept & get_basic_block_source_lines() const
Definition: source_location.h:85
json_arrayt::push_back
jsont & push_back(const jsont &json)
Definition: json.h:212
xmlt::new_element
xmlt & new_element(const std::string &key)
Definition: xml.h:95
json_stringt
Definition: json.h:269
ui_message.h