]> git.proxmox.com Git - rustc.git/blame - src/libsyntax/ext/cfg.rs
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / libsyntax / ext / cfg.rs
CommitLineData
1a4d82fc
JJ
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
15use ast;
16use codemap::Span;
17use ext::base::*;
18use ext::base;
19use ext::build::AstBuilder;
20use attr;
21use attr::*;
1a4d82fc 22use parse::token;
92a42be0 23use config::CfgDiagReal;
1a4d82fc
JJ
24
25pub fn expand_cfg<'cx>(cx: &mut ExtCtxt,
26 sp: Span,
27 tts: &[ast::TokenTree])
28 -> Box<base::MacResult+'static> {
29 let mut p = cx.new_parser_from_tts(tts);
92a42be0 30 let cfg = panictry!(p.parse_meta_item());
1a4d82fc 31
9346a6ac 32 if !panictry!(p.eat(&token::Eof)){
1a4d82fc
JJ
33 cx.span_err(sp, "expected 1 cfg-pattern");
34 return DummyResult::expr(sp);
35 }
36
92a42be0
SL
37 let matches_cfg = {
38 let mut diag = CfgDiagReal {
39 diag: &cx.parse_sess.span_diagnostic,
40 feature_gated_cfgs: cx.feature_gated_cfgs,
41 };
42 attr::cfg_matches(&cx.cfg, &cfg, &mut diag)
43 };
c34b1796 44 MacEager::expr(cx.expr_bool(sp, matches_cfg))
1a4d82fc 45}