33#![ feature( rustc_private) ]
44
55extern crate rustc_driver;
6+ extern crate rustc_hir;
67extern crate rustc_interface;
78extern crate rustc_middle;
89extern crate rustc_session;
910
1011use std:: { borrow:: Cow , env, process:: Command } ;
1112
1213use clap:: Parser ;
14+ use rustc_hir:: {
15+ intravisit:: { self , Visitor } ,
16+ Item ,
17+ } ;
1318use rustc_middle:: ty:: TyCtxt ;
1419use rustc_plugin:: { CrateFilter , RustcPlugin , RustcPluginArgs , Utf8Path } ;
1520use serde:: { Deserialize , Serialize } ;
@@ -61,15 +66,16 @@ impl RustcPlugin for PrintAllItemsPlugin {
6166 compiler_args : Vec < String > ,
6267 plugin_args : Self :: Args ,
6368 ) -> rustc_interface:: interface:: Result < ( ) > {
64- let mut callbacks = PrintAllItemsCallbacks { args : plugin_args } ;
65- let compiler = rustc_driver:: RunCompiler :: new ( & compiler_args, & mut callbacks) ;
66- compiler. run ( ) ;
69+ let mut callbacks = PrintAllItemsCallbacks {
70+ args : Some ( plugin_args) ,
71+ } ;
72+ rustc_driver:: run_compiler ( & compiler_args, & mut callbacks) ;
6773 Ok ( ( ) )
6874 }
6975}
7076
7177struct PrintAllItemsCallbacks {
72- args : PrintAllItemsPluginArgs ,
78+ args : Option < PrintAllItemsPluginArgs > ,
7379}
7480
7581impl rustc_driver:: Callbacks for PrintAllItemsCallbacks {
@@ -82,7 +88,7 @@ impl rustc_driver::Callbacks for PrintAllItemsCallbacks {
8288 tcx : TyCtxt < ' _ > ,
8389 ) -> rustc_driver:: Compilation {
8490 // We call our top-level function with access to the type context `tcx` and the CLI arguments.
85- print_all_items ( tcx, & self . args ) ;
91+ print_all_items ( tcx, self . args . take ( ) . unwrap ( ) ) ;
8692
8793 // Note that you should generally allow compilation to continue. If
8894 // your plugin is being invoked on a dependency, then you need to ensure
@@ -92,21 +98,29 @@ impl rustc_driver::Callbacks for PrintAllItemsCallbacks {
9298 }
9399}
94100
95- // The core of our analysis. It doesn't do much, just access some methods on the `TyCtxt` .
101+ // The core of our analysis. Right now it just prints out a description of each item .
96102// I recommend reading the Rustc Development Guide to better understand which compiler APIs
97103// are relevant to whatever task you have.
98- fn print_all_items ( tcx : TyCtxt , args : & PrintAllItemsPluginArgs ) {
99- let hir = tcx. hir ( ) ;
100- for item_id in hir. items ( ) {
101- let item = hir. item ( item_id) ;
104+ fn print_all_items ( tcx : TyCtxt , args : PrintAllItemsPluginArgs ) {
105+ tcx. hir_visit_all_item_likes_in_crate ( & mut PrintVisitor { args } ) ;
106+ }
107+
108+ struct PrintVisitor {
109+ args : PrintAllItemsPluginArgs ,
110+ }
111+
112+ impl Visitor < ' _ > for PrintVisitor {
113+ fn visit_item ( & mut self , item : & Item ) -> Self :: Result {
102114 let mut msg = format ! (
103115 "There is an item \" {}\" of type \" {}\" " ,
104116 item. ident,
105117 item. kind. descr( )
106118 ) ;
107- if args. allcaps {
119+ if self . args . allcaps {
108120 msg = msg. to_uppercase ( ) ;
109121 }
110122 println ! ( "{msg}" ) ;
123+
124+ intravisit:: walk_item ( self , item)
111125 }
112126}
0 commit comments