Skip to content

Commit b32c1d4

Browse files
authored
Merge pull request diffblue#226 from diffblue/feature/goto_statistics
SEC-41 : Introducing GOTO program statistics.
2 parents 31ce4d4 + e462a0f commit b32c1d4

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*******************************************************************\
2+
3+
Module: goto_statistics.cpp
4+
5+
Author: Marek Trtik
6+
7+
\*******************************************************************/
8+
9+
#include "goto_statistics.h"
10+
#include <sstream>
11+
12+
void goto_statisticst::extend(
13+
const goto_functionst &functions,
14+
const symbol_tablet &table)
15+
{
16+
for(auto const &elem : functions.function_map)
17+
{
18+
for(const auto &instr : elem.second.body.instructions)
19+
{
20+
switch(instr.type)
21+
{
22+
case FUNCTION_CALL:
23+
++num_function_calls;
24+
break;
25+
case ASSERT:
26+
++num_assertions;
27+
break;
28+
case ASSUME:
29+
++num_assumptions;
30+
break;
31+
case GOTO:
32+
if (instr.guard.is_true())
33+
++num_unconditional_gotos;
34+
else
35+
++num_conditional_gotos;
36+
if(instr.is_backwards_goto())
37+
++num_loops;
38+
break;
39+
default:
40+
break;
41+
}
42+
++num_instructions;
43+
}
44+
++num_functions;
45+
}
46+
for(const auto &name_symbol : table.symbols)
47+
{
48+
if(name_symbol.second.is_lvalue && name_symbol.second.is_state_var)
49+
{
50+
if(name_symbol.second.is_auxiliary)
51+
++num_auxiliary_variables;
52+
else
53+
++num_user_variables;
54+
}
55+
}
56+
}
57+
58+
jsont to_json(const goto_statisticst &stats)
59+
{
60+
json_objectt json_stats;
61+
json_stats["num_functions"]=
62+
json_numbert(std::to_string(stats.get_num_functions()));
63+
json_stats["num_instructions"]=
64+
json_numbert(std::to_string(stats.get_num_instructions()));
65+
json_stats["num_user_variables"]=
66+
json_numbert(std::to_string(stats.get_num_user_variables()));
67+
json_stats["num_auxiliary_variables"]=
68+
json_numbert(std::to_string(stats.get_num_auxiliary_variables()));
69+
json_stats["num_function_calls"]=
70+
json_numbert(std::to_string(stats.get_num_function_calls()));
71+
json_stats["num_unconditional_gotos"]=
72+
json_numbert(std::to_string(stats.get_num_unconditional_gotos()));
73+
json_stats["num_conditional_gotos"]=
74+
json_numbert(std::to_string(stats.get_num_conditional_gotos()));
75+
json_stats["num_loops"]=
76+
json_numbert(std::to_string(stats.get_num_loops()));
77+
78+
return json_stats;
79+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*******************************************************************\
2+
3+
Module: goto_statistics.h
4+
5+
Author: Marek Trtik
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_GOTO_PROGRAMS_GOTO_STATISTICS_H
10+
#define CPROVER_GOTO_PROGRAMS_GOTO_STATISTICS_H
11+
12+
/// \file goto-programs/goto_statistics.h
13+
/// \brief Computation of statistical properties of a GOTO program.
14+
15+
#include <cstddef>
16+
#include <util/json.h>
17+
#include "goto_model.h"
18+
19+
class goto_statisticst
20+
{
21+
public:
22+
goto_statisticst():
23+
num_functions(0UL),
24+
num_instructions(0UL),
25+
num_user_variables(0UL),
26+
num_auxiliary_variables(0UL),
27+
num_function_calls(0UL),
28+
num_unconditional_gotos(0UL),
29+
num_conditional_gotos(0UL),
30+
num_assumptions(0UL),
31+
num_assertions(0UL),
32+
num_loops(0UL)
33+
{}
34+
35+
void extend(
36+
const goto_functionst &functions,
37+
const symbol_tablet &table);
38+
39+
void extend(const goto_modelt &model)
40+
{
41+
extend(model.goto_functions, model.symbol_table);
42+
}
43+
44+
void clear() { *this=goto_statisticst(); }
45+
46+
std::size_t get_num_functions() const
47+
{
48+
return num_functions;
49+
}
50+
51+
std::size_t get_num_instructions() const
52+
{
53+
return num_instructions;
54+
}
55+
56+
std::size_t get_num_user_variables() const
57+
{
58+
return num_user_variables;
59+
}
60+
61+
std::size_t get_num_auxiliary_variables() const
62+
{
63+
return num_auxiliary_variables;
64+
}
65+
66+
std::size_t get_num_function_calls() const
67+
{
68+
return num_function_calls;
69+
}
70+
71+
std::size_t get_num_unconditional_gotos() const
72+
{
73+
return num_unconditional_gotos;
74+
}
75+
76+
std::size_t get_num_conditional_gotos() const
77+
{
78+
return num_conditional_gotos;
79+
}
80+
81+
std::size_t get_num_loops() const
82+
{
83+
return num_loops;
84+
}
85+
86+
private:
87+
std::size_t num_functions;
88+
std::size_t num_instructions;
89+
std::size_t num_user_variables;
90+
std::size_t num_auxiliary_variables;
91+
std::size_t num_function_calls;
92+
std::size_t num_unconditional_gotos;
93+
std::size_t num_conditional_gotos;
94+
std::size_t num_assumptions;
95+
std::size_t num_assertions;
96+
std::size_t num_loops;
97+
};
98+
99+
jsont to_json(const goto_statisticst &stats);
100+
101+
#endif
102+

0 commit comments

Comments
 (0)