diff options
author | Kornel Lesiński <kornel@geekhood.net> | 2017-02-15 15:45:53 +0000 |
---|---|---|
committer | Kornel Lesiński <kornel@geekhood.net> | 2017-02-15 16:29:07 +0000 |
commit | 4d628c9f0da7b35f933db00b6d686c04dedbbf22 (patch) | |
tree | 1f09d4fe47a21875bab1d928f9b5a2d3e005a4e6 | |
parent | f1caa107f463a548524572bd426fd7fe4e7d13f5 (diff) |
Use typedef-derived name for anonymous structs and unions
-rw-r--r-- | src/ir/ty.rs | 15 | ||||
-rw-r--r-- | tests/expectations/tests/bitfield_method_mangling.rs | 20 | ||||
-rw-r--r-- | tests/expectations/tests/layout_array.rs | 19 | ||||
-rw-r--r-- | tests/expectations/tests/layout_mbuf.rs | 19 | ||||
-rw-r--r-- | tests/expectations/tests/union_fields.rs | 35 | ||||
-rw-r--r-- | tests/expectations/tests/unknown_attr.rs | 23 |
6 files changed, 72 insertions, 59 deletions
diff --git a/src/ir/ty.rs b/src/ir/ty.rs index ec168f02..78ae6cb2 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -1080,6 +1080,16 @@ impl Type { let complex = CompInfo::from_ty(potential_id, ty, location, ctx) .expect("Not a complex type?"); + + if name.is_empty() { + // The pretty-printed name may contain typedefed name, + // but may also be "struct (anonymous at .h:1)" + let pretty_name = ty.spelling(); + if Self::is_valid_identifier(&pretty_name) { + name = pretty_name; + } + } + TypeKind::Comp(complex) } // FIXME: We stub vectors as arrays since in 99% of the cases the @@ -1124,6 +1134,11 @@ impl Type { // TODO: maybe declaration.canonical()? Ok(ParseResult::New(ty, Some(cursor.canonical()))) } + + fn is_valid_identifier(name: &str) -> bool { + name.chars().next().map(|first|first.is_alphabetic()).unwrap_or(false) && + name.chars().all(|ch|ch.is_alphanumeric() || ch == '_') + } } impl Trace for Type { diff --git a/tests/expectations/tests/bitfield_method_mangling.rs b/tests/expectations/tests/bitfield_method_mangling.rs index 0ab5fce5..0a6c9fdd 100644 --- a/tests/expectations/tests/bitfield_method_mangling.rs +++ b/tests/expectations/tests/bitfield_method_mangling.rs @@ -6,20 +6,23 @@ #[repr(C)] #[derive(Debug, Default, Copy)] -pub struct _bindgen_ty_1 { +pub struct mach_msg_type_descriptor_t { pub _bitfield_1: u32, } #[test] -fn bindgen_test_layout__bindgen_ty_1() { - assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 4usize , concat ! ( - "Size of: " , stringify ! ( _bindgen_ty_1 ) )); - assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 4usize , concat ! ( - "Alignment of " , stringify ! ( _bindgen_ty_1 ) )); +fn bindgen_test_layout_mach_msg_type_descriptor_t() { + assert_eq!(::std::mem::size_of::<mach_msg_type_descriptor_t>() , 4usize , + concat ! ( + "Size of: " , stringify ! ( mach_msg_type_descriptor_t ) )); + assert_eq! (::std::mem::align_of::<mach_msg_type_descriptor_t>() , 4usize + , concat ! ( + "Alignment of " , stringify ! ( mach_msg_type_descriptor_t ) + )); } -impl Clone for _bindgen_ty_1 { +impl Clone for mach_msg_type_descriptor_t { fn clone(&self) -> Self { *self } } -impl _bindgen_ty_1 { +impl mach_msg_type_descriptor_t { #[inline] pub fn pad3(&self) -> ::std::os::raw::c_uint { unsafe { @@ -48,4 +51,3 @@ impl _bindgen_ty_1 { ((val as u32 as u32) << 24u32) & (4278190080usize as u32); } } -pub type mach_msg_type_descriptor_t = _bindgen_ty_1; diff --git a/tests/expectations/tests/layout_array.rs b/tests/expectations/tests/layout_array.rs index c28d3ec8..2cc85785 100644 --- a/tests/expectations/tests/layout_array.rs +++ b/tests/expectations/tests/layout_array.rs @@ -117,26 +117,25 @@ impl Default for rte_mempool_ops { */ #[repr(C)] #[derive(Debug, Default, Copy)] -pub struct _bindgen_ty_1 { +pub struct rte_spinlock_t { /**< lock status 0 = unlocked, 1 = locked */ pub locked: ::std::os::raw::c_int, } #[test] -fn bindgen_test_layout__bindgen_ty_1() { - assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 4usize , concat ! ( - "Size of: " , stringify ! ( _bindgen_ty_1 ) )); - assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 4usize , concat ! ( - "Alignment of " , stringify ! ( _bindgen_ty_1 ) )); +fn bindgen_test_layout_rte_spinlock_t() { + assert_eq!(::std::mem::size_of::<rte_spinlock_t>() , 4usize , concat ! ( + "Size of: " , stringify ! ( rte_spinlock_t ) )); + assert_eq! (::std::mem::align_of::<rte_spinlock_t>() , 4usize , concat ! ( + "Alignment of " , stringify ! ( rte_spinlock_t ) )); assert_eq! (unsafe { - & ( * ( 0 as * const _bindgen_ty_1 ) ) . locked as * const _ + & ( * ( 0 as * const rte_spinlock_t ) ) . locked as * const _ as usize } , 0usize , concat ! ( - "Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::" + "Alignment of field: " , stringify ! ( rte_spinlock_t ) , "::" , stringify ! ( locked ) )); } -impl Clone for _bindgen_ty_1 { +impl Clone for rte_spinlock_t { fn clone(&self) -> Self { *self } } -pub type rte_spinlock_t = _bindgen_ty_1; /** * Structure storing the table of registered ops structs, each of which contain * the function pointers for the mempool ops functions. diff --git a/tests/expectations/tests/layout_mbuf.rs b/tests/expectations/tests/layout_mbuf.rs index 189b50a5..5732831a 100644 --- a/tests/expectations/tests/layout_mbuf.rs +++ b/tests/expectations/tests/layout_mbuf.rs @@ -39,26 +39,25 @@ pub type MARKER64 = [u64; 0usize]; */ #[repr(C)] #[derive(Debug, Default, Copy)] -pub struct _bindgen_ty_1 { +pub struct rte_atomic16_t { /**< An internal counter value. */ pub cnt: i16, } #[test] -fn bindgen_test_layout__bindgen_ty_1() { - assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 2usize , concat ! ( - "Size of: " , stringify ! ( _bindgen_ty_1 ) )); - assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 2usize , concat ! ( - "Alignment of " , stringify ! ( _bindgen_ty_1 ) )); +fn bindgen_test_layout_rte_atomic16_t() { + assert_eq!(::std::mem::size_of::<rte_atomic16_t>() , 2usize , concat ! ( + "Size of: " , stringify ! ( rte_atomic16_t ) )); + assert_eq! (::std::mem::align_of::<rte_atomic16_t>() , 2usize , concat ! ( + "Alignment of " , stringify ! ( rte_atomic16_t ) )); assert_eq! (unsafe { - & ( * ( 0 as * const _bindgen_ty_1 ) ) . cnt as * const _ as + & ( * ( 0 as * const rte_atomic16_t ) ) . cnt as * const _ as usize } , 0usize , concat ! ( - "Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::" + "Alignment of field: " , stringify ! ( rte_atomic16_t ) , "::" , stringify ! ( cnt ) )); } -impl Clone for _bindgen_ty_1 { +impl Clone for rte_atomic16_t { fn clone(&self) -> Self { *self } } -pub type rte_atomic16_t = _bindgen_ty_1; /** * The generic rte_mbuf, containing a packet mbuf. */ diff --git a/tests/expectations/tests/union_fields.rs b/tests/expectations/tests/union_fields.rs index 823a0b8b..8c8ef7d5 100644 --- a/tests/expectations/tests/union_fields.rs +++ b/tests/expectations/tests/union_fields.rs @@ -30,35 +30,34 @@ impl <T> ::std::fmt::Debug for __BindgenUnionField<T> { } #[repr(C)] #[derive(Debug, Default, Copy)] -pub struct _bindgen_ty_1 { +pub struct nsStyleUnion { pub mInt: __BindgenUnionField<::std::os::raw::c_int>, pub mFloat: __BindgenUnionField<f32>, pub mPointer: __BindgenUnionField<*mut ::std::os::raw::c_void>, pub bindgen_union_field: u64, } #[test] -fn bindgen_test_layout__bindgen_ty_1() { - assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 8usize , concat ! ( - "Size of: " , stringify ! ( _bindgen_ty_1 ) )); - assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 8usize , concat ! ( - "Alignment of " , stringify ! ( _bindgen_ty_1 ) )); +fn bindgen_test_layout_nsStyleUnion() { + assert_eq!(::std::mem::size_of::<nsStyleUnion>() , 8usize , concat ! ( + "Size of: " , stringify ! ( nsStyleUnion ) )); + assert_eq! (::std::mem::align_of::<nsStyleUnion>() , 8usize , concat ! ( + "Alignment of " , stringify ! ( nsStyleUnion ) )); assert_eq! (unsafe { - & ( * ( 0 as * const _bindgen_ty_1 ) ) . mInt as * const _ as + & ( * ( 0 as * const nsStyleUnion ) ) . mInt as * const _ as usize } , 0usize , concat ! ( - "Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::" - , stringify ! ( mInt ) )); + "Alignment of field: " , stringify ! ( nsStyleUnion ) , "::" , + stringify ! ( mInt ) )); assert_eq! (unsafe { - & ( * ( 0 as * const _bindgen_ty_1 ) ) . mFloat as * const _ - as usize } , 0usize , concat ! ( - "Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::" - , stringify ! ( mFloat ) )); + & ( * ( 0 as * const nsStyleUnion ) ) . mFloat as * const _ as + usize } , 0usize , concat ! ( + "Alignment of field: " , stringify ! ( nsStyleUnion ) , "::" , + stringify ! ( mFloat ) )); assert_eq! (unsafe { - & ( * ( 0 as * const _bindgen_ty_1 ) ) . mPointer as * const _ + & ( * ( 0 as * const nsStyleUnion ) ) . mPointer as * const _ as usize } , 0usize , concat ! ( - "Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::" - , stringify ! ( mPointer ) )); + "Alignment of field: " , stringify ! ( nsStyleUnion ) , "::" , + stringify ! ( mPointer ) )); } -impl Clone for _bindgen_ty_1 { +impl Clone for nsStyleUnion { fn clone(&self) -> Self { *self } } -pub type nsStyleUnion = _bindgen_ty_1; diff --git a/tests/expectations/tests/unknown_attr.rs b/tests/expectations/tests/unknown_attr.rs index efb86102..2c50c365 100644 --- a/tests/expectations/tests/unknown_attr.rs +++ b/tests/expectations/tests/unknown_attr.rs @@ -6,30 +6,29 @@ #[repr(C)] #[derive(Debug, Default, Copy)] -pub struct _bindgen_ty_1 { +pub struct max_align_t { pub __clang_max_align_nonce1: ::std::os::raw::c_longlong, pub __bindgen_padding_0: u64, pub __clang_max_align_nonce2: f64, pub __bindgen_padding_1: u64, } #[test] -fn bindgen_test_layout__bindgen_ty_1() { - assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 32usize , concat ! ( - "Size of: " , stringify ! ( _bindgen_ty_1 ) )); +fn bindgen_test_layout_max_align_t() { + assert_eq!(::std::mem::size_of::<max_align_t>() , 32usize , concat ! ( + "Size of: " , stringify ! ( max_align_t ) )); assert_eq! (unsafe { - & ( * ( 0 as * const _bindgen_ty_1 ) ) . + & ( * ( 0 as * const max_align_t ) ) . __clang_max_align_nonce1 as * const _ as usize } , 0usize , concat ! ( - "Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::" - , stringify ! ( __clang_max_align_nonce1 ) )); + "Alignment of field: " , stringify ! ( max_align_t ) , "::" , + stringify ! ( __clang_max_align_nonce1 ) )); assert_eq! (unsafe { - & ( * ( 0 as * const _bindgen_ty_1 ) ) . + & ( * ( 0 as * const max_align_t ) ) . __clang_max_align_nonce2 as * const _ as usize } , 16usize , concat ! ( - "Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::" - , stringify ! ( __clang_max_align_nonce2 ) )); + "Alignment of field: " , stringify ! ( max_align_t ) , "::" , + stringify ! ( __clang_max_align_nonce2 ) )); } -impl Clone for _bindgen_ty_1 { +impl Clone for max_align_t { fn clone(&self) -> Self { *self } } -pub type max_align_t = _bindgen_ty_1; |