Skip to content

Commit 24fd582

Browse files
author
Peter Schrammel
committed
basic json-ui
1 parent d0219ba commit 24fd582

14 files changed

+361
-81
lines changed

src/cbmc/bmc.cpp

+25-21
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include <util/source_location.h>
1616
#include <util/time_stopping.h>
1717
#include <util/message_stream.h>
18+
#include <util/json.h>
1819

1920
#include <langapi/mode.h>
2021
#include <langapi/languages.h>
@@ -87,8 +88,14 @@ void bmct::error_trace()
8788
}
8889
break;
8990

90-
default:
91-
assert(false);
91+
case ui_message_handlert::JSON_UI:
92+
{
93+
json_objectt counterexample;
94+
jsont &json_trace=counterexample["counterexample"];
95+
convert(ns, goto_trace, json_trace);
96+
std::cout << ",\n" << counterexample << "\n";
97+
}
98+
break;
9299
}
93100

94101
const std::string graphml=options.get_option("graphml-cex");
@@ -105,22 +112,6 @@ void bmct::error_trace()
105112
write_graphml(cex_graph, out);
106113
}
107114
}
108-
109-
if(options.get_option("json-cex")!="")
110-
{
111-
jsont json_trace;
112-
convert(ns, goto_trace, json_trace);
113-
114-
if(options.get_option("json-cex")=="-")
115-
{
116-
std::cout << json_trace;
117-
}
118-
else
119-
{
120-
std::ofstream out(options.get_option("json-cex").c_str());
121-
out << json_trace << '\n';
122-
}
123-
}
124115
}
125116

126117
/*******************************************************************\
@@ -223,6 +214,14 @@ void bmct::report_success()
223214
std::cout << "\n";
224215
}
225216
break;
217+
218+
case ui_message_handlert::JSON_UI:
219+
{
220+
json_objectt json_result;
221+
json_result["cProverStatus"]=json_stringt("success");
222+
std::cout << ",\n" << json_result;
223+
}
224+
break;
226225

227226
default:
228227
assert(false);
@@ -258,9 +257,14 @@ void bmct::report_failure()
258257
std::cout << "\n";
259258
}
260259
break;
261-
262-
default:
263-
assert(false);
260+
261+
case ui_message_handlert::JSON_UI:
262+
{
263+
json_objectt json_result;
264+
json_result["cProverStatus"]=json_stringt("failure");
265+
std::cout << ",\n" << json_result;
266+
}
267+
break;
264268
}
265269
}
266270

src/cbmc/cbmc_parse_options.cpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Author: Daniel Kroening, [email protected]
3838
#include <goto-programs/loop_ids.h>
3939
#include <goto-programs/link_to_library.h>
4040
#include <goto-programs/remove_skip.h>
41+
#include <goto-programs/show_goto_functions.h>
4142

4243
#include <goto-instrument/full_slicer.h>
4344
#include <goto-instrument/nondet_static.h>
@@ -442,9 +443,6 @@ void cbmc_parse_optionst::get_command_line_options(optionst &options)
442443

443444
if(cmdline.isset("graphml-cex"))
444445
options.set_option("graphml-cex", cmdline.get_value("graphml-cex"));
445-
446-
if(cmdline.isset("json-cex"))
447-
options.set_option("json-cex", cmdline.get_value("json-cex"));
448446
}
449447

450448
/*******************************************************************\
@@ -652,15 +650,17 @@ int cbmc_parse_optionst::get_goto_program(
652650

653651
if(!infile)
654652
{
655-
error() << "failed to open input file `" << filename << "'" << eom;
653+
error() << "failed to open input file `"
654+
<< filename << "'" << eom;
656655
return 6;
657656
}
658657

659658
languaget *language=get_language_from_filename(filename);
660659

661660
if(language==NULL)
662661
{
663-
error() << "failed to figure out type of file `" << filename << "'" << eom;
662+
error() << "failed to figure out type of file `"
663+
<< filename << "'" << eom;
664664
return 6;
665665
}
666666

@@ -715,7 +715,8 @@ int cbmc_parse_optionst::get_goto_program(
715715
{
716716
status() << "Reading GOTO program from file " << eom;
717717

718-
if(read_object_and_link(*it, symbol_table, goto_functions, get_message_handler()))
718+
if(read_object_and_link(*it, symbol_table, goto_functions,
719+
get_message_handler()))
719720
return 6;
720721
}
721722

@@ -964,7 +965,8 @@ bool cbmc_parse_optionst::process_goto_program(
964965
// show it?
965966
if(cmdline.isset("show-goto-functions"))
966967
{
967-
goto_functions.output(ns, std::cout);
968+
namespacet ns(symbol_table);
969+
show_goto_functions(ns, get_ui(), goto_functions);
968970
return true;
969971
}
970972
}
@@ -1164,5 +1166,6 @@ void cbmc_parse_optionst::help()
11641166
" --version show version and exit\n"
11651167
" --xml-ui use XML-formatted output\n"
11661168
" --xml-interface bi-directional XML interface\n"
1169+
" --json-ui use JSON-formatted output\n"
11671170
"\n";
11681171
}

src/cbmc/cbmc_parse_options.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class optionst;
3131
"(bounds-check)(pointer-check)(div-by-zero-check)(memory-leak-check)" \
3232
"(signed-overflow-check)(unsigned-overflow-check)(float-overflow-check)(nan-check)" \
3333
"(no-assertions)(no-assumptions)" \
34-
"(xml-ui)(xml-interface)" \
34+
"(xml-ui)(xml-interface)(json-ui)" \
3535
"(smt1)(smt2)(fpa)(cvc3)(cvc4)(boolector)(yices)(z3)(opensmt)(mathsat)" \
3636
"(no-sat-preprocessor)" \
3737
"(no-pretty-names)(beautify)" \
@@ -52,7 +52,7 @@ class optionst;
5252
"(arrays-uf-always)(arrays-uf-never)" \
5353
"(string-abstraction)(no-arch)(arch):" \
5454
"(round-to-nearest)(round-to-plus-inf)(round-to-minus-inf)(round-to-zero)" \
55-
"(graphml-cex):(json-cex):" \
55+
"(graphml-cex):" \
5656
"(floatbv)(all-claims)(all-properties)(decide)" // legacy, and will eventually disappear
5757

5858
class cbmc_parse_optionst:

src/goto-instrument/goto_instrument_parse_options.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Author: Daniel Kroening, [email protected]
3232
#include <goto-programs/remove_asm.h>
3333
#include <goto-programs/remove_unused_functions.h>
3434
#include <goto-programs/parameter_assignments.h>
35+
#include <goto-programs/show_goto_functions.h>
3536

3637
#include <pointer-analysis/value_set_analysis.h>
3738
#include <pointer-analysis/goto_program_dereference.h>
@@ -470,7 +471,7 @@ int goto_instrument_parse_optionst::doit()
470471
if(cmdline.isset("show-goto-functions"))
471472
{
472473
namespacet ns(symbol_table);
473-
goto_functions.output(ns, std::cout);
474+
show_goto_functions(ns, get_ui(), goto_functions);
474475
return 0;
475476
}
476477

@@ -1347,5 +1348,6 @@ void goto_instrument_parse_optionst::help()
13471348
" --use-system-headers with --dump-c/--dump-cpp: generate C source with includes\n"
13481349
" --version show version and exit\n"
13491350
" --xml-ui use XML-formatted output\n"
1351+
" --json-ui use JSON-formatted output\n"
13501352
"\n";
13511353
}

src/goto-instrument/goto_instrument_parse_options.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Author: Daniel Kroening, [email protected]
5454
"(cav11)" \
5555
"(show-natural-loops)(accelerate)(havoc-loops)" \
5656
"(error-label):(string-abstraction)" \
57-
"(verbosity):(version)(xml-ui)(show-loops)" \
57+
"(verbosity):(version)(xml-ui)(json-ui)(show-loops)" \
5858
"(accelerate)(constant-propagator)" \
5959
"(k-induction):(step-case)(base-case)" \
6060
"(show-call-sequences)(check-call-sequence)" \

src/goto-programs/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ SRC = goto_convert.cpp goto_convert_function_call.cpp \
1616
remove_returns.cpp osx_fat_reader.cpp remove_complex.cpp \
1717
goto_trace.cpp xml_goto_trace.cpp vcd_goto_trace.cpp \
1818
graphml_goto_trace.cpp remove_virtual_functions.cpp \
19-
class_hierarchy.cpp
19+
class_hierarchy.cpp show_goto_functions.cpp
2020

2121
INCLUDES= -I ..
2222

src/goto-programs/read_goto_binary.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ bool read_object_and_link(
370370
goto_functionst &functions,
371371
message_handlert &message_handler)
372372
{
373-
message_handler.print(8, "Reading: " + file_name);
373+
messaget(message_handler).statistics() << "Reading: "
374+
<< file_name << messaget::eom;
374375

375376
// we read into a temporary model
376377
goto_modelt temp_model;

src/goto-programs/remove_unused_functions.cpp

+30-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ Function: remove_unused_functions
2323
\*******************************************************************/
2424

2525
void remove_unused_functions(
26+
const irep_idt &start,
2627
goto_functionst &functions,
2728
message_handlert &message_handler)
2829
{
2930
std::set<irep_idt> used_functions;
3031
std::list<goto_functionst::function_mapt::iterator> unused_functions;
31-
find_used_functions(goto_functionst::entry_point(), functions, used_functions);
32+
find_used_functions(start, functions, used_functions);
33+
34+
//maybe an error should be thrown in this case
35+
if(functions.function_map.find(start) == functions.function_map.end())
36+
used_functions.clear();
3237

3338
for(goto_functionst::function_mapt::iterator it=
3439
functions.function_map.begin();
@@ -39,8 +44,11 @@ void remove_unused_functions(
3944
unused_functions.push_back(it);
4045
}
4146

47+
assert(unused_functions.size() + used_functions.size() ==
48+
functions.function_map.size());
49+
4250
messaget message(message_handler);
43-
51+
4452
if(unused_functions.size()>0)
4553
{
4654
message.statistics()
@@ -58,6 +66,26 @@ void remove_unused_functions(
5866

5967
/*******************************************************************\
6068
69+
Function: remove_unused_functions
70+
71+
Inputs:
72+
73+
Outputs:
74+
75+
Purpose:
76+
77+
\*******************************************************************/
78+
79+
void remove_unused_functions(
80+
goto_functionst &functions,
81+
message_handlert &message_handler)
82+
{
83+
remove_unused_functions(goto_functionst::entry_point(),
84+
functions, message_handler);
85+
}
86+
87+
/*******************************************************************\
88+
6189
Function: find_used_functions
6290
6391
Inputs:

src/goto-programs/remove_unused_functions.h

+6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ Author: CM Wintersteiger
1010
#define CPROVER_GOTO_PROGRAMS_REMOVE_UNUSED_FUNCTIONS_H
1111

1212
#include <util/message.h>
13+
#include <util/config.h>
1314

1415
#include <goto-programs/goto_functions.h>
1516

1617
void remove_unused_functions(
1718
goto_functionst &functions,
1819
message_handlert &message_handler);
1920

21+
void remove_unused_functions(
22+
const irep_idt &current,
23+
goto_functionst &functions,
24+
message_handlert &message_handler);
25+
2026
void find_used_functions(
2127
const irep_idt &current,
2228
goto_functionst &functions,
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*******************************************************************\
2+
3+
Module: Show goto functions
4+
5+
Author: Peter Schrammel
6+
7+
\*******************************************************************/
8+
9+
#include <iostream>
10+
11+
#include <util/xml.h>
12+
#include <util/json.h>
13+
#include <util/i2string.h>
14+
#include <util/xml_expr.h>
15+
16+
#include <langapi/language_util.h>
17+
18+
#include "show_goto_functions.h"
19+
#include "goto_functions.h"
20+
#include "goto_model.h"
21+
22+
/*******************************************************************\
23+
24+
Function: cbmc_parseoptionst::show_goto_functions
25+
26+
Inputs:
27+
28+
Outputs:
29+
30+
Purpose:
31+
32+
\*******************************************************************/
33+
34+
void show_goto_functions(
35+
const namespacet &ns,
36+
ui_message_handlert::uit ui,
37+
const goto_functionst &goto_functions)
38+
{
39+
switch(ui)
40+
{
41+
case ui_message_handlert::XML_UI:
42+
{
43+
//This only prints the list of functions
44+
xmlt xml_functions("functions");
45+
for(goto_functionst::function_mapt::const_iterator
46+
it=goto_functions.function_map.begin();
47+
it!=goto_functions.function_map.end();
48+
it++)
49+
{
50+
xmlt &xml_function=xml_functions.new_element("function");
51+
xml_function.set_attribute("name", id2string(it->first));
52+
}
53+
54+
std::cout << xml_functions << std::endl;
55+
}
56+
break;
57+
58+
case ui_message_handlert::JSON_UI:
59+
{
60+
//This only prints the list of functions
61+
json_arrayt json_functions;
62+
for(goto_functionst::function_mapt::const_iterator
63+
it=goto_functions.function_map.begin();
64+
it!=goto_functions.function_map.end();
65+
it++)
66+
{
67+
json_objectt &json_function=
68+
json_functions.push_back(jsont()).make_object();
69+
json_function["name"]=json_stringt(id2string(it->first));
70+
}
71+
json_objectt json_result;
72+
json_result["functions"]=json_functions;
73+
std::cout << ",\n" << json_result;
74+
}
75+
break;
76+
77+
case ui_message_handlert::PLAIN:
78+
goto_functions.output(ns, std::cout);
79+
break;
80+
}
81+
}
82+
83+
/*******************************************************************\
84+
85+
Function: show_goto_functions
86+
87+
Inputs:
88+
89+
Outputs:
90+
91+
Purpose:
92+
93+
\*******************************************************************/
94+
95+
void show_goto_functions(
96+
const goto_modelt &goto_model,
97+
ui_message_handlert::uit ui)
98+
{
99+
const namespacet ns(goto_model.symbol_table);
100+
show_goto_functions(ns, ui, goto_model.goto_functions);
101+
}
102+
103+
104+

0 commit comments

Comments
 (0)