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.
11 /// The compiler code necessary to support the cfg! extension, which expands to
12 /// a literal `true` or `false` based on whether the given cfg matches the
13 /// current compilation environment.
16 use syntax::codemap::Span;
17 use syntax::ext::base::*;
18 use syntax::ext::base;
19 use syntax::ext::build::AstBuilder;
21 use syntax::parse::token;
22 use syntax::config::CfgDiagReal;
24 pub fn expand_cfg<'cx>(cx: &mut ExtCtxt,
26 tts: &[ast::TokenTree])
27 -> Box<base::MacResult+'static> {
28 let mut p = cx.new_parser_from_tts(tts);
29 let cfg = panictry!(p.parse_meta_item());
31 if !p.eat(&token::Eof) {
32 cx.span_err(sp, "expected 1 cfg-pattern");
33 return DummyResult::expr(sp);
37 let mut diag = CfgDiagReal {
38 diag: &cx.parse_sess.span_diagnostic,
39 feature_gated_cfgs: cx.feature_gated_cfgs,
41 attr::cfg_matches(&cx.cfg, &cfg, &mut diag)
43 MacEager::expr(cx.expr_bool(sp, matches_cfg))