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, *};
11 use rustc_macros::Diagnostic;
18 ) -> Box<dyn base::MacResult + 'static> {
19 let sp = cx.with_def_site_ctxt(sp);
21 match parse_cfg(cx, sp, tts) {
23 let matches_cfg = attr::cfg_matches(
26 cx.current_expansion.lint_node_id,
29 MacEager::expr(cx.expr_bool(sp, matches_cfg))
39 #[diag(builtin_macros_requires_cfg_pattern)]
40 struct RequiresCfgPattern {
47 #[diag(builtin_macros_expected_one_cfg_pattern)]
48 struct OneCfgPattern {
53 fn parse_cfg<'a>(cx: &mut ExtCtxt<'a>, span: Span, tts: TokenStream) -> PResult<'a, ast::MetaItem> {
54 let mut p = cx.new_parser_from_tts(tts);
56 if p.token == token::Eof {
57 return Err(cx.create_err(RequiresCfgPattern { span }));
60 let cfg = p.parse_meta_item()?;
62 let _ = p.eat(&token::Comma);
64 if !p.eat(&token::Eof) {
65 return Err(cx.create_err(OneCfgPattern { span }));