1 use crate::util::check_builtin_macro_attribute;
4 use rustc_ast::mut_visit::MutVisitor;
5 use rustc_ast::tokenstream::CanSynthesizeMissingTokens;
6 use rustc_ast::visit::Visitor;
7 use rustc_ast::{mut_visit, visit};
8 use rustc_ast::{AstLike, Attribute};
9 use rustc_expand::base::{Annotatable, ExtCtxt};
10 use rustc_expand::config::StripUnconfigured;
11 use rustc_expand::configure;
12 use rustc_parse::parser::ForceCollect;
13 use rustc_session::utils::FlattenNonterminals;
15 use rustc_ast::ptr::P;
16 use rustc_span::symbol::sym;
18 use smallvec::SmallVec;
21 ecx: &mut ExtCtxt<'_>,
23 meta_item: &ast::MetaItem,
24 annotatable: Annotatable,
25 ) -> Vec<Annotatable> {
26 check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);
27 vec![cfg_eval(ecx, annotatable)]
30 crate fn cfg_eval(ecx: &ExtCtxt<'_>, annotatable: Annotatable) -> Annotatable {
32 cfg: &mut StripUnconfigured {
34 features: ecx.ecfg.features,
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.
44 struct CfgEval<'a, 'b> {
45 cfg: &'a mut StripUnconfigured<'b>,
48 fn flat_map_annotatable(
49 vis: &mut impl MutVisitor,
50 annotatable: Annotatable,
51 ) -> Option<Annotatable> {
53 Annotatable::Item(item) => vis.flat_map_item(item).pop().map(Annotatable::Item),
54 Annotatable::TraitItem(item) => {
55 vis.flat_map_trait_item(item).pop().map(Annotatable::TraitItem)
57 Annotatable::ImplItem(item) => {
58 vis.flat_map_impl_item(item).pop().map(Annotatable::ImplItem)
60 Annotatable::ForeignItem(item) => {
61 vis.flat_map_foreign_item(item).pop().map(Annotatable::ForeignItem)
63 Annotatable::Stmt(stmt) => {
64 vis.flat_map_stmt(stmt.into_inner()).pop().map(P).map(Annotatable::Stmt)
66 Annotatable::Expr(mut expr) => {
67 vis.visit_expr(&mut expr);
68 Some(Annotatable::Expr(expr))
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)
74 Annotatable::PatField(fp) => vis.flat_map_pat_field(fp).pop().map(Annotatable::PatField),
75 Annotatable::GenericParam(param) => {
76 vis.flat_map_generic_param(param).pop().map(Annotatable::GenericParam)
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),
85 has_cfg_or_cfg_attr: bool,
89 fn has_cfg_or_cfg_attr(annotatable: &Annotatable) -> bool {
90 let mut finder = CfgFinder { has_cfg_or_cfg_attr: false };
92 Annotatable::Item(item) => finder.visit_item(&item),
93 Annotatable::TraitItem(item) => finder.visit_assoc_item(&item, visit::AssocCtxt::Trait),
94 Annotatable::ImplItem(item) => finder.visit_assoc_item(&item, visit::AssocCtxt::Impl),
95 Annotatable::ForeignItem(item) => finder.visit_foreign_item(&item),
96 Annotatable::Stmt(stmt) => finder.visit_stmt(&stmt),
97 Annotatable::Expr(expr) => finder.visit_expr(&expr),
98 Annotatable::Arm(arm) => finder.visit_arm(&arm),
99 Annotatable::ExprField(field) => finder.visit_expr_field(&field),
100 Annotatable::PatField(field) => finder.visit_pat_field(&field),
101 Annotatable::GenericParam(param) => finder.visit_generic_param(¶m),
102 Annotatable::Param(param) => finder.visit_param(¶m),
103 Annotatable::FieldDef(field) => finder.visit_field_def(&field),
104 Annotatable::Variant(variant) => finder.visit_variant(&variant),
106 finder.has_cfg_or_cfg_attr
110 impl<'ast> visit::Visitor<'ast> for CfgFinder {
111 fn visit_attribute(&mut self, attr: &'ast Attribute) {
112 // We want short-circuiting behavior, so don't use the '|=' operator.
113 self.has_cfg_or_cfg_attr = self.has_cfg_or_cfg_attr
116 .map_or(false, |ident| ident.name == sym::cfg || ident.name == sym::cfg_attr);
120 impl CfgEval<'_, '_> {
121 fn configure<T: AstLike>(&mut self, node: T) -> Option<T> {
122 self.cfg.configure(node)
125 fn configure_annotatable(&mut self, mut annotatable: Annotatable) -> Option<Annotatable> {
126 // Tokenizing and re-parsing the `Annotatable` can have a significant
127 // performance impact, so try to avoid it if possible
128 if !CfgFinder::has_cfg_or_cfg_attr(&annotatable) {
129 return Some(annotatable);
132 // The majority of parsed attribute targets will never need to have early cfg-expansion
133 // run (e.g. they are not part of a `#[derive]` or `#[cfg_eval]` macro inoput).
134 // Therefore, we normally do not capture the necessary information about `#[cfg]`
135 // and `#[cfg_attr]` attributes during parsing.
137 // Therefore, when we actually *do* run early cfg-expansion, we need to tokenize
138 // and re-parse the attribute target, this time capturing information about
139 // the location of `#[cfg]` and `#[cfg_attr]` in the token stream. The tokenization
140 // process is lossless, so this process is invisible to proc-macros.
142 // FIXME - get rid of this clone
143 let nt = annotatable.clone().into_nonterminal();
145 let mut orig_tokens = rustc_parse::nt_to_tokenstream(
147 &self.cfg.sess.parse_sess,
148 CanSynthesizeMissingTokens::No,
151 // 'Flatten' all nonterminals (i.e. `TokenKind::Interpolated`)
152 // to `None`-delimited groups containing the corresponding tokens. This
153 // is normally delayed until the proc-macro server actually needs to
154 // provide a `TokenKind::Interpolated` to a proc-macro. We do this earlier,
155 // so that we can handle cases like:
158 // #[cfg_eval] #[cfg] $item
161 // where `$item` is `#[cfg_attr] struct Foo {}`. We want to make
162 // sure to evaluate *all* `#[cfg]` and `#[cfg_attr]` attributes - the simplest
163 // way to do this is to do a single parse of a stream without any nonterminals.
164 let mut flatten = FlattenNonterminals {
165 nt_to_tokenstream: rustc_parse::nt_to_tokenstream,
166 parse_sess: &self.cfg.sess.parse_sess,
167 synthesize_tokens: CanSynthesizeMissingTokens::No,
169 orig_tokens = flatten.process_token_stream(orig_tokens);
171 // Re-parse the tokens, setting the `capture_cfg` flag to save extra information
172 // to the captured `AttrAnnotatedTokenStream` (specifically, we capture
173 // `AttrAnnotatedTokenTree::AttributesData` for all occurences of `#[cfg]` and `#[cfg_attr]`)
175 rustc_parse::stream_to_parser(&self.cfg.sess.parse_sess, orig_tokens, None);
176 parser.capture_cfg = true;
177 annotatable = match annotatable {
178 Annotatable::Item(_) => {
179 Annotatable::Item(parser.parse_item(ForceCollect::Yes).unwrap().unwrap())
181 Annotatable::TraitItem(_) => Annotatable::TraitItem(
182 parser.parse_trait_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
184 Annotatable::ImplItem(_) => Annotatable::ImplItem(
185 parser.parse_impl_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
187 Annotatable::ForeignItem(_) => Annotatable::ForeignItem(
188 parser.parse_foreign_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
190 Annotatable::Stmt(_) => {
191 Annotatable::Stmt(P(parser.parse_stmt(ForceCollect::Yes).unwrap().unwrap()))
193 Annotatable::Expr(_) => Annotatable::Expr(parser.parse_expr_force_collect().unwrap()),
197 // Now that we have our re-parsed `AttrAnnotatedTokenStream`, recursively configuring
198 // our attribute target will correctly the tokens as well.
199 flat_map_annotatable(self, annotatable)
203 impl MutVisitor for CfgEval<'_, '_> {
204 fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
205 self.cfg.configure_expr(expr);
206 mut_visit::noop_visit_expr(expr, self);
209 fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
210 let mut expr = configure!(self, expr);
211 mut_visit::noop_visit_expr(&mut expr, self);
215 fn flat_map_generic_param(
217 param: ast::GenericParam,
218 ) -> SmallVec<[ast::GenericParam; 1]> {
219 mut_visit::noop_flat_map_generic_param(configure!(self, param), self)
222 fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
223 mut_visit::noop_flat_map_stmt(configure!(self, stmt), self)
226 fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
227 mut_visit::noop_flat_map_item(configure!(self, item), self)
230 fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
231 mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
234 fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
235 mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
238 fn flat_map_foreign_item(
240 foreign_item: P<ast::ForeignItem>,
241 ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
242 mut_visit::noop_flat_map_foreign_item(configure!(self, foreign_item), self)
245 fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
246 mut_visit::noop_flat_map_arm(configure!(self, arm), self)
249 fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
250 mut_visit::noop_flat_map_expr_field(configure!(self, field), self)
253 fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
254 mut_visit::noop_flat_map_pat_field(configure!(self, fp), self)
257 fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
258 mut_visit::noop_flat_map_param(configure!(self, p), self)
261 fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
262 mut_visit::noop_flat_map_field_def(configure!(self, sf), self)
265 fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
266 mut_visit::noop_flat_map_variant(configure!(self, variant), self)