Skip to content

Commit 6cb2f90

Browse files
author
Daniel Kroening
authored
Merge pull request #1398 from diffblue/json_direct_return
json_irept now returns ireps and jsont directly
2 parents f365afe + 9d48225 commit 6cb2f90

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

src/goto-programs/show_goto_functions_json.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,19 @@ json_objectt show_goto_functions_jsont::convert(
8585
json_arrayt operand_array;
8686
for(const exprt &operand : instruction.code.operands())
8787
{
88-
json_objectt operand_object;
89-
no_comments_irep_converter.convert_from_irep(
90-
operand, operand_object);
88+
json_objectt operand_object=
89+
no_comments_irep_converter.convert_from_irep(
90+
operand);
9191
operand_array.push_back(operand_object);
9292
}
9393
instruction_entry["operands"]=operand_array;
9494
}
9595

9696
if(!instruction.guard.is_true())
9797
{
98-
json_objectt guard_object;
99-
no_comments_irep_converter.convert_from_irep(
100-
instruction.guard,
101-
guard_object);
98+
json_objectt guard_object=
99+
no_comments_irep_converter.convert_from_irep(
100+
instruction.guard);
102101

103102
instruction_entry["guard"]=guard_object;
104103
}
@@ -109,8 +108,10 @@ json_objectt show_goto_functions_jsont::convert(
109108
json_function["instructions"]=json_instruction_array;
110109
}
111110
}
111+
112112
json_objectt json_result;
113113
json_result["functions"]=json_functions;
114+
114115
return json_result;
115116
}
116117

src/util/json_irep.cpp

+21-20
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,29 @@ Author: Thomas Kiley, [email protected]
2020
/// for the different sub trees.
2121
/// \param include_comments: when writing JSON, should the comments
2222
/// sub tree be included.
23-
json_irept::json_irept(bool include_comments):
24-
include_comments(include_comments)
25-
{}
23+
json_irept::json_irept(bool _include_comments):
24+
include_comments(_include_comments)
25+
{
26+
}
2627

2728
/// To convert to JSON from an irep structure by recursively generating JSON
2829
/// for the different sub trees.
2930
/// \param irep: The irep structure to turn into json
30-
/// \param json: The json object to be filled up.
31-
void json_irept::convert_from_irep(const irept &irep, jsont &json) const
31+
/// \return: The json object.
32+
json_objectt json_irept::convert_from_irep(const irept &irep) const
3233
{
33-
json_objectt &irep_object=json.make_object();
34+
json_objectt irep_object;
35+
3436
if(irep.id()!=ID_nil)
3537
irep_object["id"]=json_stringt(irep.id_string());
3638

3739
convert_sub_tree("sub", irep.get_sub(), irep_object);
3840
convert_named_sub_tree("namedSub", irep.get_named_sub(), irep_object);
41+
3942
if(include_comments)
40-
{
4143
convert_named_sub_tree("comment", irep.get_comments(), irep_object);
42-
}
44+
45+
return irep_object;
4346
}
4447

4548
/// To convert to JSON from a list of ireps that are in an unlabelled subtree.
@@ -48,6 +51,7 @@ void json_irept::convert_from_irep(const irept &irep, jsont &json) const
4851
/// \param sub_tree_id: the name to give the subtree in the parent object
4952
/// \param sub_trees: the list of subtrees to parse
5053
/// \param parent: the parent JSON object who should be added to
54+
5155
void json_irept::convert_sub_tree(
5256
const std::string &sub_tree_id,
5357
const irept::subt &sub_trees,
@@ -58,8 +62,7 @@ void json_irept::convert_sub_tree(
5862
json_arrayt sub_objects;
5963
for(const irept &sub_tree : sub_trees)
6064
{
61-
json_objectt sub_object;
62-
convert_from_irep(sub_tree, sub_object);
65+
json_objectt sub_object=convert_from_irep(sub_tree);
6366
sub_objects.push_back(sub_object);
6467
}
6568
parent[sub_tree_id]=sub_objects;
@@ -83,8 +86,7 @@ void json_irept::convert_named_sub_tree(
8386
json_objectt sub_objects;
8487
for(const auto &sub_tree : sub_trees)
8588
{
86-
json_objectt sub_object;
87-
convert_from_irep(sub_tree.second, sub_object);
89+
json_objectt sub_object=convert_from_irep(sub_tree.second);
8890
sub_objects[id2string(sub_tree.first)]=sub_object;
8991
}
9092
parent[sub_tree_id]=sub_objects;
@@ -94,7 +96,7 @@ void json_irept::convert_named_sub_tree(
9496
/// Deserialize a JSON irep representation.
9597
/// \param input: json object to convert
9698
/// \return result - irep equivalent of input
97-
void json_irept::convert_from_json(const jsont &in, irept &out) const
99+
irept json_irept::convert_from_json(const jsont &in) const
98100
{
99101
std::vector<std::string> have_keys;
100102
for(const auto &keyval : in.object)
@@ -104,17 +106,16 @@ void json_irept::convert_from_json(const jsont &in, irept &out) const
104106
throw "irep JSON representation is missing one of needed keys: "
105107
"'id', 'sub', 'namedSub', 'comment'";
106108

107-
out.id(in["id"].value);
109+
irept out(in["id"].value);
108110

109111
for(const auto &sub : in["sub"].array)
110-
{
111-
out.get_sub().push_back(irept());
112-
convert_from_json(sub, out.get_sub().back());
113-
}
112+
out.get_sub().push_back(convert_from_json(sub));
114113

115114
for(const auto &named_sub : in["namedSub"].object)
116-
convert_from_json(named_sub.second, out.add(named_sub.first));
115+
out.add(named_sub.first)=convert_from_json(named_sub.second);
117116

118117
for(const auto &comment : in["comment"].object)
119-
convert_from_json(comment.second, out.add(comment.first));
118+
out.add(comment.first)=convert_from_json(comment.second);
119+
120+
return out;
120121
}

src/util/json_irep.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ Author: Thomas Kiley, [email protected]
1313
#define CPROVER_UTIL_JSON_IREP_H
1414

1515
#include <util/irep.h>
16+
1617
class jsont;
1718
class json_objectt;
1819

1920
class json_irept
2021
{
2122
public:
2223
explicit json_irept(bool include_comments);
23-
void convert_from_irep(const irept &irep, jsont &json) const;
24-
void convert_from_json(const jsont &, irept &) const;
24+
json_objectt convert_from_irep(const irept &) const;
25+
irept convert_from_json(const jsont &) const;
2526

2627
private:
2728
void convert_sub_tree(

0 commit comments

Comments
 (0)