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.
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.
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.
21 use ext::build::AstBuilder;
24 use parse::attr::ParserAttr;
25 use parse::token::InternedString;
30 pub fn expand_cfg(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
31 -> Box<base::MacResult> {
32 let mut p = parse::new_parser_from_tts(cx.parse_sess(),
35 .map(|x| (*x).clone())
38 let mut cfgs = Vec::new();
39 // parse `cfg!(meta_item, meta_item(x,y), meta_item="foo", ...)`
40 while p.token != token::EOF {
41 cfgs.push(p.parse_meta_item());
42 if p.eat(&token::EOF) { break } // trailing comma is optional,.
43 p.expect(&token::COMMA);
46 // test_cfg searches for meta items looking like `cfg(foo, ...)`
47 let in_cfg = &[cx.meta_list(sp, InternedString::new("cfg"), cfgs)];
49 let matches_cfg = attr::test_cfg(cx.cfg().as_slice(),
50 in_cfg.iter().map(|&x| x));
51 let e = cx.expr_bool(sp, matches_cfg);