2021-11-24 03:20:23 -06:00
|
|
|
use crate::util::{check_builtin_macro_attribute, warn_on_duplicate_attribute};
|
2021-03-06 12:33:02 -06:00
|
|
|
|
2020-11-28 17:33:17 -06:00
|
|
|
use rustc_ast as ast;
|
|
|
|
use rustc_ast::mut_visit::MutVisitor;
|
2021-07-17 09:28:43 -05:00
|
|
|
use rustc_ast::ptr::P;
|
2020-11-28 17:33:17 -06:00
|
|
|
use rustc_ast::tokenstream::CanSynthesizeMissingTokens;
|
|
|
|
use rustc_ast::visit::Visitor;
|
2022-02-27 15:26:24 -06:00
|
|
|
use rustc_ast::NodeId;
|
2020-11-28 17:33:17 -06:00
|
|
|
use rustc_ast::{mut_visit, visit};
|
2022-05-01 12:58:24 -05:00
|
|
|
use rustc_ast::{Attribute, HasAttrs, HasTokens};
|
2021-03-06 12:33:02 -06:00
|
|
|
use rustc_expand::base::{Annotatable, ExtCtxt};
|
|
|
|
use rustc_expand::config::StripUnconfigured;
|
2021-03-06 14:37:59 -06:00
|
|
|
use rustc_expand::configure;
|
2021-07-17 09:28:43 -05:00
|
|
|
use rustc_feature::Features;
|
2021-11-28 12:41:04 -06:00
|
|
|
use rustc_parse::parser::{ForceCollect, Parser};
|
2020-11-28 17:33:17 -06:00
|
|
|
use rustc_session::utils::FlattenNonterminals;
|
2021-07-17 09:28:43 -05:00
|
|
|
use rustc_session::Session;
|
2021-03-06 12:33:02 -06:00
|
|
|
use rustc_span::symbol::sym;
|
|
|
|
use rustc_span::Span;
|
2021-03-06 14:37:59 -06:00
|
|
|
use smallvec::SmallVec;
|
2021-03-06 12:33:02 -06:00
|
|
|
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) fn expand(
|
2021-03-06 12:33:02 -06:00
|
|
|
ecx: &mut ExtCtxt<'_>,
|
|
|
|
_span: Span,
|
|
|
|
meta_item: &ast::MetaItem,
|
2021-03-06 16:11:21 -06:00
|
|
|
annotatable: Annotatable,
|
2021-03-06 12:33:02 -06:00
|
|
|
) -> Vec<Annotatable> {
|
|
|
|
check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);
|
2021-11-24 03:20:23 -06:00
|
|
|
warn_on_duplicate_attribute(&ecx, &annotatable, sym::cfg_eval);
|
2022-02-27 15:26:24 -06:00
|
|
|
vec![cfg_eval(ecx.sess, ecx.ecfg.features, annotatable, ecx.current_expansion.lint_node_id)]
|
2021-03-06 16:11:21 -06:00
|
|
|
}
|
2021-03-06 12:33:02 -06:00
|
|
|
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) fn cfg_eval(
|
2021-07-17 09:28:43 -05:00
|
|
|
sess: &Session,
|
|
|
|
features: Option<&Features>,
|
|
|
|
annotatable: Annotatable,
|
2022-02-27 15:26:24 -06:00
|
|
|
lint_node_id: NodeId,
|
2021-07-17 09:28:43 -05:00
|
|
|
) -> Annotatable {
|
2022-02-27 15:26:24 -06:00
|
|
|
CfgEval { cfg: &mut StripUnconfigured { sess, features, config_tokens: true, lint_node_id } }
|
2021-07-17 09:28:43 -05:00
|
|
|
.configure_annotatable(annotatable)
|
|
|
|
// Since the item itself has already been configured by the `InvocationCollector`,
|
|
|
|
// we know that fold result vector will contain exactly one element.
|
|
|
|
.unwrap()
|
2020-11-28 17:33:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct CfgEval<'a, 'b> {
|
|
|
|
cfg: &'a mut StripUnconfigured<'b>,
|
|
|
|
}
|
|
|
|
|
2021-06-20 10:52:10 -05:00
|
|
|
fn flat_map_annotatable(
|
|
|
|
vis: &mut impl MutVisitor,
|
|
|
|
annotatable: Annotatable,
|
|
|
|
) -> Option<Annotatable> {
|
2020-11-28 17:33:17 -06:00
|
|
|
match annotatable {
|
2021-06-20 10:52:10 -05:00
|
|
|
Annotatable::Item(item) => vis.flat_map_item(item).pop().map(Annotatable::Item),
|
2020-11-28 17:33:17 -06:00
|
|
|
Annotatable::TraitItem(item) => {
|
2021-06-20 10:52:10 -05:00
|
|
|
vis.flat_map_trait_item(item).pop().map(Annotatable::TraitItem)
|
2020-11-28 17:33:17 -06:00
|
|
|
}
|
|
|
|
Annotatable::ImplItem(item) => {
|
2021-06-20 10:52:10 -05:00
|
|
|
vis.flat_map_impl_item(item).pop().map(Annotatable::ImplItem)
|
2020-11-28 17:33:17 -06:00
|
|
|
}
|
|
|
|
Annotatable::ForeignItem(item) => {
|
2021-06-20 10:52:10 -05:00
|
|
|
vis.flat_map_foreign_item(item).pop().map(Annotatable::ForeignItem)
|
2021-03-06 12:33:02 -06:00
|
|
|
}
|
2020-11-28 17:33:17 -06:00
|
|
|
Annotatable::Stmt(stmt) => {
|
2021-06-20 10:52:10 -05:00
|
|
|
vis.flat_map_stmt(stmt.into_inner()).pop().map(P).map(Annotatable::Stmt)
|
2020-11-28 17:33:17 -06:00
|
|
|
}
|
2021-06-20 10:52:10 -05:00
|
|
|
Annotatable::Expr(mut expr) => {
|
2020-11-28 17:33:17 -06:00
|
|
|
vis.visit_expr(&mut expr);
|
2021-06-20 10:52:10 -05:00
|
|
|
Some(Annotatable::Expr(expr))
|
2020-11-28 17:33:17 -06:00
|
|
|
}
|
2021-06-20 10:52:10 -05:00
|
|
|
Annotatable::Arm(arm) => vis.flat_map_arm(arm).pop().map(Annotatable::Arm),
|
|
|
|
Annotatable::ExprField(field) => {
|
|
|
|
vis.flat_map_expr_field(field).pop().map(Annotatable::ExprField)
|
2020-11-28 17:33:17 -06:00
|
|
|
}
|
2021-06-20 10:52:10 -05:00
|
|
|
Annotatable::PatField(fp) => vis.flat_map_pat_field(fp).pop().map(Annotatable::PatField),
|
2020-11-28 17:33:17 -06:00
|
|
|
Annotatable::GenericParam(param) => {
|
2021-06-20 10:52:10 -05:00
|
|
|
vis.flat_map_generic_param(param).pop().map(Annotatable::GenericParam)
|
2020-11-28 17:33:17 -06:00
|
|
|
}
|
2021-06-20 10:52:10 -05:00
|
|
|
Annotatable::Param(param) => vis.flat_map_param(param).pop().map(Annotatable::Param),
|
|
|
|
Annotatable::FieldDef(sf) => vis.flat_map_field_def(sf).pop().map(Annotatable::FieldDef),
|
|
|
|
Annotatable::Variant(v) => vis.flat_map_variant(v).pop().map(Annotatable::Variant),
|
2021-10-17 11:32:34 -05:00
|
|
|
Annotatable::Crate(mut krate) => {
|
|
|
|
vis.visit_crate(&mut krate);
|
|
|
|
Some(Annotatable::Crate(krate))
|
|
|
|
}
|
2020-11-28 17:33:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CfgFinder {
|
|
|
|
has_cfg_or_cfg_attr: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CfgFinder {
|
|
|
|
fn has_cfg_or_cfg_attr(annotatable: &Annotatable) -> bool {
|
|
|
|
let mut finder = CfgFinder { has_cfg_or_cfg_attr: false };
|
|
|
|
match annotatable {
|
|
|
|
Annotatable::Item(item) => finder.visit_item(&item),
|
|
|
|
Annotatable::TraitItem(item) => finder.visit_assoc_item(&item, visit::AssocCtxt::Trait),
|
|
|
|
Annotatable::ImplItem(item) => finder.visit_assoc_item(&item, visit::AssocCtxt::Impl),
|
|
|
|
Annotatable::ForeignItem(item) => finder.visit_foreign_item(&item),
|
|
|
|
Annotatable::Stmt(stmt) => finder.visit_stmt(&stmt),
|
|
|
|
Annotatable::Expr(expr) => finder.visit_expr(&expr),
|
|
|
|
Annotatable::Arm(arm) => finder.visit_arm(&arm),
|
|
|
|
Annotatable::ExprField(field) => finder.visit_expr_field(&field),
|
|
|
|
Annotatable::PatField(field) => finder.visit_pat_field(&field),
|
|
|
|
Annotatable::GenericParam(param) => finder.visit_generic_param(¶m),
|
|
|
|
Annotatable::Param(param) => finder.visit_param(¶m),
|
|
|
|
Annotatable::FieldDef(field) => finder.visit_field_def(&field),
|
|
|
|
Annotatable::Variant(variant) => finder.visit_variant(&variant),
|
2021-10-17 11:32:34 -05:00
|
|
|
Annotatable::Crate(krate) => finder.visit_crate(krate),
|
2020-11-28 17:33:17 -06:00
|
|
|
};
|
|
|
|
finder.has_cfg_or_cfg_attr
|
2021-03-06 12:33:02 -06:00
|
|
|
}
|
|
|
|
}
|
2021-03-06 14:37:59 -06:00
|
|
|
|
2020-11-28 17:33:17 -06:00
|
|
|
impl<'ast> visit::Visitor<'ast> for CfgFinder {
|
|
|
|
fn visit_attribute(&mut self, attr: &'ast Attribute) {
|
|
|
|
// We want short-circuiting behavior, so don't use the '|=' operator.
|
|
|
|
self.has_cfg_or_cfg_attr = self.has_cfg_or_cfg_attr
|
|
|
|
|| attr
|
|
|
|
.ident()
|
|
|
|
.map_or(false, |ident| ident.name == sym::cfg || ident.name == sym::cfg_attr);
|
|
|
|
}
|
2021-03-06 14:37:59 -06:00
|
|
|
}
|
|
|
|
|
2020-11-28 17:33:17 -06:00
|
|
|
impl CfgEval<'_, '_> {
|
2022-05-01 12:58:24 -05:00
|
|
|
fn configure<T: HasAttrs + HasTokens>(&mut self, node: T) -> Option<T> {
|
2021-03-06 14:37:59 -06:00
|
|
|
self.cfg.configure(node)
|
|
|
|
}
|
|
|
|
|
2021-06-20 10:52:10 -05:00
|
|
|
fn configure_annotatable(&mut self, mut annotatable: Annotatable) -> Option<Annotatable> {
|
2020-11-28 17:33:17 -06:00
|
|
|
// Tokenizing and re-parsing the `Annotatable` can have a significant
|
|
|
|
// performance impact, so try to avoid it if possible
|
|
|
|
if !CfgFinder::has_cfg_or_cfg_attr(&annotatable) {
|
2021-06-20 10:52:10 -05:00
|
|
|
return Some(annotatable);
|
2020-11-28 17:33:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// The majority of parsed attribute targets will never need to have early cfg-expansion
|
2022-03-30 00:39:38 -05:00
|
|
|
// run (e.g. they are not part of a `#[derive]` or `#[cfg_eval]` macro input).
|
2020-11-28 17:33:17 -06:00
|
|
|
// Therefore, we normally do not capture the necessary information about `#[cfg]`
|
|
|
|
// and `#[cfg_attr]` attributes during parsing.
|
|
|
|
//
|
|
|
|
// Therefore, when we actually *do* run early cfg-expansion, we need to tokenize
|
|
|
|
// and re-parse the attribute target, this time capturing information about
|
|
|
|
// the location of `#[cfg]` and `#[cfg_attr]` in the token stream. The tokenization
|
|
|
|
// process is lossless, so this process is invisible to proc-macros.
|
|
|
|
|
2021-11-28 12:41:04 -06:00
|
|
|
let parse_annotatable_with: fn(&mut Parser<'_>) -> _ = match annotatable {
|
|
|
|
Annotatable::Item(_) => {
|
|
|
|
|parser| Annotatable::Item(parser.parse_item(ForceCollect::Yes).unwrap().unwrap())
|
|
|
|
}
|
|
|
|
Annotatable::TraitItem(_) => |parser| {
|
|
|
|
Annotatable::TraitItem(
|
|
|
|
parser.parse_trait_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
Annotatable::ImplItem(_) => |parser| {
|
|
|
|
Annotatable::ImplItem(
|
|
|
|
parser.parse_impl_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
Annotatable::ForeignItem(_) => |parser| {
|
|
|
|
Annotatable::ForeignItem(
|
|
|
|
parser.parse_foreign_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
Annotatable::Stmt(_) => |parser| {
|
|
|
|
Annotatable::Stmt(P(parser.parse_stmt(ForceCollect::Yes).unwrap().unwrap()))
|
|
|
|
},
|
|
|
|
Annotatable::Expr(_) => {
|
|
|
|
|parser| Annotatable::Expr(parser.parse_expr_force_collect().unwrap())
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2020-11-28 17:33:17 -06:00
|
|
|
|
2022-05-01 12:58:24 -05:00
|
|
|
let mut orig_tokens = annotatable.to_tokens(&self.cfg.sess.parse_sess);
|
2020-11-28 17:33:17 -06:00
|
|
|
|
|
|
|
// 'Flatten' all nonterminals (i.e. `TokenKind::Interpolated`)
|
|
|
|
// to `None`-delimited groups containing the corresponding tokens. This
|
|
|
|
// is normally delayed until the proc-macro server actually needs to
|
|
|
|
// provide a `TokenKind::Interpolated` to a proc-macro. We do this earlier,
|
|
|
|
// so that we can handle cases like:
|
|
|
|
//
|
|
|
|
// ```rust
|
|
|
|
// #[cfg_eval] #[cfg] $item
|
|
|
|
//```
|
|
|
|
//
|
|
|
|
// where `$item` is `#[cfg_attr] struct Foo {}`. We want to make
|
|
|
|
// sure to evaluate *all* `#[cfg]` and `#[cfg_attr]` attributes - the simplest
|
|
|
|
// way to do this is to do a single parse of a stream without any nonterminals.
|
|
|
|
let mut flatten = FlattenNonterminals {
|
|
|
|
nt_to_tokenstream: rustc_parse::nt_to_tokenstream,
|
|
|
|
parse_sess: &self.cfg.sess.parse_sess,
|
|
|
|
synthesize_tokens: CanSynthesizeMissingTokens::No,
|
|
|
|
};
|
|
|
|
orig_tokens = flatten.process_token_stream(orig_tokens);
|
|
|
|
|
|
|
|
// Re-parse the tokens, setting the `capture_cfg` flag to save extra information
|
|
|
|
// to the captured `AttrAnnotatedTokenStream` (specifically, we capture
|
2022-03-01 06:02:47 -06:00
|
|
|
// `AttrAnnotatedTokenTree::AttributesData` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
|
2020-11-28 17:33:17 -06:00
|
|
|
let mut parser =
|
|
|
|
rustc_parse::stream_to_parser(&self.cfg.sess.parse_sess, orig_tokens, None);
|
|
|
|
parser.capture_cfg = true;
|
2021-11-28 12:41:04 -06:00
|
|
|
annotatable = parse_annotatable_with(&mut parser);
|
2020-11-28 17:33:17 -06:00
|
|
|
|
|
|
|
// Now that we have our re-parsed `AttrAnnotatedTokenStream`, recursively configuring
|
|
|
|
// our attribute target will correctly the tokens as well.
|
|
|
|
flat_map_annotatable(self, annotatable)
|
2021-03-06 14:37:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 17:33:17 -06:00
|
|
|
impl MutVisitor for CfgEval<'_, '_> {
|
2021-03-06 14:37:59 -06:00
|
|
|
fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
|
|
|
|
self.cfg.configure_expr(expr);
|
|
|
|
mut_visit::noop_visit_expr(expr, self);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
|
|
|
|
let mut expr = configure!(self, expr);
|
|
|
|
mut_visit::noop_visit_expr(&mut expr, self);
|
|
|
|
Some(expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_generic_param(
|
|
|
|
&mut self,
|
|
|
|
param: ast::GenericParam,
|
|
|
|
) -> SmallVec<[ast::GenericParam; 1]> {
|
|
|
|
mut_visit::noop_flat_map_generic_param(configure!(self, param), self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
|
|
|
|
mut_visit::noop_flat_map_stmt(configure!(self, stmt), self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
|
|
|
|
mut_visit::noop_flat_map_item(configure!(self, item), self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
|
|
|
|
mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
|
|
|
|
mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
|
|
|
|
}
|
|
|
|
|
2021-03-06 15:18:51 -06:00
|
|
|
fn flat_map_foreign_item(
|
|
|
|
&mut self,
|
|
|
|
foreign_item: P<ast::ForeignItem>,
|
|
|
|
) -> SmallVec<[P<ast::ForeignItem>; 1]> {
|
|
|
|
mut_visit::noop_flat_map_foreign_item(configure!(self, foreign_item), self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
|
|
|
|
mut_visit::noop_flat_map_arm(configure!(self, arm), self)
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:36:07 -05:00
|
|
|
fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
|
|
|
|
mut_visit::noop_flat_map_expr_field(configure!(self, field), self)
|
2021-03-06 15:18:51 -06:00
|
|
|
}
|
|
|
|
|
2021-03-15 16:36:07 -05:00
|
|
|
fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
|
|
|
|
mut_visit::noop_flat_map_pat_field(configure!(self, fp), self)
|
2021-03-06 15:18:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
|
|
|
|
mut_visit::noop_flat_map_param(configure!(self, p), self)
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:36:07 -05:00
|
|
|
fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
|
|
|
|
mut_visit::noop_flat_map_field_def(configure!(self, sf), self)
|
2021-03-06 14:37:59 -06:00
|
|
|
}
|
|
|
|
|
2021-03-06 15:18:51 -06:00
|
|
|
fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
|
|
|
|
mut_visit::noop_flat_map_variant(configure!(self, variant), self)
|
2021-03-06 14:37:59 -06:00
|
|
|
}
|
|
|
|
}
|