about summary refs log tree commit diff
path: root/src/libsyntax/config.rs
diff options
context:
space:
mode:
authorKeegan McAllister <kmcallister@mozilla.com>2015-02-09 09:29:21 -0800
committerKeegan McAllister <kmcallister@mozilla.com>2015-02-09 10:12:14 -0800
commit5354317037368124d180acc3f8754bdb2b0387b8 (patch)
treee6513baa81e9707e06814054a44f919de5ec64d0 /src/libsyntax/config.rs
parent0110f5e03c67d7a3590c7c86f50f5546d75f27b1 (diff)
downloadrust-5354317037368124d180acc3f8754bdb2b0387b8.tar.gz
rust-5354317037368124d180acc3f8754bdb2b0387b8.zip
Process cfg_attr right before stripping cfg
Fixes #22070.
Fixes #19372.
Diffstat (limited to 'src/libsyntax/config.rs')
-rw-r--r--src/libsyntax/config.rs49
1 files changed, 48 insertions, 1 deletions
diff --git a/src/libsyntax/config.rs b/src/libsyntax/config.rs
index eb845e463a0..7ca0591be50 100644
--- a/src/libsyntax/config.rs
+++ b/src/libsyntax/config.rs
@@ -12,7 +12,7 @@ use attr::AttrMetaMethods;
 use diagnostic::SpanHandler;
 use fold::Folder;
 use {ast, fold, attr};
-use codemap::Spanned;
+use codemap::{Spanned, respan};
 use ptr::P;
 
 use util::small_vector::SmallVector;
@@ -26,6 +26,7 @@ struct Context<F> where F: FnMut(&[ast::Attribute]) -> bool {
 // 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: &SpanHandler, krate: ast::Crate) -> ast::Crate {
+    let krate = process_cfg_attr(diagnostic, krate);
     let config = krate.config.clone();
     strip_items(krate, |attrs| in_cfg(diagnostic, &config, attrs))
 }
@@ -281,3 +282,49 @@ fn in_cfg(diagnostic: &SpanHandler, cfg: &[P<ast::MetaItem>], attrs: &[ast::Attr
         attr::cfg_matches(diagnostic, cfg, &*mis[0])
     })
 }
+
+struct CfgAttrFolder<'a> {
+    diag: &'a SpanHandler,
+    config: ast::CrateConfig,
+}
+
+// Process `#[cfg_attr]`.
+fn process_cfg_attr(diagnostic: &SpanHandler, krate: ast::Crate) -> ast::Crate {
+    let mut fld = CfgAttrFolder {
+        diag: diagnostic,
+        config: krate.config.clone(),
+    };
+    fld.fold_crate(krate)
+}
+
+impl<'a> fold::Folder for CfgAttrFolder<'a> {
+    fn fold_attribute(&mut self, attr: ast::Attribute) -> Option<ast::Attribute> {
+        if !attr.check_name("cfg_attr") {
+            return fold::noop_fold_attribute(attr, self);
+        }
+
+        let (cfg, mi) = match attr.meta_item_list() {
+            Some([ref cfg, ref mi]) => (cfg, mi),
+            _ => {
+                self.diag.span_err(attr.span, "expected `#[cfg_attr(<cfg pattern>, <attr>)]`");
+                return None;
+            }
+        };
+
+        if attr::cfg_matches(self.diag, &self.config[], &cfg) {
+            Some(respan(mi.span, ast::Attribute_ {
+                id: attr::mk_attr_id(),
+                style: attr.node.style,
+                value: mi.clone(),
+                is_sugared_doc: false,
+            }))
+        } else {
+            None
+        }
+    }
+
+    // Need the ability to run pre-expansion.
+    fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
+        fold::noop_fold_mac(mac, self)
+    }
+}