Skip to content

Commit 64df79e

Browse files
committed
Enable construction of JSON arrays and objects in place
This enables RAII and gets us close to writing JSON in source code while still being efficient.
1 parent 29a7055 commit 64df79e

17 files changed

+169
-164
lines changed

jbmc/src/jbmc/jbmc_parse_options.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,7 @@ int jbmc_parse_optionst::doit()
453453
break;
454454
case ui_message_handlert::uit::JSON_UI:
455455
{
456-
json_objectt json_options;
457-
json_options["options"] = options.to_json();
456+
json_objectt json_options({{"options", options.to_json()}});
458457
debug() << json_options;
459458
break;
460459
}

src/analyses/ai.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,17 @@ jsont ai_baset::output_json(
9797

9898
forall_goto_program_instructions(i_it, goto_program)
9999
{
100-
json_objectt location;
101-
location["locationNumber"]=
102-
json_numbert(std::to_string(i_it->location_number));
103-
location["sourceLocation"]=
104-
json_stringt(i_it->source_location.as_string());
105-
location["abstractState"] =
106-
abstract_state_before(i_it)->output_json(*this, ns);
107-
108100
// Ideally we need output_instruction_json
109101
std::ostringstream out;
110102
goto_program.output_instruction(ns, identifier, out, *i_it);
111-
location["instruction"]=json_stringt(out.str());
112103

113-
contents.push_back(location);
104+
json_objectt location(
105+
{{"locationNumber", json_numbert(std::to_string(i_it->location_number))},
106+
{"sourceLocation", json_stringt(i_it->source_location.as_string())},
107+
{"abstractState", abstract_state_before(i_it)->output_json(*this, ns)},
108+
{"instruction", json_stringt(out.str())}});
109+
110+
contents.push_back(std::move(location));
114111
}
115112

116113
return std::move(contents);

src/analyses/dependence_graph.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,21 +284,20 @@ jsont dep_graph_domaint::output_json(
284284

285285
for(const auto &cd : control_deps)
286286
{
287-
json_objectt &link=graph.push_back().make_object();
288-
link["locationNumber"]=
289-
json_numbert(std::to_string(cd->location_number));
290-
link["sourceLocation"]=json(cd->source_location);
291-
link["type"]=json_stringt("control");
287+
json_objectt link(
288+
{{"locationNumber", json_numbert(std::to_string(cd->location_number))},
289+
{"sourceLocation", json(cd->source_location)},
290+
{"type", json_stringt("control")}});
291+
graph.push_back(std::move(link));
292292
}
293293

294294
for(const auto &dd : data_deps)
295295
{
296-
json_objectt &link=graph.push_back().make_object();
297-
link["locationNumber"]=
298-
json_numbert(std::to_string(dd->location_number));
299-
link["sourceLocation"]=json(dd->source_location);
300-
json_stringt(dd->source_location.as_string());
301-
link["type"]=json_stringt("data");
296+
json_objectt link(
297+
{{"locationNumber", json_numbert(std::to_string(dd->location_number))},
298+
{"sourceLocation", json(dd->source_location)},
299+
{"type", json_stringt("data")}});
300+
graph.push_back(std::move(link));
302301
}
303302

304303
return std::move(graph);

src/cbmc/bmc.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ void bmct::report_success(messaget &log, ui_message_handlert &handler)
176176

177177
case ui_message_handlert::uit::JSON_UI:
178178
{
179-
json_objectt json_result;
180-
json_result["cProverStatus"]=json_stringt("success");
179+
json_objectt json_result({{"cProverStatus", json_stringt("success")}});
181180
log.result() << json_result;
182181
}
183182
break;
@@ -208,8 +207,7 @@ void bmct::report_failure(messaget &log, ui_message_handlert &handler)
208207

209208
case ui_message_handlert::uit::JSON_UI:
210209
{
211-
json_objectt json_result;
212-
json_result["cProverStatus"]=json_stringt("failure");
210+
json_objectt json_result({{"cProverStatus", json_stringt("failure")}});
213211
log.result() << json_result;
214212
}
215213
break;

src/cbmc/bmc_cover.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ bool bmc_covert::operator()()
365365
json_arrayt &tests_array=json_result["tests"].make_array();
366366
for(const auto &test : tests)
367367
{
368-
json_objectt &result=tests_array.push_back().make_object();
369368
if(bmc.options.get_bool_option("trace"))
370369
{
371370
json_arrayt &json_trace = json_result["trace"].make_array();
@@ -379,20 +378,21 @@ bool bmc_covert::operator()()
379378
{
380379
if(step.is_input())
381380
{
382-
json_objectt json_input;
383-
json_input["id"] = json_stringt(step.io_id);
381+
json_objectt json_input({{"id", json_stringt(step.io_id)}});
384382
if(step.io_args.size()==1)
385383
json_input["value"]=
386384
json(step.io_args.front(), bmc.ns, ID_unknown);
387-
json_test.push_back(json_input);
385+
json_test.push_back(std::move(json_input));
388386
}
389387
}
390388
}
391-
json_arrayt &goal_refs=result["coveredGoals"].make_array();
389+
json_arrayt goal_refs;
392390
for(const auto &goal_id : test.covered_goals)
393391
{
394392
goal_refs.push_back(json_stringt(goal_id));
395393
}
394+
tests_array.push_back(
395+
json_objectt({{"coveredGoals", std::move(goal_refs)}}));
396396
}
397397

398398
break;

src/goto-analyzer/taint_analysis.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,13 @@ bool taint_analysist::operator()(
352352

353353
if(use_json)
354354
{
355-
json_objectt json;
356-
json["bugClass"] =
357-
json_stringt(i_it->source_location.get_property_class());
358-
json["file"] = json_stringt(i_it->source_location.get_file());
359-
json["line"]=
360-
json_numbert(id2string(i_it->source_location.get_line()));
361-
json_result.push_back(json);
355+
json_objectt json(
356+
{{"bugClass",
357+
json_stringt(i_it->source_location.get_property_class())},
358+
{"file", json_stringt(i_it->source_location.get_file())},
359+
{"line",
360+
json_numbert(id2string(i_it->source_location.get_line()))}});
361+
json_result.push_back(std::move(json));
362362
}
363363
else
364364
{

src/goto-analyzer/unreachable_instructions.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,19 @@ static void add_to_json(
117117
const dead_mapt &dead_map,
118118
json_arrayt &dest)
119119
{
120-
json_objectt &entry=dest.push_back().make_object();
121-
122120
PRECONDITION(!goto_program.instructions.empty());
123121
goto_programt::const_targett end_function=
124122
goto_program.instructions.end();
125123
--end_function;
126124
DATA_INVARIANT(end_function->is_end_function(),
127125
"The last instruction in a goto-program must be END_FUNCTION");
128126

129-
entry["function"] = json_stringt(end_function->function);
130-
entry["fileName"]=
131-
json_stringt(concat_dir_file(
127+
json_objectt entry(
128+
{{"function", json_stringt(end_function->function)},
129+
{"fileName",
130+
json_stringt(concat_dir_file(
132131
id2string(end_function->source_location.get_working_directory()),
133-
id2string(end_function->source_location.get_file())));
132+
id2string(end_function->source_location.get_file())))}});
134133

135134
json_arrayt &dead_ins=entry["unreachableInstructions"].make_array();
136135

@@ -152,11 +151,13 @@ static void add_to_json(
152151
s.erase(s.size()-1);
153152

154153
// print info for file actually with full path
155-
json_objectt &i_entry=dead_ins.push_back().make_object();
156154
const source_locationt &l=it->second->source_location;
157-
i_entry["sourceLocation"]=json(l);
158-
i_entry["statement"]=json_stringt(s);
155+
json_objectt i_entry(
156+
{{"sourceLocation", json(l)}, {"statement", json_stringt(s)}});
157+
dead_ins.push_back(std::move(i_entry));
159158
}
159+
160+
dest.push_back(std::move(entry));
160161
}
161162

162163
void unreachable_instructions(
@@ -255,17 +256,16 @@ static void json_output_function(
255256
const source_locationt &last_location,
256257
json_arrayt &dest)
257258
{
258-
json_objectt &entry=dest.push_back().make_object();
259-
260-
entry["function"] = json_stringt(function);
261-
entry["file name"]=
262-
json_stringt(concat_dir_file(
259+
json_objectt entry(
260+
{{"function", json_stringt(function)},
261+
{"file name",
262+
json_stringt(concat_dir_file(
263263
id2string(first_location.get_working_directory()),
264-
id2string(first_location.get_file())));
265-
entry["first line"]=
266-
json_numbert(id2string(first_location.get_line()));
267-
entry["last line"]=
268-
json_numbert(id2string(last_location.get_line()));
264+
id2string(first_location.get_file())))},
265+
{"first line", json_numbert(id2string(first_location.get_line()))},
266+
{"last line", json_numbert(id2string(last_location.get_line()))}});
267+
268+
dest.push_back(std::move(entry));
269269
}
270270

271271
static void xml_output_function(

src/goto-diff/goto_diff_base.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ void goto_difft::output_functions() const
3838
}
3939
case ui_message_handlert::uit::JSON_UI:
4040
{
41-
json_objectt json_result;
42-
json_result["totalNumberOfFunctions"]=
43-
json_stringt(std::to_string(total_functions_count));
41+
json_objectt json_result(
42+
{{"totalNumberOfFunctions",
43+
json_stringt(std::to_string(total_functions_count))}});
4444
convert_function_group_json(
4545
json_result["newFunctions"].make_array(), new_functions, goto_model2);
4646
convert_function_group_json(

src/goto-instrument/unwind.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,16 @@ jsont goto_unwindt::unwind_logt::output_log_json() const
336336
for(location_mapt::const_iterator it=location_map.begin();
337337
it!=location_map.end(); it++)
338338
{
339-
json_objectt &object=json_unwound.push_back().make_object();
340-
341339
goto_programt::const_targett target=it->first;
342340
unsigned location_number=it->second;
343341

344-
object["originalLocationNumber"]=json_numbert(std::to_string(
345-
location_number));
346-
object["newLocationNumber"]=json_numbert(std::to_string(
347-
target->location_number));
342+
json_objectt object(
343+
{{"originalLocationNumber",
344+
json_numbert(std::to_string(location_number))},
345+
{"newLocationNumber",
346+
json_numbert(std::to_string(target->location_number))}});
347+
348+
json_unwound.push_back(std::move(object));
348349
}
349350

350351
return std::move(json_result);

src/goto-programs/goto_inline_class.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -881,26 +881,25 @@ jsont goto_inlinet::goto_inline_logt::output_inline_log_json() const
881881

882882
for(const auto &it : log_map)
883883
{
884-
json_objectt &object=json_inlined.push_back().make_object();
885-
886884
goto_programt::const_targett start=it.first;
887885
const goto_inline_log_infot &info=it.second;
888886
goto_programt::const_targett end=info.end;
889887

890888
PRECONDITION(start->location_number <= end->location_number);
891889

892-
object["call"]=json_numbert(std::to_string(info.call_location_number));
893-
object["function"]=json_stringt(info.function.c_str());
890+
json_arrayt json_orig(
891+
{json_numbert(std::to_string(info.begin_location_number)),
892+
json_numbert(std::to_string(info.end_location_number))});
893+
json_arrayt json_new({json_numbert(std::to_string(start->location_number)),
894+
json_numbert(std::to_string(end->location_number))});
894895

895-
json_arrayt &json_orig=object["originalSegment"].make_array();
896-
json_orig.push_back()=json_numbert(std::to_string(
897-
info.begin_location_number));
898-
json_orig.push_back()=json_numbert(std::to_string(
899-
info.end_location_number));
896+
json_objectt object(
897+
{{"call", json_numbert(std::to_string(info.call_location_number))},
898+
{"function", json_stringt(info.function.c_str())},
899+
{"originalSegment", std::move(json_orig)},
900+
{"inlinedSegment", std::move(json_new)}});
900901

901-
json_arrayt &json_new=object["inlinedSegment"].make_array();
902-
json_new.push_back()=json_numbert(std::to_string(start->location_number));
903-
json_new.push_back()=json_numbert(std::to_string(end->location_number));
902+
json_inlined.push_back(std::move(object));
904903
}
905904

906905
return std::move(json_result);

src/goto-programs/json_goto_trace.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,10 @@ void convert_return(
282282
: step.function;
283283

284284
const symbolt &symbol = ns.lookup(function_identifier);
285-
json_objectt &json_function = json_call_return["function"].make_object();
286-
json_function["displayName"] = json_stringt(symbol.display_name());
287-
json_function["identifier"] = json_stringt(function_identifier);
288-
json_function["sourceLocation"] = json(symbol.location);
285+
json_call_return["function"] =
286+
json_objectt({{"displayName", json_stringt(symbol.display_name())},
287+
{"identifier", json_stringt(function_identifier)},
288+
{"sourceLocation", json(symbol.location)}});
289289

290290
if(!location.is_null())
291291
json_call_return["sourceLocation"] = location;

src/goto-programs/loop_ids.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ void show_loop_ids_json(
8888
std::string id =
8989
id2string(function_identifier) + "." + std::to_string(loop_id);
9090

91-
json_objectt &loop=loops.push_back().make_object();
92-
loop["name"]=json_stringt(id);
93-
loop["sourceLocation"]=json(it->source_location);
91+
loops.push_back(
92+
json_objectt({{"name", json_stringt(id)},
93+
{"sourceLocation", json(it->source_location)}}));
9494
}
9595
}
9696
}

src/goto-programs/show_goto_functions_json.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,8 @@ json_objectt show_goto_functions_jsont::convert(
7171
for(const goto_programt::instructiont &instruction :
7272
function.body.instructions)
7373
{
74-
json_objectt instruction_entry=json_objectt();
75-
76-
instruction_entry["instructionId"]=
77-
json_stringt(instruction.to_string());
74+
json_objectt instruction_entry(
75+
{{"instructionId", json_stringt(instruction.to_string())}});
7876

7977
if(instruction.code.source_location().is_not_nil())
8078
{
@@ -99,7 +97,7 @@ json_objectt show_goto_functions_jsont::convert(
9997
operand);
10098
operand_array.push_back(operand_object);
10199
}
102-
instruction_entry["operands"]=operand_array;
100+
instruction_entry["operands"] = std::move(operand_array);
103101
}
104102

105103
if(!instruction.guard.is_true())
@@ -108,20 +106,17 @@ json_objectt show_goto_functions_jsont::convert(
108106
no_comments_irep_converter.convert_from_irep(
109107
instruction.guard);
110108

111-
instruction_entry["guard"]=guard_object;
109+
instruction_entry["guard"] = std::move(guard_object);
112110
}
113111

114-
json_instruction_array.push_back(instruction_entry);
112+
json_instruction_array.push_back(std::move(instruction_entry));
115113
}
116114

117-
json_function["instructions"]=json_instruction_array;
115+
json_function["instructions"] = std::move(json_instruction_array);
118116
}
119117
}
120118

121-
json_objectt json_result;
122-
json_result["functions"]=json_functions;
123-
124-
return json_result;
119+
return json_objectt({{"functions", json_functions}});
125120
}
126121

127122
/// Print the json object generated by

0 commit comments

Comments
 (0)