1 use crate::util::{check_builtin_macro_attribute, warn_on_duplicate_attribute};
4 use rustc_ast::mut_visit::MutVisitor;
6 use rustc_ast::tokenstream::CanSynthesizeMissingTokens;
7 use rustc_ast::visit::Visitor;
9 use rustc_ast::{mut_visit, visit};
10 use rustc_ast::{AstLike, Attribute};
11 use rustc_expand::base::{Annotatable, ExtCtxt};
12 use rustc_expand::config::StripUnconfigured;
13 use rustc_expand::configure;
14 use rustc_feature::Features;
15 use rustc_parse::parser::{ForceCollect, Parser};
16 use rustc_session::utils::FlattenNonterminals;
17 use rustc_session::Session;
18 use rustc_span::symbol::sym;
20 use smallvec::SmallVec;
23 ecx: &mut ExtCtxt<'_>,
25 meta_item: &ast::MetaItem,
26 annotatable: Annotatable,
27 ) -> Vec<Annotatable> {
28 check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);
29 warn_on_duplicate_attribute(&ecx, &annotatable, sym::cfg_eval);
30 vec![cfg_eval(ecx.sess, ecx.ecfg.features, annotatable, ecx.current_expansion.lint_node_id)]
35 features: Option<&Features>,
36 annotatable: Annotatable,
39 CfgEval { cfg: &mut StripUnconfigured { sess, features, config_tokens: true, lint_node_id } }
40 .configure_annotatable(annotatable)
41 // Since the item itself has already been configured by the `InvocationCollector`,
42 // we know that fold result vector will contain exactly one element.
46 struct CfgEval<'a, 'b> {
47 cfg: &'a mut StripUnconfigured<'b>,
50 fn flat_map_annotatable(
51 vis: &mut impl MutVisitor,
52 annotatable: Annotatable,
53 ) -> Option<Annotatable> {
55 Annotatable::Item(item) => vis.flat_map_item(item).pop().map(Annotatable::Item),
56 Annotatable::TraitItem(item) => {
57 vis.flat_map_trait_item(item).pop().map(Annotatable::TraitItem)
59 Annotatable::ImplItem(item) => {
60 vis.flat_map_impl_item(item).pop().map(Annotatable::ImplItem)
62 Annotatable::ForeignItem(item) => {
63 vis.flat_map_foreign_item(item).pop().map(Annotatable::ForeignItem)
65 Annotatable::Stmt(stmt) => {
66 vis.flat_map_stmt(stmt.into_inner()).pop().map(P).map(Annotatable::Stmt)
68 Annotatable::Expr(mut expr) => {
69 vis.visit_expr(&mut expr);
70 Some(Annotatable::Expr(expr))
72 Annotatable::Arm(arm) => vis.flat_map_arm(arm).pop().map(Annotatable::Arm),
73 Annotatable::ExprField(field) => {
74 vis.flat_map_expr_field(field).pop().map(Annotatable::ExprField)
76 Annotatable::PatField(fp) => vis.flat_map_pat_field(fp).pop().map(Annotatable::PatField),
77 Annotatable::GenericParam(param) => {
78 vis.flat_map_generic_param(param).pop().map(Annotatable::GenericParam)
80 Annotatable::Param(param) => vis.flat_map_param(param).pop().map(Annotatable::Param),
81 Annotatable::FieldDef(sf) => vis.flat_map_field_def(sf).pop().map(Annotatable::FieldDef),
82 Annotatable::Variant(v) => vis.flat_map_variant(v).pop().map(Annotatable::Variant),
83 Annotatable::Crate(mut krate) => {
84 vis.visit_crate(&mut krate);
85 Some(Annotatable::Crate(krate))
91 has_cfg_or_cfg_attr: bool,
95 fn has_cfg_or_cfg_attr(annotatable: &Annotatable) -> bool {
96 let mut finder = CfgFinder { has_cfg_or_cfg_attr: false };
98 Annotatable::Item(item) => finder.visit_item(&item),
99 Annotatable::TraitItem(item) => finder.visit_assoc_item(&item, visit::AssocCtxt::Trait),
100 Annotatable::ImplItem(item) => finder.visit_assoc_item(&item, visit::AssocCtxt::Impl),
101 Annotatable::ForeignItem(item) => finder.visit_foreign_item(&item),
102 Annotatable::Stmt(stmt) => finder.visit_stmt(&stmt),
103 Annotatable::Expr(expr) => finder.visit_expr(&expr),
104 Annotatable::Arm(arm) => finder.visit_arm(&arm),
105 Annotatable::ExprField(field) => finder.visit_expr_field(&field),
106 Annotatable::PatField(field) => finder.visit_pat_field(&field),
107 Annotatable::GenericParam(param) => finder.visit_generic_param(¶m),
108 Annotatable::Param(param) => finder.visit_param(¶m),
109 Annotatable::FieldDef(field) => finder.visit_field_def(&field),
110 Annotatable::Variant(variant) => finder.visit_variant(&variant),
111 Annotatable::Crate(krate) => finder.visit_crate(krate),
113 finder.has_cfg_or_cfg_attr
117 impl<'ast> visit::Visitor<'ast> for CfgFinder {
118 fn visit_attribute(&mut self, attr: &'ast Attribute) {
119 // We want short-circuiting behavior, so don't use the '|=' operator.
120 self.has_cfg_or_cfg_attr = self.has_cfg_or_cfg_attr
123 .map_or(false, |ident| ident.name == sym::cfg || ident.name == sym::cfg_attr);
127 impl CfgEval<'_, '_> {
128 fn configure<T: AstLike>(&mut self, node: T) -> Option<T> {
129 self.cfg.configure(node)
132 fn configure_annotatable(&mut self, mut annotatable: Annotatable) -> Option<Annotatable> {
133 // Tokenizing and re-parsing the `Annotatable` can have a significant
134 // performance impact, so try to avoid it if possible
135 if !CfgFinder::has_cfg_or_cfg_attr(&annotatable) {
136 return Some(annotatable);
139 // The majority of parsed attribute targets will never need to have early cfg-expansion
140 // run (e.g. they are not part of a `#[derive]` or `#[cfg_eval]` macro input).
141 // Therefore, we normally do not capture the necessary information about `#[cfg]`
142 // and `#[cfg_attr]` attributes during parsing.
144 // Therefore, when we actually *do* run early cfg-expansion, we need to tokenize
145 // and re-parse the attribute target, this time capturing information about
146 // the location of `#[cfg]` and `#[cfg_attr]` in the token stream. The tokenization
147 // process is lossless, so this process is invisible to proc-macros.
149 let parse_annotatable_with: fn(&mut Parser<'_>) -> _ = match annotatable {
150 Annotatable::Item(_) => {
151 |parser| Annotatable::Item(parser.parse_item(ForceCollect::Yes).unwrap().unwrap())
153 Annotatable::TraitItem(_) => |parser| {
154 Annotatable::TraitItem(
155 parser.parse_trait_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
158 Annotatable::ImplItem(_) => |parser| {
159 Annotatable::ImplItem(
160 parser.parse_impl_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
163 Annotatable::ForeignItem(_) => |parser| {
164 Annotatable::ForeignItem(
165 parser.parse_foreign_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
168 Annotatable::Stmt(_) => |parser| {
169 Annotatable::Stmt(P(parser.parse_stmt(ForceCollect::Yes).unwrap().unwrap()))
171 Annotatable::Expr(_) => {
172 |parser| Annotatable::Expr(parser.parse_expr_force_collect().unwrap())
176 let nt = annotatable.into_nonterminal();
178 let mut orig_tokens = rustc_parse::nt_to_tokenstream(
180 &self.cfg.sess.parse_sess,
181 CanSynthesizeMissingTokens::No,
184 // 'Flatten' all nonterminals (i.e. `TokenKind::Interpolated`)
185 // to `None`-delimited groups containing the corresponding tokens. This
186 // is normally delayed until the proc-macro server actually needs to
187 // provide a `TokenKind::Interpolated` to a proc-macro. We do this earlier,
188 // so that we can handle cases like:
191 // #[cfg_eval] #[cfg] $item
194 // where `$item` is `#[cfg_attr] struct Foo {}`. We want to make
195 // sure to evaluate *all* `#[cfg]` and `#[cfg_attr]` attributes - the simplest
196 // way to do this is to do a single parse of a stream without any nonterminals.
197 let mut flatten = FlattenNonterminals {
198 nt_to_tokenstream: rustc_parse::nt_to_tokenstream,
199 parse_sess: &self.cfg.sess.parse_sess,
200 synthesize_tokens: CanSynthesizeMissingTokens::No,
202 orig_tokens = flatten.process_token_stream(orig_tokens);
204 // Re-parse the tokens, setting the `capture_cfg` flag to save extra information
205 // to the captured `AttrAnnotatedTokenStream` (specifically, we capture
206 // `AttrAnnotatedTokenTree::AttributesData` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
208 rustc_parse::stream_to_parser(&self.cfg.sess.parse_sess, orig_tokens, None);
209 parser.capture_cfg = true;
210 annotatable = parse_annotatable_with(&mut parser);
212 // Now that we have our re-parsed `AttrAnnotatedTokenStream`, recursively configuring
213 // our attribute target will correctly the tokens as well.
214 flat_map_annotatable(self, annotatable)
218 impl MutVisitor for CfgEval<'_, '_> {
219 fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
220 self.cfg.configure_expr(expr);
221 mut_visit::noop_visit_expr(expr, self);
224 fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
225 let mut expr = configure!(self, expr);
226 mut_visit::noop_visit_expr(&mut expr, self);
230 fn flat_map_generic_param(
232 param: ast::GenericParam,
233 ) -> SmallVec<[ast::GenericParam; 1]> {
234 mut_visit::noop_flat_map_generic_param(configure!(self, param), self)
237 fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
238 mut_visit::noop_flat_map_stmt(configure!(self, stmt), self)
241 fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
242 mut_visit::noop_flat_map_item(configure!(self, item), self)
245 fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
246 mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
249 fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
250 mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
253 fn flat_map_foreign_item(
255 foreign_item: P<ast::ForeignItem>,
256 ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
257 mut_visit::noop_flat_map_foreign_item(configure!(self, foreign_item), self)
260 fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
261 mut_visit::noop_flat_map_arm(configure!(self, arm), self)
264 fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
265 mut_visit::noop_flat_map_expr_field(configure!(self, field), self)
268 fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
269 mut_visit::noop_flat_map_pat_field(configure!(self, fp), self)
272 fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
273 mut_visit::noop_flat_map_param(configure!(self, p), self)
276 fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
277 mut_visit::noop_flat_map_field_def(configure!(self, sf), self)
280 fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
281 mut_visit::noop_flat_map_variant(configure!(self, variant), self)