summary refs log tree commit diff
path: root/src/libsyntax/fold.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-11-04 14:59:42 -0800
committerAlex Crichton <alex@alexcrichton.com>2014-11-07 12:04:28 -0800
commit3dbd32854f6bdee94c98c5e3e5da58fb79d79fd9 (patch)
treec38be4cf6f5ffdf89a3983635849a90b502d87fb /src/libsyntax/fold.rs
parent45cbdec4174778bf915f17561ef971c068a7fcbc (diff)
downloadrust-3dbd32854f6bdee94c98c5e3e5da58fb79d79fd9.tar.gz
rust-3dbd32854f6bdee94c98c5e3e5da58fb79d79fd9.zip
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
Diffstat (limited to 'src/libsyntax/fold.rs')
-rw-r--r--src/libsyntax/fold.rs37
1 files changed, 33 insertions, 4 deletions
diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs
index cd4a3d10c48..1132ea372b7 100644
--- a/src/libsyntax/fold.rs
+++ b/src/libsyntax/fold.rs
@@ -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,
     }
 }