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.
|
|
|
|
|
2014-09-24 22:22:57 -05:00
|
|
|
use attr::AttrMetaMethods;
|
|
|
|
use diagnostic::SpanHandler;
|
2015-07-13 19:10:44 -05:00
|
|
|
use feature_gate::GatedCfg;
|
2014-07-24 21:44:24 -05:00
|
|
|
use fold::Folder;
|
|
|
|
use {ast, fold, attr};
|
2015-02-09 11:29:21 -06:00
|
|
|
use codemap::{Spanned, respan};
|
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;
|
|
|
|
|
2014-07-09 16:48:12 -05:00
|
|
|
/// A folder that strips out items that do not belong in the current
|
|
|
|
/// configuration.
|
2014-12-08 12:28:32 -06:00
|
|
|
struct Context<F> where F: FnMut(&[ast::Attribute]) -> bool {
|
|
|
|
in_cfg: F,
|
2013-02-19 01:40:42 -06:00
|
|
|
}
|
2011-06-30 00:32:08 -05:00
|
|
|
|
|
|
|
// Support conditional compilation by transforming the AST, stripping out
|
|
|
|
// any items that do not belong in the current configuration
|
2015-07-13 19:10:44 -05:00
|
|
|
pub fn strip_unconfigured_items(diagnostic: &SpanHandler, krate: ast::Crate,
|
|
|
|
feature_gated_cfgs: &mut Vec<GatedCfg>)
|
|
|
|
-> ast::Crate
|
|
|
|
{
|
|
|
|
let krate = process_cfg_attr(diagnostic, krate, feature_gated_cfgs);
|
2014-02-05 15:15:24 -06:00
|
|
|
let config = krate.config.clone();
|
2015-07-13 19:10:44 -05:00
|
|
|
strip_items(krate, |attrs| in_cfg(diagnostic, &config, attrs, feature_gated_cfgs))
|
2012-01-05 19:30:00 -06:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
impl<F> fold::Folder for Context<F> where F: FnMut(&[ast::Attribute]) -> bool {
|
2014-09-07 12:09:06 -05:00
|
|
|
fn fold_mod(&mut self, module: ast::Mod) -> ast::Mod {
|
2013-08-29 17:04:09 -05:00
|
|
|
fold_mod(self, module)
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
fn fold_block(&mut self, block: P<ast::Block>) -> P<ast::Block> {
|
2013-08-29 17:04:09 -05:00
|
|
|
fold_block(self, block)
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
|
2014-01-09 07:05:33 -06:00
|
|
|
fold_foreign_mod(self, foreign_mod)
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
fn fold_item_underscore(&mut self, item: ast::Item_) -> ast::Item_ {
|
2013-08-29 17:04:09 -05:00
|
|
|
fold_item_underscore(self, item)
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
|
2014-04-22 23:54:48 -05:00
|
|
|
fold_expr(self, expr)
|
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
|
2014-07-25 14:00:33 -05:00
|
|
|
fold::noop_fold_mac(mac, self)
|
2014-07-09 16:48:12 -05:00
|
|
|
}
|
2014-11-04 16:59:42 -06:00
|
|
|
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
|
|
|
|
fold_item(self, item)
|
|
|
|
}
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
2011-06-30 00:32:08 -05:00
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
pub fn strip_items<F>(krate: ast::Crate, in_cfg: F) -> ast::Crate where
|
|
|
|
F: FnMut(&[ast::Attribute]) -> bool,
|
|
|
|
{
|
2013-12-27 21:34:51 -06:00
|
|
|
let mut ctxt = Context {
|
2013-08-29 14:10:02 -05:00
|
|
|
in_cfg: in_cfg,
|
2013-06-27 19:41:35 -05:00
|
|
|
};
|
2014-02-05 15:15:24 -06:00
|
|
|
ctxt.fold_crate(krate)
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
fn fold_mod<F>(cx: &mut Context<F>,
|
2015-01-13 09:30:17 -06:00
|
|
|
ast::Mod {inner, items}: ast::Mod)
|
|
|
|
-> ast::Mod where
|
2014-12-08 12:28:32 -06:00
|
|
|
F: FnMut(&[ast::Attribute]) -> bool
|
|
|
|
{
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::Mod {
|
2014-09-07 12:09:06 -05:00
|
|
|
inner: inner,
|
2014-11-04 16:59:42 -06:00
|
|
|
items: items.into_iter().flat_map(|a| {
|
|
|
|
cx.fold_item(a).into_iter()
|
|
|
|
}).collect()
|
2013-01-15 18:05:20 -06:00
|
|
|
}
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
fn filter_foreign_item<F>(cx: &mut Context<F>,
|
|
|
|
item: P<ast::ForeignItem>)
|
|
|
|
-> Option<P<ast::ForeignItem>> where
|
|
|
|
F: FnMut(&[ast::Attribute]) -> bool
|
|
|
|
{
|
2014-05-16 12:15:33 -05:00
|
|
|
if foreign_item_in_cfg(cx, &*item) {
|
2013-08-29 17:04:09 -05:00
|
|
|
Some(item)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
fn fold_foreign_mod<F>(cx: &mut Context<F>,
|
2015-01-13 09:30:17 -06:00
|
|
|
ast::ForeignMod {abi, items}: ast::ForeignMod)
|
2014-12-08 12:28:32 -06:00
|
|
|
-> ast::ForeignMod where
|
|
|
|
F: FnMut(&[ast::Attribute]) -> bool
|
|
|
|
{
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ForeignMod {
|
2014-09-07 12:09:06 -05:00
|
|
|
abi: abi,
|
2014-09-14 22:27:36 -05:00
|
|
|
items: items.into_iter()
|
2014-09-07 12:09:06 -05:00
|
|
|
.filter_map(|a| filter_foreign_item(cx, a))
|
|
|
|
.collect()
|
2013-01-15 18:05:20 -06:00
|
|
|
}
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
fn fold_item<F>(cx: &mut Context<F>, item: P<ast::Item>) -> SmallVector<P<ast::Item>> where
|
|
|
|
F: FnMut(&[ast::Attribute]) -> bool
|
|
|
|
{
|
2014-11-04 16:59:42 -06:00
|
|
|
if item_in_cfg(cx, &*item) {
|
|
|
|
SmallVector::one(item.map(|i| cx.fold_item_simple(i)))
|
|
|
|
} else {
|
|
|
|
SmallVector::zero()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
fn fold_item_underscore<F>(cx: &mut Context<F>, item: ast::Item_) -> ast::Item_ where
|
|
|
|
F: FnMut(&[ast::Attribute]) -> bool
|
|
|
|
{
|
2014-09-07 12:09:06 -05:00
|
|
|
let item = match item {
|
2014-12-28 16:33:18 -06:00
|
|
|
ast::ItemImpl(u, o, a, b, c, impl_items) => {
|
2014-09-14 22:27:36 -05:00
|
|
|
let impl_items = impl_items.into_iter()
|
2015-03-10 05:28:44 -05:00
|
|
|
.filter(|ii| (cx.in_cfg)(&ii.attrs))
|
2014-08-04 15:56:56 -05:00
|
|
|
.collect();
|
2014-12-28 16:33:18 -06:00
|
|
|
ast::ItemImpl(u, o, a, b, c, impl_items)
|
2012-11-01 17:48:37 -05:00
|
|
|
}
|
2014-12-24 00:38:10 -06:00
|
|
|
ast::ItemTrait(u, a, b, methods) => {
|
2014-09-14 22:27:36 -05:00
|
|
|
let methods = methods.into_iter()
|
2015-03-10 05:28:44 -05:00
|
|
|
.filter(|ti| (cx.in_cfg)(&ti.attrs))
|
2013-08-29 17:04:09 -05:00
|
|
|
.collect();
|
2014-12-24 00:38:10 -06:00
|
|
|
ast::ItemTrait(u, a, b, methods)
|
2012-11-01 17:48:37 -05:00
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
ast::ItemStruct(def, generics) => {
|
|
|
|
ast::ItemStruct(fold_struct(cx, def), generics)
|
2013-12-20 19:10:59 -06:00
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
ast::ItemEnum(def, generics) => {
|
2014-11-06 11:32:37 -06:00
|
|
|
let variants = def.variants.into_iter().filter_map(|v| {
|
2015-02-01 20:53:25 -06:00
|
|
|
if !(cx.in_cfg)(&v.node.attrs) {
|
2014-02-07 18:56:27 -06:00
|
|
|
None
|
|
|
|
} else {
|
2015-10-07 19:20:57 -05:00
|
|
|
Some(v.map(|Spanned {node: ast::Variant_ {name, attrs, data,
|
2015-09-16 12:01:15 -05:00
|
|
|
disr_expr}, span}| {
|
2014-09-07 12:09:06 -05:00
|
|
|
Spanned {
|
|
|
|
node: ast::Variant_ {
|
|
|
|
name: name,
|
|
|
|
attrs: attrs,
|
2015-10-07 19:20:57 -05:00
|
|
|
data: fold_struct(cx, data),
|
2014-09-07 12:09:06 -05:00
|
|
|
disr_expr: disr_expr,
|
|
|
|
},
|
|
|
|
span: span
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
});
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemEnum(ast::EnumDef {
|
2013-12-20 19:10:59 -06:00
|
|
|
variants: variants.collect(),
|
2014-09-07 12:09:06 -05:00
|
|
|
}, generics)
|
2013-12-20 19:10:59 -06:00
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
item => item,
|
2012-11-01 17:48:37 -05:00
|
|
|
};
|
|
|
|
|
2014-09-07 12:09:06 -05:00
|
|
|
fold::noop_fold_item_underscore(item, cx)
|
2012-11-01 17:48:37 -05:00
|
|
|
}
|
|
|
|
|
2015-10-07 19:20:57 -05:00
|
|
|
fn fold_struct<F>(cx: &mut Context<F>, def: P<ast::VariantData>) -> P<ast::VariantData> where
|
2014-12-08 12:28:32 -06:00
|
|
|
F: FnMut(&[ast::Attribute]) -> bool
|
|
|
|
{
|
2015-10-09 19:28:40 -05:00
|
|
|
def.map(|vdata| {
|
|
|
|
match vdata {
|
|
|
|
ast::VariantData::Struct(fields, id) => {
|
|
|
|
ast::VariantData::Struct(fields.into_iter().filter(|m| {
|
|
|
|
(cx.in_cfg)(&m.node.attrs)
|
|
|
|
}).collect(), id)
|
|
|
|
}
|
|
|
|
ast::VariantData::Tuple(fields, id) => {
|
|
|
|
ast::VariantData::Tuple(fields.into_iter().filter(|m| {
|
|
|
|
(cx.in_cfg)(&m.node.attrs)
|
|
|
|
}).collect(), id)
|
|
|
|
}
|
|
|
|
ast::VariantData::Unit(id) => ast::VariantData::Unit(id)
|
2014-09-07 12:09:06 -05:00
|
|
|
}
|
|
|
|
})
|
2013-12-20 19:10:59 -06:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
fn retain_stmt<F>(cx: &mut Context<F>, stmt: &ast::Stmt) -> bool where
|
|
|
|
F: FnMut(&[ast::Attribute]) -> bool
|
|
|
|
{
|
2012-08-06 14:34:08 -05:00
|
|
|
match stmt.node {
|
2014-09-07 12:09:06 -05:00
|
|
|
ast::StmtDecl(ref decl, _) => {
|
|
|
|
match decl.node {
|
|
|
|
ast::DeclItem(ref item) => {
|
|
|
|
item_in_cfg(cx, &**item)
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
2011-06-30 15:04:02 -05:00
|
|
|
}
|
2014-09-07 12:09:06 -05:00
|
|
|
_ => true
|
2011-06-30 15:04:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
fn fold_block<F>(cx: &mut Context<F>, b: P<ast::Block>) -> P<ast::Block> where
|
|
|
|
F: FnMut(&[ast::Attribute]) -> bool
|
|
|
|
{
|
2015-01-13 09:30:17 -06:00
|
|
|
b.map(|ast::Block {id, stmts, expr, rules, span}| {
|
2014-09-07 12:09:06 -05:00
|
|
|
let resulting_stmts: Vec<P<ast::Stmt>> =
|
2014-09-14 22:27:36 -05:00
|
|
|
stmts.into_iter().filter(|a| retain_stmt(cx, &**a)).collect();
|
|
|
|
let resulting_stmts = resulting_stmts.into_iter()
|
|
|
|
.flat_map(|stmt| cx.fold_stmt(stmt).into_iter())
|
2014-09-07 12:09:06 -05:00
|
|
|
.collect();
|
|
|
|
ast::Block {
|
|
|
|
id: id,
|
|
|
|
stmts: resulting_stmts,
|
|
|
|
expr: expr.map(|x| cx.fold_expr(x)),
|
|
|
|
rules: rules,
|
|
|
|
span: span,
|
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
})
|
2011-06-30 15:04:02 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
fn fold_expr<F>(cx: &mut Context<F>, expr: P<ast::Expr>) -> P<ast::Expr> where
|
|
|
|
F: FnMut(&[ast::Attribute]) -> bool
|
|
|
|
{
|
2014-09-07 12:09:06 -05:00
|
|
|
expr.map(|ast::Expr {id, span, node}| {
|
|
|
|
fold::noop_fold_expr(ast::Expr {
|
|
|
|
id: id,
|
|
|
|
node: match node {
|
2015-09-29 23:18:09 -05:00
|
|
|
ast::ExprMatch(m, arms) => {
|
2014-09-14 22:27:36 -05:00
|
|
|
ast::ExprMatch(m, arms.into_iter()
|
2015-02-01 20:53:25 -06:00
|
|
|
.filter(|a| (cx.in_cfg)(&a.attrs))
|
2015-09-29 23:18:09 -05:00
|
|
|
.collect())
|
2014-09-07 12:09:06 -05:00
|
|
|
}
|
|
|
|
_ => node
|
|
|
|
},
|
|
|
|
span: span
|
|
|
|
}, cx)
|
|
|
|
})
|
2014-04-22 23:54:48 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
fn item_in_cfg<F>(cx: &mut Context<F>, item: &ast::Item) -> bool where
|
|
|
|
F: FnMut(&[ast::Attribute]) -> bool
|
|
|
|
{
|
2015-02-01 20:53:25 -06:00
|
|
|
return (cx.in_cfg)(&item.attrs);
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2014-12-08 12:28:32 -06:00
|
|
|
fn foreign_item_in_cfg<F>(cx: &mut Context<F>, item: &ast::ForeignItem) -> bool where
|
|
|
|
F: FnMut(&[ast::Attribute]) -> bool
|
|
|
|
{
|
2015-02-01 20:53:25 -06:00
|
|
|
return (cx.in_cfg)(&item.attrs);
|
2011-07-05 15:29:21 -05:00
|
|
|
}
|
|
|
|
|
2011-06-30 00:32:08 -05:00
|
|
|
// Determine if an item should be translated in the current crate
|
|
|
|
// configuration based on the item's attributes
|
2015-07-13 19:10:44 -05:00
|
|
|
fn in_cfg(diagnostic: &SpanHandler, cfg: &[P<ast::MetaItem>], attrs: &[ast::Attribute],
|
|
|
|
feature_gated_cfgs: &mut Vec<GatedCfg>) -> bool {
|
2014-10-25 18:43:14 -05:00
|
|
|
attrs.iter().all(|attr| {
|
2014-09-24 22:22:57 -05:00
|
|
|
let mis = match attr.node.value.node {
|
|
|
|
ast::MetaList(_, ref mis) if attr.check_name("cfg") => mis,
|
2014-10-25 18:43:14 -05:00
|
|
|
_ => return true
|
2014-09-24 22:22:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if mis.len() != 1 {
|
2014-10-11 20:05:54 -05:00
|
|
|
diagnostic.span_err(attr.span, "expected 1 cfg-pattern");
|
2014-10-25 18:43:14 -05:00
|
|
|
return true;
|
2014-09-24 22:22:57 -05:00
|
|
|
}
|
|
|
|
|
2015-07-13 19:10:44 -05:00
|
|
|
attr::cfg_matches(diagnostic, cfg, &*mis[0],
|
|
|
|
feature_gated_cfgs)
|
2014-10-25 18:43:14 -05:00
|
|
|
})
|
2011-06-30 00:32:08 -05:00
|
|
|
}
|
2015-02-09 11:29:21 -06:00
|
|
|
|
2015-07-13 19:10:44 -05:00
|
|
|
struct CfgAttrFolder<'a, 'b> {
|
2015-02-09 11:29:21 -06:00
|
|
|
diag: &'a SpanHandler,
|
|
|
|
config: ast::CrateConfig,
|
2015-07-13 19:10:44 -05:00
|
|
|
feature_gated_cfgs: &'b mut Vec<GatedCfg>
|
2015-02-09 11:29:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process `#[cfg_attr]`.
|
2015-07-13 19:10:44 -05:00
|
|
|
fn process_cfg_attr(diagnostic: &SpanHandler, krate: ast::Crate,
|
|
|
|
feature_gated_cfgs: &mut Vec<GatedCfg>) -> ast::Crate {
|
2015-02-09 11:29:21 -06:00
|
|
|
let mut fld = CfgAttrFolder {
|
|
|
|
diag: diagnostic,
|
|
|
|
config: krate.config.clone(),
|
2015-07-13 19:10:44 -05:00
|
|
|
feature_gated_cfgs: feature_gated_cfgs,
|
2015-02-09 11:29:21 -06:00
|
|
|
};
|
|
|
|
fld.fold_crate(krate)
|
|
|
|
}
|
|
|
|
|
2015-07-13 19:10:44 -05:00
|
|
|
impl<'a,'b> fold::Folder for CfgAttrFolder<'a,'b> {
|
2015-02-09 11:29:21 -06:00
|
|
|
fn fold_attribute(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> {
|
|
|
|
if !attr.check_name("cfg_attr") {
|
|
|
|
return fold::noop_fold_attribute(attr, self);
|
|
|
|
}
|
|
|
|
|
2015-04-16 00:12:12 -05:00
|
|
|
let attr_list = match attr.meta_item_list() {
|
|
|
|
Some(attr_list) => attr_list,
|
|
|
|
None => {
|
|
|
|
self.diag.span_err(attr.span, "expected `#[cfg_attr(<cfg pattern>, <attr>)]`");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let (cfg, mi) = match (attr_list.len(), attr_list.get(0), attr_list.get(1)) {
|
|
|
|
(2, Some(cfg), Some(mi)) => (cfg, mi),
|
2015-02-09 11:29:21 -06:00
|
|
|
_ => {
|
|
|
|
self.diag.span_err(attr.span, "expected `#[cfg_attr(<cfg pattern>, <attr>)]`");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-13 19:10:44 -05:00
|
|
|
if attr::cfg_matches(self.diag, &self.config[..], &cfg,
|
|
|
|
self.feature_gated_cfgs) {
|
2015-02-09 11:29:21 -06:00
|
|
|
Some(respan(mi.span, ast::Attribute_ {
|
|
|
|
id: attr::mk_attr_id(),
|
|
|
|
style: attr.node.style,
|
|
|
|
value: mi.clone(),
|
|
|
|
is_sugared_doc: false,
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need the ability to run pre-expansion.
|
|
|
|
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
|
|
|
|
fold::noop_fold_mac(mac, self)
|
|
|
|
}
|
|
|
|
}
|