]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/cfg.rs
Auto merge of #38861 - est31:master, r=alexcrichton
[rust.git] / src / libsyntax_ext / cfg.rs
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 /// 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.
14
15 use syntax::ext::base::*;
16 use syntax::ext::base;
17 use syntax::ext::build::AstBuilder;
18 use syntax::attr;
19 use syntax::tokenstream;
20 use syntax::parse::token;
21 use syntax_pos::Span;
22
23 pub fn expand_cfg<'cx>(cx: &mut ExtCtxt,
24                        sp: Span,
25                        tts: &[tokenstream::TokenTree])
26                        -> Box<base::MacResult + 'static> {
27     let mut p = cx.new_parser_from_tts(tts);
28     let cfg = panictry!(p.parse_meta_item());
29
30     if !p.eat(&token::Eof) {
31         cx.span_err(sp, "expected 1 cfg-pattern");
32         return DummyResult::expr(sp);
33     }
34
35     let matches_cfg = attr::cfg_matches(&cfg, cx.parse_sess, cx.ecfg.features);
36     MacEager::expr(cx.expr_bool(sp, matches_cfg))
37 }