]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/cfg.rs
complete openbsd support for `std::env`
[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 ast;
16 use codemap::Span;
17 use ext::base::*;
18 use ext::base;
19 use ext::build::AstBuilder;
20 use attr;
21 use attr::*;
22 use parse::attr::ParserAttr;
23 use parse::token;
24
25 pub 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);
30     let cfg = p.parse_meta_item();
31
32     if !p.eat(&token::Eof) {
33         cx.span_err(sp, "expected 1 cfg-pattern");
34         return DummyResult::expr(sp);
35     }
36
37     let matches_cfg = attr::cfg_matches(&cx.parse_sess.span_diagnostic, cx.cfg.as_slice(), &*cfg);
38     MacExpr::new(cx.expr_bool(sp, matches_cfg))
39 }