2020-01-09 04:18:47 -06:00
|
|
|
//! The compiler code necessary to support the cfg! extension, which expands to
|
|
|
|
//! a literal `true` or `false` based on whether the given cfg matches the
|
|
|
|
//! current compilation environment.
|
2019-02-04 06:49:54 -06:00
|
|
|
|
2020-04-27 12:56:11 -05:00
|
|
|
use rustc_ast as ast;
|
2020-02-29 11:37:32 -06:00
|
|
|
use rustc_ast::token;
|
|
|
|
use rustc_ast::tokenstream::TokenStream;
|
2020-01-11 06:15:20 -06:00
|
|
|
use rustc_attr as attr;
|
2022-01-27 03:44:25 -06:00
|
|
|
use rustc_errors::PResult;
|
2019-12-29 08:23:55 -06:00
|
|
|
use rustc_expand::base::{self, *};
|
2022-09-18 10:46:56 -05:00
|
|
|
use rustc_macros::Diagnostic;
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::Span;
|
2013-08-01 08:03:03 -05:00
|
|
|
|
2019-06-11 05:20:33 -05:00
|
|
|
pub fn expand_cfg(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
2019-08-31 12:08:06 -05:00
|
|
|
tts: TokenStream,
|
2019-06-11 05:20:33 -05:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-09-14 15:17:11 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2018-12-04 13:10:32 -06:00
|
|
|
|
|
|
|
match parse_cfg(cx, sp, tts) {
|
|
|
|
Ok(cfg) => {
|
2022-02-27 15:26:24 -06:00
|
|
|
let matches_cfg = attr::cfg_matches(
|
|
|
|
&cfg,
|
|
|
|
&cx.sess.parse_sess,
|
|
|
|
cx.current_expansion.lint_node_id,
|
|
|
|
cx.ecfg.features,
|
|
|
|
);
|
2018-12-04 13:10:32 -06:00
|
|
|
MacEager::expr(cx.expr_bool(sp, matches_cfg))
|
|
|
|
}
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
2019-08-13 12:51:54 -05:00
|
|
|
DummyResult::any(sp)
|
2018-12-04 13:10:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 10:46:56 -05:00
|
|
|
#[derive(Diagnostic)]
|
2022-10-22 04:07:54 -05:00
|
|
|
#[diag(builtin_macros_requires_cfg_pattern)]
|
2022-06-21 14:10:31 -05:00
|
|
|
struct RequiresCfgPattern {
|
|
|
|
#[primary_span]
|
|
|
|
#[label]
|
|
|
|
span: Span,
|
|
|
|
}
|
|
|
|
|
2022-09-18 10:46:56 -05:00
|
|
|
#[derive(Diagnostic)]
|
2022-10-22 04:07:54 -05:00
|
|
|
#[diag(builtin_macros_expected_one_cfg_pattern)]
|
2022-06-21 14:20:00 -05:00
|
|
|
struct OneCfgPattern {
|
|
|
|
#[primary_span]
|
|
|
|
span: Span,
|
|
|
|
}
|
|
|
|
|
2022-06-21 14:10:31 -05:00
|
|
|
fn parse_cfg<'a>(cx: &mut ExtCtxt<'a>, span: Span, tts: TokenStream) -> PResult<'a, ast::MetaItem> {
|
2014-07-03 04:42:24 -05:00
|
|
|
let mut p = cx.new_parser_from_tts(tts);
|
2018-12-04 13:10:32 -06:00
|
|
|
|
|
|
|
if p.token == token::Eof {
|
2022-06-21 14:10:31 -05:00
|
|
|
return Err(cx.create_err(RequiresCfgPattern { span }));
|
2018-12-04 13:10:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let cfg = p.parse_meta_item()?;
|
2013-08-01 08:03:03 -05:00
|
|
|
|
2018-02-07 08:32:26 -06:00
|
|
|
let _ = p.eat(&token::Comma);
|
|
|
|
|
2015-12-30 17:11:53 -06:00
|
|
|
if !p.eat(&token::Eof) {
|
2022-06-21 14:20:00 -05:00
|
|
|
return Err(cx.create_err(OneCfgPattern { span }));
|
2014-09-24 22:22:57 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 13:10:32 -06:00
|
|
|
Ok(cfg)
|
2013-08-01 08:03:03 -05:00
|
|
|
}
|