]>
Commit | Line | Data |
---|---|---|
a2a8927a | 1 | use crate::util::{check_builtin_macro_attribute, warn_on_duplicate_attribute}; |
6a06907d | 2 | |
cdc7bbd5 XL |
3 | use rustc_ast as ast; |
4 | use rustc_ast::mut_visit::MutVisitor; | |
c295e0f8 | 5 | use rustc_ast::ptr::P; |
cdc7bbd5 | 6 | use rustc_ast::visit::Visitor; |
5e7ed085 | 7 | use rustc_ast::NodeId; |
cdc7bbd5 | 8 | use rustc_ast::{mut_visit, visit}; |
04454e1e | 9 | use rustc_ast::{Attribute, HasAttrs, HasTokens}; |
6a06907d XL |
10 | use rustc_expand::base::{Annotatable, ExtCtxt}; |
11 | use rustc_expand::config::StripUnconfigured; | |
12 | use rustc_expand::configure; | |
c295e0f8 | 13 | use rustc_feature::Features; |
a2a8927a | 14 | use rustc_parse::parser::{ForceCollect, Parser}; |
c295e0f8 | 15 | use rustc_session::Session; |
6a06907d XL |
16 | use rustc_span::symbol::sym; |
17 | use rustc_span::Span; | |
18 | use smallvec::SmallVec; | |
19 | ||
923072b8 | 20 | pub(crate) fn expand( |
6a06907d XL |
21 | ecx: &mut ExtCtxt<'_>, |
22 | _span: Span, | |
23 | meta_item: &ast::MetaItem, | |
24 | annotatable: Annotatable, | |
25 | ) -> Vec<Annotatable> { | |
26 | check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval); | |
a2a8927a | 27 | warn_on_duplicate_attribute(&ecx, &annotatable, sym::cfg_eval); |
5e7ed085 | 28 | vec![cfg_eval(ecx.sess, ecx.ecfg.features, annotatable, ecx.current_expansion.lint_node_id)] |
6a06907d XL |
29 | } |
30 | ||
923072b8 | 31 | pub(crate) fn cfg_eval( |
c295e0f8 XL |
32 | sess: &Session, |
33 | features: Option<&Features>, | |
34 | annotatable: Annotatable, | |
5e7ed085 | 35 | lint_node_id: NodeId, |
c295e0f8 | 36 | ) -> Annotatable { |
5e7ed085 | 37 | CfgEval { cfg: &mut StripUnconfigured { sess, features, config_tokens: true, lint_node_id } } |
c295e0f8 XL |
38 | .configure_annotatable(annotatable) |
39 | // Since the item itself has already been configured by the `InvocationCollector`, | |
40 | // we know that fold result vector will contain exactly one element. | |
41 | .unwrap() | |
cdc7bbd5 XL |
42 | } |
43 | ||
44 | struct CfgEval<'a, 'b> { | |
45 | cfg: &'a mut StripUnconfigured<'b>, | |
46 | } | |
47 | ||
136023e0 XL |
48 | fn flat_map_annotatable( |
49 | vis: &mut impl MutVisitor, | |
50 | annotatable: Annotatable, | |
51 | ) -> Option<Annotatable> { | |
cdc7bbd5 | 52 | match annotatable { |
136023e0 | 53 | Annotatable::Item(item) => vis.flat_map_item(item).pop().map(Annotatable::Item), |
cdc7bbd5 | 54 | Annotatable::TraitItem(item) => { |
136023e0 | 55 | vis.flat_map_trait_item(item).pop().map(Annotatable::TraitItem) |
cdc7bbd5 XL |
56 | } |
57 | Annotatable::ImplItem(item) => { | |
136023e0 | 58 | vis.flat_map_impl_item(item).pop().map(Annotatable::ImplItem) |
cdc7bbd5 XL |
59 | } |
60 | Annotatable::ForeignItem(item) => { | |
136023e0 | 61 | vis.flat_map_foreign_item(item).pop().map(Annotatable::ForeignItem) |
6a06907d | 62 | } |
cdc7bbd5 | 63 | Annotatable::Stmt(stmt) => { |
136023e0 | 64 | vis.flat_map_stmt(stmt.into_inner()).pop().map(P).map(Annotatable::Stmt) |
cdc7bbd5 | 65 | } |
136023e0 | 66 | Annotatable::Expr(mut expr) => { |
cdc7bbd5 | 67 | vis.visit_expr(&mut expr); |
136023e0 | 68 | Some(Annotatable::Expr(expr)) |
cdc7bbd5 | 69 | } |
136023e0 XL |
70 | Annotatable::Arm(arm) => vis.flat_map_arm(arm).pop().map(Annotatable::Arm), |
71 | Annotatable::ExprField(field) => { | |
72 | vis.flat_map_expr_field(field).pop().map(Annotatable::ExprField) | |
cdc7bbd5 | 73 | } |
136023e0 | 74 | Annotatable::PatField(fp) => vis.flat_map_pat_field(fp).pop().map(Annotatable::PatField), |
cdc7bbd5 | 75 | Annotatable::GenericParam(param) => { |
136023e0 | 76 | vis.flat_map_generic_param(param).pop().map(Annotatable::GenericParam) |
cdc7bbd5 | 77 | } |
136023e0 XL |
78 | Annotatable::Param(param) => vis.flat_map_param(param).pop().map(Annotatable::Param), |
79 | Annotatable::FieldDef(sf) => vis.flat_map_field_def(sf).pop().map(Annotatable::FieldDef), | |
80 | Annotatable::Variant(v) => vis.flat_map_variant(v).pop().map(Annotatable::Variant), | |
a2a8927a XL |
81 | Annotatable::Crate(mut krate) => { |
82 | vis.visit_crate(&mut krate); | |
83 | Some(Annotatable::Crate(krate)) | |
84 | } | |
cdc7bbd5 XL |
85 | } |
86 | } | |
87 | ||
88 | struct CfgFinder { | |
89 | has_cfg_or_cfg_attr: bool, | |
90 | } | |
91 | ||
92 | impl CfgFinder { | |
93 | fn has_cfg_or_cfg_attr(annotatable: &Annotatable) -> bool { | |
94 | let mut finder = CfgFinder { has_cfg_or_cfg_attr: false }; | |
95 | match annotatable { | |
96 | Annotatable::Item(item) => finder.visit_item(&item), | |
97 | Annotatable::TraitItem(item) => finder.visit_assoc_item(&item, visit::AssocCtxt::Trait), | |
98 | Annotatable::ImplItem(item) => finder.visit_assoc_item(&item, visit::AssocCtxt::Impl), | |
99 | Annotatable::ForeignItem(item) => finder.visit_foreign_item(&item), | |
100 | Annotatable::Stmt(stmt) => finder.visit_stmt(&stmt), | |
101 | Annotatable::Expr(expr) => finder.visit_expr(&expr), | |
102 | Annotatable::Arm(arm) => finder.visit_arm(&arm), | |
103 | Annotatable::ExprField(field) => finder.visit_expr_field(&field), | |
104 | Annotatable::PatField(field) => finder.visit_pat_field(&field), | |
105 | Annotatable::GenericParam(param) => finder.visit_generic_param(¶m), | |
106 | Annotatable::Param(param) => finder.visit_param(¶m), | |
107 | Annotatable::FieldDef(field) => finder.visit_field_def(&field), | |
108 | Annotatable::Variant(variant) => finder.visit_variant(&variant), | |
a2a8927a | 109 | Annotatable::Crate(krate) => finder.visit_crate(krate), |
cdc7bbd5 XL |
110 | }; |
111 | finder.has_cfg_or_cfg_attr | |
6a06907d | 112 | } |
6a06907d XL |
113 | } |
114 | ||
cdc7bbd5 XL |
115 | impl<'ast> visit::Visitor<'ast> for CfgFinder { |
116 | fn visit_attribute(&mut self, attr: &'ast Attribute) { | |
117 | // We want short-circuiting behavior, so don't use the '|=' operator. | |
118 | self.has_cfg_or_cfg_attr = self.has_cfg_or_cfg_attr | |
119 | || attr | |
120 | .ident() | |
121 | .map_or(false, |ident| ident.name == sym::cfg || ident.name == sym::cfg_attr); | |
122 | } | |
6a06907d XL |
123 | } |
124 | ||
cdc7bbd5 | 125 | impl CfgEval<'_, '_> { |
04454e1e | 126 | fn configure<T: HasAttrs + HasTokens>(&mut self, node: T) -> Option<T> { |
6a06907d XL |
127 | self.cfg.configure(node) |
128 | } | |
129 | ||
136023e0 | 130 | fn configure_annotatable(&mut self, mut annotatable: Annotatable) -> Option<Annotatable> { |
cdc7bbd5 XL |
131 | // Tokenizing and re-parsing the `Annotatable` can have a significant |
132 | // performance impact, so try to avoid it if possible | |
133 | if !CfgFinder::has_cfg_or_cfg_attr(&annotatable) { | |
136023e0 | 134 | return Some(annotatable); |
cdc7bbd5 XL |
135 | } |
136 | ||
137 | // The majority of parsed attribute targets will never need to have early cfg-expansion | |
5e7ed085 | 138 | // run (e.g. they are not part of a `#[derive]` or `#[cfg_eval]` macro input). |
cdc7bbd5 XL |
139 | // Therefore, we normally do not capture the necessary information about `#[cfg]` |
140 | // and `#[cfg_attr]` attributes during parsing. | |
141 | // | |
142 | // Therefore, when we actually *do* run early cfg-expansion, we need to tokenize | |
143 | // and re-parse the attribute target, this time capturing information about | |
144 | // the location of `#[cfg]` and `#[cfg_attr]` in the token stream. The tokenization | |
145 | // process is lossless, so this process is invisible to proc-macros. | |
146 | ||
a2a8927a XL |
147 | let parse_annotatable_with: fn(&mut Parser<'_>) -> _ = match annotatable { |
148 | Annotatable::Item(_) => { | |
149 | |parser| Annotatable::Item(parser.parse_item(ForceCollect::Yes).unwrap().unwrap()) | |
150 | } | |
151 | Annotatable::TraitItem(_) => |parser| { | |
152 | Annotatable::TraitItem( | |
153 | parser.parse_trait_item(ForceCollect::Yes).unwrap().unwrap().unwrap(), | |
154 | ) | |
155 | }, | |
156 | Annotatable::ImplItem(_) => |parser| { | |
157 | Annotatable::ImplItem( | |
158 | parser.parse_impl_item(ForceCollect::Yes).unwrap().unwrap().unwrap(), | |
159 | ) | |
160 | }, | |
161 | Annotatable::ForeignItem(_) => |parser| { | |
162 | Annotatable::ForeignItem( | |
163 | parser.parse_foreign_item(ForceCollect::Yes).unwrap().unwrap().unwrap(), | |
164 | ) | |
165 | }, | |
166 | Annotatable::Stmt(_) => |parser| { | |
167 | Annotatable::Stmt(P(parser.parse_stmt(ForceCollect::Yes).unwrap().unwrap())) | |
168 | }, | |
169 | Annotatable::Expr(_) => { | |
170 | |parser| Annotatable::Expr(parser.parse_expr_force_collect().unwrap()) | |
171 | } | |
172 | _ => unreachable!(), | |
173 | }; | |
cdc7bbd5 | 174 | |
cdc7bbd5 XL |
175 | // 'Flatten' all nonterminals (i.e. `TokenKind::Interpolated`) |
176 | // to `None`-delimited groups containing the corresponding tokens. This | |
177 | // is normally delayed until the proc-macro server actually needs to | |
178 | // provide a `TokenKind::Interpolated` to a proc-macro. We do this earlier, | |
179 | // so that we can handle cases like: | |
180 | // | |
181 | // ```rust | |
182 | // #[cfg_eval] #[cfg] $item | |
183 | //``` | |
184 | // | |
185 | // where `$item` is `#[cfg_attr] struct Foo {}`. We want to make | |
186 | // sure to evaluate *all* `#[cfg]` and `#[cfg_attr]` attributes - the simplest | |
187 | // way to do this is to do a single parse of a stream without any nonterminals. | |
923072b8 | 188 | let orig_tokens = annotatable.to_tokens().flattened(); |
cdc7bbd5 XL |
189 | |
190 | // Re-parse the tokens, setting the `capture_cfg` flag to save extra information | |
191 | // to the captured `AttrAnnotatedTokenStream` (specifically, we capture | |
5e7ed085 | 192 | // `AttrAnnotatedTokenTree::AttributesData` for all occurrences of `#[cfg]` and `#[cfg_attr]`) |
cdc7bbd5 XL |
193 | let mut parser = |
194 | rustc_parse::stream_to_parser(&self.cfg.sess.parse_sess, orig_tokens, None); | |
195 | parser.capture_cfg = true; | |
a2a8927a | 196 | annotatable = parse_annotatable_with(&mut parser); |
cdc7bbd5 XL |
197 | |
198 | // Now that we have our re-parsed `AttrAnnotatedTokenStream`, recursively configuring | |
199 | // our attribute target will correctly the tokens as well. | |
200 | flat_map_annotatable(self, annotatable) | |
6a06907d XL |
201 | } |
202 | } | |
203 | ||
cdc7bbd5 | 204 | impl MutVisitor for CfgEval<'_, '_> { |
6a06907d XL |
205 | fn visit_expr(&mut self, expr: &mut P<ast::Expr>) { |
206 | self.cfg.configure_expr(expr); | |
207 | mut_visit::noop_visit_expr(expr, self); | |
208 | } | |
209 | ||
210 | fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> { | |
211 | let mut expr = configure!(self, expr); | |
212 | mut_visit::noop_visit_expr(&mut expr, self); | |
213 | Some(expr) | |
214 | } | |
215 | ||
216 | fn flat_map_generic_param( | |
217 | &mut self, | |
218 | param: ast::GenericParam, | |
219 | ) -> SmallVec<[ast::GenericParam; 1]> { | |
220 | mut_visit::noop_flat_map_generic_param(configure!(self, param), self) | |
221 | } | |
222 | ||
223 | fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> { | |
224 | mut_visit::noop_flat_map_stmt(configure!(self, stmt), self) | |
225 | } | |
226 | ||
227 | fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> { | |
228 | mut_visit::noop_flat_map_item(configure!(self, item), self) | |
229 | } | |
230 | ||
231 | fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> { | |
232 | mut_visit::noop_flat_map_assoc_item(configure!(self, item), self) | |
233 | } | |
234 | ||
235 | fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> { | |
236 | mut_visit::noop_flat_map_assoc_item(configure!(self, item), self) | |
237 | } | |
238 | ||
239 | fn flat_map_foreign_item( | |
240 | &mut self, | |
241 | foreign_item: P<ast::ForeignItem>, | |
242 | ) -> SmallVec<[P<ast::ForeignItem>; 1]> { | |
243 | mut_visit::noop_flat_map_foreign_item(configure!(self, foreign_item), self) | |
244 | } | |
245 | ||
246 | fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> { | |
247 | mut_visit::noop_flat_map_arm(configure!(self, arm), self) | |
248 | } | |
249 | ||
250 | fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> { | |
251 | mut_visit::noop_flat_map_expr_field(configure!(self, field), self) | |
252 | } | |
253 | ||
254 | fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> { | |
255 | mut_visit::noop_flat_map_pat_field(configure!(self, fp), self) | |
256 | } | |
257 | ||
258 | fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> { | |
259 | mut_visit::noop_flat_map_param(configure!(self, p), self) | |
260 | } | |
261 | ||
262 | fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> { | |
263 | mut_visit::noop_flat_map_field_def(configure!(self, sf), self) | |
264 | } | |
265 | ||
266 | fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> { | |
267 | mut_visit::noop_flat_map_variant(configure!(self, variant), self) | |
268 | } | |
269 | } |