1 //! The compiler code necessary to support the cfg! extension, which expands to
2 //! a literal `true` or `false` based on whether the given cfg matches the
3 //! current compilation environment.
7 use rustc_ast::tokenstream::TokenStream;
8 use rustc_attr as attr;
9 use rustc_errors::PResult;
10 use rustc_expand::base::{self, *};
17 ) -> Box<dyn base::MacResult + 'static> {
18 let sp = cx.with_def_site_ctxt(sp);
20 match parse_cfg(cx, sp, tts) {
22 let matches_cfg = attr::cfg_matches(
25 cx.current_expansion.lint_node_id,
28 MacEager::expr(cx.expr_bool(sp, matches_cfg))
37 fn parse_cfg<'a>(cx: &mut ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a, ast::MetaItem> {
38 let mut p = cx.new_parser_from_tts(tts);
40 if p.token == token::Eof {
41 let mut err = cx.struct_span_err(sp, "macro requires a cfg-pattern as an argument");
42 err.span_label(sp, "cfg-pattern required");
46 let cfg = p.parse_meta_item()?;
48 let _ = p.eat(&token::Comma);
50 if !p.eat(&token::Eof) {
51 return Err(cx.struct_span_err(sp, "expected 1 cfg-pattern"));