Skip to content

Commit 5d53f85

Browse files
author
bors-servo
authored
Auto merge of #911 - upsuper:layout-test-name, r=emilio
Stablize name of reference and nested combination of ref, ptr, and array
2 parents 8c71eed + e271386 commit 5d53f85

File tree

4 files changed

+158
-47
lines changed

4 files changed

+158
-47
lines changed

src/ir/item.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -694,35 +694,7 @@ impl Item {
694694
})
695695
}
696696
ItemKind::Type(ref ty) => {
697-
let name = match *ty.kind() {
698-
TypeKind::ResolvedTypeRef(..) => {
699-
panic!("should have resolved this in name_target()")
700-
}
701-
TypeKind::Pointer(inner) => {
702-
ctx.resolve_item(inner)
703-
.expect_type().name()
704-
.map(|name| {
705-
format!("ptr_{}", Type::sanitize_name(name))
706-
})
707-
}
708-
TypeKind::Array(inner, length) => {
709-
ctx.resolve_item(inner)
710-
.expect_type().name()
711-
.map(|name| {
712-
format!(
713-
"array_{}_{}",
714-
Type::sanitize_name(name),
715-
length,
716-
)
717-
})
718-
}
719-
_ => {
720-
ty.name()
721-
.map(Type::sanitize_name)
722-
.map(Into::into)
723-
}
724-
};
725-
name.unwrap_or_else(|| {
697+
ty.sanitized_name(ctx).map(Into::into).unwrap_or_else(|| {
726698
format!("_bindgen_ty_{}", self.exposed_id(ctx))
727699
})
728700
}

src/ir/ty.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -261,28 +261,41 @@ impl Type {
261261
match self.kind {
262262
TypeKind::Named => {
263263
let name = self.name().expect("Unnamed named type?");
264-
!Self::is_valid_identifier(&name)
264+
!clang::is_valid_identifier(&name)
265265
}
266266
_ => false,
267267
}
268268
}
269269

270-
/// Checks whether the name looks like an identifier,
271-
/// i.e. is alphanumeric (including '_') and does not start with a digit.
272-
pub fn is_valid_identifier(name: &str) -> bool {
273-
clang::is_valid_identifier(name)
274-
}
275-
276270
/// Takes `name`, and returns a suitable identifier representation for it.
277-
pub fn sanitize_name<'a>(name: &'a str) -> Cow<'a, str> {
278-
if Self::is_valid_identifier(name) {
271+
fn sanitize_name<'a>(name: &'a str) -> Cow<'a, str> {
272+
if clang::is_valid_identifier(name) {
279273
return Cow::Borrowed(name)
280274
}
281275

282276
let name = name.replace(|c| c == ' ' || c == ':' || c == '.' , "_");
283277
Cow::Owned(name)
284278
}
285279

280+
/// Get this type's santizied name.
281+
pub fn sanitized_name<'a>(&'a self, ctx: &BindgenContext) -> Option<Cow<'a, str>> {
282+
let name_info = match *self.kind() {
283+
TypeKind::Pointer(inner) => Some((inner, Cow::Borrowed("ptr"))),
284+
TypeKind::Reference(inner) => Some((inner, Cow::Borrowed("ref"))),
285+
TypeKind::Array(inner, length) => {
286+
Some((inner, format!("array{}", length).into()))
287+
}
288+
_ => None,
289+
};
290+
if let Some((inner, prefix)) = name_info {
291+
ctx.resolve_item(inner)
292+
.expect_type().sanitized_name(ctx)
293+
.map(|name| format!("{}_{}", prefix, name).into())
294+
} else {
295+
self.name().map(Self::sanitize_name)
296+
}
297+
}
298+
286299
/// See safe_canonical_type.
287300
pub fn canonical_type<'tr>(&'tr self,
288301
ctx: &'tr BindgenContext)
@@ -1093,7 +1106,7 @@ impl Type {
10931106

10941107
if name.is_empty() {
10951108
let pretty_name = ty.spelling();
1096-
if Self::is_valid_identifier(&pretty_name) {
1109+
if clang::is_valid_identifier(&pretty_name) {
10971110
name = pretty_name;
10981111
}
10991112
}
@@ -1111,7 +1124,7 @@ impl Type {
11111124
// The pretty-printed name may contain typedefed name,
11121125
// but may also be "struct (anonymous at .h:1)"
11131126
let pretty_name = ty.spelling();
1114-
if Self::is_valid_identifier(&pretty_name) {
1127+
if clang::is_valid_identifier(&pretty_name) {
11151128
name = pretty_name;
11161129
}
11171130
}

tests/expectations/tests/template.rs

Lines changed: 126 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,22 @@ pub struct C {
3939
pub mB: B<::std::os::raw::c_uint>,
4040
pub mBConstPtr: B<*const ::std::os::raw::c_int>,
4141
pub mBConstStructPtr: B<*const mozilla_Foo>,
42+
pub mBConstStructPtrArray: B<[*const mozilla_Foo; 1usize]>,
4243
pub mBConst: B<::std::os::raw::c_int>,
4344
pub mBVolatile: B<::std::os::raw::c_int>,
4445
pub mBConstBool: B<bool>,
4546
pub mBConstChar: B<u16>,
4647
pub mBArray: B<[::std::os::raw::c_int; 1usize]>,
48+
pub mBPtrArray: B<[*mut ::std::os::raw::c_int; 1usize]>,
49+
pub mBArrayPtr: B<*mut [::std::os::raw::c_int; 1usize]>,
50+
pub mBRef: B<*mut ::std::os::raw::c_int>,
51+
pub mBConstRef: B<*const ::std::os::raw::c_int>,
52+
pub mPtrRef: B<*mut *mut ::std::os::raw::c_int>,
53+
pub mArrayRef: B<*mut [::std::os::raw::c_int; 1usize]>,
4754
}
4855
#[test]
4956
fn bindgen_test_layout_C() {
50-
assert_eq!(::std::mem::size_of::<C>() , 40usize , concat ! (
57+
assert_eq!(::std::mem::size_of::<C>() , 96usize , concat ! (
5158
"Size of: " , stringify ! ( C ) ));
5259
assert_eq! (::std::mem::align_of::<C>() , 8usize , concat ! (
5360
"Alignment of " , stringify ! ( C ) ));
@@ -65,31 +72,66 @@ fn bindgen_test_layout_C() {
6572
usize } , 16usize , concat ! (
6673
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
6774
! ( mBConstStructPtr ) ));
75+
assert_eq! (unsafe {
76+
& ( * ( 0 as * const C ) ) . mBConstStructPtrArray as * const
77+
_ as usize } , 24usize , concat ! (
78+
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
79+
! ( mBConstStructPtrArray ) ));
6880
assert_eq! (unsafe {
6981
& ( * ( 0 as * const C ) ) . mBConst as * const _ as usize } ,
70-
24usize , concat ! (
82+
32usize , concat ! (
7183
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
7284
! ( mBConst ) ));
7385
assert_eq! (unsafe {
7486
& ( * ( 0 as * const C ) ) . mBVolatile as * const _ as usize
75-
} , 28usize , concat ! (
87+
} , 36usize , concat ! (
7688
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
7789
! ( mBVolatile ) ));
7890
assert_eq! (unsafe {
7991
& ( * ( 0 as * const C ) ) . mBConstBool as * const _ as usize
80-
} , 32usize , concat ! (
92+
} , 40usize , concat ! (
8193
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
8294
! ( mBConstBool ) ));
8395
assert_eq! (unsafe {
8496
& ( * ( 0 as * const C ) ) . mBConstChar as * const _ as usize
85-
} , 34usize , concat ! (
97+
} , 42usize , concat ! (
8698
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
8799
! ( mBConstChar ) ));
88100
assert_eq! (unsafe {
89101
& ( * ( 0 as * const C ) ) . mBArray as * const _ as usize } ,
90-
36usize , concat ! (
102+
44usize , concat ! (
91103
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
92104
! ( mBArray ) ));
105+
assert_eq! (unsafe {
106+
& ( * ( 0 as * const C ) ) . mBPtrArray as * const _ as usize
107+
} , 48usize , concat ! (
108+
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
109+
! ( mBPtrArray ) ));
110+
assert_eq! (unsafe {
111+
& ( * ( 0 as * const C ) ) . mBArrayPtr as * const _ as usize
112+
} , 56usize , concat ! (
113+
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
114+
! ( mBArrayPtr ) ));
115+
assert_eq! (unsafe {
116+
& ( * ( 0 as * const C ) ) . mBRef as * const _ as usize } ,
117+
64usize , concat ! (
118+
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
119+
! ( mBRef ) ));
120+
assert_eq! (unsafe {
121+
& ( * ( 0 as * const C ) ) . mBConstRef as * const _ as usize
122+
} , 72usize , concat ! (
123+
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
124+
! ( mBConstRef ) ));
125+
assert_eq! (unsafe {
126+
& ( * ( 0 as * const C ) ) . mPtrRef as * const _ as usize } ,
127+
80usize , concat ! (
128+
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
129+
! ( mPtrRef ) ));
130+
assert_eq! (unsafe {
131+
& ( * ( 0 as * const C ) ) . mArrayRef as * const _ as usize }
132+
, 88usize , concat ! (
133+
"Alignment of field: " , stringify ! ( C ) , "::" , stringify
134+
! ( mArrayRef ) ));
93135
}
94136
impl Clone for C {
95137
fn clone(&self) -> Self { *self }
@@ -353,6 +395,17 @@ fn __bindgen_test_layout_B_open0_ptr_const_mozilla__Foo_close0_instantiation() {
353395
B<*const mozilla_Foo> ) ));
354396
}
355397
#[test]
398+
fn __bindgen_test_layout_B_open0_array1_ptr_const_mozilla__Foo_close0_instantiation() {
399+
assert_eq!(::std::mem::size_of::<B<[*const mozilla_Foo; 1usize]>>() ,
400+
8usize , concat ! (
401+
"Size of template specialization: " , stringify ! (
402+
B<[*const mozilla_Foo; 1usize]> ) ));
403+
assert_eq!(::std::mem::align_of::<B<[*const mozilla_Foo; 1usize]>>() ,
404+
8usize , concat ! (
405+
"Alignment of template specialization: " , stringify ! (
406+
B<[*const mozilla_Foo; 1usize]> ) ));
407+
}
408+
#[test]
356409
fn __bindgen_test_layout_B_open0_const_int_close0_instantiation() {
357410
assert_eq!(::std::mem::size_of::<B<::std::os::raw::c_int>>() , 4usize ,
358411
concat ! (
@@ -393,7 +446,7 @@ fn __bindgen_test_layout_B_open0_const_char16_t_close0_instantiation() {
393446
) ));
394447
}
395448
#[test]
396-
fn __bindgen_test_layout_B_open0_array_int_1_close0_instantiation() {
449+
fn __bindgen_test_layout_B_open0_array1_int_close0_instantiation() {
397450
assert_eq!(::std::mem::size_of::<B<[::std::os::raw::c_int; 1usize]>>() ,
398451
4usize , concat ! (
399452
"Size of template specialization: " , stringify ! (
@@ -404,6 +457,72 @@ fn __bindgen_test_layout_B_open0_array_int_1_close0_instantiation() {
404457
B<[::std::os::raw::c_int; 1usize]> ) ));
405458
}
406459
#[test]
460+
fn __bindgen_test_layout_B_open0_array1_ptr_int_close0_instantiation() {
461+
assert_eq!(::std::mem::size_of::<B<[*mut ::std::os::raw::c_int; 1usize]>>()
462+
, 8usize , concat ! (
463+
"Size of template specialization: " , stringify ! (
464+
B<[*mut ::std::os::raw::c_int; 1usize]> ) ));
465+
assert_eq!(::std::mem::align_of::<B<[*mut ::std::os::raw::c_int; 1usize]>>()
466+
, 8usize , concat ! (
467+
"Alignment of template specialization: " , stringify ! (
468+
B<[*mut ::std::os::raw::c_int; 1usize]> ) ));
469+
}
470+
#[test]
471+
fn __bindgen_test_layout_B_open0_ptr_array1_int_close0_instantiation() {
472+
assert_eq!(::std::mem::size_of::<B<*mut [::std::os::raw::c_int; 1usize]>>()
473+
, 8usize , concat ! (
474+
"Size of template specialization: " , stringify ! (
475+
B<*mut [::std::os::raw::c_int; 1usize]> ) ));
476+
assert_eq!(::std::mem::align_of::<B<*mut [::std::os::raw::c_int; 1usize]>>()
477+
, 8usize , concat ! (
478+
"Alignment of template specialization: " , stringify ! (
479+
B<*mut [::std::os::raw::c_int; 1usize]> ) ));
480+
}
481+
#[test]
482+
fn __bindgen_test_layout_B_open0_ref_int_close0_instantiation() {
483+
assert_eq!(::std::mem::size_of::<B<*mut ::std::os::raw::c_int>>() , 8usize
484+
, concat ! (
485+
"Size of template specialization: " , stringify ! (
486+
B<*mut ::std::os::raw::c_int> ) ));
487+
assert_eq!(::std::mem::align_of::<B<*mut ::std::os::raw::c_int>>() ,
488+
8usize , concat ! (
489+
"Alignment of template specialization: " , stringify ! (
490+
B<*mut ::std::os::raw::c_int> ) ));
491+
}
492+
#[test]
493+
fn __bindgen_test_layout_B_open0_ref_const_int_close0_instantiation() {
494+
assert_eq!(::std::mem::size_of::<B<*const ::std::os::raw::c_int>>() ,
495+
8usize , concat ! (
496+
"Size of template specialization: " , stringify ! (
497+
B<*const ::std::os::raw::c_int> ) ));
498+
assert_eq!(::std::mem::align_of::<B<*const ::std::os::raw::c_int>>() ,
499+
8usize , concat ! (
500+
"Alignment of template specialization: " , stringify ! (
501+
B<*const ::std::os::raw::c_int> ) ));
502+
}
503+
#[test]
504+
fn __bindgen_test_layout_B_open0_ref_ptr_int_close0_instantiation() {
505+
assert_eq!(::std::mem::size_of::<B<*mut *mut ::std::os::raw::c_int>>() ,
506+
8usize , concat ! (
507+
"Size of template specialization: " , stringify ! (
508+
B<*mut *mut ::std::os::raw::c_int> ) ));
509+
assert_eq!(::std::mem::align_of::<B<*mut *mut ::std::os::raw::c_int>>() ,
510+
8usize , concat ! (
511+
"Alignment of template specialization: " , stringify ! (
512+
B<*mut *mut ::std::os::raw::c_int> ) ));
513+
}
514+
#[test]
515+
fn __bindgen_test_layout_B_open0_ref_array1_int_close0_instantiation() {
516+
assert_eq!(::std::mem::size_of::<B<*mut [::std::os::raw::c_int; 1usize]>>()
517+
, 8usize , concat ! (
518+
"Size of template specialization: " , stringify ! (
519+
B<*mut [::std::os::raw::c_int; 1usize]> ) ));
520+
assert_eq!(::std::mem::align_of::<B<*mut [::std::os::raw::c_int; 1usize]>>()
521+
, 8usize , concat ! (
522+
"Alignment of template specialization: " , stringify ! (
523+
B<*mut [::std::os::raw::c_int; 1usize]> ) ));
524+
}
525+
#[test]
407526
fn __bindgen_test_layout_Foo_open0_int_int_close0_instantiation_1() {
408527
assert_eq!(::std::mem::size_of::<Foo<::std::os::raw::c_int>>() , 24usize ,
409528
concat ! (

tests/headers/template.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,18 @@ struct C {
2020
B<unsigned int> mB;
2121
B<const int*> mBConstPtr;
2222
B<const mozilla::Foo*> mBConstStructPtr;
23+
B<const mozilla::Foo*[1]> mBConstStructPtrArray;
2324
B<const int> mBConst;
2425
B<volatile int> mBVolatile;
2526
B<const bool> mBConstBool;
2627
B<const char16_t> mBConstChar;
2728
B<int[1]> mBArray;
29+
B<int*[1]> mBPtrArray;
30+
B<int(*)[1]> mBArrayPtr;
31+
B<int&> mBRef;
32+
B<const int&> mBConstRef;
33+
B<int*&> mPtrRef;
34+
B<int(&)[1]> mArrayRef;
2835
// clang 3.x ignores const in this case, so they generate different
2936
// result than clang 4.0.
3037
// B<const int[1]> mBConstArray;

0 commit comments

Comments
 (0)