cprover
show_goto_functions_json.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Goto Program
4 
5 Author: Thomas Kiley
6 
7 \*******************************************************************/
8 
11 
13 
14 #include <iostream>
15 #include <sstream>
16 
17 #include <util/json_expr.h>
18 #include <util/json_irep.h>
19 #include <util/cprover_prefix.h>
20 #include <util/prefix.h>
21 
22 #include <langapi/language_util.h>
23 
24 #include "goto_functions.h"
25 #include "goto_model.h"
26 
31  const namespacet &_ns,
32  bool _list_only)
33  : ns(_ns), list_only(_list_only)
34 {}
35 
40  const goto_functionst &goto_functions)
41 {
42  json_arrayt json_functions;
43  const json_irept no_comments_irep_converter(false);
44  for(const auto &function_entry : goto_functions.function_map)
45  {
46  const irep_idt &function_name=function_entry.first;
47  const goto_functionst::goto_functiont &function=function_entry.second;
48 
49  json_objectt &json_function=
50  json_functions.push_back(jsont()).make_object();
51  json_function["name"] = json_stringt(function_name);
52  json_function["isBodyAvailable"]=
53  jsont::json_boolean(function.body_available());
54  bool is_internal=
55  has_prefix(id2string(function_name), CPROVER_PREFIX) ||
56  has_prefix(id2string(function_name), "java::array[") ||
57  has_prefix(id2string(function_name), "java::org.cprover") ||
58  has_prefix(id2string(function_name), "java::java");
59  json_function["isInternal"]=jsont::json_boolean(is_internal);
60 
61  if(list_only)
62  continue;
63 
64  if(function.body_available())
65  {
66  json_arrayt json_instruction_array=json_arrayt();
67 
68  for(const goto_programt::instructiont &instruction :
69  function.body.instructions)
70  {
71  json_objectt instruction_entry=json_objectt();
72 
73  instruction_entry["instructionId"]=
74  json_stringt(instruction.to_string());
75 
76  if(instruction.code.source_location().is_not_nil())
77  {
78  instruction_entry["sourceLocation"]=
79  json(instruction.code.source_location());
80  }
81 
82  std::ostringstream instruction_builder;
83  function.body.output_instruction(
84  ns, function_name, instruction_builder, instruction);
85 
86  instruction_entry["instruction"]=
87  json_stringt(instruction_builder.str());
88 
89  if(!instruction.code.operands().empty())
90  {
91  json_arrayt operand_array;
92  for(const exprt &operand : instruction.code.operands())
93  {
94  json_objectt operand_object=
95  no_comments_irep_converter.convert_from_irep(
96  operand);
97  operand_array.push_back(operand_object);
98  }
99  instruction_entry["operands"]=operand_array;
100  }
101 
102  if(!instruction.guard.is_true())
103  {
104  json_objectt guard_object=
105  no_comments_irep_converter.convert_from_irep(
106  instruction.guard);
107 
108  instruction_entry["guard"]=guard_object;
109  }
110 
111  json_instruction_array.push_back(instruction_entry);
112  }
113 
114  json_function["instructions"]=json_instruction_array;
115  }
116  }
117 
118  json_objectt json_result;
119  json_result["functions"]=json_functions;
120 
121  return json_result;
122 }
123 
132  const goto_functionst &goto_functions,
133  std::ostream &out,
134  bool append)
135 {
136  if(append)
137  {
138  out << ",\n";
139  }
140  out << convert(goto_functions);
141 }
exprt guard
Guard for gotos, assume, assert.
Definition: goto_program.h:188
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
bool is_not_nil() const
Definition: irep.h:173
std::string to_string() const
Definition: goto_program.h:386
#define CPROVER_PREFIX
Goto Programs with Functions.
Definition: json.h:23
static jsont json_boolean(bool value)
Definition: json.h:85
bool is_true() const
Definition: expr.cpp:124
function_mapt function_map
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...
show_goto_functions_jsont(const namespacet &_ns, bool _list_only=false)
For outputting the GOTO program in a readable JSON format.
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:173
jsont & push_back(const jsont &json)
Definition: json.h:163
Symbol Table + CFG.
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...
Expressions in JSON.
TO_BE_DOCUMENTED.
Definition: namespace.h:74
::goto_functiont goto_functiont
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
Definition: dstring.h:33
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:32
Base class for all expressions.
Definition: expr.h:42
const source_locationt & source_location() const
Definition: expr.h:125
json_objectt & make_object()
Definition: json.h:290
operandst & operands()
Definition: expr.h:66
json_objectt json(const source_locationt &location)
Definition: json_expr.cpp:83