1use std::convert::identity;
2
3use rustc_ast as ast;
4use rustc_ast::token::DocFragmentKind;
5use rustc_ast::{AttrItemKind, AttrStyle, NodeId, Safety};
6use rustc_data_structures::fx::FxIndexSet;
7use rustc_errors::DiagCtxtHandle;
8use rustc_feature::{AttributeTemplate, Features};
9use rustc_hir::attrs::AttributeKind;
10use rustc_hir::lints::AttributeLintKind;
11use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, Target};
12use rustc_session::Session;
13use rustc_session::lint::LintId;
14use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
15
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 AttributeParser<'sess, S: Stage = Late> {
25 pub(crate) tools: Option<&'sess FxIndexSet<Ident>>,
26 pub(crate) features: Option<&'sess Features>,
27 pub(crate) sess: &'sess Session,
28 pub(crate) stage: S,
29
30 parse_only: Option<Symbol>,
34}
35
36impl<'sess> AttributeParser<'sess, Early> {
37 pub fn parse_limited(
54 sess: &'sess Session,
55 attrs: &[ast::Attribute],
56 sym: Symbol,
57 target_span: Span,
58 target_node_id: NodeId,
59 features: Option<&'sess Features>,
60 ) -> Option<Attribute> {
61 Self::parse_limited_should_emit(
62 sess,
63 attrs,
64 sym,
65 target_span,
66 target_node_id,
67 Target::Crate, features,
69 ShouldEmit::Nothing,
70 )
71 }
72
73 pub fn parse_limited_should_emit(
78 sess: &'sess Session,
79 attrs: &[ast::Attribute],
80 sym: Symbol,
81 target_span: Span,
82 target_node_id: NodeId,
83 target: Target,
84 features: Option<&'sess Features>,
85 should_emit: ShouldEmit,
86 ) -> Option<Attribute> {
87 let mut parsed = Self::parse_limited_all(
88 sess,
89 attrs,
90 Some(sym),
91 target,
92 target_span,
93 target_node_id,
94 features,
95 should_emit,
96 None,
97 );
98 if !(parsed.len() <= 1) {
::core::panicking::panic("assertion failed: parsed.len() <= 1")
};assert!(parsed.len() <= 1);
99 parsed.pop()
100 }
101
102 pub fn parse_limited_all<'a>(
110 sess: &'sess Session,
111 attrs: impl IntoIterator<Item = &'a ast::Attribute>,
112 parse_only: Option<Symbol>,
113 target: Target,
114 target_span: Span,
115 target_node_id: NodeId,
116 features: Option<&'sess Features>,
117 emit_errors: ShouldEmit,
118 tools: Option<&'sess FxIndexSet<Ident>>,
119 ) -> Vec<Attribute> {
120 let mut p = Self { features, tools, parse_only, sess, stage: Early { emit_errors } };
121 p.parse_attribute_list(
122 attrs,
123 target_span,
124 target,
125 OmitDoc::Skip,
126 std::convert::identity,
127 |lint_id, span, kind| sess.psess.buffer_lint(lint_id.lint, span, target_node_id, kind),
128 )
129 }
130
131 pub fn parse_limited_all_filtered<'a>(
134 sess: &'sess Session,
135 attrs: impl IntoIterator<Item = &'a ast::Attribute>,
136 filter: &[Symbol],
137 target: Target,
138 target_span: Span,
139 target_node_id: NodeId,
140 features: Option<&'sess Features>,
141 emit_errors: ShouldEmit,
142 tools: &'sess FxIndexSet<Ident>,
143 ) -> Vec<Attribute> {
144 Self::parse_limited_all(
145 sess,
146 attrs.into_iter().filter(|attr| attr.has_any_name(filter)),
147 None,
148 target,
149 target_span,
150 target_node_id,
151 features,
152 emit_errors,
153 Some(tools),
154 )
155 }
156
157 pub fn parse_single<T>(
160 sess: &'sess Session,
161 attr: &ast::Attribute,
162 target_span: Span,
163 target_node_id: NodeId,
164 target: Target,
165 features: Option<&'sess Features>,
166 emit_errors: ShouldEmit,
167 parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &ArgParser) -> Option<T>,
168 template: &AttributeTemplate,
169 allow_expr_metavar: AllowExprMetavar,
170 ) -> Option<T> {
171 let ast::AttrKind::Normal(normal_attr) = &attr.kind else {
172 {
::core::panicking::panic_fmt(format_args!("parse_single called on a doc attr"));
}panic!("parse_single called on a doc attr")
173 };
174 let parts =
175 normal_attr.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
176
177 let path = AttrPath::from_ast(&normal_attr.item.path, identity);
178 let args = ArgParser::from_attr_args(
179 &normal_attr.item.args.unparsed_ref().unwrap(),
180 &parts,
181 &sess.psess,
182 emit_errors,
183 allow_expr_metavar,
184 )?;
185 Self::parse_single_args(
186 sess,
187 attr.span,
188 normal_attr.item.span(),
189 attr.style,
190 path,
191 Some(normal_attr.item.unsafety),
192 ParsedDescription::Attribute,
193 target_span,
194 target_node_id,
195 target,
196 features,
197 emit_errors,
198 &args,
199 parse_fn,
200 template,
201 )
202 }
203
204 pub fn parse_single_args<T, I>(
207 sess: &'sess Session,
208 attr_span: Span,
209 inner_span: Span,
210 attr_style: AttrStyle,
211 attr_path: AttrPath,
212 attr_safety: Option<Safety>,
213 parsed_description: ParsedDescription,
214 target_span: Span,
215 target_node_id: NodeId,
216 target: Target,
217 features: Option<&'sess Features>,
218 emit_errors: ShouldEmit,
219 args: &I,
220 parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &I) -> T,
221 template: &AttributeTemplate,
222 ) -> T {
223 let mut parser =
224 Self { features, tools: None, parse_only: None, sess, stage: Early { emit_errors } };
225 let mut emit_lint = |lint_id: LintId, span: Span, kind: AttributeLintKind| {
226 sess.psess.buffer_lint(lint_id.lint, span, target_node_id, kind)
227 };
228 if let Some(safety) = attr_safety {
229 parser.check_attribute_safety(&attr_path, inner_span, safety, &mut emit_lint)
230 }
231 let attr_id = sess.psess.attr_id_generator.mk_attr_id();
232 let mut cx: AcceptContext<'_, 'sess, Early> = AcceptContext {
233 shared: SharedContext {
234 cx: &mut parser,
235 target_span,
236 target,
237 emit_lint: &mut emit_lint,
238 },
239 attr_span,
240 inner_span,
241 attr_style,
242 parsed_description,
243 template,
244 attr_path,
245 attr_id,
246 };
247 parse_fn(&mut cx, args)
248 }
249}
250
251impl<'sess, S: Stage> AttributeParser<'sess, S> {
252 pub fn new(
253 sess: &'sess Session,
254 features: &'sess Features,
255 tools: &'sess FxIndexSet<Ident>,
256 stage: S,
257 ) -> Self {
258 Self { features: Some(features), tools: Some(tools), parse_only: None, sess, stage }
259 }
260
261 pub(crate) fn sess(&self) -> &'sess Session {
262 &self.sess
263 }
264
265 pub(crate) fn features(&self) -> &'sess Features {
266 self.features.expect("features not available at this point in the compiler")
267 }
268
269 pub(crate) fn features_option(&self) -> Option<&'sess Features> {
270 self.features
271 }
272
273 pub(crate) fn dcx(&self) -> DiagCtxtHandle<'sess> {
274 self.sess().dcx()
275 }
276
277 pub fn parse_attribute_list<'a>(
282 &mut self,
283 attrs: impl IntoIterator<Item = &'a ast::Attribute>,
284 target_span: Span,
285 target: Target,
286 omit_doc: OmitDoc,
287 lower_span: impl Copy + Fn(Span) -> Span,
288 mut emit_lint: impl FnMut(LintId, Span, AttributeLintKind),
289 ) -> Vec<Attribute> {
290 let mut attributes = Vec::new();
291 let mut dropped_attributes = Vec::new();
296 let mut attr_paths: Vec<RefPathParser<'_>> = Vec::new();
297 let mut early_parsed_state = EarlyParsedState::default();
298
299 let mut finalizers: Vec<&FinalizeFn<S>> = Vec::new();
300
301 for attr in attrs.into_iter() {
302 if let Some(expected) = self.parse_only {
304 if !attr.has_name(expected) {
305 continue;
306 }
307 }
308
309 let is_doc_attribute = attr.has_name(sym::doc);
315 if omit_doc == OmitDoc::Skip && is_doc_attribute {
316 continue;
317 }
318
319 let attr_span = lower_span(attr.span);
320 match &attr.kind {
321 ast::AttrKind::DocComment(comment_kind, symbol) => {
322 if omit_doc == OmitDoc::Skip {
323 continue;
324 }
325
326 attributes.push(Attribute::Parsed(AttributeKind::DocComment {
327 style: attr.style,
328 kind: DocFragmentKind::Sugared(*comment_kind),
329 span: attr_span,
330 comment: *symbol,
331 }));
332 }
333 ast::AttrKind::Normal(n) => {
334 attr_paths.push(PathParser(&n.item.path));
335 let attr_path = AttrPath::from_ast(&n.item.path, lower_span);
336
337 let args = match &n.item.args {
338 AttrItemKind::Unparsed(args) => args,
339 AttrItemKind::Parsed(parsed) => {
340 early_parsed_state
341 .accept_early_parsed_attribute(attr_span, lower_span, parsed);
342 continue;
343 }
344 };
345
346 self.check_attribute_safety(
347 &attr_path,
348 lower_span(n.item.span()),
349 n.item.unsafety,
350 &mut emit_lint,
351 );
352
353 let parts =
354 n.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
355
356 if let Some(accept) = S::parsers().accepters.get(parts.as_slice()) {
357 let Some(args) = ArgParser::from_attr_args(
358 args,
359 &parts,
360 &self.sess.psess,
361 self.stage.should_emit(),
362 AllowExprMetavar::No,
363 ) else {
364 continue;
365 };
366
367 if is_doc_attribute
383 && let ArgParser::NameValue(nv) = &args
384 && let Some(comment) = nv.value_as_str()
388 {
389 attributes.push(Attribute::Parsed(AttributeKind::DocComment {
390 style: attr.style,
391 kind: DocFragmentKind::Raw(nv.value_span),
392 span: attr_span,
393 comment,
394 }));
395 continue;
396 }
397
398 let mut cx: AcceptContext<'_, 'sess, S> = AcceptContext {
399 shared: SharedContext {
400 cx: self,
401 target_span,
402 target,
403 emit_lint: &mut emit_lint,
404 },
405 attr_span,
406 inner_span: lower_span(n.item.span()),
407 attr_style: attr.style,
408 parsed_description: ParsedDescription::Attribute,
409 template: &accept.template,
410 attr_path: attr_path.clone(),
411 attr_id: attr.id,
412 };
413
414 (accept.accept_fn)(&mut cx, &args);
415 finalizers.push(&accept.finalizer);
416
417 if !#[allow(non_exhaustive_omitted_patterns)] match cx.stage.should_emit() {
ShouldEmit::Nothing => true,
_ => false,
}matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
418 Self::check_target(&accept.allowed_targets, target, &mut cx);
419 }
420 } else {
421 let attr = AttrItem {
422 path: attr_path.clone(),
423 args: self
424 .lower_attr_args(n.item.args.unparsed_ref().unwrap(), lower_span),
425 id: HashIgnoredAttrId { attr_id: attr.id },
426 style: attr.style,
427 span: attr_span,
428 };
429
430 if !#[allow(non_exhaustive_omitted_patterns)] match self.stage.should_emit() {
ShouldEmit::Nothing => true,
_ => false,
}matches!(self.stage.should_emit(), ShouldEmit::Nothing)
431 && target == Target::Crate
432 {
433 self.check_invalid_crate_level_attr_item(&attr, n.item.span());
434 }
435
436 let attr = Attribute::Unparsed(Box::new(attr));
437
438 if self
439 .tools
440 .is_some_and(|tools| tools.iter().any(|tool| tool.name == 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}