@@ -3,17 +3,18 @@ use kani::{CallGraph, KANI_TOOL_ATTRS, collect_reachable_items};
33use rustc_middle:: ty:: TyCtxt ;
44use rustc_smir:: rustc_internal:: internal;
55use rustc_span:: { Span , source_map:: SourceMap } ;
6- use serde:: Serialize ;
76use stable_mir:: {
8- CrateDef ,
7+ CrateDef , DefId ,
8+ crate_def:: Attribute ,
99 mir:: mono:: { Instance , MonoItem } ,
1010 ty:: { FnDef , RigidTy , Ty , TyKind } ,
1111} ;
1212
13- mod callees;
1413mod kani;
14+ mod serialization;
15+ pub use serialization:: SerFunction ;
1516
16- pub fn analyze ( tcx : TyCtxt , src_map : & SourceMap ) -> Vec < Function > {
17+ pub fn analyze ( tcx : TyCtxt , src_map : & SourceMap ) -> Vec < SerFunction > {
1718 let local_items = stable_mir:: all_local_items ( ) ;
1819 let cap = local_items. len ( ) ;
1920
@@ -28,22 +29,29 @@ pub fn analyze(tcx: TyCtxt, src_map: &SourceMap) -> Vec<Function> {
2829
2930 let ( mono_items, callgraph) = collect_reachable_items ( tcx, & entries) ;
3031
31- mono_items. iter ( ) . filter_map ( |item| Function :: new ( item, & callgraph, tcx, src_map) ) . collect ( )
32+ // Filter out non kanitool functions.
33+ mono_items
34+ . iter ( )
35+ . filter_map ( |item| Function :: new ( item, & callgraph, tcx, src_map, |x| !x. attrs . is_empty ( ) ) )
36+ . map ( SerFunction :: new)
37+ . collect ( )
3238}
3339
3440/// A Rust funtion with its file source, attributes, and raw function content.
35- #[ derive( Debug , Default , Serialize ) ]
41+ #[ derive( Debug ) ]
3642pub struct Function {
43+ /// DefId in stable_mir.
44+ def_id : DefId ,
3745 /// Every funtion must be declared in a specific file, even for those
3846 /// generated by macros.
3947 file : String ,
4048 /// Attributes are attached the function, but it seems that attributes
4149 /// and function must be separated to query.
42- attrs : Vec < String > ,
50+ attrs : Vec < Attribute > ,
4351 /// Raw function string, including name, signature, and body.
4452 func : String ,
4553 /// Recursive fnction calls inside the body.
46- callees : Vec < String > ,
54+ callees : IndexSet < Instance > ,
4755}
4856
4957impl Function {
@@ -52,34 +60,33 @@ impl Function {
5260 callgraph : & CallGraph ,
5361 tcx : TyCtxt ,
5462 src_map : & SourceMap ,
63+ filter : impl FnOnce ( & Self ) -> bool ,
5564 ) -> Option < Self > {
5665 // skip non fn items
5766 let & MonoItem :: Fn ( inst) = item else {
5867 return None ;
5968 } ;
6069
61- let fn_def = ty_to_fndef ( inst. ty ( ) ) ?;
6270 let inst_def = inst. def ;
63- let span = inst_def. span ( ) ;
6471
72+ // Only need kanitool attrs: proof, proof_for_contract, contract, ...
73+ let attrs = KANI_TOOL_ATTRS . iter ( ) . flat_map ( |v| inst_def. tool_attrs ( v) ) . collect ( ) ;
74+
75+ let span = inst_def. span ( ) ;
6576 let file = span. get_filename ( ) ;
77+
78+ let fn_def = ty_to_fndef ( inst. ty ( ) ) ?;
6679 let body = fn_def. body ( ) ?;
6780
6881 let mut callees = IndexSet :: new ( ) ;
6982 callgraph. recursive_callees ( item, & mut callees) ;
70- let callees = callees. into_iter ( ) . map ( |x| format ! ( "{:?}" , x. def. def_id( ) ) ) . collect ( ) ;
7183
7284 let func = source_code_with ( body. span , tcx, src_map) ;
7385 info ! ( " - {:?} ({span:?}): {func}" , inst_def. name( ) ) ;
7486
75- // Only need kanitool attrs: proof, proof_for_contract, contract, ...
76- let attrs = KANI_TOOL_ATTRS
77- . iter ( )
78- . flat_map ( |v| inst_def. tool_attrs ( v) )
79- . map ( |attr| attr. as_str ( ) . to_owned ( ) )
80- . collect ( ) ;
81-
82- Some ( Function { file, attrs, func, callees } )
87+ let def_id = inst_def. def_id ( ) ;
88+ let this = Function { def_id, file, attrs, func, callees } ;
89+ filter ( & this) . then_some ( this)
8390 }
8491}
8592
0 commit comments