@@ -4,13 +4,14 @@ use crate::intermediate_public_item::IntermediatePublicItem;
4
4
use crate :: nameable_item:: NameableItem ;
5
5
use crate :: path_component:: PathComponent ;
6
6
use crate :: tokens:: Token ;
7
- use std:: { cmp:: Ordering , collections:: HashMap , vec } ;
7
+ use std:: { borrow :: Cow , cmp:: Ordering , collections:: HashMap } ;
8
8
9
9
use rustdoc_types:: {
10
- Abi , AssocItemConstraint , AssocItemConstraintKind , Constant , Crate , FunctionHeader ,
11
- FunctionPointer , FunctionSignature , GenericArg , GenericArgs , GenericBound , GenericParamDef ,
12
- GenericParamDefKind , Generics , Id , Impl , Item , ItemEnum , MacroKind , Path , PolyTrait ,
13
- StructKind , Term , Trait , TraitBoundModifier , Type , VariantKind , WherePredicate ,
10
+ Abi , AssocItemConstraint , AssocItemConstraintKind , Attribute , AttributeRepr , Constant , Crate ,
11
+ FunctionHeader , FunctionPointer , FunctionSignature , GenericArg , GenericArgs , GenericBound ,
12
+ GenericParamDef , GenericParamDefKind , Generics , Id , Impl , Item , ItemEnum , MacroKind , Path ,
13
+ PolyTrait , ReprKind , StructKind , Term , Trait , TraitBoundModifier , Type , VariantKind ,
14
+ WherePredicate ,
14
15
} ;
15
16
16
17
/// A simple macro to write `Token::Whitespace` in less characters.
@@ -41,9 +42,46 @@ impl<'c> RenderingContext<'c> {
41
42
let mut tokens = vec ! [ ] ;
42
43
43
44
for attr in & item. attrs {
44
- let attr = attr. trim ( ) ;
45
- if attr_relevant_for_public_apis ( attr) {
46
- tokens. push ( Token :: Annotation ( attr. to_string ( ) ) ) ;
45
+ if let Some ( annotation) = match attr {
46
+ Attribute :: ExportName ( name) => Some ( format ! ( "#[export_name = \" {name}\" ]" ) ) ,
47
+ Attribute :: LinkSection ( section) => Some ( format ! ( "#[link_section = \" {section}\" ]" ) ) ,
48
+ Attribute :: NoMangle => Some ( "#[no_mangle]" . to_string ( ) ) ,
49
+ Attribute :: NonExhaustive => Some ( "#[non_exhaustive]" . to_string ( ) ) ,
50
+ Attribute :: Repr ( AttributeRepr {
51
+ kind,
52
+ align,
53
+ packed,
54
+ int,
55
+ } ) => {
56
+ let mut items: Vec < Cow < ' static , str > > = vec ! [ ] ;
57
+ if let Some ( kind) = match kind {
58
+ ReprKind :: Rust => None ,
59
+ ReprKind :: C => Some ( "C" ) ,
60
+ ReprKind :: Transparent => Some ( "transparent" ) ,
61
+ ReprKind :: Simd => Some ( "simd" ) ,
62
+ } {
63
+ items. push ( Cow :: Borrowed ( kind) ) ;
64
+ }
65
+ if let Some ( align) = align {
66
+ items. push ( Cow :: Owned ( format ! ( "align({align})" ) ) )
67
+ }
68
+ if let Some ( packed) = packed {
69
+ items. push ( Cow :: Owned ( format ! ( "packed({packed})" ) ) )
70
+ }
71
+ if let Some ( int) = int {
72
+ items. push ( Cow :: Owned ( int. to_string ( ) ) )
73
+ }
74
+ ( !items. is_empty ( ) ) . then ( || {
75
+ let mut s = String :: new ( ) ;
76
+ s. push_str ( "#[repr(" ) ;
77
+ s. push_str ( & items. join ( ", " ) ) ;
78
+ s. push_str ( ")]" ) ;
79
+ s
80
+ } )
81
+ }
82
+ _ => None ,
83
+ } {
84
+ tokens. push ( Token :: Annotation ( annotation) ) ;
47
85
tokens. push ( ws ! ( ) ) ;
48
86
}
49
87
}
@@ -1023,26 +1061,6 @@ impl<'c> RenderingContext<'c> {
1023
1061
}
1024
1062
}
1025
1063
1026
- /// Our list of allowed attributes comes from
1027
- /// <https://github.com/rust-lang/rust/blob/68d0b29098/src/librustdoc/html/render/mod.rs#L941-L942>
1028
- fn attr_relevant_for_public_apis ( attr : & str ) -> bool {
1029
- let keywords = [
1030
- "export_name" ,
1031
- "link_section" ,
1032
- "no_mangle" ,
1033
- "non_exhaustive" ,
1034
- "repr" ,
1035
- ] ;
1036
-
1037
- for keyword in keywords {
1038
- if attr. to_lowercase ( ) . contains ( keyword) {
1039
- return true ;
1040
- }
1041
- }
1042
-
1043
- false
1044
- }
1045
-
1046
1064
fn pub_ ( ) -> Vec < Token > {
1047
1065
vec ! [ Token :: qualifier( "pub" ) , ws!( ) ]
1048
1066
}
0 commit comments