@@ -4,17 +4,17 @@ use syntax::{
44 ast:: { self , Attr , HasAttrs , Meta , VariantList } ,
55 AstNode , SyntaxElement , SyntaxNode , T ,
66} ;
7- use tracing:: { info , warn} ;
7+ use tracing:: { debug , warn} ;
88
99use crate :: { db:: ExpandDatabase , MacroCallKind , MacroCallLoc } ;
1010
1111fn check_cfg_attr ( attr : & Attr , loc : & MacroCallLoc , db : & dyn ExpandDatabase ) -> Option < bool > {
1212 if !attr. simple_name ( ) . as_deref ( ) . map ( |v| v == "cfg" ) ? {
1313 return None ;
1414 }
15- info ! ( "Evaluating cfg {}" , attr) ;
15+ debug ! ( "Evaluating cfg {}" , attr) ;
1616 let cfg = cfg:: CfgExpr :: parse_from_attr_meta ( attr. meta ( ) ?) ?;
17- info ! ( "Checking cfg {:?}" , cfg) ;
17+ debug ! ( "Checking cfg {:?}" , cfg) ;
1818 let enabled = db. crate_graph ( ) [ loc. krate ] . cfg_options . check ( & cfg) != Some ( false ) ;
1919 Some ( enabled)
2020}
@@ -23,10 +23,9 @@ fn check_cfg_attr_attr(attr: &Attr, loc: &MacroCallLoc, db: &dyn ExpandDatabase)
2323 if !attr. simple_name ( ) . as_deref ( ) . map ( |v| v == "cfg_attr" ) ? {
2424 return None ;
2525 }
26- info ! ( "Evaluating cfg_attr {}" , attr) ;
27-
26+ debug ! ( "Evaluating cfg_attr {}" , attr) ;
2827 let cfg_expr = cfg:: CfgExpr :: parse_from_attr_meta ( attr. meta ( ) ?) ?;
29- info ! ( "Checking cfg_attr {:?}" , cfg_expr) ;
28+ debug ! ( "Checking cfg_attr {:?}" , cfg_expr) ;
3029 let enabled = db. crate_graph ( ) [ loc. krate ] . cfg_options . check ( & cfg_expr) != Some ( false ) ;
3130 Some ( enabled)
3231}
@@ -40,25 +39,22 @@ fn process_has_attrs_with_possible_comma<I: HasAttrs>(
4039 for item in items {
4140 let field_attrs = item. attrs ( ) ;
4241 ' attrs: for attr in field_attrs {
43- if let Some ( enabled) = check_cfg_attr ( & attr, loc, db) {
44- // Rustc does not strip the attribute if it is enabled. So we will will leave it
45- if !enabled {
46- info ! ( "censoring type {:?}" , item. syntax( ) ) ;
47- remove. insert ( item. syntax ( ) . clone ( ) . into ( ) ) ;
48- // We need to remove the , as well
49- add_comma ( & item, remove) ;
50- break ' attrs;
51- }
52- } ;
42+ if check_cfg_attr ( & attr, loc, db) . map ( |enabled| !enabled) . unwrap_or_default ( ) {
43+ debug ! ( "censoring type {:?}" , item. syntax( ) ) ;
44+ remove. insert ( item. syntax ( ) . clone ( ) . into ( ) ) ;
45+ // We need to remove the , as well
46+ add_comma ( & item, remove) ;
47+ break ' attrs;
48+ }
5349
5450 if let Some ( enabled) = check_cfg_attr_attr ( & attr, loc, db) {
5551 if enabled {
56- info ! ( "Removing cfg_attr tokens {:?}" , attr) ;
52+ debug ! ( "Removing cfg_attr tokens {:?}" , attr) ;
5753 let meta = attr. meta ( ) ?;
5854 let removes_from_cfg_attr = remove_tokens_within_cfg_attr ( meta) ?;
5955 remove. extend ( removes_from_cfg_attr) ;
6056 } else {
61- info ! ( "censoring type cfg_attr {:?}" , item. syntax( ) ) ;
57+ debug ! ( "censoring type cfg_attr {:?}" , item. syntax( ) ) ;
6258 remove. insert ( attr. syntax ( ) . clone ( ) . into ( ) ) ;
6359 continue ;
6460 }
@@ -70,18 +66,18 @@ fn process_has_attrs_with_possible_comma<I: HasAttrs>(
7066
7167fn remove_tokens_within_cfg_attr ( meta : Meta ) -> Option < FxHashSet < SyntaxElement > > {
7268 let mut remove: FxHashSet < SyntaxElement > = FxHashSet :: default ( ) ;
73- info ! ( "Enabling attribute {}" , meta) ;
69+ debug ! ( "Enabling attribute {}" , meta) ;
7470 let meta_path = meta. path ( ) ?;
75- info ! ( "Removing {:?}" , meta_path. syntax( ) ) ;
71+ debug ! ( "Removing {:?}" , meta_path. syntax( ) ) ;
7672 remove. insert ( meta_path. syntax ( ) . clone ( ) . into ( ) ) ;
7773
7874 let meta_tt = meta. token_tree ( ) ?;
79- info ! ( "meta_tt {}" , meta_tt) ;
75+ debug ! ( "meta_tt {}" , meta_tt) ;
8076 // Remove the left paren
8177 remove. insert ( meta_tt. l_paren_token ( ) ?. into ( ) ) ;
8278 let mut found_comma = false ;
8379 for tt in meta_tt. token_trees_and_tokens ( ) . skip ( 1 ) {
84- info ! ( "Checking {:?}" , tt) ;
80+ debug ! ( "Checking {:?}" , tt) ;
8581 // Check if it is a subtree or a token. If it is a token check if it is a comma. If so, remove it and break.
8682 match tt {
8783 syntax:: NodeOrToken :: Node ( node) => {
@@ -119,25 +115,23 @@ fn process_enum(
119115) -> Option < ( ) > {
120116 ' variant: for variant in variants. variants ( ) {
121117 for attr in variant. attrs ( ) {
122- if let Some ( enabled ) = check_cfg_attr ( & attr, loc, db) {
118+ if check_cfg_attr ( & attr, loc, db) . map ( |enabled| !enabled ) . unwrap_or_default ( ) {
123119 // Rustc does not strip the attribute if it is enabled. So we will will leave it
124- if !enabled {
125- info ! ( "censoring type {:?}" , variant. syntax( ) ) ;
126- remove. insert ( variant. syntax ( ) . clone ( ) . into ( ) ) ;
127- // We need to remove the , as well
128- add_comma ( & variant, remove) ;
129- continue ' variant;
130- }
120+ debug ! ( "censoring type {:?}" , variant. syntax( ) ) ;
121+ remove. insert ( variant. syntax ( ) . clone ( ) . into ( ) ) ;
122+ // We need to remove the , as well
123+ add_comma ( & variant, remove) ;
124+ continue ' variant;
131125 } ;
132126
133127 if let Some ( enabled) = check_cfg_attr_attr ( & attr, loc, db) {
134128 if enabled {
135- info ! ( "Removing cfg_attr tokens {:?}" , attr) ;
129+ debug ! ( "Removing cfg_attr tokens {:?}" , attr) ;
136130 let meta = attr. meta ( ) ?;
137131 let removes_from_cfg_attr = remove_tokens_within_cfg_attr ( meta) ?;
138132 remove. extend ( removes_from_cfg_attr) ;
139133 } else {
140- info ! ( "censoring type cfg_attr {:?}" , variant. syntax( ) ) ;
134+ debug ! ( "censoring type cfg_attr {:?}" , variant. syntax( ) ) ;
141135 remove. insert ( attr. syntax ( ) . clone ( ) . into ( ) ) ;
142136 continue ;
143137 }
@@ -166,31 +160,45 @@ pub(crate) fn process_cfg_attrs(
166160 if !matches ! ( loc. kind, MacroCallKind :: Derive { .. } ) {
167161 return None ;
168162 }
169- let mut res = FxHashSet :: default ( ) ;
163+ let mut remove = FxHashSet :: default ( ) ;
170164
171165 let item = ast:: Item :: cast ( node. clone ( ) ) ?;
166+ for attr in item. attrs ( ) {
167+ if let Some ( enabled) = check_cfg_attr_attr ( & attr, loc, db) {
168+ if enabled {
169+ debug ! ( "Removing cfg_attr tokens {:?}" , attr) ;
170+ let meta = attr. meta ( ) ?;
171+ let removes_from_cfg_attr = remove_tokens_within_cfg_attr ( meta) ?;
172+ remove. extend ( removes_from_cfg_attr) ;
173+ } else {
174+ debug ! ( "censoring type cfg_attr {:?}" , item. syntax( ) ) ;
175+ remove. insert ( attr. syntax ( ) . clone ( ) . into ( ) ) ;
176+ continue ;
177+ }
178+ }
179+ }
172180 match item {
173181 ast:: Item :: Struct ( it) => match it. field_list ( ) ? {
174182 ast:: FieldList :: RecordFieldList ( fields) => {
175- process_has_attrs_with_possible_comma ( fields. fields ( ) , loc, db, & mut res ) ?;
183+ process_has_attrs_with_possible_comma ( fields. fields ( ) , loc, db, & mut remove ) ?;
176184 }
177185 ast:: FieldList :: TupleFieldList ( fields) => {
178- process_has_attrs_with_possible_comma ( fields. fields ( ) , loc, db, & mut res ) ?;
186+ process_has_attrs_with_possible_comma ( fields. fields ( ) , loc, db, & mut remove ) ?;
179187 }
180188 } ,
181189 ast:: Item :: Enum ( it) => {
182- process_enum ( it. variant_list ( ) ?, loc, db, & mut res ) ?;
190+ process_enum ( it. variant_list ( ) ?, loc, db, & mut remove ) ?;
183191 }
184192 ast:: Item :: Union ( it) => {
185193 process_has_attrs_with_possible_comma (
186194 it. record_field_list ( ) ?. fields ( ) ,
187195 loc,
188196 db,
189- & mut res ,
197+ & mut remove ,
190198 ) ?;
191199 }
192200 // FIXME: Implement for other items if necessary. As we do not support #[cfg_eval] yet, we do not need to implement it for now
193201 _ => { }
194202 }
195- Some ( res )
203+ Some ( remove )
196204}
0 commit comments