1use std::convert::identity;
2
3use rustc_ast as ast;
4use rustc_ast::token::DocFragmentKind;
5use rustc_ast::{AttrItemKind, AttrStyle, CRATE_NODE_ID, NodeId, Safety};
6use rustc_data_structures::sync::{DynSend, DynSync};
7use rustc_errors::{Diag, DiagCtxtHandle, Level, MultiSpan};
8use rustc_feature::{AttributeTemplate, Features};
9use rustc_hir::attrs::AttributeKind;
10use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, Target};
11use rustc_session::Session;
12use rustc_session::lint::LintId;
13use rustc_span::{DUMMY_SP, Span, Symbol, sym};
14
15use crate::attributes::AttributeSafety;
16use crate::context::{AcceptContext, FinalizeContext, FinalizeFn, SharedContext, Stage};
17use crate::early_parsed::{EARLY_PARSED_ATTRIBUTES, EarlyParsedState};
18use crate::parser::{AllowExprMetavar, ArgParser, PathParser, RefPathParser};
19use crate::session_diagnostics::ParsedDescription;
20use crate::{Early, Late, OmitDoc, ShouldEmit};
21
22pub struct EmitAttribute(
23 pub Box<
24 dyn for<'a> Fn(DiagCtxtHandle<'a>, Level, &Session) -> Diag<'a, ()>
25 + DynSend
26 + DynSync
27 + 'static,
28 >,
29);
30
31pub struct AttributeParser<'sess, S: Stage = Late> {
34 pub(crate) tools: Vec<Symbol>,
35 pub(crate) features: Option<&'sess Features>,
36 pub(crate) sess: &'sess Session,
37 pub(crate) stage: S,
38
39 parse_only: Option<&'static [Symbol]>,
43}
44
45impl<'sess> AttributeParser<'sess, Early> {
46 pub fn parse_limited(
61 sess: &'sess Session,
62 attrs: &[ast::Attribute],
63 sym: &'static [Symbol],
64 ) -> Option<Attribute> {
65 Self::parse_limited_should_emit(
66 sess,
67 attrs,
68 sym,
69 DUMMY_SP,
71 CRATE_NODE_ID,
72 Target::Crate,
73 None,
74 ShouldEmit::Nothing,
75 )
76 }
77
78 pub fn parse_limited_should_emit(
81 sess: &'sess Session,
82 attrs: &[ast::Attribute],
83 sym: &'static [Symbol],
84 target_span: Span,
85 target_node_id: NodeId,
86 target: Target,
87 features: Option<&'sess Features>,
88 should_emit: ShouldEmit,
89 ) -> Option<Attribute> {
90 let mut parsed = Self::parse_limited_all(
91 sess,
92 attrs,
93 Some(sym),
94 target,
95 target_span,
96 target_node_id,
97 features,
98 should_emit,
99 );
100 if !(parsed.len() <= 1) {
::core::panicking::panic("assertion failed: parsed.len() <= 1")
};assert!(parsed.len() <= 1);
101 parsed.pop()
102 }
103
104 pub fn parse_limited_all(
112 sess: &'sess Session,
113 attrs: &[ast::Attribute],
114 parse_only: Option<&'static [Symbol]>,
115 target: Target,
116 target_span: Span,
117 target_node_id: NodeId,
118 features: Option<&'sess Features>,
119 emit_errors: ShouldEmit,
120 ) -> Vec<Attribute> {
121 let mut p =
122 Self { features, tools: Vec::new(), parse_only, sess, stage: Early { emit_errors } };
123 p.parse_attribute_list(
124 attrs,
125 target_span,
126 target,
127 OmitDoc::Skip,
128 std::convert::identity,
129 |lint_id, span, kind| {
130 sess.psess.dyn_buffer_lint_sess(lint_id.lint, span, target_node_id, kind.0)
131 },
132 )
133 }
134
135 pub fn parse_single<T>(
138 sess: &'sess Session,
139 attr: &ast::Attribute,
140 target_span: Span,
141 target_node_id: NodeId,
142 target: Target,
143 features: Option<&'sess Features>,
144 emit_errors: ShouldEmit,
145 parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &ArgParser) -> Option<T>,
146 template: &AttributeTemplate,
147 allow_expr_metavar: AllowExprMetavar,
148 expected_safety: AttributeSafety,
149 ) -> Option<T> {
150 let ast::AttrKind::Normal(normal_attr) = &attr.kind else {
151 {
::core::panicking::panic_fmt(format_args!("parse_single called on a doc attr"));
}panic!("parse_single called on a doc attr")
152 };
153 let parts =
154 normal_attr.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
155
156 let path = AttrPath::from_ast(&normal_attr.item.path, identity);
157 let args = ArgParser::from_attr_args(
158 &normal_attr.item.args.unparsed_ref().unwrap(),
159 &parts,
160 &sess.psess,
161 emit_errors,
162 allow_expr_metavar,
163 )?;
164 Self::parse_single_args(
165 sess,
166 attr.span,
167 normal_attr.item.span(),
168 attr.style,
169 path,
170 Some(normal_attr.item.unsafety),
171 expected_safety,
172 ParsedDescription::Attribute,
173 target_span,
174 target_node_id,
175 target,
176 features,
177 emit_errors,
178 &args,
179 parse_fn,
180 template,
181 )
182 }
183
184 pub fn parse_single_args<T, I>(
187 sess: &'sess Session,
188 attr_span: Span,
189 inner_span: Span,
190 attr_style: AttrStyle,
191 attr_path: AttrPath,
192 attr_safety: Option<Safety>,
193 expected_safety: AttributeSafety,
194 parsed_description: ParsedDescription,
195 target_span: Span,
196 target_node_id: NodeId,
197 target: Target,
198 features: Option<&'sess Features>,
199 emit_errors: ShouldEmit,
200 args: &I,
201 parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &I) -> T,
202 template: &AttributeTemplate,
203 ) -> T {
204 let mut parser = Self {
205 features,
206 tools: Vec::new(),
207 parse_only: None,
208 sess,
209 stage: Early { emit_errors },
210 };
211 let mut emit_lint = |lint_id: LintId, span: MultiSpan, kind: EmitAttribute| {
212 sess.psess.dyn_buffer_lint_sess(lint_id.lint, span, target_node_id, kind.0)
213 };
214 if let Some(safety) = attr_safety {
215 parser.check_attribute_safety(
216 &attr_path,
217 inner_span,
218 safety,
219 expected_safety,
220 &mut emit_lint,
221 );
222 }
223 let mut cx: AcceptContext<'_, 'sess, Early> = AcceptContext {
224 shared: SharedContext {
225 cx: &mut parser,
226 target_span,
227 target,
228 emit_lint: &mut emit_lint,
229 },
230 attr_span,
231 inner_span,
232 attr_style,
233 parsed_description,
234 template,
235 attr_path,
236 };
237 parse_fn(&mut cx, args)
238 }
239}
240
241impl<'sess, S: Stage> AttributeParser<'sess, S> {
242 pub fn new(
243 sess: &'sess Session,
244 features: &'sess Features,
245 tools: Vec<Symbol>,
246 stage: S,
247 ) -> Self {
248 Self { features: Some(features), tools, parse_only: None, sess, stage }
249 }
250
251 pub(crate) fn sess(&self) -> &'sess Session {
252 &self.sess
253 }
254
255 pub(crate) fn features(&self) -> &'sess Features {
256 self.features.expect("features not available at this point in the compiler")
257 }
258
259 pub(crate) fn features_option(&self) -> Option<&'sess Features> {
260 self.features
261 }
262
263 pub(crate) fn dcx(&self) -> DiagCtxtHandle<'sess> {
264 self.sess().dcx()
265 }
266
267 pub fn parse_attribute_list(
272 &mut self,
273 attrs: &[ast::Attribute],
274 target_span: Span,
275 target: Target,
276 omit_doc: OmitDoc,
277 lower_span: impl Copy + Fn(Span) -> Span,
278 mut emit_lint: impl FnMut(LintId, MultiSpan, EmitAttribute),
279 ) -> Vec<Attribute> {
280 let mut attributes = Vec::new();
281 let mut dropped_attributes = Vec::new();
286 let mut attr_paths: Vec<RefPathParser<'_>> = Vec::new();
287 let mut early_parsed_state = EarlyParsedState::default();
288
289 let mut finalizers: Vec<FinalizeFn<S>> = Vec::with_capacity(attrs.len());
290
291 for attr in attrs {
292 if let Some(expected) = self.parse_only {
294 if !attr.path_matches(expected) {
295 continue;
296 }
297 }
298
299 let is_doc_attribute = attr.has_name(sym::doc);
305 if omit_doc == OmitDoc::Skip && is_doc_attribute {
306 continue;
307 }
308
309 let attr_span = lower_span(attr.span);
310 match &attr.kind {
311 ast::AttrKind::DocComment(comment_kind, symbol) => {
312 if omit_doc == OmitDoc::Skip {
313 continue;
314 }
315
316 attributes.push(Attribute::Parsed(AttributeKind::DocComment {
317 style: attr.style,
318 kind: DocFragmentKind::Sugared(*comment_kind),
319 span: attr_span,
320 comment: *symbol,
321 }));
322 }
323 ast::AttrKind::Normal(n) => {
324 attr_paths.push(PathParser(&n.item.path));
325 let attr_path = AttrPath::from_ast(&n.item.path, lower_span);
326
327 let args = match &n.item.args {
328 AttrItemKind::Unparsed(args) => args,
329 AttrItemKind::Parsed(parsed) => {
330 early_parsed_state
331 .accept_early_parsed_attribute(attr_span, lower_span, parsed);
332 continue;
333 }
334 };
335
336 let parts =
337 n.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
338
339 if let Some(accept) = S::parsers().accepters.get(parts.as_slice()) {
340 self.check_attribute_safety(
341 &attr_path,
342 lower_span(n.item.span()),
343 n.item.unsafety,
344 accept.safety,
345 &mut emit_lint,
346 );
347
348 let Some(args) = ArgParser::from_attr_args(
349 args,
350 &parts,
351 &self.sess.psess,
352 self.stage.should_emit(),
353 AllowExprMetavar::No,
354 ) else {
355 continue;
356 };
357
358 if is_doc_attribute
374 && let ArgParser::NameValue(nv) = &args
375 && let Some(comment) = nv.value_as_str()
379 {
380 attributes.push(Attribute::Parsed(AttributeKind::DocComment {
381 style: attr.style,
382 kind: DocFragmentKind::Raw(nv.value_span),
383 span: attr_span,
384 comment,
385 }));
386 continue;
387 }
388
389 let mut cx: AcceptContext<'_, 'sess, S> = AcceptContext {
390 shared: SharedContext {
391 cx: self,
392 target_span,
393 target,
394 emit_lint: &mut emit_lint,
395 },
396 attr_span,
397 inner_span: lower_span(n.item.span()),
398 attr_style: attr.style,
399 parsed_description: ParsedDescription::Attribute,
400 template: &accept.template,
401 attr_path: attr_path.clone(),
402 };
403
404 (accept.accept_fn)(&mut cx, &args);
405 finalizers.push(accept.finalizer);
406
407 if !#[allow(non_exhaustive_omitted_patterns)] match cx.stage.should_emit() {
ShouldEmit::Nothing => true,
_ => false,
}matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
408 Self::check_target(&accept.allowed_targets, target, &mut cx);
409 }
410 } else {
411 let attr = AttrItem {
412 path: attr_path.clone(),
413 args: self
414 .lower_attr_args(n.item.args.unparsed_ref().unwrap(), lower_span),
415 id: HashIgnoredAttrId { attr_id: attr.id },
416 style: attr.style,
417 span: attr_span,
418 };
419
420 self.check_attribute_safety(
421 &attr_path,
422 lower_span(n.item.span()),
423 n.item.unsafety,
424 AttributeSafety::Normal,
425 &mut emit_lint,
426 );
427
428 if !#[allow(non_exhaustive_omitted_patterns)] match self.stage.should_emit() {
ShouldEmit::Nothing => true,
_ => false,
}matches!(self.stage.should_emit(), ShouldEmit::Nothing)
429 && target == Target::Crate
430 {
431 self.check_invalid_crate_level_attr_item(&attr, n.item.span());
432 }
433
434 let attr = Attribute::Unparsed(Box::new(attr));
435
436 if self.tools.contains(&parts[0])
437 || [sym::allow, sym::deny, sym::expect, sym::forbid, sym::warn]
440 .contains(&parts[0])
441 {
442 attributes.push(attr);
443 } else {
444 dropped_attributes.push(attr);
445 }
446 }
447 }
448 }
449 }
450
451 early_parsed_state.finalize_early_parsed_attributes(&mut attributes);
452 for f in &finalizers {
453 if let Some(attr) = f(&mut FinalizeContext {
454 shared: SharedContext { cx: self, target_span, target, emit_lint: &mut emit_lint },
455 all_attrs: &attr_paths,
456 }) {
457 attributes.push(Attribute::Parsed(attr));
458 }
459 }
460
461 if !#[allow(non_exhaustive_omitted_patterns)] match self.stage.should_emit() {
ShouldEmit::Nothing => true,
_ => false,
}matches!(self.stage.should_emit(), ShouldEmit::Nothing)
462 && target == Target::WherePredicate
463 {
464 self.check_invalid_where_predicate_attrs(attributes.iter().chain(&dropped_attributes));
465 }
466
467 attributes
468 }
469
470 pub fn is_parsed_attribute(path: &[Symbol]) -> bool {
472 const SPECIAL_ATTRIBUTES: &[&[Symbol]] = &[
475 &[sym::cfg],
477 &[sym::cfg_attr],
478 ];
479
480 Late::parsers().accepters.contains_key(path)
481 || EARLY_PARSED_ATTRIBUTES.contains(&path)
482 || SPECIAL_ATTRIBUTES.contains(&path)
483 }
484
485 fn lower_attr_args(&self, args: &ast::AttrArgs, lower_span: impl Fn(Span) -> Span) -> AttrArgs {
486 match args {
487 ast::AttrArgs::Empty => AttrArgs::Empty,
488 ast::AttrArgs::Delimited(args) => AttrArgs::Delimited(args.clone()),
489 ast::AttrArgs::Eq { eq_span, expr } => {
493 let lit = if let ast::ExprKind::Lit(token_lit) = expr.kind
496 && let Ok(lit) =
497 ast::MetaItemLit::from_token_lit(token_lit, lower_span(expr.span))
498 {
499 lit
500 } else {
501 let guar = self.dcx().span_delayed_bug(
502 args.span().unwrap_or(DUMMY_SP),
503 "expr in place where literal is expected (builtin attr parsing)",
504 );
505 ast::MetaItemLit {
506 symbol: sym::dummy,
507 suffix: None,
508 kind: ast::LitKind::Err(guar),
509 span: DUMMY_SP,
510 }
511 };
512 AttrArgs::Eq { eq_span: lower_span(*eq_span), expr: lit }
513 }
514 }
515 }
516}