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;
28 pub fn expand_cfg<'cx>(cx: &mut ExtCtxt,
30 tts: &[ast::TokenTree])
31 -> Box<base::MacResult+'static> {
32 let mut p = cx.new_parser_from_tts(tts);
33 let mut cfgs = Vec::new();
34 // parse `cfg!(meta_item, meta_item(x,y), meta_item="foo", ...)`
35 while p.token != token::EOF {
36 cfgs.push(p.parse_meta_item());
37 if p.eat(&token::EOF) { break } // trailing comma is optional,.
38 p.expect(&token::COMMA);
41 // NOTE: turn on after snapshot
44 cx.span_warn(sp, "The use of multiple cfgs at the top level of `cfg!` \
45 is deprecated. Change `cfg!(a, b)` to \
50 let matches_cfg = cfgs.iter().all(|cfg| attr::cfg_matches(&cx.parse_sess.span_diagnostic,
51 cx.cfg.as_slice(), &**cfg));
53 MacExpr::new(cx.expr_bool(sp, matches_cfg))