cprover
Loading...
Searching...
No Matches
show_goto_functions_json.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Goto Program
4
5Author: Thomas Kiley
6
7\*******************************************************************/
8
11
13
14#include <util/cprover_prefix.h>
15#include <util/json_irep.h>
16
17#include "goto_functions.h"
18
24
29 const goto_functionst &goto_functions)
30{
31 json_arrayt json_functions;
32 const json_irept no_comments_irep_converter(false);
33
34 const auto sorted = goto_functions.sorted();
35
36 for(const auto &function_entry : sorted)
37 {
38 const irep_idt &function_name = function_entry->first;
39 const goto_functionst::goto_functiont &function = function_entry->second;
40
41 json_objectt &json_function=
42 json_functions.push_back(jsont()).make_object();
43 json_function["name"] = json_stringt(function_name);
44 json_function["isBodyAvailable"]=
45 jsont::json_boolean(function.body_available());
46 bool is_internal = function_name.starts_with(CPROVER_PREFIX) ||
47 function_name.starts_with("java::array[") ||
48 function_name.starts_with("java::org.cprover") ||
49 function_name.starts_with("java::java");
50 json_function["isInternal"]=jsont::json_boolean(is_internal);
51 json_function["isHidden"] = jsont::json_boolean(function.is_hidden());
52 auto json_parameter_id_range =
54 function.parameter_identifiers.begin(),
55 function.parameter_identifiers.end())
56 .map([](const irep_idt &id) { return json_stringt{id}; });
57 json_function["parameterIdentifiers"] = json_arrayt{
58 json_parameter_id_range.begin(), json_parameter_id_range.end()};
59
60 if(list_only)
61 continue;
62
63 if(function.body_available())
64 {
65 json_arrayt json_instruction_array=json_arrayt();
66
67 for(const goto_programt::instructiont &instruction :
68 function.body.instructions)
69 {
70 json_objectt instruction_entry{
71 {"instructionId", json_stringt(instruction.to_string())},
72 {"locationNumber",
73 json_numbert{std::to_string(instruction.location_number)}}};
74
75 if(instruction.source_location().is_not_nil())
76 {
77 instruction_entry["sourceLocation"] =
78 json(instruction.source_location());
79 }
80
81 std::ostringstream instruction_builder;
82 instruction.output(instruction_builder);
83
84 instruction_entry["instruction"]=
85 json_stringt(instruction_builder.str());
86
87 if(instruction.code().is_not_nil())
88 {
89 json_objectt code_object =
90 no_comments_irep_converter.convert_from_irep(instruction.code());
91
92 instruction_entry["code"] = std::move(code_object);
93 }
94
95 if(instruction.has_condition())
96 {
97 json_objectt guard_object =
98 no_comments_irep_converter.convert_from_irep(
99 instruction.condition());
100
101 instruction_entry["guard"] = std::move(guard_object);
102 }
103
104 if(!instruction.targets.empty())
105 {
106 auto json_target_range =
107 make_range(instruction.targets.begin(), instruction.targets.end())
108 .map(
109 [](const goto_programt::targett &target) {
110 return json_numbert{std::to_string(target->location_number)};
111 });
112 instruction_entry["targets"] =
113 json_arrayt{json_target_range.begin(), json_target_range.end()};
114 }
115
116 if(!instruction.labels.empty())
117 {
118 auto json_label_range =
119 make_range(instruction.labels.begin(), instruction.labels.end())
120 .map([](const irep_idt &id) { return json_stringt{id}; });
121 instruction_entry["labels"] =
122 json_arrayt{json_label_range.begin(), json_label_range.end()};
123 }
124
125 json_instruction_array.push_back(std::move(instruction_entry));
126 }
127
128 json_function["instructions"] = std::move(json_instruction_array);
129 }
130 }
131
132 return json_objectt({{"functions", json_functions}});
133}
134
143 const goto_functionst &goto_functions,
144 std::ostream &out,
145 bool append)
146{
147 if(append)
148 {
149 out << ",\n";
150 }
151 out << convert(goto_functions);
152}
bool starts_with(const char *s) const
equivalent of as_string().starts_with(s)
Definition dstring.h:95
A collection of goto functions.
std::vector< function_mapt::const_iterator > sorted() const
returns a vector of the iterators in alphabetical order
::goto_functiont goto_functiont
This class represents an instruction in the GOTO intermediate representation.
const goto_instruction_codet & code() const
Get the code represented by this instruction.
bool has_condition() const
Does this instruction have a condition?
targetst targets
The list of successor instructions.
std::string to_string() const
unsigned location_number
A globally unique number to identify a program location.
std::ostream & output(std::ostream &) const
Output this instruction to the given stream.
const exprt & condition() const
Get the condition of gotos, assume, assert.
const source_locationt & source_location() const
instructionst::iterator targett
bool is_not_nil() const
Definition irep.h:372
jsont & push_back(const jsont &json)
Definition json.h:212
json_objectt convert_from_irep(const irept &) const
To convert to JSON from an irep structure by recursively generating JSON for the different sub trees.
Definition json_irep.cpp:31
Definition json.h:27
json_objectt & make_object()
Definition json.h:436
static jsont json_boolean(bool value)
Definition json.h:97
void operator()(const goto_functionst &goto_functions, std::ostream &out, bool append=true)
Print the json object generated by show_goto_functions_jsont::show_goto_functions to the provided str...
json_objectt convert(const goto_functionst &goto_functions)
Walks through all of the functions in the program and returns a JSON object representing all their fu...
show_goto_functions_jsont(bool _list_only=false)
For outputting the GOTO program in a readable JSON format.
#define CPROVER_PREFIX
Goto Programs with Functions.
static void json(json_objectT &result, const irep_idt &property_id, const property_infot &property_info)
ranget< iteratort > make_range(iteratort begin, iteratort end)
Definition range.h:522
dstringt irep_idt