1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use attr::AttrMetaMethods;
12 use diagnostic::SpanHandler;
14 use {ast, fold, attr};
18 /// A folder that strips out items that do not belong in the current
21 in_cfg: |attrs: &[ast::Attribute]|: 'a -> bool,
24 // Support conditional compilation by transforming the AST, stripping out
25 // any items that do not belong in the current configuration
26 pub fn strip_unconfigured_items(diagnostic: &SpanHandler, krate: ast::Crate) -> ast::Crate {
27 let config = krate.config.clone();
28 strip_items(krate, |attrs| in_cfg(diagnostic, config.as_slice(), attrs))
31 impl<'a> fold::Folder for Context<'a> {
32 fn fold_mod(&mut self, module: ast::Mod) -> ast::Mod {
33 fold_mod(self, module)
35 fn fold_block(&mut self, block: P<ast::Block>) -> P<ast::Block> {
36 fold_block(self, block)
38 fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
39 fold_foreign_mod(self, foreign_mod)
41 fn fold_item_underscore(&mut self, item: ast::Item_) -> ast::Item_ {
42 fold_item_underscore(self, item)
44 fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
47 fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
48 fold::noop_fold_mac(mac, self)
52 pub fn strip_items(krate: ast::Crate,
53 in_cfg: |attrs: &[ast::Attribute]| -> bool)
55 let mut ctxt = Context {
58 ctxt.fold_crate(krate)
61 fn filter_view_item(cx: &mut Context, view_item: ast::ViewItem) -> Option<ast::ViewItem> {
62 if view_item_in_cfg(cx, &view_item) {
69 fn fold_mod(cx: &mut Context, ast::Mod {inner, view_items, items}: ast::Mod) -> ast::Mod {
72 view_items: view_items.into_iter().filter_map(|a| {
73 filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
75 items: items.into_iter().filter_map(|a| {
76 if item_in_cfg(cx, &*a) {
81 }).flat_map(|x| x.into_iter()).collect()
85 fn filter_foreign_item(cx: &mut Context, item: P<ast::ForeignItem>)
86 -> Option<P<ast::ForeignItem>> {
87 if foreign_item_in_cfg(cx, &*item) {
94 fn fold_foreign_mod(cx: &mut Context, ast::ForeignMod {abi, view_items, items}: ast::ForeignMod)
98 view_items: view_items.into_iter().filter_map(|a| {
99 filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
101 items: items.into_iter()
102 .filter_map(|a| filter_foreign_item(cx, a))
107 fn fold_item_underscore(cx: &mut Context, item: ast::Item_) -> ast::Item_ {
108 let item = match item {
109 ast::ItemImpl(a, b, c, impl_items) => {
110 let impl_items = impl_items.into_iter()
111 .filter(|ii| impl_item_in_cfg(cx, ii))
113 ast::ItemImpl(a, b, c, impl_items)
115 ast::ItemTrait(a, b, c, methods) => {
116 let methods = methods.into_iter()
117 .filter(|m| trait_method_in_cfg(cx, m))
119 ast::ItemTrait(a, b, c, methods)
121 ast::ItemStruct(def, generics) => {
122 ast::ItemStruct(fold_struct(cx, def), generics)
124 ast::ItemEnum(def, generics) => {
125 let mut variants = def.variants.into_iter().filter_map(|v| {
126 if !(cx.in_cfg)(v.node.attrs.as_slice()) {
129 Some(v.map(|Spanned {node: ast::Variant_ {id, name, attrs, kind,
130 disr_expr, vis}, span}| {
132 node: ast::Variant_ {
137 ast::TupleVariantKind(..) => kind,
138 ast::StructVariantKind(def) => {
139 ast::StructVariantKind(fold_struct(cx, def))
142 disr_expr: disr_expr,
150 ast::ItemEnum(ast::EnumDef {
151 variants: variants.collect(),
157 fold::noop_fold_item_underscore(item, cx)
160 fn fold_struct(cx: &mut Context, def: P<ast::StructDef>) -> P<ast::StructDef> {
161 def.map(|ast::StructDef {fields, ctor_id, super_struct, is_virtual}| {
163 fields: fields.into_iter().filter(|m| {
164 (cx.in_cfg)(m.node.attrs.as_slice())
167 super_struct: super_struct,
168 is_virtual: is_virtual,
173 fn retain_stmt(cx: &mut Context, stmt: &ast::Stmt) -> bool {
175 ast::StmtDecl(ref decl, _) => {
177 ast::DeclItem(ref item) => {
178 item_in_cfg(cx, &**item)
187 fn fold_block(cx: &mut Context, b: P<ast::Block>) -> P<ast::Block> {
188 b.map(|ast::Block {id, view_items, stmts, expr, rules, span}| {
189 let resulting_stmts: Vec<P<ast::Stmt>> =
190 stmts.into_iter().filter(|a| retain_stmt(cx, &**a)).collect();
191 let resulting_stmts = resulting_stmts.into_iter()
192 .flat_map(|stmt| cx.fold_stmt(stmt).into_iter())
194 let filtered_view_items = view_items.into_iter().filter_map(|a| {
195 filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
199 view_items: filtered_view_items,
200 stmts: resulting_stmts,
201 expr: expr.map(|x| cx.fold_expr(x)),
208 fn fold_expr(cx: &mut Context, expr: P<ast::Expr>) -> P<ast::Expr> {
209 expr.map(|ast::Expr {id, span, node}| {
210 fold::noop_fold_expr(ast::Expr {
213 ast::ExprMatch(m, arms, source) => {
214 ast::ExprMatch(m, arms.into_iter()
215 .filter(|a| (cx.in_cfg)(a.attrs.as_slice()))
225 fn item_in_cfg(cx: &mut Context, item: &ast::Item) -> bool {
226 return (cx.in_cfg)(item.attrs.as_slice());
229 fn foreign_item_in_cfg(cx: &mut Context, item: &ast::ForeignItem) -> bool {
230 return (cx.in_cfg)(item.attrs.as_slice());
233 fn view_item_in_cfg(cx: &mut Context, item: &ast::ViewItem) -> bool {
234 return (cx.in_cfg)(item.attrs.as_slice());
237 fn trait_method_in_cfg(cx: &mut Context, meth: &ast::TraitItem) -> bool {
239 ast::RequiredMethod(ref meth) => (cx.in_cfg)(meth.attrs.as_slice()),
240 ast::ProvidedMethod(ref meth) => (cx.in_cfg)(meth.attrs.as_slice()),
241 ast::TypeTraitItem(ref typ) => (cx.in_cfg)(typ.attrs.as_slice()),
245 fn impl_item_in_cfg(cx: &mut Context, impl_item: &ast::ImplItem) -> bool {
247 ast::MethodImplItem(ref meth) => (cx.in_cfg)(meth.attrs.as_slice()),
248 ast::TypeImplItem(ref typ) => (cx.in_cfg)(typ.attrs.as_slice()),
252 // Determine if an item should be translated in the current crate
253 // configuration based on the item's attributes
254 fn in_cfg(diagnostic: &SpanHandler, cfg: &[P<ast::MetaItem>], attrs: &[ast::Attribute]) -> bool {
255 let mut in_cfg = false;
256 let mut seen_cfg = false;
257 for attr in attrs.iter() {
258 let mis = match attr.node.value.node {
259 ast::MetaList(_, ref mis) if attr.check_name("cfg") => mis,
264 diagnostic.span_warn(attr.span, "The use of multiple cfgs in the top level of \
265 `#[cfg(..)]` is deprecated. Change `#[cfg(a, b)]` to \
266 `#[cfg(all(a, b))]`.");
270 diagnostic.span_warn(attr.span, "The semantics of multiple `#[cfg(..)]` attributes on \
271 same item are changing from the union of the cfgs to \
272 the intersection of the cfgs. Change `#[cfg(a)] \
273 #[cfg(b)]` to `#[cfg(any(a, b))]`.");
277 in_cfg |= mis.iter().all(|mi| attr::cfg_matches(diagnostic, cfg, &**mi));