diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2017-08-14 05:05:30 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-14 05:05:30 -0500 |
commit | 5d53f85fa96b7c7d86ba3291c80d6014f27938d6 (patch) | |
tree | 346f44253038b66b7d661a91a1453f5d9edbf1c4 | |
parent | 8c71eed3ad740d736c8d6425c3910cf0e06e8004 (diff) | |
parent | e2713861d007d9c4e071679ccf9637521f4b1912 (diff) |
Auto merge of #911 - upsuper:layout-test-name, r=emilio
Stablize name of reference and nested combination of ref, ptr, and array
-rw-r--r-- | src/ir/item.rs | 30 | ||||
-rw-r--r-- | src/ir/ty.rs | 35 | ||||
-rw-r--r-- | tests/expectations/tests/template.rs | 133 | ||||
-rw-r--r-- | tests/headers/template.hpp | 7 |
4 files changed, 158 insertions, 47 deletions
diff --git a/src/ir/item.rs b/src/ir/item.rs index 156d4829..b51a45ac 100644 --- a/src/ir/item.rs +++ b/src/ir/item.rs @@ -694,35 +694,7 @@ impl Item { }) } ItemKind::Type(ref ty) => { - let name = match *ty.kind() { - TypeKind::ResolvedTypeRef(..) => { - panic!("should have resolved this in name_target()") - } - TypeKind::Pointer(inner) => { - ctx.resolve_item(inner) - .expect_type().name() - .map(|name| { - format!("ptr_{}", Type::sanitize_name(name)) - }) - } - TypeKind::Array(inner, length) => { - ctx.resolve_item(inner) - .expect_type().name() - .map(|name| { - format!( - "array_{}_{}", - Type::sanitize_name(name), - length, - ) - }) - } - _ => { - ty.name() - .map(Type::sanitize_name) - .map(Into::into) - } - }; - name.unwrap_or_else(|| { + ty.sanitized_name(ctx).map(Into::into).unwrap_or_else(|| { format!("_bindgen_ty_{}", self.exposed_id(ctx)) }) } diff --git a/src/ir/ty.rs b/src/ir/ty.rs index 4a2b7668..d1d4f00d 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -261,21 +261,15 @@ impl Type { match self.kind { TypeKind::Named => { let name = self.name().expect("Unnamed named type?"); - !Self::is_valid_identifier(&name) + !clang::is_valid_identifier(&name) } _ => false, } } - /// Checks whether the name looks like an identifier, - /// i.e. is alphanumeric (including '_') and does not start with a digit. - pub fn is_valid_identifier(name: &str) -> bool { - clang::is_valid_identifier(name) - } - /// Takes `name`, and returns a suitable identifier representation for it. - pub fn sanitize_name<'a>(name: &'a str) -> Cow<'a, str> { - if Self::is_valid_identifier(name) { + fn sanitize_name<'a>(name: &'a str) -> Cow<'a, str> { + if clang::is_valid_identifier(name) { return Cow::Borrowed(name) } @@ -283,6 +277,25 @@ impl Type { Cow::Owned(name) } + /// Get this type's santizied name. + pub fn sanitized_name<'a>(&'a self, ctx: &BindgenContext) -> Option<Cow<'a, str>> { + let name_info = match *self.kind() { + TypeKind::Pointer(inner) => Some((inner, Cow::Borrowed("ptr"))), + TypeKind::Reference(inner) => Some((inner, Cow::Borrowed("ref"))), + TypeKind::Array(inner, length) => { + Some((inner, format!("array{}", length).into())) + } + _ => None, + }; + if let Some((inner, prefix)) = name_info { + ctx.resolve_item(inner) + .expect_type().sanitized_name(ctx) + .map(|name| format!("{}_{}", prefix, name).into()) + } else { + self.name().map(Self::sanitize_name) + } + } + /// See safe_canonical_type. pub fn canonical_type<'tr>(&'tr self, ctx: &'tr BindgenContext) @@ -1093,7 +1106,7 @@ impl Type { if name.is_empty() { let pretty_name = ty.spelling(); - if Self::is_valid_identifier(&pretty_name) { + if clang::is_valid_identifier(&pretty_name) { name = pretty_name; } } @@ -1111,7 +1124,7 @@ impl Type { // 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) { + if clang::is_valid_identifier(&pretty_name) { name = pretty_name; } } diff --git a/tests/expectations/tests/template.rs b/tests/expectations/tests/template.rs index 9cea4d97..f2d639b6 100644 --- a/tests/expectations/tests/template.rs +++ b/tests/expectations/tests/template.rs @@ -39,15 +39,22 @@ pub struct C { pub mB: B<::std::os::raw::c_uint>, pub mBConstPtr: B<*const ::std::os::raw::c_int>, pub mBConstStructPtr: B<*const mozilla_Foo>, + pub mBConstStructPtrArray: B<[*const mozilla_Foo; 1usize]>, pub mBConst: B<::std::os::raw::c_int>, pub mBVolatile: B<::std::os::raw::c_int>, pub mBConstBool: B<bool>, pub mBConstChar: B<u16>, pub mBArray: B<[::std::os::raw::c_int; 1usize]>, + pub mBPtrArray: B<[*mut ::std::os::raw::c_int; 1usize]>, + pub mBArrayPtr: B<*mut [::std::os::raw::c_int; 1usize]>, + pub mBRef: B<*mut ::std::os::raw::c_int>, + pub mBConstRef: B<*const ::std::os::raw::c_int>, + pub mPtrRef: B<*mut *mut ::std::os::raw::c_int>, + pub mArrayRef: B<*mut [::std::os::raw::c_int; 1usize]>, } #[test] fn bindgen_test_layout_C() { - assert_eq!(::std::mem::size_of::<C>() , 40usize , concat ! ( + assert_eq!(::std::mem::size_of::<C>() , 96usize , concat ! ( "Size of: " , stringify ! ( C ) )); assert_eq! (::std::mem::align_of::<C>() , 8usize , concat ! ( "Alignment of " , stringify ! ( C ) )); @@ -66,30 +73,65 @@ fn bindgen_test_layout_C() { "Alignment of field: " , stringify ! ( C ) , "::" , stringify ! ( mBConstStructPtr ) )); assert_eq! (unsafe { + & ( * ( 0 as * const C ) ) . mBConstStructPtrArray as * const + _ as usize } , 24usize , concat ! ( + "Alignment of field: " , stringify ! ( C ) , "::" , stringify + ! ( mBConstStructPtrArray ) )); + assert_eq! (unsafe { & ( * ( 0 as * const C ) ) . mBConst as * const _ as usize } , - 24usize , concat ! ( + 32usize , concat ! ( "Alignment of field: " , stringify ! ( C ) , "::" , stringify ! ( mBConst ) )); assert_eq! (unsafe { & ( * ( 0 as * const C ) ) . mBVolatile as * const _ as usize - } , 28usize , concat ! ( + } , 36usize , concat ! ( "Alignment of field: " , stringify ! ( C ) , "::" , stringify ! ( mBVolatile ) )); assert_eq! (unsafe { & ( * ( 0 as * const C ) ) . mBConstBool as * const _ as usize - } , 32usize , concat ! ( + } , 40usize , concat ! ( "Alignment of field: " , stringify ! ( C ) , "::" , stringify ! ( mBConstBool ) )); assert_eq! (unsafe { & ( * ( 0 as * const C ) ) . mBConstChar as * const _ as usize - } , 34usize , concat ! ( + } , 42usize , concat ! ( "Alignment of field: " , stringify ! ( C ) , "::" , stringify ! ( mBConstChar ) )); assert_eq! (unsafe { & ( * ( 0 as * const C ) ) . mBArray as * const _ as usize } , - 36usize , concat ! ( + 44usize , concat ! ( "Alignment of field: " , stringify ! ( C ) , "::" , stringify ! ( mBArray ) )); + assert_eq! (unsafe { + & ( * ( 0 as * const C ) ) . mBPtrArray as * const _ as usize + } , 48usize , concat ! ( + "Alignment of field: " , stringify ! ( C ) , "::" , stringify + ! ( mBPtrArray ) )); + assert_eq! (unsafe { + & ( * ( 0 as * const C ) ) . mBArrayPtr as * const _ as usize + } , 56usize , concat ! ( + "Alignment of field: " , stringify ! ( C ) , "::" , stringify + ! ( mBArrayPtr ) )); + assert_eq! (unsafe { + & ( * ( 0 as * const C ) ) . mBRef as * const _ as usize } , + 64usize , concat ! ( + "Alignment of field: " , stringify ! ( C ) , "::" , stringify + ! ( mBRef ) )); + assert_eq! (unsafe { + & ( * ( 0 as * const C ) ) . mBConstRef as * const _ as usize + } , 72usize , concat ! ( + "Alignment of field: " , stringify ! ( C ) , "::" , stringify + ! ( mBConstRef ) )); + assert_eq! (unsafe { + & ( * ( 0 as * const C ) ) . mPtrRef as * const _ as usize } , + 80usize , concat ! ( + "Alignment of field: " , stringify ! ( C ) , "::" , stringify + ! ( mPtrRef ) )); + assert_eq! (unsafe { + & ( * ( 0 as * const C ) ) . mArrayRef as * const _ as usize } + , 88usize , concat ! ( + "Alignment of field: " , stringify ! ( C ) , "::" , stringify + ! ( mArrayRef ) )); } impl Clone for C { fn clone(&self) -> Self { *self } @@ -353,6 +395,17 @@ fn __bindgen_test_layout_B_open0_ptr_const_mozilla__Foo_close0_instantiation() { B<*const mozilla_Foo> ) )); } #[test] +fn __bindgen_test_layout_B_open0_array1_ptr_const_mozilla__Foo_close0_instantiation() { + assert_eq!(::std::mem::size_of::<B<[*const mozilla_Foo; 1usize]>>() , + 8usize , concat ! ( + "Size of template specialization: " , stringify ! ( + B<[*const mozilla_Foo; 1usize]> ) )); + assert_eq!(::std::mem::align_of::<B<[*const mozilla_Foo; 1usize]>>() , + 8usize , concat ! ( + "Alignment of template specialization: " , stringify ! ( + B<[*const mozilla_Foo; 1usize]> ) )); +} +#[test] fn __bindgen_test_layout_B_open0_const_int_close0_instantiation() { assert_eq!(::std::mem::size_of::<B<::std::os::raw::c_int>>() , 4usize , concat ! ( @@ -393,7 +446,7 @@ fn __bindgen_test_layout_B_open0_const_char16_t_close0_instantiation() { ) )); } #[test] -fn __bindgen_test_layout_B_open0_array_int_1_close0_instantiation() { +fn __bindgen_test_layout_B_open0_array1_int_close0_instantiation() { assert_eq!(::std::mem::size_of::<B<[::std::os::raw::c_int; 1usize]>>() , 4usize , concat ! ( "Size of template specialization: " , stringify ! ( @@ -404,6 +457,72 @@ fn __bindgen_test_layout_B_open0_array_int_1_close0_instantiation() { B<[::std::os::raw::c_int; 1usize]> ) )); } #[test] +fn __bindgen_test_layout_B_open0_array1_ptr_int_close0_instantiation() { + assert_eq!(::std::mem::size_of::<B<[*mut ::std::os::raw::c_int; 1usize]>>() + , 8usize , concat ! ( + "Size of template specialization: " , stringify ! ( + B<[*mut ::std::os::raw::c_int; 1usize]> ) )); + assert_eq!(::std::mem::align_of::<B<[*mut ::std::os::raw::c_int; 1usize]>>() + , 8usize , concat ! ( + "Alignment of template specialization: " , stringify ! ( + B<[*mut ::std::os::raw::c_int; 1usize]> ) )); +} +#[test] +fn __bindgen_test_layout_B_open0_ptr_array1_int_close0_instantiation() { + assert_eq!(::std::mem::size_of::<B<*mut [::std::os::raw::c_int; 1usize]>>() + , 8usize , concat ! ( + "Size of template specialization: " , stringify ! ( + B<*mut [::std::os::raw::c_int; 1usize]> ) )); + assert_eq!(::std::mem::align_of::<B<*mut [::std::os::raw::c_int; 1usize]>>() + , 8usize , concat ! ( + "Alignment of template specialization: " , stringify ! ( + B<*mut [::std::os::raw::c_int; 1usize]> ) )); +} +#[test] +fn __bindgen_test_layout_B_open0_ref_int_close0_instantiation() { + assert_eq!(::std::mem::size_of::<B<*mut ::std::os::raw::c_int>>() , 8usize + , concat ! ( + "Size of template specialization: " , stringify ! ( + B<*mut ::std::os::raw::c_int> ) )); + assert_eq!(::std::mem::align_of::<B<*mut ::std::os::raw::c_int>>() , + 8usize , concat ! ( + "Alignment of template specialization: " , stringify ! ( + B<*mut ::std::os::raw::c_int> ) )); +} +#[test] +fn __bindgen_test_layout_B_open0_ref_const_int_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_ref_ptr_int_close0_instantiation() { + assert_eq!(::std::mem::size_of::<B<*mut *mut ::std::os::raw::c_int>>() , + 8usize , concat ! ( + "Size of template specialization: " , stringify ! ( + B<*mut *mut ::std::os::raw::c_int> ) )); + assert_eq!(::std::mem::align_of::<B<*mut *mut ::std::os::raw::c_int>>() , + 8usize , concat ! ( + "Alignment of template specialization: " , stringify ! ( + B<*mut *mut ::std::os::raw::c_int> ) )); +} +#[test] +fn __bindgen_test_layout_B_open0_ref_array1_int_close0_instantiation() { + assert_eq!(::std::mem::size_of::<B<*mut [::std::os::raw::c_int; 1usize]>>() + , 8usize , concat ! ( + "Size of template specialization: " , stringify ! ( + B<*mut [::std::os::raw::c_int; 1usize]> ) )); + assert_eq!(::std::mem::align_of::<B<*mut [::std::os::raw::c_int; 1usize]>>() + , 8usize , concat ! ( + "Alignment of template specialization: " , stringify ! ( + B<*mut [::std::os::raw::c_int; 1usize]> ) )); +} +#[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 ! ( diff --git a/tests/headers/template.hpp b/tests/headers/template.hpp index 0f067053..24638745 100644 --- a/tests/headers/template.hpp +++ b/tests/headers/template.hpp @@ -20,11 +20,18 @@ struct C { B<unsigned int> mB; B<const int*> mBConstPtr; B<const mozilla::Foo*> mBConstStructPtr; + B<const mozilla::Foo*[1]> mBConstStructPtrArray; B<const int> mBConst; B<volatile int> mBVolatile; B<const bool> mBConstBool; B<const char16_t> mBConstChar; B<int[1]> mBArray; + B<int*[1]> mBPtrArray; + B<int(*)[1]> mBArrayPtr; + B<int&> mBRef; + B<const int&> mBConstRef; + B<int*&> mPtrRef; + B<int(&)[1]> mArrayRef; // clang 3.x ignores const in this case, so they generate different // result than clang 4.0. // B<const int[1]> mBConstArray; |