Skip to content

Commit 518f87d

Browse files
author
Daniel Kroening
committed
use jsont to produce JSON output; write json into given file
1 parent 7722195 commit 518f87d

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

src/goto-analyzer/goto_analyzer_parse_options.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,17 +487,17 @@ int goto_analyzer_parse_optionst::doit()
487487
{
488488
const namespacet ns(symbol_table);
489489
std::string taint_file=cmdline.get_value("taint");
490-
bool json=cmdline.isset("json");
491490

492491
if(cmdline.isset("show-taint"))
493492
{
494-
taint_analysis(goto_functions, ns, taint_file, get_message_handler(), true, json);
493+
taint_analysis(goto_functions, ns, taint_file, get_message_handler(), true, "");
495494
return 0;
496495
}
497496
else
498497
{
498+
std::string json_file=cmdline.get_value("json");
499499
bool result=
500-
taint_analysis(goto_functions, ns, taint_file, get_message_handler(), false, json);
500+
taint_analysis(goto_functions, ns, taint_file, get_message_handler(), false, json_file);
501501
return result?10:0;
502502
}
503503
}

src/goto-analyzer/goto_analyzer_parse_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class optionst;
3030
"(verbosity):(version)" \
3131
"(gcc)(arch):" \
3232
"(taint):(show-taint)" \
33-
"(json)"
33+
"(json):"
3434

3535
class goto_analyzer_parse_optionst:
3636
public parse_options_baset,

src/goto-analyzer/taint_analysis.cpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ Author: Daniel Kroening, [email protected]
77
\*******************************************************************/
88

99
#include <iostream>
10+
#include <fstream>
1011

1112
#include <util/prefix.h>
1213
#include <util/simplify_expr.h>
14+
#include <util/json.h>
1315

1416
#include <ansi-c/string_constant.h>
1517

@@ -39,7 +41,7 @@ class taint_analysist:public messaget
3941
const std::string &taint_file_name,
4042
goto_functionst &goto_functions,
4143
bool show_full,
42-
bool json);
44+
const std::string &json_file_name);
4345

4446
protected:
4547
const namespacet &ns;
@@ -218,10 +220,13 @@ bool taint_analysist::operator()(
218220
const std::string &taint_file_name,
219221
goto_functionst &goto_functions,
220222
bool show_full,
221-
bool json)
223+
const std::string &json_file_name)
222224
{
223225
try
224226
{
227+
jsont json_result=jsont::json_array();
228+
bool use_json=!json_file_name.empty();
229+
225230
status() << "Reading taint file `" << taint_file_name
226231
<< "'" << eom;
227232

@@ -323,18 +328,18 @@ bool taint_analysist::operator()(
323328
if(first)
324329
{
325330
first=false;
326-
if(!json)
331+
if(!use_json)
327332
std::cout << "\n"
328333
"******** Function " << symbol.display_name() << '\n';
329334
}
330335

331-
if(json)
336+
if(use_json)
332337
{
333-
std::cout << "{\n";
334-
std::cout << " \"bug_class\": \"" << i_it->source_location.get_property_class() << "\",\n";
335-
std::cout << " \"file\": \"" << i_it->source_location.get_file() << "\",\n";
336-
std::cout << " \"line\": " << i_it->source_location.get_line() << "\n";
337-
std::cout << "}\n";
338+
jsont json=jsont::json_object();
339+
json["bug_class"]=jsont::json_string(id2string(i_it->source_location.get_property_class()));
340+
json["file"]=jsont::json_string(id2string(i_it->source_location.get_file()));
341+
json["line"]=jsont::json_number(id2string(i_it->source_location.get_line()));
342+
json_result.array.push_back(json);
338343
}
339344
else
340345
{
@@ -346,7 +351,24 @@ bool taint_analysist::operator()(
346351
}
347352
}
348353
}
354+
355+
if(use_json)
356+
{
357+
std::ofstream json_out(json_file_name);
349358

359+
if(!json_out)
360+
{
361+
error() << "Failed to open json output `"
362+
<< json_file_name << "'" << eom;
363+
return true;
364+
}
365+
366+
status() << "Analysis result is written to `"
367+
<< json_file_name << "'" << eom;
368+
369+
json_out << json_result << '\n';
370+
}
371+
350372
return false;
351373
}
352374
catch(const char *error_msg)
@@ -383,10 +405,11 @@ bool taint_analysis(
383405
const std::string &taint_file_name,
384406
message_handlert &message_handler,
385407
bool show_full,
386-
bool json)
408+
const std::string &json_file_name)
387409
{
388410
taint_analysist taint_analysis(ns);
389411
taint_analysis.set_message_handler(message_handler);
390-
return taint_analysis(taint_file_name, goto_functions, show_full, json);
412+
return taint_analysis(
413+
taint_file_name, goto_functions, show_full, json_file_name);
391414
}
392415

src/goto-analyzer/taint_analysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ bool taint_analysis(
2020
const std::string &taint_file_name,
2121
message_handlert &,
2222
bool show_full,
23-
bool json);
23+
const std::string &json_file_name);
2424

2525
#endif

0 commit comments

Comments
 (0)