Skip to content

Commit d6e8eaf

Browse files
authored
Merge pull request #1476 from emilio/ctypes-ident
codegen: ctypes_prefix may not be an ident.
2 parents 2da6f06 + fbb8420 commit d6e8eaf

File tree

3 files changed

+85
-23
lines changed

3 files changed

+85
-23
lines changed

src/codegen/helpers.rs

+24-23
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,54 @@
22
33
use ir::context::BindgenContext;
44
use ir::layout::Layout;
5-
use proc_macro2::{self, Ident, Span};
5+
use proc_macro2::{Ident, Span, TokenStream};
66
use quote::TokenStreamExt;
77

88
pub mod attributes {
9-
use proc_macro2::{self, Ident, Span};
9+
use proc_macro2::{Ident, Span, TokenStream};
1010

11-
pub fn repr(which: &str) -> proc_macro2::TokenStream {
11+
pub fn repr(which: &str) -> TokenStream {
1212
let which = Ident::new(which, Span::call_site());
1313
quote! {
1414
#[repr( #which )]
1515
}
1616
}
1717

18-
pub fn repr_list(which_ones: &[&str]) -> proc_macro2::TokenStream {
18+
pub fn repr_list(which_ones: &[&str]) -> TokenStream {
1919
let which_ones = which_ones.iter().cloned().map(|one| Ident::new(one, Span::call_site()));
2020
quote! {
2121
#[repr( #( #which_ones ),* )]
2222
}
2323
}
2424

25-
pub fn derives(which_ones: &[&str]) -> proc_macro2::TokenStream {
25+
pub fn derives(which_ones: &[&str]) -> TokenStream {
2626
let which_ones = which_ones.iter().cloned().map(|one| Ident::new(one, Span::call_site()));
2727
quote! {
2828
#[derive( #( #which_ones ),* )]
2929
}
3030
}
3131

32-
pub fn inline() -> proc_macro2::TokenStream {
32+
pub fn inline() -> TokenStream {
3333
quote! {
3434
#[inline]
3535
}
3636
}
3737

38-
pub fn must_use() -> proc_macro2::TokenStream {
38+
pub fn must_use() -> TokenStream {
3939
quote! {
4040
#[must_use]
4141
}
4242
}
4343

44-
pub fn doc(comment: String) -> proc_macro2::TokenStream {
44+
pub fn doc(comment: String) -> TokenStream {
4545
use std::str::FromStr;
4646

4747
// NOTE(emilio): By this point comments are already preprocessed and in
4848
// `///` form. Quote turns them into `#[doc]` comments, but oh well.
49-
proc_macro2::TokenStream::from_str(&comment).unwrap()
49+
TokenStream::from_str(&comment).unwrap()
5050
}
5151

52-
pub fn link_name(name: &str) -> proc_macro2::TokenStream {
52+
pub fn link_name(name: &str) -> TokenStream {
5353
// LLVM mangles the name by default but it's already mangled.
5454
// Prefixing the name with \u{1} should tell LLVM to not mangle it.
5555
let name = format!("\u{1}{}", name);
@@ -61,7 +61,7 @@ pub mod attributes {
6161

6262
/// Generates a proper type for a field or type with a given `Layout`, that is,
6363
/// a type with the correct size and alignment restrictions.
64-
pub fn blob(ctx: &BindgenContext, layout: Layout) -> proc_macro2::TokenStream {
64+
pub fn blob(ctx: &BindgenContext, layout: Layout) -> TokenStream {
6565
let opaque = layout.opaque();
6666

6767
// FIXME(emilio, #412): We fall back to byte alignment, but there are
@@ -92,14 +92,14 @@ pub fn blob(ctx: &BindgenContext, layout: Layout) -> proc_macro2::TokenStream {
9292
}
9393

9494
/// Integer type of the same size as the given `Layout`.
95-
pub fn integer_type(ctx: &BindgenContext, layout: Layout) -> Option<proc_macro2::TokenStream> {
95+
pub fn integer_type(ctx: &BindgenContext, layout: Layout) -> Option<TokenStream> {
9696
let name = Layout::known_type_for_size(ctx, layout.size)?;
9797
let name = Ident::new(name, Span::call_site());
9898
Some(quote! { #name })
9999
}
100100

101101
/// Generates a bitfield allocation unit type for a type with the given `Layout`.
102-
pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> proc_macro2::TokenStream {
102+
pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> TokenStream {
103103
let mut tokens = quote! {};
104104

105105
if ctx.options().enable_cxx_namespaces {
@@ -126,13 +126,14 @@ pub mod ast_ty {
126126
use ir::function::FunctionSig;
127127
use ir::layout::Layout;
128128
use ir::ty::FloatKind;
129-
use proc_macro2;
129+
use std::str::FromStr;
130+
use proc_macro2::{self, TokenStream};
130131

131-
pub fn raw_type(ctx: &BindgenContext, name: &str) -> proc_macro2::TokenStream {
132+
pub fn raw_type(ctx: &BindgenContext, name: &str) -> TokenStream {
132133
let ident = ctx.rust_ident_raw(name);
133134
match ctx.options().ctypes_prefix {
134135
Some(ref prefix) => {
135-
let prefix = ctx.rust_ident_raw(prefix.as_str());
136+
let prefix = TokenStream::from_str(prefix.as_str()).unwrap();
136137
quote! {
137138
#prefix::#ident
138139
}
@@ -147,7 +148,7 @@ pub mod ast_ty {
147148
ctx: &BindgenContext,
148149
fk: FloatKind,
149150
layout: Option<Layout>,
150-
) -> proc_macro2::TokenStream {
151+
) -> TokenStream {
151152
// TODO: we probably should take the type layout into account more
152153
// often?
153154
//
@@ -187,25 +188,25 @@ pub mod ast_ty {
187188
}
188189
}
189190

190-
pub fn int_expr(val: i64) -> proc_macro2::TokenStream {
191+
pub fn int_expr(val: i64) -> TokenStream {
191192
// Don't use quote! { #val } because that adds the type suffix.
192193
let val = proc_macro2::Literal::i64_unsuffixed(val);
193194
quote!(#val)
194195
}
195196

196-
pub fn uint_expr(val: u64) -> proc_macro2::TokenStream {
197+
pub fn uint_expr(val: u64) -> TokenStream {
197198
// Don't use quote! { #val } because that adds the type suffix.
198199
let val = proc_macro2::Literal::u64_unsuffixed(val);
199200
quote!(#val)
200201
}
201202

202-
pub fn byte_array_expr(bytes: &[u8]) -> proc_macro2::TokenStream {
203+
pub fn byte_array_expr(bytes: &[u8]) -> TokenStream {
203204
let mut bytes: Vec<_> = bytes.iter().cloned().collect();
204205
bytes.push(0);
205206
quote! { [ #(#bytes),* ] }
206207
}
207208

208-
pub fn cstr_expr(mut string: String) -> proc_macro2::TokenStream {
209+
pub fn cstr_expr(mut string: String) -> TokenStream {
209210
string.push('\0');
210211
let b = proc_macro2::Literal::byte_string(&string.as_bytes());
211212
quote! {
@@ -216,7 +217,7 @@ pub mod ast_ty {
216217
pub fn float_expr(
217218
ctx: &BindgenContext,
218219
f: f64,
219-
) -> Result<proc_macro2::TokenStream, ()> {
220+
) -> Result<TokenStream, ()> {
220221
if f.is_finite() {
221222
let val = proc_macro2::Literal::f64_unsuffixed(f);
222223

@@ -250,7 +251,7 @@ pub mod ast_ty {
250251
pub fn arguments_from_signature(
251252
signature: &FunctionSig,
252253
ctx: &BindgenContext,
253-
) -> Vec<proc_macro2::TokenStream> {
254+
) -> Vec<TokenStream> {
254255
let mut unnamed_arguments = 0;
255256
signature
256257
.argument_types()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
#![no_std]
10+
mod libc {
11+
pub mod foo {
12+
pub type c_int = i32;
13+
pub enum c_void {}
14+
}
15+
}
16+
17+
#[repr(C)]
18+
#[derive(Debug, Copy, Clone)]
19+
pub struct foo {
20+
pub a: libc::foo::c_int,
21+
pub b: libc::foo::c_int,
22+
pub bar: *mut libc::foo::c_void,
23+
}
24+
#[test]
25+
fn bindgen_test_layout_foo() {
26+
assert_eq!(
27+
::core::mem::size_of::<foo>(),
28+
16usize,
29+
concat!("Size of: ", stringify!(foo))
30+
);
31+
assert_eq!(
32+
::core::mem::align_of::<foo>(),
33+
8usize,
34+
concat!("Alignment of ", stringify!(foo))
35+
);
36+
assert_eq!(
37+
unsafe { &(*(::core::ptr::null::<foo>())).a as *const _ as usize },
38+
0usize,
39+
concat!("Offset of field: ", stringify!(foo), "::", stringify!(a))
40+
);
41+
assert_eq!(
42+
unsafe { &(*(::core::ptr::null::<foo>())).b as *const _ as usize },
43+
4usize,
44+
concat!("Offset of field: ", stringify!(foo), "::", stringify!(b))
45+
);
46+
assert_eq!(
47+
unsafe { &(*(::core::ptr::null::<foo>())).bar as *const _ as usize },
48+
8usize,
49+
concat!("Offset of field: ", stringify!(foo), "::", stringify!(bar))
50+
);
51+
}
52+
impl Default for foo {
53+
fn default() -> Self {
54+
unsafe { ::core::mem::zeroed() }
55+
}
56+
}

tests/headers/ctypes-prefix-path.h

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// bindgen-flags: --ctypes-prefix "libc::foo" --use-core --raw-line "#![no_std]" --raw-line "mod libc { pub mod foo { pub type c_int = i32; pub enum c_void {} } }" --rustified-enum ".*"
2+
struct foo {
3+
int a, b;
4+
void* bar;
5+
};

0 commit comments

Comments
 (0)