Skip to content

Commit cf62e25

Browse files
authored
Merge pull request diffblue#227 from diffblue/feature/adding_goto_statistics_to_goto-instrument
SEC-41 : Adding computation and save of goto program statistics.
2 parents 7a3f285 + 478e23c commit cf62e25

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

cbmc/src/goto-instrument/goto_instrument_parse_options.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Author: Daniel Kroening, [email protected]
4242
#include <goto-programs/parameter_assignments.h>
4343
#include <goto-programs/slice_global_inits.h>
4444
#include <goto-programs/show_symbol_table.h>
45+
#include <goto-programs/goto_statistics.h>
4546

4647
#include <pointer-analysis/value_set_analysis.h>
4748
#include <pointer-analysis/goto_program_dereference.h>
@@ -61,6 +62,8 @@ Author: Daniel Kroening, [email protected]
6162
#include <analyses/constant_propagator.h>
6263
#include <analyses/is_threaded.h>
6364

65+
#include <util/file_util.h>
66+
6467
#include <cbmc/version.h>
6568

6669
#include "document_properties.h"
@@ -127,6 +130,29 @@ int goto_instrument_parse_optionst::doit()
127130
return 0;
128131
}
129132

133+
if(cmdline.isset("save-code-statistics"))
134+
{
135+
// Here we only early-check for requirements and we handle the option later.
136+
const std::string out_json_file_pathname=
137+
cmdline.get_value("save-code-statistics");
138+
if(fileutl_is_directory(out_json_file_pathname))
139+
{
140+
error() << "The path-name '" << out_json_file_pathname
141+
<< "'passed to the option '--save-code-statistics' "
142+
"represents an existing directory."
143+
<< eom;
144+
return 12;
145+
}
146+
if(fileutl_parse_extension_in_pathname(out_json_file_pathname)!=".json")
147+
{
148+
error() << "The file of the path-name '" << out_json_file_pathname
149+
<< "'passed to the option '--save-code-statistics' does "
150+
"not have '.json' extension."
151+
<< eom;
152+
return 13;
153+
}
154+
}
155+
130156
eval_verbosity();
131157

132158
try
@@ -738,6 +764,31 @@ int goto_instrument_parse_optionst::doit()
738764
undefined_function_abort_path(goto_model);
739765
}
740766

767+
if(cmdline.isset("save-code-statistics"))
768+
{
769+
goto_statisticst stats;
770+
stats.extend(goto_model);
771+
const std::string out_json_file_pathname=
772+
cmdline.get_value("save-code-statistics");
773+
INVARIANT(!fileutl_is_directory(out_json_file_pathname),
774+
"The early check passed so the JSON file indeed should not be "
775+
" a directory.");
776+
INVARIANT(
777+
fileutl_parse_extension_in_pathname(out_json_file_pathname)==".json",
778+
"The early check passed so the JSON file indeed should have "
779+
"'.json' extension.");
780+
std::ofstream ofile(out_json_file_pathname);
781+
if(!ofile)
782+
{
783+
error() << "Failed to open the JSON file '" << out_json_file_pathname
784+
<< "' passed to the option '--save-code-statistics' "
785+
<< "for writing."
786+
<< eom;
787+
return 13;
788+
}
789+
ofile << to_json(stats);
790+
}
791+
741792
// write new binary?
742793
if(cmdline.args.size()==2)
743794
{
@@ -1450,6 +1501,9 @@ void goto_instrument_parse_optionst::help()
14501501
" --list-calls-args list all function calls with their arguments\n"
14511502
// NOLINTNEXTLINE(whitespace/line_length)
14521503
" --print-path-lengths print statistics about control-flow graph paths\n"
1504+
" --save-code-statistics saves a json file with basic statistical data of"
1505+
" the analysed program after all analyses and\n"
1506+
" transformations have been performed on it.\n"
14531507
"\n"
14541508
"Safety checks:\n"
14551509
" --no-assertions ignore user assertions\n"

cbmc/src/goto-instrument/goto_instrument_parse_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Author: Daniel Kroening, [email protected]
5959
"(show-uninitialized)(show-locations)" \
6060
"(full-slice)(reachability-slice)(slice-global-inits)" \
6161
"(inline)(partial-inline)(function-inline):(log):(no-caching)" \
62+
"(save-code-statistics):" \
6263
OPT_REMOVE_CONST_FUNCTION_POINTERS \
6364
"(print-internal-representation)" \
6465
"(remove-function-pointers)" \

0 commit comments

Comments
 (0)