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, HasAttrs};
13 use feature_gate::GatedCfgAttr;
15 use {ast, fold, attr};
16 use codemap::{Spanned, respan};
20 use util::small_vector::SmallVector;
22 /// A folder that strips out items that do not belong in the current configuration.
23 pub struct StripUnconfigured<'a> {
24 diag: CfgDiagReal<'a, 'a>,
26 config: &'a ast::CrateConfig,
29 impl<'a> StripUnconfigured<'a> {
30 pub fn new(config: &'a ast::CrateConfig,
32 diagnostic: &'a Handler,
33 feature_gated_cfgs: &'a mut Vec<GatedCfgAttr>)
37 should_test: should_test,
38 diag: CfgDiagReal { diag: diagnostic, feature_gated_cfgs: feature_gated_cfgs },
42 fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
43 let node = self.process_cfg_attrs(node);
44 if self.in_cfg(node.attrs()) { Some(node) } else { None }
47 fn process_cfg_attrs<T: HasAttrs>(&mut self, node: T) -> T {
48 node.map_attrs(|attrs| {
49 attrs.into_iter().filter_map(|attr| self.process_cfg_attr(attr)).collect()
53 fn process_cfg_attr(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> {
54 if !attr.check_name("cfg_attr") {
58 let attr_list = match attr.meta_item_list() {
59 Some(attr_list) => attr_list,
61 let msg = "expected `#[cfg_attr(<cfg pattern>, <attr>)]`";
62 self.diag.diag.span_err(attr.span, msg);
66 let (cfg, mi) = match (attr_list.len(), attr_list.get(0), attr_list.get(1)) {
67 (2, Some(cfg), Some(mi)) => (cfg, mi),
69 let msg = "expected `#[cfg_attr(<cfg pattern>, <attr>)]`";
70 self.diag.diag.span_err(attr.span, msg);
75 if attr::cfg_matches(self.config, &cfg, &mut self.diag) {
76 self.process_cfg_attr(respan(mi.span, ast::Attribute_ {
77 id: attr::mk_attr_id(),
78 style: attr.node.style,
80 is_sugared_doc: false,
87 // Determine if a node with the given attributes should be included in this configuation.
88 fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
89 attrs.iter().all(|attr| {
90 // When not compiling with --test we should not compile the #[test] functions
91 if !self.should_test && is_test_or_bench(attr) {
95 let mis = match attr.node.value.node {
96 ast::MetaItemKind::List(_, ref mis) if is_cfg(&attr) => mis,
101 self.diag.emit_error(|diagnostic| {
102 diagnostic.span_err(attr.span, "expected 1 cfg-pattern");
107 attr::cfg_matches(self.config, &mis[0], &mut self.diag)
111 // Visit attributes on expression and statements (but not attributes on items in blocks).
112 fn visit_stmt_or_expr_attrs(&mut self, attrs: &[ast::Attribute]) {
113 // flag the offending attributes
114 for attr in attrs.iter() {
115 self.diag.feature_gated_cfgs.push(GatedCfgAttr::GatedAttr(attr.span));
119 // Visit unremovable (non-optional) expressions -- c.f. `fold_expr` vs `fold_opt_expr`.
120 fn visit_unremovable_expr(&mut self, expr: &ast::Expr) {
121 if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a) || is_test_or_bench(a)) {
122 let msg = "removing an expression is not supported in this position";
123 self.diag.diag.span_err(attr.span, msg);
128 // Support conditional compilation by transforming the AST, stripping out
129 // any items that do not belong in the current configuration
130 pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate, should_test: bool,
131 feature_gated_cfgs: &mut Vec<GatedCfgAttr>)
134 let config = &krate.config.clone();
135 StripUnconfigured::new(config, should_test, diagnostic, feature_gated_cfgs).fold_crate(krate)
138 impl<'a> fold::Folder for StripUnconfigured<'a> {
139 fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
141 abi: foreign_mod.abi,
142 items: foreign_mod.items.into_iter().filter_map(|item| {
143 self.configure(item).map(|item| fold::noop_fold_foreign_item(item, self))
148 fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
149 let fold_struct = |this: &mut Self, vdata| match vdata {
150 ast::VariantData::Struct(fields, id) => {
151 let fields = fields.into_iter().filter_map(|field| this.configure(field));
152 ast::VariantData::Struct(fields.collect(), id)
154 ast::VariantData::Tuple(fields, id) => {
155 let fields = fields.into_iter().filter_map(|field| this.configure(field));
156 ast::VariantData::Tuple(fields.collect(), id)
158 ast::VariantData::Unit(id) => ast::VariantData::Unit(id)
161 let item = match item {
162 ast::ItemKind::Struct(def, generics) => {
163 ast::ItemKind::Struct(fold_struct(self, def), generics)
165 ast::ItemKind::Enum(def, generics) => {
166 let variants = def.variants.into_iter().filter_map(|v| {
167 self.configure(v).map(|v| {
169 node: ast::Variant_ {
172 data: fold_struct(self, v.node.data),
173 disr_expr: v.node.disr_expr,
179 ast::ItemKind::Enum(ast::EnumDef {
180 variants: variants.collect(),
186 fold::noop_fold_item_kind(item, self)
189 fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
190 self.visit_stmt_or_expr_attrs(expr.attrs());
191 // If an expr is valid to cfg away it will have been removed by the
192 // outer stmt or expression folder before descending in here.
193 // Anything else is always required, and thus has to error out
194 // in case of a cfg attr.
196 // NB: This is intentionally not part of the fold_expr() function
197 // in order for fold_opt_expr() to be able to avoid this check
198 self.visit_unremovable_expr(&expr);
199 let expr = self.process_cfg_attrs(expr);
200 fold_expr(self, expr)
203 fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
204 self.configure(expr).map(|expr| fold_expr(self, expr))
207 fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
208 let is_item = match stmt.node {
209 ast::StmtKind::Decl(ref decl, _) => match decl.node {
210 ast::DeclKind::Item(_) => true,
216 // avoid calling `visit_stmt_or_expr_attrs` on items
218 self.visit_stmt_or_expr_attrs(stmt.attrs());
221 self.configure(stmt).map(|stmt| fold::noop_fold_stmt(stmt, self))
222 .unwrap_or(SmallVector::zero())
225 fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
226 fold::noop_fold_mac(mac, self)
229 fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
230 self.configure(item).map(|item| fold::noop_fold_item(item, self))
231 .unwrap_or(SmallVector::zero())
234 fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector<ast::ImplItem> {
235 self.configure(item).map(|item| fold::noop_fold_impl_item(item, self))
236 .unwrap_or(SmallVector::zero())
239 fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector<ast::TraitItem> {
240 self.configure(item).map(|item| fold::noop_fold_trait_item(item, self))
241 .unwrap_or(SmallVector::zero())
244 fn fold_interpolated(&mut self, nt: token::Nonterminal) -> token::Nonterminal {
245 // Don't configure interpolated AST (c.f. #34171).
246 // Interpolated AST will get configured once the surrounding tokens are parsed.
251 fn fold_expr(folder: &mut StripUnconfigured, expr: P<ast::Expr>) -> P<ast::Expr> {
252 expr.map(|ast::Expr {id, span, node, attrs}| {
253 fold::noop_fold_expr(ast::Expr {
256 ast::ExprKind::Match(m, arms) => {
257 ast::ExprKind::Match(m, arms.into_iter()
258 .filter_map(|a| folder.configure(a))
269 fn is_cfg(attr: &ast::Attribute) -> bool {
270 attr.check_name("cfg")
273 fn is_test_or_bench(attr: &ast::Attribute) -> bool {
274 attr.check_name("test") || attr.check_name("bench")
278 fn emit_error<F>(&mut self, f: F) where F: FnMut(&Handler);
279 fn flag_gated<F>(&mut self, f: F) where F: FnMut(&mut Vec<GatedCfgAttr>);
282 pub struct CfgDiagReal<'a, 'b> {
283 pub diag: &'a Handler,
284 pub feature_gated_cfgs: &'b mut Vec<GatedCfgAttr>,
287 impl<'a, 'b> CfgDiag for CfgDiagReal<'a, 'b> {
288 fn emit_error<F>(&mut self, mut f: F) where F: FnMut(&Handler) {
291 fn flag_gated<F>(&mut self, mut f: F) where F: FnMut(&mut Vec<GatedCfgAttr>) {
292 f(self.feature_gated_cfgs)