about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-08-02 03:52:51 -0700
committerbors <bors@rust-lang.org>2013-08-02 03:52:51 -0700
commit2460170e6aea8dd7aa3e316456047baf18f2f680 (patch)
tree6a90b461f7328987caa9bbf3c4b9caa49e092adc /src/libsyntax
parentdbde42e59e6854979085f3b8a949f307b4da8ffa (diff)
parente995d9935b5bccb484db85b75a344258e74f21a5 (diff)
downloadrust-2460170e6aea8dd7aa3e316456047baf18f2f680.tar.gz
rust-2460170e6aea8dd7aa3e316456047baf18f2f680.zip
auto merge of #8188 : huonw/rust/cfg-macro, r=pcwalton
Example:

    if cfg!(test) {
       calculation_to_run_only_when_testing();
    }

Closes #8130.
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ext/base.rs2
-rw-r--r--src/libsyntax/ext/cfg.rs45
-rw-r--r--src/libsyntax/syntax.rs1
3 files changed, 48 insertions, 0 deletions
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs
index 90e1c7db91a..1dda2493da2 100644
--- a/src/libsyntax/ext/base.rs
+++ b/src/libsyntax/ext/base.rs
@@ -199,6 +199,8 @@ pub fn syntax_expander_table() -> SyntaxEnv {
                                 ext::source_util::expand_mod));
     syntax_expanders.insert(intern(&"asm"),
                             builtin_normal_tt(ext::asm::expand_asm));
+    syntax_expanders.insert(intern(&"cfg"),
+                            builtin_normal_tt(ext::cfg::expand_cfg));
     syntax_expanders.insert(
         intern(&"trace_macros"),
         builtin_normal_tt(ext::trace_macros::expand_trace_macros));
diff --git a/src/libsyntax/ext/cfg.rs b/src/libsyntax/ext/cfg.rs
new file mode 100644
index 00000000000..069cac01036
--- /dev/null
+++ b/src/libsyntax/ext/cfg.rs
@@ -0,0 +1,45 @@
+// Copyright 2013 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.
+
+/**
+The compiler code necessary to support the cfg! extension, which
+expands to a literal `true` or `false` based on whether the given cfgs
+match the current compilation environment.
+*/
+
+use ast;
+use codemap::span;
+use ext::base::*;
+use ext::base;
+use ext::build::AstBuilder;
+use attr;
+use attr::*;
+use parse;
+use parse::token;
+use parse::attr::parser_attr;
+
+pub fn expand_cfg(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree]) -> base::MacResult {
+    let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts.to_owned());
+
+    let mut cfgs = ~[];
+    // parse `cfg!(meta_item, meta_item(x,y), meta_item="foo", ...)`
+    while *p.token != token::EOF {
+        cfgs.push(p.parse_meta_item());
+        if p.eat(&token::EOF) { break } // trailing comma is optional,.
+        p.expect(&token::COMMA);
+    }
+
+    // test_cfg searches for meta items looking like `cfg(foo, ...)`
+    let in_cfg = &[cx.meta_list(sp, @"cfg", cfgs)];
+
+    let matches_cfg = attr::test_cfg(cx.cfg(), in_cfg.iter().transform(|&x| x));
+    let e = cx.expr_bool(sp, matches_cfg);
+    MRExpr(e)
+}
diff --git a/src/libsyntax/syntax.rs b/src/libsyntax/syntax.rs
index f39351bf91f..4d604faa6e1 100644
--- a/src/libsyntax/syntax.rs
+++ b/src/libsyntax/syntax.rs
@@ -70,6 +70,7 @@ pub mod ext {
     }
 
 
+    pub mod cfg;
     pub mod fmt;
     pub mod env;
     pub mod bytes;