@@ -4,17 +4,17 @@ use syntax::{
4
4
ast:: { self , Attr , HasAttrs , Meta , VariantList } ,
5
5
AstNode , SyntaxElement , SyntaxNode , T ,
6
6
} ;
7
- use tracing:: { info , warn} ;
7
+ use tracing:: { debug , warn} ;
8
8
9
9
use crate :: { db:: ExpandDatabase , MacroCallKind , MacroCallLoc } ;
10
10
11
11
fn check_cfg_attr ( attr : & Attr , loc : & MacroCallLoc , db : & dyn ExpandDatabase ) -> Option < bool > {
12
12
if !attr. simple_name ( ) . as_deref ( ) . map ( |v| v == "cfg" ) ? {
13
13
return None ;
14
14
}
15
- info ! ( "Evaluating cfg {}" , attr) ;
15
+ debug ! ( "Evaluating cfg {}" , attr) ;
16
16
let cfg = cfg:: CfgExpr :: parse_from_attr_meta ( attr. meta ( ) ?) ?;
17
- info ! ( "Checking cfg {:?}" , cfg) ;
17
+ debug ! ( "Checking cfg {:?}" , cfg) ;
18
18
let enabled = db. crate_graph ( ) [ loc. krate ] . cfg_options . check ( & cfg) != Some ( false ) ;
19
19
Some ( enabled)
20
20
}
@@ -23,10 +23,9 @@ fn check_cfg_attr_attr(attr: &Attr, loc: &MacroCallLoc, db: &dyn ExpandDatabase)
23
23
if !attr. simple_name ( ) . as_deref ( ) . map ( |v| v == "cfg_attr" ) ? {
24
24
return None ;
25
25
}
26
- info ! ( "Evaluating cfg_attr {}" , attr) ;
27
-
26
+ debug ! ( "Evaluating cfg_attr {}" , attr) ;
28
27
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) ;
30
29
let enabled = db. crate_graph ( ) [ loc. krate ] . cfg_options . check ( & cfg_expr) != Some ( false ) ;
31
30
Some ( enabled)
32
31
}
@@ -40,25 +39,22 @@ fn process_has_attrs_with_possible_comma<I: HasAttrs>(
40
39
for item in items {
41
40
let field_attrs = item. attrs ( ) ;
42
41
' 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
+ }
53
49
54
50
if let Some ( enabled) = check_cfg_attr_attr ( & attr, loc, db) {
55
51
if enabled {
56
- info ! ( "Removing cfg_attr tokens {:?}" , attr) ;
52
+ debug ! ( "Removing cfg_attr tokens {:?}" , attr) ;
57
53
let meta = attr. meta ( ) ?;
58
54
let removes_from_cfg_attr = remove_tokens_within_cfg_attr ( meta) ?;
59
55
remove. extend ( removes_from_cfg_attr) ;
60
56
} else {
61
- info ! ( "censoring type cfg_attr {:?}" , item. syntax( ) ) ;
57
+ debug ! ( "censoring type cfg_attr {:?}" , item. syntax( ) ) ;
62
58
remove. insert ( attr. syntax ( ) . clone ( ) . into ( ) ) ;
63
59
continue ;
64
60
}
@@ -70,18 +66,18 @@ fn process_has_attrs_with_possible_comma<I: HasAttrs>(
70
66
71
67
fn remove_tokens_within_cfg_attr ( meta : Meta ) -> Option < FxHashSet < SyntaxElement > > {
72
68
let mut remove: FxHashSet < SyntaxElement > = FxHashSet :: default ( ) ;
73
- info ! ( "Enabling attribute {}" , meta) ;
69
+ debug ! ( "Enabling attribute {}" , meta) ;
74
70
let meta_path = meta. path ( ) ?;
75
- info ! ( "Removing {:?}" , meta_path. syntax( ) ) ;
71
+ debug ! ( "Removing {:?}" , meta_path. syntax( ) ) ;
76
72
remove. insert ( meta_path. syntax ( ) . clone ( ) . into ( ) ) ;
77
73
78
74
let meta_tt = meta. token_tree ( ) ?;
79
- info ! ( "meta_tt {}" , meta_tt) ;
75
+ debug ! ( "meta_tt {}" , meta_tt) ;
80
76
// Remove the left paren
81
77
remove. insert ( meta_tt. l_paren_token ( ) ?. into ( ) ) ;
82
78
let mut found_comma = false ;
83
79
for tt in meta_tt. token_trees_and_tokens ( ) . skip ( 1 ) {
84
- info ! ( "Checking {:?}" , tt) ;
80
+ debug ! ( "Checking {:?}" , tt) ;
85
81
// 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.
86
82
match tt {
87
83
syntax:: NodeOrToken :: Node ( node) => {
@@ -119,25 +115,23 @@ fn process_enum(
119
115
) -> Option < ( ) > {
120
116
' variant: for variant in variants. variants ( ) {
121
117
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 ( ) {
123
119
// 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;
131
125
} ;
132
126
133
127
if let Some ( enabled) = check_cfg_attr_attr ( & attr, loc, db) {
134
128
if enabled {
135
- info ! ( "Removing cfg_attr tokens {:?}" , attr) ;
129
+ debug ! ( "Removing cfg_attr tokens {:?}" , attr) ;
136
130
let meta = attr. meta ( ) ?;
137
131
let removes_from_cfg_attr = remove_tokens_within_cfg_attr ( meta) ?;
138
132
remove. extend ( removes_from_cfg_attr) ;
139
133
} else {
140
- info ! ( "censoring type cfg_attr {:?}" , variant. syntax( ) ) ;
134
+ debug ! ( "censoring type cfg_attr {:?}" , variant. syntax( ) ) ;
141
135
remove. insert ( attr. syntax ( ) . clone ( ) . into ( ) ) ;
142
136
continue ;
143
137
}
@@ -166,31 +160,45 @@ pub(crate) fn process_cfg_attrs(
166
160
if !matches ! ( loc. kind, MacroCallKind :: Derive { .. } ) {
167
161
return None ;
168
162
}
169
- let mut res = FxHashSet :: default ( ) ;
163
+ let mut remove = FxHashSet :: default ( ) ;
170
164
171
165
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
+ }
172
180
match item {
173
181
ast:: Item :: Struct ( it) => match it. field_list ( ) ? {
174
182
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 ) ?;
176
184
}
177
185
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 ) ?;
179
187
}
180
188
} ,
181
189
ast:: Item :: Enum ( it) => {
182
- process_enum ( it. variant_list ( ) ?, loc, db, & mut res ) ?;
190
+ process_enum ( it. variant_list ( ) ?, loc, db, & mut remove ) ?;
183
191
}
184
192
ast:: Item :: Union ( it) => {
185
193
process_has_attrs_with_possible_comma (
186
194
it. record_field_list ( ) ?. fields ( ) ,
187
195
loc,
188
196
db,
189
- & mut res ,
197
+ & mut remove ,
190
198
) ?;
191
199
}
192
200
// FIXME: Implement for other items if necessary. As we do not support #[cfg_eval] yet, we do not need to implement it for now
193
201
_ => { }
194
202
}
195
- Some ( res )
203
+ Some ( remove )
196
204
}
0 commit comments