1
1
mod extract;
2
2
mod graph;
3
3
mod lifetime;
4
- mod ty;
5
4
mod visitor;
6
- use crate :: { rap_info , utils :: fs :: rap_create_file } ;
5
+ use crate :: { rap_debug , rap_info } ;
7
6
use graph:: ApiDepGraph ;
8
7
use rustc_hir:: {
9
- def_id:: { DefId , LocalDefId } ,
8
+ def_id:: { DefId , LocalDefId , LOCAL_CRATE } ,
10
9
intravisit:: { self , FnKind , Visitor } ,
11
10
BodyId , FnDecl ,
12
11
} ;
13
- use rustc_middle:: { dep_graph, ty:: TyCtxt } ;
14
- use rustc_span:: Span ;
12
+ use rustc_middle:: ty:: TyCtxt ;
15
13
16
- use std:: io:: Write ;
17
14
use visitor:: FnVisitor ;
18
15
19
16
pub struct ApiDep < ' tcx > {
@@ -24,31 +21,34 @@ impl<'tcx> ApiDep<'tcx> {
24
21
pub fn new ( tcx : TyCtxt < ' tcx > ) -> ApiDep < ' tcx > {
25
22
ApiDep { tcx }
26
23
}
27
- pub fn start ( & self ) {
28
- rap_info ! ( "Build API dependency graph" ) ;
24
+ pub fn start ( & self ) -> ApiDepGraph < ' tcx > {
25
+ let local_crate_name = self . tcx . crate_name ( LOCAL_CRATE ) ;
26
+ let local_crate_type = self . tcx . crate_types ( ) [ 0 ] ;
27
+ rap_debug ! (
28
+ "Build API dependency graph on {} ({})" ,
29
+ local_crate_name. as_str( ) ,
30
+ local_crate_type
31
+ ) ;
32
+
29
33
let mut api_graph = ApiDepGraph :: new ( ) ;
30
34
let mut fn_visitor = FnVisitor :: new ( self . tcx , & mut api_graph) ;
31
35
self . tcx
32
36
. hir ( )
33
37
. visit_all_item_likes_in_crate ( & mut fn_visitor) ;
34
- rap_info ! ( "visitor find {} APIs." , fn_visitor. fn_cnt( ) ) ;
35
- let mut file = rap_create_file ( "visitor.txt" , "fail when create file" ) ;
36
- fn_visitor. write_funcs ( & mut file) ;
38
+ rap_debug ! ( "api-dep find {} APIs." , fn_visitor. fn_cnt( ) ) ;
37
39
38
- api_graph. dump_to_dot ( "api_graph.dot" , self . tcx ) ;
40
+ let statistics = api_graph. statistics ( ) ;
41
+ // print all statistics
42
+ rap_debug ! (
43
+ "API Graph contains {} API nodes, {} type nodes, {} generic parameter def nodes" ,
44
+ statistics. api_count,
45
+ statistics. type_count,
46
+ statistics. generic_param_count
47
+ ) ;
39
48
40
- let mut file = rap_create_file ( "traverse.txt" , "fail when create file" ) ;
41
- let mut fn_cnt = 0 ;
42
- // TODO: try self.tcx.mir_keys(())
43
- for local_def_id in self . tcx . iter_local_def_id ( ) {
44
- let hir_map = self . tcx . hir ( ) ;
45
- if hir_map. maybe_body_owned_by ( local_def_id) . is_some ( ) {
46
- write ! ( & mut file, "{}\n " , self . tcx. def_path_str( local_def_id) )
47
- . expect ( "fail when write file" ) ;
48
- // rap_info!("find API: {}", self.tcx.def_path_str(local_def_id));
49
- fn_cnt += 1 ;
50
- }
51
- }
52
- rap_info ! ( "find {} APIs." , fn_cnt) ;
49
+ let dot_filename = format ! ( "api_graph_{}_{}.dot" , local_crate_name, local_crate_type) ;
50
+ rap_info ! ( "Dump API dependency graph to {}" , dot_filename) ;
51
+ api_graph. dump_to_dot ( dot_filename, self . tcx ) ;
52
+ api_graph
53
53
}
54
54
}
0 commit comments