Skip to content

Commit 2460170

Browse files
committedAug 2, 2013
auto merge of #8188 : huonw/rust/cfg-macro, r=pcwalton
Example: if cfg!(test) { calculation_to_run_only_when_testing(); } Closes #8130.
2 parents dbde42e + e995d99 commit 2460170

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed
 

Diff for: ‎src/libsyntax/ext/base.rs

+2
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ pub fn syntax_expander_table() -> SyntaxEnv {
199199
ext::source_util::expand_mod));
200200
syntax_expanders.insert(intern(&"asm"),
201201
builtin_normal_tt(ext::asm::expand_asm));
202+
syntax_expanders.insert(intern(&"cfg"),
203+
builtin_normal_tt(ext::cfg::expand_cfg));
202204
syntax_expanders.insert(
203205
intern(&"trace_macros"),
204206
builtin_normal_tt(ext::trace_macros::expand_trace_macros));

Diff for: ‎src/libsyntax/ext/cfg.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
/**
12+
The compiler code necessary to support the cfg! extension, which
13+
expands to a literal `true` or `false` based on whether the given cfgs
14+
match the current compilation environment.
15+
*/
16+
17+
use ast;
18+
use codemap::span;
19+
use ext::base::*;
20+
use ext::base;
21+
use ext::build::AstBuilder;
22+
use attr;
23+
use attr::*;
24+
use parse;
25+
use parse::token;
26+
use parse::attr::parser_attr;
27+
28+
pub fn expand_cfg(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree]) -> base::MacResult {
29+
let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts.to_owned());
30+
31+
let mut cfgs = ~[];
32+
// parse `cfg!(meta_item, meta_item(x,y), meta_item="foo", ...)`
33+
while *p.token != token::EOF {
34+
cfgs.push(p.parse_meta_item());
35+
if p.eat(&token::EOF) { break } // trailing comma is optional,.
36+
p.expect(&token::COMMA);
37+
}
38+
39+
// test_cfg searches for meta items looking like `cfg(foo, ...)`
40+
let in_cfg = &[cx.meta_list(sp, @"cfg", cfgs)];
41+
42+
let matches_cfg = attr::test_cfg(cx.cfg(), in_cfg.iter().transform(|&x| x));
43+
let e = cx.expr_bool(sp, matches_cfg);
44+
MRExpr(e)
45+
}

Diff for: ‎src/libsyntax/syntax.rs

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub mod ext {
7070
}
7171

7272

73+
pub mod cfg;
7374
pub mod fmt;
7475
pub mod env;
7576
pub mod bytes;

Diff for: ‎src/test/run-pass/syntax-extension-cfg.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// xfail-fast compile-flags doesn't work with fast-check
12+
// compile-flags: --cfg foo --cfg bar(baz) --cfg qux="foo"
13+
14+
fn main() {
15+
// check
16+
if ! cfg!(foo) { fail!() }
17+
if cfg!(not(foo)) { fail!() }
18+
19+
if ! cfg!(bar(baz)) { fail!() }
20+
if cfg!(not(bar(baz))) { fail!() }
21+
22+
if ! cfg!(qux="foo") { fail!() }
23+
if cfg!(not(qux="foo")) { fail!() }
24+
25+
if ! cfg!(foo, bar(baz), qux="foo") { fail!() }
26+
if cfg!(not(foo, bar(baz), qux="foo")) { fail!() }
27+
28+
if cfg!(not_a_cfg) { fail!() }
29+
if cfg!(not_a_cfg, foo, bar(baz), qux="foo") { fail!() }
30+
31+
if ! cfg!(not(not_a_cfg)) { fail!() }
32+
if ! cfg!(not(not_a_cfg), foo, bar(baz), qux="foo") { fail!() }
33+
34+
if cfg!(trailing_comma, ) { fail!() }
35+
}

0 commit comments

Comments
 (0)
Please sign in to comment.