@@ -4,26 +4,28 @@ use ir::context::BindgenContext;
4
4
use ir:: layout:: Layout ;
5
5
use quote;
6
6
use std:: mem;
7
+ use proc_macro2:: { Term , Span } ;
7
8
8
9
pub mod attributes {
9
10
use quote;
11
+ use proc_macro2:: { Term , Span } ;
10
12
11
13
pub fn repr ( which : & str ) -> quote:: Tokens {
12
- let which = quote :: Ident :: new ( which) ;
14
+ let which = Term :: new ( which, Span :: call_site ( ) ) ;
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 ( |one| Term :: new ( one , Span :: call_site ( ) ) ) ;
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 ( |one| Term :: new ( one , Span :: call_site ( ) ) ) ;
27
29
quote ! {
28
30
#[ derive( #( #which_ones ) , * ) ]
29
31
}
@@ -40,9 +42,9 @@ pub mod attributes {
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
44
let mut tokens = quote ! { } ;
43
- tokens. append ( "\n " ) ;
44
- tokens. append ( comment) ;
45
- tokens. append ( "\n " ) ;
45
+ tokens. append ( Term :: new ( "\n " , Span :: call_site ( ) ) ) ;
46
+ tokens. append ( Term :: new ( & comment, Span :: call_site ( ) ) ) ;
47
+ tokens. append ( Term :: new ( "\n " , Span :: call_site ( ) ) ) ;
46
48
tokens
47
49
}
48
50
@@ -73,7 +75,7 @@ pub fn blob(layout: Layout) -> quote::Tokens {
73
75
}
74
76
} ;
75
77
76
- let ty_name = quote :: Ident :: new ( ty_name) ;
78
+ let ty_name = Term :: new ( ty_name, Span :: call_site ( ) ) ;
77
79
78
80
let data_len = opaque. array_size ( ) . unwrap_or ( layout. size ) ;
79
81
@@ -103,7 +105,7 @@ pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
103
105
let mut tokens = quote ! { } ;
104
106
105
107
if ctx. options ( ) . enable_cxx_namespaces {
106
- tokens. append ( quote ! { root:: } ) ;
108
+ tokens. append_all ( quote ! { root:: } ) ;
107
109
}
108
110
109
111
let align = match layout. align {
@@ -114,7 +116,7 @@ pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
114
116
} ;
115
117
116
118
let size = layout. size ;
117
- tokens. append ( quote ! {
119
+ tokens. append_all ( quote ! {
118
120
__BindgenBitfieldUnit<[ u8 ; #size] , #align>
119
121
} ) ;
120
122
@@ -126,6 +128,7 @@ pub mod ast_ty {
126
128
use ir:: function:: FunctionSig ;
127
129
use ir:: ty:: FloatKind ;
128
130
use quote;
131
+ use proc_macro2;
129
132
130
133
pub fn raw_type ( ctx : & BindgenContext , name : & str ) -> quote:: Tokens {
131
134
let ident = ctx. rust_ident_raw ( name) ;
@@ -166,29 +169,25 @@ pub mod ast_ty {
166
169
167
170
pub fn int_expr ( val : i64 ) -> quote:: Tokens {
168
171
// Don't use quote! { #val } because that adds the type suffix.
169
- let mut tokens = quote ! { } ;
170
- tokens. append ( val. to_string ( ) ) ;
171
- tokens
172
+ let val = proc_macro2:: Literal :: i64_unsuffixed ( val) ;
173
+ quote ! ( #val)
172
174
}
173
175
174
176
pub fn uint_expr ( val : u64 ) -> quote:: Tokens {
175
177
// Don't use quote! { #val } because that adds the type suffix.
176
- let mut tokens = quote ! { } ;
177
- tokens. append ( val. to_string ( ) ) ;
178
- tokens
178
+ let val = proc_macro2:: Literal :: u64_unsuffixed ( val) ;
179
+ quote ! ( #val)
179
180
}
180
181
181
182
pub fn byte_array_expr ( bytes : & [ u8 ] ) -> quote:: Tokens {
182
183
let mut bytes: Vec < _ > = bytes. iter ( ) . cloned ( ) . collect ( ) ;
183
184
bytes. push ( 0 ) ;
184
- quote ! {
185
- #bytes
186
- }
185
+ quote ! { [ #( #bytes) , * ] }
187
186
}
188
187
189
188
pub fn cstr_expr ( mut string : String ) -> quote:: Tokens {
190
189
string. push ( '\0' ) ;
191
- let b = quote :: ByteStr ( & string) ;
190
+ let b = proc_macro2 :: Literal :: byte_string ( & string. as_bytes ( ) ) ;
192
191
quote ! {
193
192
#b
194
193
}
@@ -199,16 +198,9 @@ pub mod ast_ty {
199
198
f : f64 ,
200
199
) -> Result < quote:: Tokens , ( ) > {
201
200
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
- }
201
+ let val = proc_macro2:: Literal :: f64_unsuffixed ( f) ;
208
202
209
- let mut tokens = quote ! { } ;
210
- tokens. append ( string) ;
211
- return Ok ( tokens) ;
203
+ return Ok ( quote ! ( #val) ) ;
212
204
}
213
205
214
206
let prefix = ctx. trait_prefix ( ) ;
0 commit comments