|
1 | 1 | //! Functions dealing with attributes and meta items. |
2 | 2 |
|
3 | | -use std::fmt::Debug; |
| 3 | +use std::fmt::{self, Debug, Display}; |
4 | 4 | use std::sync::atomic::{AtomicU32, Ordering}; |
5 | 5 |
|
6 | 6 | use rustc_index::bit_set::GrowableBitSet; |
@@ -720,6 +720,31 @@ impl MetaItemLit { |
720 | 720 | } |
721 | 721 | } |
722 | 722 |
|
| 723 | +/// An attribute relevant to the expansion of the proc macro harness performed |
| 724 | +/// by `rustc_builtin_macros`. |
| 725 | +#[derive(PartialEq, Debug)] |
| 726 | +pub enum ProcMacroAttr { |
| 727 | + /// `#[proc_macro_derive]` |
| 728 | + Derive, |
| 729 | + /// `#[proc_macro_attribute]` |
| 730 | + Attribute, |
| 731 | + /// `#[proc_macro]` |
| 732 | + Bang, |
| 733 | + /// `#[proc_macro_lint]` |
| 734 | + Lint, |
| 735 | +} |
| 736 | + |
| 737 | +impl Display for ProcMacroAttr { |
| 738 | + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 739 | + formatter.write_str(match self { |
| 740 | + ProcMacroAttr::Derive => "proc_macro_derive", |
| 741 | + ProcMacroAttr::Attribute => "proc_macro_attribute", |
| 742 | + ProcMacroAttr::Bang => "proc_macro", |
| 743 | + ProcMacroAttr::Lint => "proc_macro_lint", |
| 744 | + }) |
| 745 | + } |
| 746 | +} |
| 747 | + |
723 | 748 | pub trait AttributeExt: Debug { |
724 | 749 | fn id(&self) -> AttrId; |
725 | 750 |
|
@@ -776,10 +801,18 @@ pub trait AttributeExt: Debug { |
776 | 801 | /// * `#[doc(...)]` returns `None`. |
777 | 802 | fn doc_str(&self) -> Option<Symbol>; |
778 | 803 |
|
779 | | - fn is_proc_macro_attr(&self) -> bool { |
780 | | - [sym::proc_macro, sym::proc_macro_attribute, sym::proc_macro_derive] |
781 | | - .iter() |
782 | | - .any(|kind| self.has_name(*kind)) |
| 804 | + fn proc_macro_attr(&self) -> Option<ProcMacroAttr> { |
| 805 | + if self.has_name(sym::proc_macro_derive) { |
| 806 | + Some(ProcMacroAttr::Derive) |
| 807 | + } else if self.has_name(sym::proc_macro_attribute) { |
| 808 | + Some(ProcMacroAttr::Attribute) |
| 809 | + } else if self.has_name(sym::proc_macro) { |
| 810 | + Some(ProcMacroAttr::Bang) |
| 811 | + } else if self.has_name(sym::proc_macro_lint) { |
| 812 | + Some(ProcMacroAttr::Lint) |
| 813 | + } else { |
| 814 | + None |
| 815 | + } |
783 | 816 | } |
784 | 817 |
|
785 | 818 | /// Returns the documentation and its kind if this is a doc comment or a sugared doc comment. |
@@ -852,8 +885,8 @@ impl Attribute { |
852 | 885 | AttributeExt::doc_str(self) |
853 | 886 | } |
854 | 887 |
|
855 | | - pub fn is_proc_macro_attr(&self) -> bool { |
856 | | - AttributeExt::is_proc_macro_attr(self) |
| 888 | + pub fn proc_macro_attr(&self) -> Option<ProcMacroAttr> { |
| 889 | + AttributeExt::proc_macro_attr(self) |
857 | 890 | } |
858 | 891 |
|
859 | 892 | pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> { |
|
0 commit comments