]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/cfg_attr.rs
complete openbsd support for `std::env`
[rust.git] / src / libsyntax / ext / cfg_attr.rs
1 // Copyright 2014 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 use ast;
12 use attr;
13 use codemap::Span;
14 use ext::base::ExtCtxt;
15 use ext::build::AstBuilder;
16 use ptr::P;
17
18 pub fn expand(cx: &mut ExtCtxt, sp: Span, mi: &ast::MetaItem, it: P<ast::Item>) -> P<ast::Item> {
19     let (cfg, attr) = match mi.node {
20         ast::MetaList(_, ref mis) if mis.len() == 2 => (&mis[0], &mis[1]),
21         _ => {
22             cx.span_err(sp, "expected `#[cfg_attr(<cfg pattern>, <attr>)]`");
23             return it;
24         }
25     };
26
27     let mut out = (*it).clone();
28     if attr::cfg_matches(&cx.parse_sess.span_diagnostic, cx.cfg.as_slice(), &**cfg) {
29         out.attrs.push(cx.attribute(attr.span, attr.clone()));
30     }
31
32     P(out)
33 }
34