@@ -4,13 +4,14 @@ use crate::intermediate_public_item::IntermediatePublicItem;
44use crate :: nameable_item:: NameableItem ;
55use crate :: path_component:: PathComponent ;
66use crate :: tokens:: Token ;
7- use std:: { cmp:: Ordering , collections:: HashMap , vec } ;
7+ use std:: { borrow :: Cow , cmp:: Ordering , collections:: HashMap } ;
88
99use 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 ,
1415} ;
1516
1617/// A simple macro to write `Token::Whitespace` in less characters.
@@ -41,9 +42,46 @@ impl<'c> RenderingContext<'c> {
4142 let mut tokens = vec ! [ ] ;
4243
4344 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) ) ;
4785 tokens. push ( ws ! ( ) ) ;
4886 }
4987 }
@@ -1023,26 +1061,6 @@ impl<'c> RenderingContext<'c> {
10231061 }
10241062}
10251063
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-
10461064fn pub_ ( ) -> Vec < Token > {
10471065 vec ! [ Token :: qualifier( "pub" ) , ws!( ) ]
10481066}
0 commit comments