@@ -4,26 +4,28 @@ use ir::context::BindgenContext;
44use ir:: layout:: Layout ;
55use quote;
66use std:: mem;
7+ use proc_macro2:: { Term , Span } ;
78
89pub mod attributes {
910 use quote;
11+ use proc_macro2:: { Term , Span } ;
1012
1113 pub fn repr ( which : & str ) -> quote:: Tokens {
12- let which = quote :: Ident :: new ( which) ;
14+ let which = Term :: new ( which, Span :: call_site ( ) ) ;
1315 quote ! {
1416 #[ repr( #which ) ]
1517 }
1618 }
1719
1820 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 ( ) ) ) ;
2022 quote ! {
2123 #[ repr( #( #which_ones ) , * ) ]
2224 }
2325 }
2426
2527 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 ( ) ) ) ;
2729 quote ! {
2830 #[ derive( #( #which_ones ) , * ) ]
2931 }
@@ -40,9 +42,9 @@ pub mod attributes {
4042 // time they get here. Just make sure that we have newlines around it so
4143 // that nothing else gets wrapped into the comment.
4244 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 ( ) ) ) ;
4648 tokens
4749 }
4850
@@ -73,7 +75,7 @@ pub fn blob(layout: Layout) -> quote::Tokens {
7375 }
7476 } ;
7577
76- let ty_name = quote :: Ident :: new ( ty_name) ;
78+ let ty_name = Term :: new ( ty_name, Span :: call_site ( ) ) ;
7779
7880 let data_len = opaque. array_size ( ) . unwrap_or ( layout. size ) ;
7981
@@ -103,7 +105,7 @@ pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
103105 let mut tokens = quote ! { } ;
104106
105107 if ctx. options ( ) . enable_cxx_namespaces {
106- tokens. append ( quote ! { root:: } ) ;
108+ tokens. append_all ( quote ! { root:: } ) ;
107109 }
108110
109111 let align = match layout. align {
@@ -114,7 +116,7 @@ pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
114116 } ;
115117
116118 let size = layout. size ;
117- tokens. append ( quote ! {
119+ tokens. append_all ( quote ! {
118120 __BindgenBitfieldUnit<[ u8 ; #size] , #align>
119121 } ) ;
120122
@@ -126,6 +128,7 @@ pub mod ast_ty {
126128 use ir:: function:: FunctionSig ;
127129 use ir:: ty:: FloatKind ;
128130 use quote;
131+ use proc_macro2;
129132
130133 pub fn raw_type ( ctx : & BindgenContext , name : & str ) -> quote:: Tokens {
131134 let ident = ctx. rust_ident_raw ( name) ;
@@ -166,29 +169,25 @@ pub mod ast_ty {
166169
167170 pub fn int_expr ( val : i64 ) -> quote:: Tokens {
168171 // 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)
172174 }
173175
174176 pub fn uint_expr ( val : u64 ) -> quote:: Tokens {
175177 // 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)
179180 }
180181
181182 pub fn byte_array_expr ( bytes : & [ u8 ] ) -> quote:: Tokens {
182183 let mut bytes: Vec < _ > = bytes. iter ( ) . cloned ( ) . collect ( ) ;
183184 bytes. push ( 0 ) ;
184- quote ! {
185- #bytes
186- }
185+ quote ! { [ #( #bytes) , * ] }
187186 }
188187
189188 pub fn cstr_expr ( mut string : String ) -> quote:: Tokens {
190189 string. push ( '\0' ) ;
191- let b = quote :: ByteStr ( & string) ;
190+ let b = proc_macro2 :: Literal :: byte_string ( & string. as_bytes ( ) ) ;
192191 quote ! {
193192 #b
194193 }
@@ -199,16 +198,9 @@ pub mod ast_ty {
199198 f : f64 ,
200199 ) -> Result < quote:: Tokens , ( ) > {
201200 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) ;
208202
209- let mut tokens = quote ! { } ;
210- tokens. append ( string) ;
211- return Ok ( tokens) ;
203+ return Ok ( quote ! ( #val) ) ;
212204 }
213205
214206 let prefix = ctx. trait_prefix ( ) ;
0 commit comments