|
| 1 | +use either::Either; |
| 2 | +use rustc_ast::tokenstream::{TokenStream, TokenTree}; |
| 3 | +use rustc_ast::{MetaItemInner, token}; |
| 4 | +use rustc_attr_parsing as attr; |
| 5 | +use rustc_errors::PResult; |
| 6 | +use rustc_expand::base::{ExtCtxt, MacroExpanderResult, *}; |
| 7 | +use rustc_parse::exp; |
| 8 | +use rustc_parse::parser::Parser; |
| 9 | +use rustc_span::{Span, kw}; |
| 10 | + |
| 11 | +use crate::errors::{CfgSelectNoMatches, CfgSelectUnreachable}; |
| 12 | + |
| 13 | +#[derive(Default)] |
| 14 | +struct SelectBranches { |
| 15 | + reachable: Vec<(MetaItemInner, TokenStream)>, |
| 16 | + wildcard: Option<(Span, TokenStream)>, |
| 17 | + // Either the span of the `_`, or the rule of the arm. |
| 18 | + unreachable: Vec<(Either<Span, MetaItemInner>, TokenStream)>, |
| 19 | +} |
| 20 | + |
| 21 | +impl SelectBranches { |
| 22 | + /// Selects the first arm whose rule evaluates to true. |
| 23 | + fn select_arm(self, cx: &ExtCtxt<'_>) -> Option<TokenStream> { |
| 24 | + for (cfg, tt) in self.reachable { |
| 25 | + if attr::cfg_matches( |
| 26 | + &cfg, |
| 27 | + &cx.sess, |
| 28 | + cx.current_expansion.lint_node_id, |
| 29 | + Some(cx.ecfg.features), |
| 30 | + ) { |
| 31 | + return Some(tt); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + self.wildcard.map(|(_, tt)| tt) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// Parses a `TokenTree` that must be of the form `{ /* ... */ }`, and returns a `TokenStream` where |
| 40 | +/// the the surrounding braces are stripped. |
| 41 | +fn parse_token_tree<'a>(p: &mut Parser<'a>) -> PResult<'a, TokenStream> { |
| 42 | + // generate an error if the `=>` is not followed by `{` |
| 43 | + if p.token != token::OpenBrace { |
| 44 | + p.expect(exp!(OpenBrace))?; |
| 45 | + } |
| 46 | + |
| 47 | + // Strip the outer '{' and '}' |
| 48 | + match p.parse_token_tree() { |
| 49 | + TokenTree::Token(..) => unreachable!("because of the expect above"), |
| 50 | + TokenTree::Delimited(.., tts) => Ok(tts), |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +fn parse_args<'a>(p: &mut Parser<'a>) -> PResult<'a, SelectBranches> { |
| 55 | + let mut branches = SelectBranches::default(); |
| 56 | + |
| 57 | + while p.token != token::Eof { |
| 58 | + if p.token.is_keyword(kw::Underscore) { |
| 59 | + let span = p.token.span; |
| 60 | + p.bump(); |
| 61 | + p.expect(exp!(FatArrow))?; |
| 62 | + |
| 63 | + let tts = parse_token_tree(p)?; |
| 64 | + |
| 65 | + match branches.wildcard { |
| 66 | + None => branches.wildcard = Some((span, tts)), |
| 67 | + Some(_) => branches.unreachable.push((Either::Left(span), tts)), |
| 68 | + } |
| 69 | + } else { |
| 70 | + let meta_item = p.parse_meta_item_inner()?; |
| 71 | + p.expect(exp!(FatArrow))?; |
| 72 | + |
| 73 | + let tts = parse_token_tree(p)?; |
| 74 | + |
| 75 | + match branches.wildcard { |
| 76 | + None => branches.reachable.push((meta_item, tts)), |
| 77 | + Some(_) => branches.unreachable.push((Either::Right(meta_item), tts)), |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + Ok(branches) |
| 83 | +} |
| 84 | + |
| 85 | +pub(super) fn expand_cfg_select<'cx>( |
| 86 | + ecx: &'cx mut ExtCtxt<'_>, |
| 87 | + sp: Span, |
| 88 | + tts: TokenStream, |
| 89 | +) -> MacroExpanderResult<'cx> { |
| 90 | + ExpandResult::Ready(match parse_args(&mut ecx.new_parser_from_tts(tts)) { |
| 91 | + Ok(branches) => { |
| 92 | + if let Some((wildcard_span, _)) = branches.wildcard { |
| 93 | + // Warn for every unreachable rule. |
| 94 | + for (rule, _) in &branches.unreachable { |
| 95 | + let span = match rule { |
| 96 | + Either::Left(wildcard_span) => *wildcard_span, |
| 97 | + Either::Right(rule) => rule.span(), |
| 98 | + }; |
| 99 | + let err = CfgSelectUnreachable { span, wildcard_span }; |
| 100 | + ecx.dcx().emit_warn(err); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if let Some(tts) = branches.select_arm(ecx) { |
| 105 | + rustc_expand::expand_token_stream(ecx, sp, tts) |
| 106 | + } else { |
| 107 | + // Emit a compiler error when none of the rules matched. |
| 108 | + let guar = ecx.dcx().emit_err(CfgSelectNoMatches { span: sp }); |
| 109 | + DummyResult::any(sp, guar) |
| 110 | + } |
| 111 | + } |
| 112 | + Err(err) => { |
| 113 | + let guar = err.emit(); |
| 114 | + DummyResult::any(sp, guar) |
| 115 | + } |
| 116 | + }) |
| 117 | +} |
0 commit comments