@@ -5,7 +5,6 @@ use rustc_data_structures::fx::FxHashSet;
55use rustc_expand:: base:: resolve_path;
66use rustc_hir as hir;
77use rustc_hir:: def_id:: CrateNum ;
8- use rustc_hir:: itemlikevisit:: ItemLikeVisitor ;
98use rustc_hir:: { HirId , Target } ;
109use rustc_middle:: ty:: query:: Providers ;
1110use rustc_middle:: ty:: TyCtxt ;
@@ -14,96 +13,71 @@ use rustc_span::{sym, DebuggerVisualizerFile, DebuggerVisualizerType};
1413
1514use std:: sync:: Arc ;
1615
17- struct DebuggerVisualizerCollector < ' tcx > {
18- debugger_visualizers : FxHashSet < DebuggerVisualizerFile > ,
16+ fn check_for_debugger_visualizer < ' tcx > (
1917 tcx : TyCtxt < ' tcx > ,
20- }
21-
22- impl < ' v , ' tcx > ItemLikeVisitor < ' v > for DebuggerVisualizerCollector < ' tcx > {
23- fn visit_item ( & mut self , item : & hir:: Item < ' _ > ) {
24- let target = Target :: from_item ( item) ;
25- match target {
26- Target :: Mod => {
27- self . check_for_debugger_visualizer ( item. hir_id ( ) ) ;
28- }
29- _ => { }
30- }
31- }
32-
33- fn visit_trait_item ( & mut self , _: & hir:: TraitItem < ' _ > ) { }
34-
35- fn visit_impl_item ( & mut self , _: & hir:: ImplItem < ' _ > ) { }
36-
37- fn visit_foreign_item ( & mut self , _: & hir:: ForeignItem < ' _ > ) { }
38- }
39-
40- impl < ' tcx > DebuggerVisualizerCollector < ' tcx > {
41- fn new ( tcx : TyCtxt < ' tcx > ) -> DebuggerVisualizerCollector < ' tcx > {
42- DebuggerVisualizerCollector { tcx, debugger_visualizers : FxHashSet :: default ( ) }
43- }
44-
45- fn check_for_debugger_visualizer ( & mut self , hir_id : HirId ) {
46- let attrs = self . tcx . hir ( ) . attrs ( hir_id) ;
47- for attr in attrs {
48- if attr. has_name ( sym:: debugger_visualizer) {
49- let list = match attr. meta_item_list ( ) {
50- Some ( list) => list,
51- _ => continue ,
52- } ;
53-
54- let meta_item = match list. len ( ) {
55- 1 => match list[ 0 ] . meta_item ( ) {
56- Some ( meta_item) => meta_item,
57- _ => continue ,
58- } ,
18+ hir_id : HirId ,
19+ debugger_visualizers : & mut FxHashSet < DebuggerVisualizerFile >
20+ ) {
21+ let attrs = tcx. hir ( ) . attrs ( hir_id) ;
22+ for attr in attrs {
23+ if attr. has_name ( sym:: debugger_visualizer) {
24+ let list = match attr. meta_item_list ( ) {
25+ Some ( list) => list,
26+ _ => continue ,
27+ } ;
28+
29+ let meta_item = match list. len ( ) {
30+ 1 => match list[ 0 ] . meta_item ( ) {
31+ Some ( meta_item) => meta_item,
5932 _ => continue ,
60- } ;
61-
62- let file = match ( meta_item. name_or_empty ( ) , meta_item. value_str ( ) ) {
63- ( sym:: natvis_file, Some ( value) ) => {
64- match resolve_path ( & self . tcx . sess . parse_sess , value. as_str ( ) , attr. span ) {
65- Ok ( file) => file,
66- Err ( mut err) => {
67- err. emit ( ) ;
68- continue ;
69- }
33+ } ,
34+ _ => continue ,
35+ } ;
36+
37+ let file = match ( meta_item. name_or_empty ( ) , meta_item. value_str ( ) ) {
38+ ( sym:: natvis_file, Some ( value) ) => {
39+ match resolve_path ( & tcx. sess . parse_sess , value. as_str ( ) , attr. span ) {
40+ Ok ( file) => file,
41+ Err ( mut err) => {
42+ err. emit ( ) ;
43+ continue ;
7044 }
7145 }
72- ( _, _) => continue ,
46+ }
47+ ( _, _) => continue ,
48+ } ;
49+
50+ if file. is_file ( ) {
51+ let contents = match std:: fs:: read ( & file) {
52+ Ok ( contents) => contents,
53+ Err ( err) => {
54+ tcx
55+ . sess
56+ . struct_span_err (
57+ attr. span ,
58+ & format ! (
59+ "Unable to read contents of file `{}`. {}" ,
60+ file. display( ) ,
61+ err
62+ ) ,
63+ )
64+ . emit ( ) ;
65+ continue ;
66+ }
7367 } ;
7468
75- if file. is_file ( ) {
76- let contents = match std:: fs:: read ( & file) {
77- Ok ( contents) => contents,
78- Err ( err) => {
79- self . tcx
80- . sess
81- . struct_span_err (
82- attr. span ,
83- & format ! (
84- "Unable to read contents of file `{}`. {}" ,
85- file. display( ) ,
86- err
87- ) ,
88- )
89- . emit ( ) ;
90- continue ;
91- }
92- } ;
93-
94- self . debugger_visualizers . insert ( DebuggerVisualizerFile :: new (
95- Arc :: from ( contents) ,
96- DebuggerVisualizerType :: Natvis ,
97- ) ) ;
98- } else {
99- self . tcx
100- . sess
101- . struct_span_err (
102- attr. span ,
103- & format ! ( "{} is not a valid file" , file. display( ) ) ,
104- )
105- . emit ( ) ;
106- }
69+ debugger_visualizers. insert ( DebuggerVisualizerFile :: new (
70+ Arc :: from ( contents) ,
71+ DebuggerVisualizerType :: Natvis ,
72+ ) ) ;
73+ } else {
74+ tcx
75+ . sess
76+ . struct_span_err (
77+ attr. span ,
78+ & format ! ( "{} is not a valid file" , file. display( ) ) ,
79+ )
80+ . emit ( ) ;
10781 }
10882 }
10983 }
@@ -114,17 +88,21 @@ fn debugger_visualizers<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> Vec<Debugger
11488 assert_eq ! ( cnum, LOCAL_CRATE ) ;
11589
11690 // Initialize the collector.
117- let mut collector = DebuggerVisualizerCollector :: new ( tcx ) ;
91+ let mut debugger_visualizers = FxHashSet :: default ( ) ;
11892
11993 // Collect debugger visualizers in this crate.
120- tcx. hir ( ) . visit_all_item_likes ( & mut collector) ;
94+ for id in tcx. hir ( ) . items ( ) {
95+ let target = Target :: from_def_kind ( tcx. def_kind ( id. def_id ) ) ;
96+ if let Target :: Mod = target {
97+ check_for_debugger_visualizer ( tcx, id. hir_id ( ) , & mut debugger_visualizers) ;
98+ }
99+ }
121100
122101 // Collect debugger visualizers on the crate attributes.
123- collector . check_for_debugger_visualizer ( CRATE_HIR_ID ) ;
102+ check_for_debugger_visualizer ( tcx , CRATE_HIR_ID , & mut debugger_visualizers ) ;
124103
125- // Extract out the found debugger_visualizer items.
126- let DebuggerVisualizerCollector { debugger_visualizers, .. } = collector;
127104
105+ // Extract out the found debugger_visualizer items.
128106 let mut visualizers = debugger_visualizers. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
129107
130108 // Sort the visualizers so we always get a deterministic query result.
0 commit comments