rustc: Process #[cfg]/#[cfg_attr] on crates

This commit implements processing these two attributes at the crate level as
well as at the item level. When #[cfg] is applied at the crate level, then the
entire crate will be omitted if the cfg doesn't match. The #[cfg_attr] attribute
is processed as usual in that the attribute is included or not depending on
whether the cfg matches.

This was spurred on by motivations of #18585 where #[cfg_attr] annotations will
be applied at the crate-level.

cc #18585
This commit is contained in:
Alex Crichton
2014-11-04 14:59:42 -08:00
parent 45cbdec417
commit 3dbd32854f
11 changed files with 127 additions and 23 deletions

View File

@@ -1057,12 +1057,41 @@ pub fn noop_fold_mod<T: Folder>(Mod {inner, view_items, items}: Mod, folder: &mu
pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, config, exported_macros, span}: Crate,
folder: &mut T) -> Crate {
let config = folder.fold_meta_items(config);
let mut items = folder.fold_item(P(ast::Item {
ident: token::special_idents::invalid,
attrs: attrs,
id: ast::DUMMY_NODE_ID,
vis: ast::Public,
span: span,
node: ast::ItemMod(module),
})).into_iter();
let (module, attrs, span) = match items.next() {
Some(item) => {
assert!(items.next().is_none(),
"a crate cannot expand to more than one item");
item.and_then(|ast::Item { attrs, span, node, .. }| {
match node {
ast::ItemMod(m) => (m, attrs, span),
_ => panic!("fold converted a module to not a module"),
}
})
}
None => (ast::Mod {
inner: span,
view_items: Vec::new(),
items: Vec::new(),
}, Vec::new(), span)
};
Crate {
module: folder.fold_mod(module),
attrs: attrs.move_map(|x| folder.fold_attribute(x)),
config: folder.fold_meta_items(config),
module: module,
attrs: attrs,
config: config,
exported_macros: exported_macros,
span: folder.new_span(span)
span: span,
}
}