1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// 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.
use attr::{AttrMetaMethods, HasAttrs};
use errors::Handler;
use feature_gate::GatedCfgAttr;
use fold::Folder;
use {ast, fold, attr};
use codemap::{Spanned, respan};
use ptr::P;
use util::small_vector::SmallVector;
pub trait CfgFolder: fold::Folder {
// Check if a node with the given attributes is in this configuration.
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool;
// Update a node before checking if it is in this configuration (used to implement `cfg_attr`).
fn process_attrs<T: HasAttrs>(&mut self, node: T) -> T { node }
// Visit attributes on expression and statements (but not attributes on items in blocks).
fn visit_stmt_or_expr_attrs(&mut self, _attrs: &[ast::Attribute]) {}
// Visit unremovable (non-optional) expressions -- c.f. `fold_expr` vs `fold_opt_expr`.
fn visit_unremovable_expr(&mut self, _expr: &ast::Expr) {}
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
let node = self.process_attrs(node);
if self.in_cfg(node.attrs()) { Some(node) } else { None }
}
}
/// A folder that strips out items that do not belong in the current
/// configuration.
pub struct StripUnconfigured<'a> {
diag: CfgDiagReal<'a, 'a>,
config: &'a ast::CrateConfig,
}
impl<'a> StripUnconfigured<'a> {
pub fn new(config: &'a ast::CrateConfig,
diagnostic: &'a Handler,
feature_gated_cfgs: &'a mut Vec<GatedCfgAttr>)
-> Self {
StripUnconfigured {
config: config,
diag: CfgDiagReal { diag: diagnostic, feature_gated_cfgs: feature_gated_cfgs },
}
}
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>)]`";
self.diag.diag.span_err(attr.span, msg);
return None;
}
};
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>)]`";
self.diag.diag.span_err(attr.span, msg);
return None;
}
};
if attr::cfg_matches(self.config, &cfg, &mut self.diag) {
Some(respan(mi.span, ast::Attribute_ {
id: attr::mk_attr_id(),
style: attr.node.style,
value: mi.clone(),
is_sugared_doc: false,
}))
} else {
None
}
}
}
impl<'a> CfgFolder for StripUnconfigured<'a> {
// Determine if an item should be translated in the current crate
// configuration based on the item's attributes
fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
attrs.iter().all(|attr| {
let mis = match attr.node.value.node {
ast::MetaItemKind::List(_, ref mis) if is_cfg(&attr) => mis,
_ => return true
};
if mis.len() != 1 {
self.diag.emit_error(|diagnostic| {
diagnostic.span_err(attr.span, "expected 1 cfg-pattern");
});
return true;
}
attr::cfg_matches(self.config, &mis[0], &mut self.diag)
})
}
fn process_attrs<T: HasAttrs>(&mut self, node: T) -> T {
node.map_attrs(|attrs| {
attrs.into_iter().filter_map(|attr| self.process_cfg_attr(attr)).collect()
})
}
fn visit_stmt_or_expr_attrs(&mut self, attrs: &[ast::Attribute]) {
// flag the offending attributes
for attr in attrs.iter() {
self.diag.feature_gated_cfgs.push(GatedCfgAttr::GatedAttr(attr.span));
}
}
fn visit_unremovable_expr(&mut self, expr: &ast::Expr) {
if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
let msg = "removing an expression is not supported in this position";
self.diag.diag.span_err(attr.span, msg);
}
}
}
// Support conditional compilation by transforming the AST, stripping out
// any items that do not belong in the current configuration
pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate,
feature_gated_cfgs: &mut Vec<GatedCfgAttr>)
-> ast::Crate
{
let config = &krate.config.clone();
StripUnconfigured::new(config, diagnostic, feature_gated_cfgs).fold_crate(krate)
}
impl<T: CfgFolder> fold::Folder for T {
fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
ast::ForeignMod {
abi: foreign_mod.abi,
items: foreign_mod.items.into_iter().filter_map(|item| {
self.configure(item).map(|item| fold::noop_fold_foreign_item(item, self))
}).collect(),
}
}
fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
let fold_struct = |this: &mut Self, vdata| match vdata {
ast::VariantData::Struct(fields, id) => {
let fields = fields.into_iter().filter_map(|field| this.configure(field));
ast::VariantData::Struct(fields.collect(), id)
}
ast::VariantData::Tuple(fields, id) => {
let fields = fields.into_iter().filter_map(|field| this.configure(field));
ast::VariantData::Tuple(fields.collect(), id)
}
ast::VariantData::Unit(id) => ast::VariantData::Unit(id)
};
let item = match item {
ast::ItemKind::Struct(def, generics) => {
ast::ItemKind::Struct(fold_struct(self, def), generics)
}
ast::ItemKind::Enum(def, generics) => {
let variants = def.variants.into_iter().filter_map(|v| {
self.configure(v).map(|v| {
Spanned {
node: ast::Variant_ {
name: v.node.name,
attrs: v.node.attrs,
data: fold_struct(self, v.node.data),
disr_expr: v.node.disr_expr,
},
span: v.span
}
})
});
ast::ItemKind::Enum(ast::EnumDef {
variants: variants.collect(),
}, generics)
}
item => item,
};
fold::noop_fold_item_kind(item, self)
}
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
self.visit_stmt_or_expr_attrs(expr.attrs());
// 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.
//
// NB: This is intentionally not part of the fold_expr() function
// in order for fold_opt_expr() to be able to avoid this check
self.visit_unremovable_expr(&expr);
let expr = self.process_attrs(expr);
fold_expr(self, expr)
}
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
self.configure(expr).map(|expr| fold_expr(self, expr))
}
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
let is_item = match stmt.node {
ast::StmtKind::Decl(ref decl, _) => match decl.node {
ast::DeclKind::Item(_) => true,
_ => false,
},
_ => false,
};
// avoid calling `visit_stmt_or_expr_attrs` on items
if !is_item {
self.visit_stmt_or_expr_attrs(stmt.attrs());
}
self.configure(stmt).map(|stmt| fold::noop_fold_stmt(stmt, self))
.unwrap_or(SmallVector::zero())
}
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(mac, self)
}
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
self.configure(item).map(|item| fold::noop_fold_item(item, self))
.unwrap_or(SmallVector::zero())
}
fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector<ast::ImplItem> {
self.configure(item).map(|item| fold::noop_fold_impl_item(item, self))
.unwrap_or(SmallVector::zero())
}
fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector<ast::TraitItem> {
self.configure(item).map(|item| fold::noop_fold_trait_item(item, self))
.unwrap_or(SmallVector::zero())
}
}
fn fold_expr<F: CfgFolder>(folder: &mut F, expr: P<ast::Expr>) -> P<ast::Expr> {
expr.map(|ast::Expr {id, span, node, attrs}| {
fold::noop_fold_expr(ast::Expr {
id: id,
node: match node {
ast::ExprKind::Match(m, arms) => {
ast::ExprKind::Match(m, arms.into_iter()
.filter_map(|a| folder.configure(a))
.collect())
}
_ => node
},
span: span,
attrs: attrs,
}, folder)
})
}
fn is_cfg(attr: &ast::Attribute) -> bool {
attr.check_name("cfg")
}
pub trait CfgDiag {
fn emit_error<F>(&mut self, f: F) where F: FnMut(&Handler);
fn flag_gated<F>(&mut self, f: F) where F: FnMut(&mut Vec<GatedCfgAttr>);
}
pub struct CfgDiagReal<'a, 'b> {
pub diag: &'a Handler,
pub feature_gated_cfgs: &'b mut Vec<GatedCfgAttr>,
}
impl<'a, 'b> CfgDiag for CfgDiagReal<'a, 'b> {
fn emit_error<F>(&mut self, mut f: F) where F: FnMut(&Handler) {
f(self.diag)
}
fn flag_gated<F>(&mut self, mut f: F) where F: FnMut(&mut Vec<GatedCfgAttr>) {
f(self.feature_gated_cfgs)
}
}
|