-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfunction_stats.rs
More file actions
171 lines (151 loc) · 5.44 KB
/
function_stats.rs
File metadata and controls
171 lines (151 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use crate::function_target_pipeline::FunctionTargetsHolder;
use move_binary_format::file_format::Visibility;
use move_model::{
ast::Attribute,
model::{FunctionEnv, GlobalEnv},
};
use std::collections::{BTreeMap, BTreeSet};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProofStatus {
Skipped,
NoSpec,
NoProve,
SuccessfulProof,
IgnoreAborts,
}
impl std::fmt::Display for ProofStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ProofStatus::SuccessfulProof => write!(f, "✅ has spec"),
ProofStatus::IgnoreAborts => write!(f, "⚠️ spec but with ignore_abort"),
ProofStatus::Skipped => write!(f, "⏭️ skipped spec"),
ProofStatus::NoProve => write!(f, "✖️ no prove"),
ProofStatus::NoSpec => write!(f, "❌ no spec"),
}
}
}
/// Checks if a function has a specific attribute (e.g., "spec_only", "test_only").
fn has_attribute(func_env: &FunctionEnv, attr_name: &str) -> bool {
func_env.get_attributes().iter().any(|attr| {
matches!(
attr,
Attribute::Apply(_, name, _) | Attribute::Assign(_, name, _)
if name.display(func_env.symbol_pool()).to_string() == attr_name
)
})
}
/// Determines if a function should be included in statistics.
///
/// Filters out:
/// - Non-public functions
/// - Functions with `spec_only` attribute
/// - Functions with `test_only` attribute
/// - Spec functions themselves
fn should_include_function(func_env: &FunctionEnv, targets: &FunctionTargetsHolder) -> bool {
let func_id = func_env.get_qualified_id();
if func_env.visibility() != Visibility::Public {
return false;
}
if has_attribute(func_env, "spec_only") {
return false;
}
if has_attribute(func_env, "test_only") {
return false;
}
if targets.is_spec(&func_id) {
return false;
}
true
}
/// Determines the proof status of a function by checking if it has a spec
/// and what verification properties are set.
///
/// Returns:
/// - `Skipped` - Spec is marked to be skipped
/// - `NoProve` - Spec exists but is not marked for verification
/// - `IgnoreAborts` - Spec is verified but ignores abort conditions
/// - `SuccessfulProof` - Spec is verified normally
/// - `NoSpec` - No specification exists for this function
fn determine_spec_status(func_env: &FunctionEnv, targets: &FunctionTargetsHolder) -> ProofStatus {
let func_id = func_env.get_qualified_id();
let skip_specs_set: BTreeSet<_> = targets.skip_specs().collect();
if let Some(spec_id) = targets.get_spec_by_fun(&func_id) {
if skip_specs_set.contains(&spec_id) {
ProofStatus::Skipped
} else if !targets.is_verified_spec(spec_id) {
ProofStatus::NoProve
} else if targets.ignores_aborts(spec_id) {
ProofStatus::IgnoreAborts
} else {
ProofStatus::SuccessfulProof
}
} else {
ProofStatus::NoSpec
}
}
/// Displays statistics for all public functions in the project.
///
/// Shows:
/// - Functions grouped by module
/// - Proof status for each function (has spec, no spec, skipped, etc.)
/// - Summary with total counts
///
/// Excludes:
/// - System/framework modules
/// - Non-public functions
/// - Test-only and spec-only functions
pub fn display_function_stats(env: &GlobalEnv, targets: &FunctionTargetsHolder) {
println!("📊 Function Statistics\n");
let excluded_addresses = [
0u16.into(), // System address (core framework)
1u16.into(), // Tests address
2u16.into(), // Event address
3u16.into(), // Stdlib address
0xdee9u16.into(), // DeepBook address
];
let mut total_public_functions = 0;
let mut stats_by_status = BTreeMap::new();
let mut functions_by_module: BTreeMap<String, Vec<_>> = BTreeMap::new();
for module_env in env.get_modules() {
let module_addr = module_env.get_name().addr();
let module_name = module_env
.get_name()
.name()
.display(env.symbol_pool())
.to_string();
if excluded_addresses.contains(module_addr)
|| GlobalEnv::SPECS_MODULES_NAMES.contains(&module_name.as_str())
{
continue;
}
for func_env in module_env.get_functions() {
if should_include_function(&func_env, targets) {
let module_name_str = func_env
.module_env
.get_name()
.display(env.symbol_pool())
.to_string();
functions_by_module
.entry(module_name_str)
.or_default()
.push(func_env.get_qualified_id());
}
}
}
for (module_name, func_ids) in functions_by_module {
println!("📦 Module: {}", module_name);
for func_id in func_ids {
let func_env = env.get_function(func_id);
total_public_functions += 1;
let status = determine_spec_status(&func_env, targets);
*stats_by_status.entry(format!("{}", status)).or_insert(0) += 1;
println!(" {} {}", status, func_env.get_name_str());
}
println!();
}
println!("📈 Summary:");
println!("Total public functions: {}", total_public_functions);
for (status, count) in stats_by_status {
println!(" {}: {}", status, count);
}
}