3
3
use ir:: context:: BindgenContext ;
4
4
use ir:: layout:: Layout ;
5
5
use quote;
6
+ use proc_macro2;
6
7
use std:: mem;
7
8
8
9
pub mod attributes {
9
10
use quote;
11
+ use proc_macro2;
10
12
11
13
pub fn repr ( which : & str ) -> quote:: Tokens {
12
- let which = quote :: Ident :: new ( which) ;
14
+ let which = proc_macro2 :: Term :: intern ( which) ;
13
15
quote ! {
14
16
#[ repr( #which ) ]
15
17
}
16
18
}
17
19
18
20
pub fn repr_list ( which_ones : & [ & str ] ) -> quote:: Tokens {
19
- let which_ones = which_ones. iter ( ) . cloned ( ) . map ( quote :: Ident :: new ) ;
21
+ let which_ones = which_ones. iter ( ) . cloned ( ) . map ( proc_macro2 :: Term :: intern ) ;
20
22
quote ! {
21
23
#[ repr( #( #which_ones ) , * ) ]
22
24
}
23
25
}
24
26
25
27
pub fn derives ( which_ones : & [ & str ] ) -> quote:: Tokens {
26
- let which_ones = which_ones. iter ( ) . cloned ( ) . map ( quote :: Ident :: new ) ;
28
+ let which_ones = which_ones. iter ( ) . cloned ( ) . map ( proc_macro2 :: Term :: intern ) ;
27
29
quote ! {
28
30
#[ derive( #( #which_ones ) , * ) ]
29
31
}
@@ -39,11 +41,8 @@ pub mod attributes {
39
41
// Doc comments are already preprocessed into nice `///` formats by the
40
42
// time they get here. Just make sure that we have newlines around it so
41
43
// that nothing else gets wrapped into the comment.
42
- let mut tokens = quote ! { } ;
43
- tokens. append ( "\n " ) ;
44
- tokens. append ( comment) ;
45
- tokens. append ( "\n " ) ;
46
- tokens
44
+ let comment = proc_macro2:: Literal :: doccomment ( & comment) ;
45
+ quote ! { #comment}
47
46
}
48
47
49
48
pub fn link_name ( name : & str ) -> quote:: Tokens {
@@ -73,7 +72,7 @@ pub fn blob(layout: Layout) -> quote::Tokens {
73
72
}
74
73
} ;
75
74
76
- let ty_name = quote :: Ident :: new ( ty_name) ;
75
+ let ty_name = proc_macro2 :: Term :: intern ( ty_name) ;
77
76
78
77
let data_len = opaque. array_size ( ) . unwrap_or ( layout. size ) ;
79
78
@@ -103,7 +102,7 @@ pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
103
102
let mut tokens = quote ! { } ;
104
103
105
104
if ctx. options ( ) . enable_cxx_namespaces {
106
- tokens. append ( quote ! { root:: } ) ;
105
+ tokens. append_all ( quote ! { root:: } ) ;
107
106
}
108
107
109
108
let align = match layout. align {
@@ -114,7 +113,7 @@ pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
114
113
} ;
115
114
116
115
let size = layout. size ;
117
- tokens. append ( quote ! {
116
+ tokens. append_all ( quote ! {
118
117
__BindgenBitfieldUnit<[ u8 ; #size] , #align>
119
118
} ) ;
120
119
@@ -126,6 +125,7 @@ pub mod ast_ty {
126
125
use ir:: function:: FunctionSig ;
127
126
use ir:: ty:: FloatKind ;
128
127
use quote;
128
+ use proc_macro2;
129
129
130
130
pub fn raw_type ( ctx : & BindgenContext , name : & str ) -> quote:: Tokens {
131
131
let ident = ctx. rust_ident_raw ( name) ;
@@ -166,29 +166,25 @@ pub mod ast_ty {
166
166
167
167
pub fn int_expr ( val : i64 ) -> quote:: Tokens {
168
168
// Don't use quote! { #val } because that adds the type suffix.
169
- let mut tokens = quote ! { } ;
170
- tokens. append ( val. to_string ( ) ) ;
171
- tokens
169
+ let val = proc_macro2:: Literal :: integer ( val) ;
170
+ quote ! ( #val)
172
171
}
173
172
174
173
pub fn uint_expr ( val : u64 ) -> quote:: Tokens {
175
174
// Don't use quote! { #val } because that adds the type suffix.
176
- let mut tokens = quote ! { } ;
177
- tokens. append ( val. to_string ( ) ) ;
178
- tokens
175
+ let val = proc_macro2:: Term :: intern ( & val. to_string ( ) ) ;
176
+ quote ! ( #val)
179
177
}
180
178
181
179
pub fn byte_array_expr ( bytes : & [ u8 ] ) -> quote:: Tokens {
182
180
let mut bytes: Vec < _ > = bytes. iter ( ) . cloned ( ) . collect ( ) ;
183
181
bytes. push ( 0 ) ;
184
- quote ! {
185
- #bytes
186
- }
182
+ quote ! { [ #( #bytes) , * ] }
187
183
}
188
184
189
185
pub fn cstr_expr ( mut string : String ) -> quote:: Tokens {
190
186
string. push ( '\0' ) ;
191
- let b = quote :: ByteStr ( & string) ;
187
+ let b = proc_macro2 :: Literal :: byte_string ( & string. as_bytes ( ) ) ;
192
188
quote ! {
193
189
#b
194
190
}
@@ -199,16 +195,9 @@ pub mod ast_ty {
199
195
f : f64 ,
200
196
) -> Result < quote:: Tokens , ( ) > {
201
197
if f. is_finite ( ) {
202
- let mut string = f. to_string ( ) ;
203
-
204
- // So it gets properly recognised as a floating point constant.
205
- if !string. contains ( '.' ) {
206
- string. push ( '.' ) ;
207
- }
198
+ let val = proc_macro2:: Literal :: float ( f) ;
208
199
209
- let mut tokens = quote ! { } ;
210
- tokens. append ( string) ;
211
- return Ok ( tokens) ;
200
+ return Ok ( quote ! ( #val) ) ;
212
201
}
213
202
214
203
let prefix = ctx. trait_prefix ( ) ;
0 commit comments