Skip to content

Commit

Permalink
Never use spaces in generated name (fixes #844)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Jul 23, 2017
1 parent 3c98018 commit 333f21a
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ impl Type {

/// Get this type's name.
pub fn name(&self) -> Option<&str> {
if let TypeKind::Int(ik) = *self.kind() {
// these types will return spaces in their names
// unless we manually change it
match ik {
IntKind::Char { .. } => return Some("char"),
IntKind::SChar => return Some("signed_char"),
IntKind::UChar => return Some("unsigned_char"),
IntKind::Short => return Some("short"),
IntKind::UShort => return Some("unsigned_short"),
IntKind::Int => return Some("int"),
IntKind::UInt => return Some("unsigned_int"),
IntKind::Long => return Some("long"),
IntKind::ULong => return Some("unsigned_long"),
IntKind::LongLong => return Some("longlong"),
IntKind::ULongLong => return Some("unsigned_longlong"),
_ => ()
}
}
self.name.as_ref().map(|name| &**name)
}

Expand Down
97 changes: 95 additions & 2 deletions tests/expectations/tests/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,60 @@ pub struct Foo<T> {
impl <T> Default for Foo<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct B<T> {
pub m_member: T,
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for B<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
extern "C" {
#[link_name = "_Z3bar3FooIiiE"]
pub fn bar(foo: Foo<::std::os::raw::c_int>);
}
#[repr(C)]
#[derive(Debug, Copy)]
pub struct C {
pub mB: B<::std::os::raw::c_uint>,
pub mBConstPtr: B<*const ::std::os::raw::c_int>,
pub mBConst: B<::std::os::raw::c_int>,
pub mBVolatile: B<::std::os::raw::c_int>,
}
#[test]
fn bindgen_test_layout_C() {
assert_eq!(::std::mem::size_of::<C>() , 24usize , concat ! (
"Size of: " , stringify ! ( C ) ));
assert_eq! (::std::mem::align_of::<C>() , 8usize , concat ! (
"Alignment of " , stringify ! ( C ) ));
assert_eq! (unsafe { & ( * ( 0 as * const C ) ) . mB as * const _ as usize
} , 0usize , concat ! (
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
! ( mB ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const C ) ) . mBConstPtr as * const _ as usize
} , 8usize , concat ! (
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
! ( mBConstPtr ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const C ) ) . mBConst as * const _ as usize } ,
16usize , concat ! (
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
! ( mBConst ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const C ) ) . mBVolatile as * const _ as usize
} , 20usize , concat ! (
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
! ( mBVolatile ) ));
}
impl Clone for C {
fn clone(&self) -> Self { *self }
}
impl Default for C {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[repr(C)]
#[derive(Debug)]
pub struct D {
pub m_foo: D_MyFoo,
Expand Down Expand Up @@ -245,6 +294,50 @@ fn __bindgen_test_layout_Foo_open0_int_int_close0_instantiation() {
Foo<::std::os::raw::c_int> ) ));
}
#[test]
fn __bindgen_test_layout_B_open0_unsigned_int_close0_instantiation() {
assert_eq!(::std::mem::size_of::<B<::std::os::raw::c_uint>>() , 4usize ,
concat ! (
"Size of template specialization: " , stringify ! (
B<::std::os::raw::c_uint> ) ));
assert_eq!(::std::mem::align_of::<B<::std::os::raw::c_uint>>() , 4usize ,
concat ! (
"Alignment of template specialization: " , stringify ! (
B<::std::os::raw::c_uint> ) ));
}
#[test]
fn __bindgen_test_layout_B_open0__bindgen_ty_id_111_close0_instantiation() {
assert_eq!(::std::mem::size_of::<B<*const ::std::os::raw::c_int>>() ,
8usize , concat ! (
"Size of template specialization: " , stringify ! (
B<*const ::std::os::raw::c_int> ) ));
assert_eq!(::std::mem::align_of::<B<*const ::std::os::raw::c_int>>() ,
8usize , concat ! (
"Alignment of template specialization: " , stringify ! (
B<*const ::std::os::raw::c_int> ) ));
}
#[test]
fn __bindgen_test_layout_B_open0_int_close0_instantiation() {
assert_eq!(::std::mem::size_of::<B<::std::os::raw::c_int>>() , 4usize ,
concat ! (
"Size of template specialization: " , stringify ! (
B<::std::os::raw::c_int> ) ));
assert_eq!(::std::mem::align_of::<B<::std::os::raw::c_int>>() , 4usize ,
concat ! (
"Alignment of template specialization: " , stringify ! (
B<::std::os::raw::c_int> ) ));
}
#[test]
fn __bindgen_test_layout_B_open0_int_close0_instantiation_1() {
assert_eq!(::std::mem::size_of::<B<::std::os::raw::c_int>>() , 4usize ,
concat ! (
"Size of template specialization: " , stringify ! (
B<::std::os::raw::c_int> ) ));
assert_eq!(::std::mem::align_of::<B<::std::os::raw::c_int>>() , 4usize ,
concat ! (
"Alignment of template specialization: " , stringify ! (
B<::std::os::raw::c_int> ) ));
}
#[test]
fn __bindgen_test_layout_Foo_open0_int_int_close0_instantiation_1() {
assert_eq!(::std::mem::size_of::<Foo<::std::os::raw::c_int>>() , 24usize ,
concat ! (
Expand All @@ -256,7 +349,7 @@ fn __bindgen_test_layout_Foo_open0_int_int_close0_instantiation_1() {
Foo<::std::os::raw::c_int> ) ));
}
#[test]
fn __bindgen_test_layout_Rooted_open0__bindgen_ty_id_108_close0_instantiation() {
fn __bindgen_test_layout_Rooted_open0__bindgen_ty_id_134_close0_instantiation() {
assert_eq!(::std::mem::size_of::<Rooted<*mut ::std::os::raw::c_void>>() ,
24usize , concat ! (
"Size of template specialization: " , stringify ! (
Expand All @@ -267,7 +360,7 @@ fn __bindgen_test_layout_Rooted_open0__bindgen_ty_id_108_close0_instantiation()
Rooted<*mut ::std::os::raw::c_void> ) ));
}
#[test]
fn __bindgen_test_layout_Rooted_open0__bindgen_ty_id_114_close0_instantiation() {
fn __bindgen_test_layout_Rooted_open0__bindgen_ty_id_140_close0_instantiation() {
assert_eq!(::std::mem::size_of::<Rooted<*mut ::std::os::raw::c_void>>() ,
24usize , concat ! (
"Size of template specialization: " , stringify ! (
Expand Down
11 changes: 11 additions & 0 deletions tests/headers/template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@ template<typename T, typename U> class Foo {
T m_member_arr[1];
};

template<typename T> class B {
T m_member;
};

void bar(Foo<int, int> foo);

struct C {
B<unsigned int> mB;
B<const int*> mBConstPtr;
B<const int> mBConst;
B<volatile int> mBVolatile;
};

template<typename T>
class D {
typedef Foo<int, int> MyFoo;
Expand Down

0 comments on commit 333f21a

Please sign in to comment.