2014-04-22 23:54:48 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-08-22 22:54:53 -05:00
|
|
|
use attr::HasAttrs;
|
2016-10-04 12:10:33 -05:00
|
|
|
use feature_gate::{feature_err, EXPLAIN_STMT_ATTR_SYNTAX, Features, get_features, GateIssue};
|
2016-06-21 17:08:13 -05:00
|
|
|
use {fold, attr};
|
|
|
|
use ast;
|
2016-11-14 06:00:25 -06:00
|
|
|
use codemap::Spanned;
|
2016-09-01 20:44:23 -05:00
|
|
|
use parse::ParseSess;
|
2014-07-24 21:44:24 -05:00
|
|
|
use ptr::P;
|
2014-05-16 12:15:33 -05:00
|
|
|
|
2014-11-04 16:59:42 -06:00
|
|
|
use util::small_vector::SmallVector;
|
|
|
|
|
2016-05-31 21:13:45 -05:00
|
|
|
/// A folder that strips out items that do not belong in the current configuration.
|
2016-05-15 04:22:58 -05:00
|
|
|
pub struct StripUnconfigured<'a> {
|
2016-06-10 20:37:24 -05:00
|
|
|
pub should_test: bool,
|
|
|
|
pub sess: &'a ParseSess,
|
|
|
|
pub features: Option<&'a Features>,
|
2013-02-19 01:40:42 -06:00
|
|
|
}
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2016-08-31 18:39:16 -05:00
|
|
|
// `cfg_attr`-process the crate's attributes and compute the crate's features.
|
|
|
|
pub fn features(mut krate: ast::Crate, sess: &ParseSess, should_test: bool)
|
|
|
|
-> (ast::Crate, Features) {
|
|
|
|
let features;
|
|
|
|
{
|
|
|
|
let mut strip_unconfigured = StripUnconfigured {
|
|
|
|
should_test: should_test,
|
|
|
|
sess: sess,
|
|
|
|
features: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let unconfigured_attrs = krate.attrs.clone();
|
|
|
|
let err_count = sess.span_diagnostic.err_count();
|
|
|
|
if let Some(attrs) = strip_unconfigured.configure(krate.attrs) {
|
|
|
|
krate.attrs = attrs;
|
|
|
|
} else { // the entire crate is unconfigured
|
|
|
|
krate.attrs = Vec::new();
|
|
|
|
krate.module.items = Vec::new();
|
|
|
|
return (krate, Features::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
features = get_features(&sess.span_diagnostic, &krate.attrs);
|
|
|
|
|
|
|
|
// Avoid reconfiguring malformed `cfg_attr`s
|
|
|
|
if err_count == sess.span_diagnostic.err_count() {
|
|
|
|
strip_unconfigured.features = Some(&features);
|
|
|
|
strip_unconfigured.configure(unconfigured_attrs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(krate, features)
|
|
|
|
}
|
|
|
|
|
2016-09-01 20:44:23 -05:00
|
|
|
macro_rules! configure {
|
|
|
|
($this:ident, $node:ident) => {
|
|
|
|
match $this.configure($node) {
|
|
|
|
Some(node) => node,
|
|
|
|
None => return Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-15 23:42:57 -05:00
|
|
|
impl<'a> StripUnconfigured<'a> {
|
2016-09-07 17:24:01 -05:00
|
|
|
pub fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
|
2016-05-31 21:13:45 -05:00
|
|
|
let node = self.process_cfg_attrs(node);
|
|
|
|
if self.in_cfg(node.attrs()) { Some(node) } else { None }
|
|
|
|
}
|
|
|
|
|
2016-06-29 04:28:50 -05:00
|
|
|
pub fn process_cfg_attrs<T: HasAttrs>(&mut self, node: T) -> T {
|
2016-05-31 21:13:45 -05:00
|
|
|
node.map_attrs(|attrs| {
|
|
|
|
attrs.into_iter().filter_map(|attr| self.process_cfg_attr(attr)).collect()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-15 23:42:57 -05:00
|
|
|
fn process_cfg_attr(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> {
|
|
|
|
if !attr.check_name("cfg_attr") {
|
|
|
|
return Some(attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
let attr_list = match attr.meta_item_list() {
|
|
|
|
Some(attr_list) => attr_list,
|
|
|
|
None => {
|
|
|
|
let msg = "expected `#[cfg_attr(<cfg pattern>, <attr>)]`";
|
2016-06-10 20:37:24 -05:00
|
|
|
self.sess.span_diagnostic.span_err(attr.span, msg);
|
2016-05-15 23:42:57 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
2016-08-19 20:58:14 -05:00
|
|
|
|
2016-05-15 23:42:57 -05:00
|
|
|
let (cfg, mi) = match (attr_list.len(), attr_list.get(0), attr_list.get(1)) {
|
|
|
|
(2, Some(cfg), Some(mi)) => (cfg, mi),
|
|
|
|
_ => {
|
|
|
|
let msg = "expected `#[cfg_attr(<cfg pattern>, <attr>)]`";
|
2016-06-10 20:37:24 -05:00
|
|
|
self.sess.span_diagnostic.span_err(attr.span, msg);
|
2016-05-15 23:42:57 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-19 20:58:14 -05:00
|
|
|
use attr::cfg_matches;
|
|
|
|
match (cfg.meta_item(), mi.meta_item()) {
|
|
|
|
(Some(cfg), Some(mi)) =>
|
2016-10-27 01:36:56 -05:00
|
|
|
if cfg_matches(&cfg, self.sess, self.features) {
|
2016-11-14 06:00:25 -06:00
|
|
|
self.process_cfg_attr(ast::Attribute {
|
2016-08-19 20:58:14 -05:00
|
|
|
id: attr::mk_attr_id(),
|
2016-11-14 06:00:25 -06:00
|
|
|
style: attr.style,
|
2016-08-19 20:58:14 -05:00
|
|
|
value: mi.clone(),
|
|
|
|
is_sugared_doc: false,
|
2016-11-14 06:00:25 -06:00
|
|
|
span: mi.span,
|
|
|
|
})
|
2016-08-19 20:58:14 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
let msg = "unexpected literal(s) in `#[cfg_attr(<cfg pattern>, <attr>)]`";
|
|
|
|
self.sess.span_diagnostic.span_err(attr.span, msg);
|
|
|
|
None
|
|
|
|
}
|
2016-05-15 21:42:24 -05:00
|
|
|
}
|
2016-05-15 04:15:02 -05:00
|
|
|
}
|
2016-05-15 23:42:57 -05:00
|
|
|
|
2016-05-31 21:13:45 -05:00
|
|
|
// Determine if a node with the given attributes should be included in this configuation.
|
2016-09-14 17:36:42 -05:00
|
|
|
pub fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
|
2016-05-26 18:56:25 -05:00
|
|
|
attrs.iter().all(|attr| {
|
2016-05-31 20:27:12 -05:00
|
|
|
// When not compiling with --test we should not compile the #[test] functions
|
|
|
|
if !self.should_test && is_test_or_bench(attr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-14 06:00:25 -06:00
|
|
|
let mis = match attr.value.node {
|
2016-05-26 18:56:25 -05:00
|
|
|
ast::MetaItemKind::List(_, ref mis) if is_cfg(&attr) => mis,
|
|
|
|
_ => return true
|
|
|
|
};
|
|
|
|
|
|
|
|
if mis.len() != 1 {
|
2016-06-10 20:37:24 -05:00
|
|
|
self.sess.span_diagnostic.span_err(attr.span, "expected 1 cfg-pattern");
|
2016-05-26 18:56:25 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-19 20:58:14 -05:00
|
|
|
if !mis[0].is_meta_item() {
|
|
|
|
self.sess.span_diagnostic.span_err(mis[0].span, "unexpected literal");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-27 01:36:56 -05:00
|
|
|
attr::cfg_matches(mis[0].meta_item().unwrap(), self.sess, self.features)
|
2016-05-26 18:56:25 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-31 21:13:45 -05:00
|
|
|
// Visit attributes on expression and statements (but not attributes on items in blocks).
|
2016-10-05 22:44:59 -05:00
|
|
|
fn visit_expr_attrs(&mut self, attrs: &[ast::Attribute]) {
|
2016-05-15 23:42:57 -05:00
|
|
|
// flag the offending attributes
|
|
|
|
for attr in attrs.iter() {
|
2016-06-10 20:37:24 -05:00
|
|
|
if !self.features.map(|features| features.stmt_expr_attributes).unwrap_or(true) {
|
2016-10-04 12:10:33 -05:00
|
|
|
let mut err = feature_err(&self.sess,
|
|
|
|
"stmt_expr_attributes",
|
|
|
|
attr.span,
|
|
|
|
GateIssue::Language,
|
|
|
|
EXPLAIN_STMT_ATTR_SYNTAX);
|
2016-11-14 06:00:25 -06:00
|
|
|
if attr.is_sugared_doc {
|
2016-10-04 12:10:33 -05:00
|
|
|
err.help("`///` is for documentation comments. For a plain comment, use `//`.");
|
|
|
|
}
|
|
|
|
err.emit();
|
2016-06-10 20:37:24 -05:00
|
|
|
}
|
2016-05-15 04:15:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 17:24:01 -05:00
|
|
|
pub fn configure_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
|
2016-05-14 21:34:32 -05:00
|
|
|
ast::ForeignMod {
|
|
|
|
abi: foreign_mod.abi,
|
2016-09-01 20:44:23 -05:00
|
|
|
items: foreign_mod.items.into_iter().filter_map(|item| self.configure(item)).collect(),
|
2016-05-14 21:34:32 -05:00
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2016-05-14 21:34:32 -05:00
|
|
|
|
2016-09-01 20:44:23 -05:00
|
|
|
fn configure_variant_data(&mut self, vdata: ast::VariantData) -> ast::VariantData {
|
|
|
|
match vdata {
|
2016-05-14 21:34:32 -05:00
|
|
|
ast::VariantData::Struct(fields, id) => {
|
2016-09-01 20:44:23 -05:00
|
|
|
let fields = fields.into_iter().filter_map(|field| self.configure(field));
|
2016-05-15 21:42:24 -05:00
|
|
|
ast::VariantData::Struct(fields.collect(), id)
|
2016-05-14 21:34:32 -05:00
|
|
|
}
|
|
|
|
ast::VariantData::Tuple(fields, id) => {
|
2016-09-01 20:44:23 -05:00
|
|
|
let fields = fields.into_iter().filter_map(|field| self.configure(field));
|
2016-05-15 21:42:24 -05:00
|
|
|
ast::VariantData::Tuple(fields.collect(), id)
|
2016-05-14 21:34:32 -05:00
|
|
|
}
|
|
|
|
ast::VariantData::Unit(id) => ast::VariantData::Unit(id)
|
2016-09-01 20:44:23 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-14 21:34:32 -05:00
|
|
|
|
2016-09-07 17:24:01 -05:00
|
|
|
pub fn configure_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
|
2016-09-01 20:44:23 -05:00
|
|
|
match item {
|
2016-05-14 21:34:32 -05:00
|
|
|
ast::ItemKind::Struct(def, generics) => {
|
2016-09-01 20:44:23 -05:00
|
|
|
ast::ItemKind::Struct(self.configure_variant_data(def), generics)
|
2016-05-14 21:34:32 -05:00
|
|
|
}
|
2016-08-29 00:04:31 -05:00
|
|
|
ast::ItemKind::Union(def, generics) => {
|
2016-09-01 20:44:23 -05:00
|
|
|
ast::ItemKind::Union(self.configure_variant_data(def), generics)
|
2016-08-29 00:04:31 -05:00
|
|
|
}
|
2016-05-14 21:34:32 -05:00
|
|
|
ast::ItemKind::Enum(def, generics) => {
|
|
|
|
let variants = def.variants.into_iter().filter_map(|v| {
|
2016-05-15 21:42:24 -05:00
|
|
|
self.configure(v).map(|v| {
|
|
|
|
Spanned {
|
2016-05-14 21:34:32 -05:00
|
|
|
node: ast::Variant_ {
|
|
|
|
name: v.node.name,
|
|
|
|
attrs: v.node.attrs,
|
2016-09-01 20:44:23 -05:00
|
|
|
data: self.configure_variant_data(v.node.data),
|
2016-05-14 21:34:32 -05:00
|
|
|
disr_expr: v.node.disr_expr,
|
|
|
|
},
|
|
|
|
span: v.span
|
2016-05-15 21:42:24 -05:00
|
|
|
}
|
|
|
|
})
|
2016-05-14 21:34:32 -05:00
|
|
|
});
|
|
|
|
ast::ItemKind::Enum(ast::EnumDef {
|
|
|
|
variants: variants.collect(),
|
|
|
|
}, generics)
|
|
|
|
}
|
|
|
|
item => item,
|
2016-09-01 20:44:23 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-14 21:34:32 -05:00
|
|
|
|
2016-09-07 17:24:01 -05:00
|
|
|
pub fn configure_expr_kind(&mut self, expr_kind: ast::ExprKind) -> ast::ExprKind {
|
2016-09-01 20:44:23 -05:00
|
|
|
if let ast::ExprKind::Match(m, arms) = expr_kind {
|
|
|
|
let arms = arms.into_iter().filter_map(|a| self.configure(a)).collect();
|
|
|
|
ast::ExprKind::Match(m, arms)
|
|
|
|
} else {
|
|
|
|
expr_kind
|
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2016-05-14 21:34:32 -05:00
|
|
|
|
2016-09-07 17:24:01 -05:00
|
|
|
pub fn configure_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
|
2016-10-05 22:44:59 -05:00
|
|
|
self.visit_expr_attrs(expr.attrs());
|
2016-06-10 20:37:24 -05:00
|
|
|
|
2015-11-03 10:39:51 -06:00
|
|
|
// If an expr is valid to cfg away it will have been removed by the
|
|
|
|
// outer stmt or expression folder before descending in here.
|
|
|
|
// Anything else is always required, and thus has to error out
|
|
|
|
// in case of a cfg attr.
|
|
|
|
//
|
2015-11-15 10:17:50 -06:00
|
|
|
// NB: This is intentionally not part of the fold_expr() function
|
2015-11-03 10:39:51 -06:00
|
|
|
// in order for fold_opt_expr() to be able to avoid this check
|
2016-06-10 20:37:24 -05:00
|
|
|
if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a) || is_test_or_bench(a)) {
|
|
|
|
let msg = "removing an expression is not supported in this position";
|
|
|
|
self.sess.span_diagnostic.span_err(attr.span, msg);
|
|
|
|
}
|
|
|
|
|
2016-09-01 20:44:23 -05:00
|
|
|
self.process_cfg_attrs(expr)
|
2014-04-22 23:54:48 -05:00
|
|
|
}
|
2016-05-14 21:34:32 -05:00
|
|
|
|
2016-09-07 17:24:01 -05:00
|
|
|
pub fn configure_stmt(&mut self, stmt: ast::Stmt) -> Option<ast::Stmt> {
|
2016-09-01 20:44:23 -05:00
|
|
|
self.configure(stmt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> fold::Folder for StripUnconfigured<'a> {
|
|
|
|
fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
|
|
|
|
let foreign_mod = self.configure_foreign_mod(foreign_mod);
|
|
|
|
fold::noop_fold_foreign_mod(foreign_mod, self)
|
2015-11-03 10:39:51 -06:00
|
|
|
}
|
2016-05-14 21:34:32 -05:00
|
|
|
|
2016-09-01 20:44:23 -05:00
|
|
|
fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
|
|
|
|
let item = self.configure_item_kind(item);
|
|
|
|
fold::noop_fold_item_kind(item, self)
|
2015-11-03 10:39:51 -06:00
|
|
|
}
|
2016-05-14 21:34:32 -05:00
|
|
|
|
2016-09-01 20:44:23 -05:00
|
|
|
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
|
|
|
|
let mut expr = self.configure_expr(expr).unwrap();
|
|
|
|
expr.node = self.configure_expr_kind(expr.node);
|
|
|
|
P(fold::noop_fold_expr(expr, self))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
|
|
|
|
let mut expr = configure!(self, expr).unwrap();
|
|
|
|
expr.node = self.configure_expr_kind(expr.node);
|
|
|
|
Some(P(fold::noop_fold_expr(expr, self)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
|
|
|
|
match self.configure_stmt(stmt) {
|
|
|
|
Some(stmt) => fold::noop_fold_stmt(stmt, self),
|
2016-11-02 23:33:35 -05:00
|
|
|
None => return SmallVector::new(),
|
2016-09-01 20:44:23 -05:00
|
|
|
}
|
2014-07-09 16:48:12 -05:00
|
|
|
}
|
2016-05-14 21:34:32 -05:00
|
|
|
|
2014-11-04 16:59:42 -06:00
|
|
|
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
|
2016-09-01 20:44:23 -05:00
|
|
|
fold::noop_fold_item(configure!(self, item), self)
|
2016-06-02 02:34:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector<ast::ImplItem> {
|
2016-09-01 20:44:23 -05:00
|
|
|
fold::noop_fold_impl_item(configure!(self, item), self)
|
2016-06-02 02:34:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector<ast::TraitItem> {
|
2016-09-01 20:44:23 -05:00
|
|
|
fold::noop_fold_trait_item(configure!(self, item), self)
|
2014-11-04 16:59:42 -06:00
|
|
|
}
|
2016-06-08 19:26:35 -05:00
|
|
|
|
2016-09-01 20:44:23 -05:00
|
|
|
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
|
2016-06-08 19:26:35 -05:00
|
|
|
// Don't configure interpolated AST (c.f. #34171).
|
|
|
|
// Interpolated AST will get configured once the surrounding tokens are parsed.
|
2016-09-01 20:44:23 -05:00
|
|
|
mac
|
2016-06-08 19:26:35 -05:00
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2015-11-03 10:39:51 -06:00
|
|
|
fn is_cfg(attr: &ast::Attribute) -> bool {
|
|
|
|
attr.check_name("cfg")
|
|
|
|
}
|
|
|
|
|
2016-09-23 02:23:01 -05:00
|
|
|
pub fn is_test_or_bench(attr: &ast::Attribute) -> bool {
|
2016-05-31 20:27:12 -05:00
|
|
|
attr.check_name("test") || attr.check_name("bench")
|
|
|
|
}
|