@@ -71,15 +71,109 @@ impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
71
71
pub struct State < ' a > {
72
72
pub s : pp:: Printer ,
73
73
comments : Option < Comments < ' a > > ,
74
- attrs : & ' a dyn Fn ( HirId ) -> & ' a [ ast :: Attribute ] ,
74
+ attrs : & ' a dyn Fn ( HirId ) -> & ' a [ hir :: Attribute ] ,
75
75
ann : & ' a ( dyn PpAnn + ' a ) ,
76
76
}
77
77
78
78
impl < ' a > State < ' a > {
79
- fn attrs ( & self , id : HirId ) -> & ' a [ ast :: Attribute ] {
79
+ fn attrs ( & self , id : HirId ) -> & ' a [ hir :: Attribute ] {
80
80
( self . attrs ) ( id)
81
81
}
82
82
83
+ fn print_inner_attributes ( & mut self , attrs : & [ hir:: Attribute ] ) -> bool {
84
+ self . print_either_attributes ( attrs, ast:: AttrStyle :: Inner , false , true )
85
+ }
86
+
87
+ fn print_outer_attributes ( & mut self , attrs : & [ hir:: Attribute ] ) -> bool {
88
+ self . print_either_attributes ( attrs, ast:: AttrStyle :: Outer , false , true )
89
+ }
90
+
91
+ fn print_either_attributes (
92
+ & mut self ,
93
+ attrs : & [ hir:: Attribute ] ,
94
+ kind : ast:: AttrStyle ,
95
+ is_inline : bool ,
96
+ trailing_hardbreak : bool ,
97
+ ) -> bool {
98
+ let mut printed = false ;
99
+ for attr in attrs {
100
+ if attr. style == kind {
101
+ self . print_attribute_inline ( attr, is_inline) ;
102
+ if is_inline {
103
+ self . nbsp ( ) ;
104
+ }
105
+ printed = true ;
106
+ }
107
+ }
108
+ if printed && trailing_hardbreak && !is_inline {
109
+ self . hardbreak_if_not_bol ( ) ;
110
+ }
111
+ printed
112
+ }
113
+
114
+ fn print_attribute_inline ( & mut self , attr : & hir:: Attribute , is_inline : bool ) {
115
+ if !is_inline {
116
+ self . hardbreak_if_not_bol ( ) ;
117
+ }
118
+ self . maybe_print_comment ( attr. span . lo ( ) ) ;
119
+ match & attr. kind {
120
+ hir:: AttrKind :: Normal ( normal) => {
121
+ match attr. style {
122
+ ast:: AttrStyle :: Inner => self . word ( "#![" ) ,
123
+ ast:: AttrStyle :: Outer => self . word ( "#[" ) ,
124
+ }
125
+ self . print_attr_item ( & normal, attr. span ) ;
126
+ self . word ( "]" ) ;
127
+ }
128
+ hir:: AttrKind :: DocComment ( comment_kind, data) => {
129
+ self . word ( rustc_ast_pretty:: pprust:: state:: doc_comment_to_string (
130
+ * comment_kind,
131
+ attr. style ,
132
+ * data,
133
+ ) ) ;
134
+ self . hardbreak ( )
135
+ }
136
+ }
137
+ }
138
+
139
+ fn print_attr_item ( & mut self , item : & hir:: AttrItem , span : Span ) {
140
+ self . ibox ( 0 ) ;
141
+ let path = Path {
142
+ span,
143
+ segments : item
144
+ . path
145
+ . segments
146
+ . iter ( )
147
+ . map ( |i| PathSegment { ident : * i, id : DUMMY_NODE_ID , args : None } )
148
+ . collect ( ) ,
149
+ tokens : None ,
150
+ } ;
151
+
152
+ match & item. args {
153
+ hir:: AttrArgs :: Delimited ( DelimArgs { dspan : _, delim, tokens } ) => self
154
+ . print_mac_common (
155
+ Some ( MacHeader :: Path ( & path) ) ,
156
+ false ,
157
+ None ,
158
+ * delim,
159
+ tokens,
160
+ true ,
161
+ span,
162
+ ) ,
163
+ hir:: AttrArgs :: Empty => {
164
+ PrintState :: print_path ( self , & path, false , 0 ) ;
165
+ }
166
+ hir:: AttrArgs :: Eq { eq_span : _, value } => {
167
+ PrintState :: print_path ( self , & path, false , 0 ) ;
168
+ self . space ( ) ;
169
+ self . word_space ( "=" ) ;
170
+ let token_str = self . meta_item_lit_to_string ( value) ;
171
+ self . word ( token_str) ;
172
+ }
173
+ }
174
+ self . end ( ) ;
175
+ }
176
+
83
177
fn print_node ( & mut self , node : Node < ' _ > ) {
84
178
match node {
85
179
Node :: Param ( a) => self . print_param ( a) ,
@@ -199,6 +293,10 @@ where
199
293
printer. s . eof ( )
200
294
}
201
295
296
+ pub fn attribute_to_string ( ann : & dyn PpAnn , attr : & hir:: Attribute ) -> String {
297
+ to_string ( ann, |s| s. print_attribute_inline ( attr, false ) )
298
+ }
299
+
202
300
pub fn ty_to_string ( ann : & dyn PpAnn , ty : & hir:: Ty < ' _ > ) -> String {
203
301
to_string ( ann, |s| s. print_type ( ty) )
204
302
}
0 commit comments