rustc_hir/attrs/
mod.rs

1//! Data structures for representing parsed attributes in the Rust compiler.
2//! Formerly `rustc_attr_data_structures`.
3//!
4//! For detailed documentation about attribute processing,
5//! see [rustc_attr_parsing](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_attr_parsing/index.html).
6
7pub use data_structures::*;
8pub use encode_cross_crate::EncodeCrossCrate;
9pub use pretty_printing::PrintAttribute;
10
11mod data_structures;
12mod encode_cross_crate;
13mod pretty_printing;
14
15/// Finds attributes in sequences of attributes by pattern matching.
16///
17/// A little like `matches` but for attributes.
18///
19/// ```rust,ignore (illustrative)
20/// // finds the repr attribute
21/// if let Some(r) = find_attr!(attrs, AttributeKind::Repr(r) => r) {
22///
23/// }
24///
25/// // checks if one has matched
26/// if find_attr!(attrs, AttributeKind::Repr(_)) {
27///
28/// }
29/// ```
30///
31/// Often this requires you to first end up with a list of attributes.
32/// A common way to get those is through `tcx.get_all_attrs(did)`
33#[macro_export]
34macro_rules! find_attr {
35    ($attributes_list: expr, $pattern: pat $(if $guard: expr)?) => {{
36        $crate::find_attr!($attributes_list, $pattern $(if $guard)? => ()).is_some()
37    }};
38
39    ($attributes_list: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => {{
40        'done: {
41            for i in $attributes_list {
42                let i: &rustc_hir::Attribute = i;
43                match i {
44                    rustc_hir::Attribute::Parsed($pattern) $(if $guard)? => {
45                        break 'done Some($e);
46                    }
47                    _ => {}
48                }
49            }
50
51            None
52        }
53    }};
54}