Skip to content

Commit aca88ca

Browse files
author
Daniel Kroening
authored
Merge pull request #269 from danpoe/vcc-json-output
Output vccs in json format
2 parents 0486245 + a14a30d commit aca88ca

File tree

2 files changed

+105
-26
lines changed

2 files changed

+105
-26
lines changed

src/cbmc/bmc.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ class bmct:public safety_checkert
8989
void do_conversion();
9090

9191
virtual void show_vcc();
92+
virtual void show_vcc_plain(std::ostream &out);
93+
virtual void show_vcc_json(std::ostream &out);
94+
9295
virtual resultt all_properties(
9396
const goto_functionst &goto_functions,
9497
prop_convt &solver);
9598
virtual resultt stop_on_fail(
9699
const goto_functionst &goto_functions,
97100
prop_convt &solver);
98-
virtual void show_vcc(std::ostream &out);
99101
virtual void show_program();
100102
virtual void report_success();
101103
virtual void report_failure();

src/cbmc/show_vcc.cpp

+102-25
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ Author: Daniel Kroening, [email protected]
1515

1616
#include <ansi-c/ansi_c_language.h>
1717

18+
#include <util/json.h>
19+
#include <util/json_expr.h>
20+
1821
#include "bmc.h"
1922

2023
/*******************************************************************\
2124
22-
Function: bmct::show_vcc
25+
Function: bmct::show_vcc_plain
2326
2427
Inputs:
2528
@@ -29,21 +32,8 @@ Function: bmct::show_vcc
2932
3033
\*******************************************************************/
3134

32-
void bmct::show_vcc(std::ostream &out)
35+
void bmct::show_vcc_plain(std::ostream &out)
3336
{
34-
switch(ui)
35-
{
36-
case ui_message_handlert::XML_UI:
37-
error() << "not supported" << eom;
38-
return;
39-
40-
case ui_message_handlert::PLAIN:
41-
break;
42-
43-
default:
44-
assert(false);
45-
}
46-
4737
out << "\n" << "VERIFICATION CONDITIONS:" << "\n" << "\n";
4838

4939
languagest languages(ns, new_ansi_c_language());
@@ -99,6 +89,75 @@ void bmct::show_vcc(std::ostream &out)
9989

10090
/*******************************************************************\
10191
92+
Function: bmct::show_vcc_json
93+
94+
Inputs:
95+
96+
Outputs:
97+
98+
Purpose:
99+
100+
\*******************************************************************/
101+
102+
void bmct::show_vcc_json(std::ostream &out)
103+
{
104+
json_objectt json_result;
105+
106+
json_arrayt &json_vccs=json_result["vccs"].make_array();
107+
108+
languagest languages(ns, new_ansi_c_language());
109+
110+
bool has_threads=equation.has_threads();
111+
112+
for(symex_target_equationt::SSA_stepst::iterator
113+
s_it=equation.SSA_steps.begin();
114+
s_it!=equation.SSA_steps.end();
115+
s_it++)
116+
{
117+
if(!s_it->is_assert()) continue;
118+
119+
// vcc object
120+
json_objectt &object=json_vccs.push_back(jsont()).make_object();
121+
122+
const source_locationt &source_location=s_it->source.pc->source_location;
123+
if(source_location.is_not_nil())
124+
object["sourceLocation"]=json(source_location);
125+
126+
const std::string &s=s_it->comment;
127+
if(!s.empty())
128+
object["comment"]=json_stringt(s);
129+
130+
// we show everything in case there are threads
131+
symex_target_equationt::SSA_stepst::const_iterator
132+
last_it=has_threads?equation.SSA_steps.end():s_it;
133+
134+
json_arrayt &json_constraints=object["constraints"].make_array();
135+
136+
for(symex_target_equationt::SSA_stepst::const_iterator p_it
137+
=equation.SSA_steps.begin();
138+
p_it!=last_it; p_it++)
139+
{
140+
if((p_it->is_assume() ||
141+
p_it->is_assignment() ||
142+
p_it->is_constraint()) &&
143+
!p_it->ignore)
144+
{
145+
std::string string_value;
146+
languages.from_expr(p_it->cond_expr, string_value);
147+
json_constraints.push_back(json_stringt(string_value));
148+
}
149+
}
150+
151+
std::string string_value;
152+
languages.from_expr(s_it->cond_expr, string_value);
153+
object["expression"]=json_stringt(string_value);
154+
}
155+
156+
out << ",\n" << json_result;
157+
}
158+
159+
/*******************************************************************\
160+
102161
Function: bmct::show_vcc
103162
104163
Inputs:
@@ -112,16 +171,34 @@ Function: bmct::show_vcc
112171
void bmct::show_vcc()
113172
{
114173
const std::string &filename=options.get_option("outfile");
115-
116-
if(filename.empty() || filename=="-")
117-
show_vcc(std::cout);
118-
else
174+
bool have_file=!filename.empty() && filename!="-";
175+
176+
std::ofstream of;
177+
178+
if(have_file)
119179
{
120-
std::ofstream out(filename.c_str());
121-
if(!out)
122-
std::cerr << "failed to open " << filename << std::endl;
123-
else
124-
show_vcc(out);
180+
of.open(filename);
181+
if(!of)
182+
throw "failed to open file "+filename;
183+
}
184+
185+
std::ostream &out=have_file?of:std::cout;
186+
187+
switch(ui)
188+
{
189+
case ui_message_handlert::XML_UI:
190+
error() << "XML UI not supported" << eom;
191+
return;
192+
193+
case ui_message_handlert::JSON_UI:
194+
show_vcc_json(out);
195+
break;
196+
197+
case ui_message_handlert::PLAIN:
198+
show_vcc_plain(out);
199+
break;
125200
}
126-
}
127201

202+
if(have_file)
203+
of.close();
204+
}

0 commit comments

Comments
 (0)