Skip to content

Commit 6774efe

Browse files
author
bors-servo
authored
Auto merge of #518 - pornel:typedefstruct, r=emilio
typedef struct {} name Fixes #427 It looks like clang is doing the hard work of getting the right name from the typedef, but it falls back to arbitrary pretty-printed descriptions if it can't find a typedef. I couldn't find an API to check whether the name comes from a typedef, so I just filter out non-ident-like spellings.
2 parents ac49717 + 70c61e1 commit 6774efe

12 files changed

+263
-74
lines changed

src/ir/ty.rs

+28-9
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,21 @@ impl Type {
311311
match self.kind {
312312
TypeKind::Named => {
313313
let name = self.name().expect("Unnamed named type?");
314-
let mut chars = name.chars();
315-
let first = chars.next().unwrap();
316-
let mut remaining = chars;
317-
318-
let valid = (first.is_alphabetic() || first == '_') &&
319-
remaining.all(|c| c.is_alphanumeric() || c == '_');
320-
321-
!valid
314+
!Self::is_valid_identifier(&name)
322315
}
323316
_ => false,
324317
}
325318
}
326319

320+
/// Checks whether the name looks like an identifier,
321+
/// i.e. is alphanumeric (including '_') and does not start with a digit.
322+
pub fn is_valid_identifier(name: &str) -> bool {
323+
let mut chars = name.chars();
324+
let first_valid = chars.next().map(|c| c.is_alphabetic() || c == '_').unwrap_or(false);
325+
326+
first_valid && chars.all(|c| c.is_alphanumeric() || c == '_')
327+
}
328+
327329
/// See safe_canonical_type.
328330
pub fn canonical_type<'tr>(&'tr self,
329331
ctx: &'tr BindgenContext)
@@ -454,7 +456,6 @@ fn is_invalid_named_type_unnamed() {
454456
}
455457

456458
#[test]
457-
#[should_panic]
458459
fn is_invalid_named_type_empty_name() {
459460
let ty = Type::new(Some("".into()), None, TypeKind::Named, false);
460461
assert!(ty.is_invalid_named_type())
@@ -1074,12 +1075,30 @@ impl Type {
10741075
}
10751076
CXType_Enum => {
10761077
let enum_ = Enum::from_ty(ty, ctx).expect("Not an enum?");
1078+
1079+
if name.is_empty() {
1080+
let pretty_name = ty.spelling();
1081+
if Self::is_valid_identifier(&pretty_name) {
1082+
name = pretty_name;
1083+
}
1084+
}
1085+
10771086
TypeKind::Enum(enum_)
10781087
}
10791088
CXType_Record => {
10801089
let complex =
10811090
CompInfo::from_ty(potential_id, ty, location, ctx)
10821091
.expect("Not a complex type?");
1092+
1093+
if name.is_empty() {
1094+
// The pretty-printed name may contain typedefed name,
1095+
// but may also be "struct (anonymous at .h:1)"
1096+
let pretty_name = ty.spelling();
1097+
if Self::is_valid_identifier(&pretty_name) {
1098+
name = pretty_name;
1099+
}
1100+
}
1101+
10831102
TypeKind::Comp(complex)
10841103
}
10851104
// FIXME: We stub vectors as arrays since in 99% of the cases the

tests/expectations/tests/anon_enum.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ fn bindgen_test_layout_Test() {
3434
impl Clone for Test {
3535
fn clone(&self) -> Self { *self }
3636
}
37-
pub const Foo: _bindgen_ty_1 = _bindgen_ty_1::Foo;
38-
pub const Bar: _bindgen_ty_1 = _bindgen_ty_1::Bar;
3937
#[repr(u32)]
4038
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
41-
pub enum _bindgen_ty_1 { Foo = 0, Bar = 1, }
42-
pub use self::_bindgen_ty_1 as Baz;
39+
pub enum Baz { Foo = 0, Bar = 1, }

tests/expectations/tests/bitfield_method_mangling.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@
66

77
#[repr(C)]
88
#[derive(Debug, Default, Copy)]
9-
pub struct _bindgen_ty_1 {
9+
pub struct mach_msg_type_descriptor_t {
1010
pub _bitfield_1: u32,
1111
}
1212
#[test]
13-
fn bindgen_test_layout__bindgen_ty_1() {
14-
assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 4usize , concat ! (
15-
"Size of: " , stringify ! ( _bindgen_ty_1 ) ));
16-
assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 4usize , concat ! (
17-
"Alignment of " , stringify ! ( _bindgen_ty_1 ) ));
13+
fn bindgen_test_layout_mach_msg_type_descriptor_t() {
14+
assert_eq!(::std::mem::size_of::<mach_msg_type_descriptor_t>() , 4usize ,
15+
concat ! (
16+
"Size of: " , stringify ! ( mach_msg_type_descriptor_t ) ));
17+
assert_eq! (::std::mem::align_of::<mach_msg_type_descriptor_t>() , 4usize
18+
, concat ! (
19+
"Alignment of " , stringify ! ( mach_msg_type_descriptor_t )
20+
));
1821
}
19-
impl Clone for _bindgen_ty_1 {
22+
impl Clone for mach_msg_type_descriptor_t {
2023
fn clone(&self) -> Self { *self }
2124
}
22-
impl _bindgen_ty_1 {
25+
impl mach_msg_type_descriptor_t {
2326
#[inline]
2427
pub fn pad3(&self) -> ::std::os::raw::c_uint {
2528
unsafe {
@@ -48,4 +51,3 @@ impl _bindgen_ty_1 {
4851
((val as u32 as u32) << 24u32) & (4278190080usize as u32);
4952
}
5053
}
51-
pub type mach_msg_type_descriptor_t = _bindgen_ty_1;

tests/expectations/tests/issue-410.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,5 @@ pub mod root {
3838
}
3939
}
4040
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
41-
pub enum _bindgen_ty_1 { }
42-
pub use self::super::root::_bindgen_ty_1 as JSWhyMagic;
41+
pub enum JSWhyMagic { }
4342
}

tests/expectations/tests/layout_array.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -117,26 +117,25 @@ impl Default for rte_mempool_ops {
117117
*/
118118
#[repr(C)]
119119
#[derive(Debug, Default, Copy)]
120-
pub struct _bindgen_ty_1 {
120+
pub struct rte_spinlock_t {
121121
/**< lock status 0 = unlocked, 1 = locked */
122122
pub locked: ::std::os::raw::c_int,
123123
}
124124
#[test]
125-
fn bindgen_test_layout__bindgen_ty_1() {
126-
assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 4usize , concat ! (
127-
"Size of: " , stringify ! ( _bindgen_ty_1 ) ));
128-
assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 4usize , concat ! (
129-
"Alignment of " , stringify ! ( _bindgen_ty_1 ) ));
125+
fn bindgen_test_layout_rte_spinlock_t() {
126+
assert_eq!(::std::mem::size_of::<rte_spinlock_t>() , 4usize , concat ! (
127+
"Size of: " , stringify ! ( rte_spinlock_t ) ));
128+
assert_eq! (::std::mem::align_of::<rte_spinlock_t>() , 4usize , concat ! (
129+
"Alignment of " , stringify ! ( rte_spinlock_t ) ));
130130
assert_eq! (unsafe {
131-
& ( * ( 0 as * const _bindgen_ty_1 ) ) . locked as * const _
131+
& ( * ( 0 as * const rte_spinlock_t ) ) . locked as * const _
132132
as usize } , 0usize , concat ! (
133-
"Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::"
133+
"Alignment of field: " , stringify ! ( rte_spinlock_t ) , "::"
134134
, stringify ! ( locked ) ));
135135
}
136-
impl Clone for _bindgen_ty_1 {
136+
impl Clone for rte_spinlock_t {
137137
fn clone(&self) -> Self { *self }
138138
}
139-
pub type rte_spinlock_t = _bindgen_ty_1;
140139
/**
141140
* Structure storing the table of registered ops structs, each of which contain
142141
* the function pointers for the mempool ops functions.

tests/expectations/tests/layout_mbuf.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,25 @@ pub type MARKER64 = [u64; 0usize];
3939
*/
4040
#[repr(C)]
4141
#[derive(Debug, Default, Copy)]
42-
pub struct _bindgen_ty_1 {
42+
pub struct rte_atomic16_t {
4343
/**< An internal counter value. */
4444
pub cnt: i16,
4545
}
4646
#[test]
47-
fn bindgen_test_layout__bindgen_ty_1() {
48-
assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 2usize , concat ! (
49-
"Size of: " , stringify ! ( _bindgen_ty_1 ) ));
50-
assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 2usize , concat ! (
51-
"Alignment of " , stringify ! ( _bindgen_ty_1 ) ));
47+
fn bindgen_test_layout_rte_atomic16_t() {
48+
assert_eq!(::std::mem::size_of::<rte_atomic16_t>() , 2usize , concat ! (
49+
"Size of: " , stringify ! ( rte_atomic16_t ) ));
50+
assert_eq! (::std::mem::align_of::<rte_atomic16_t>() , 2usize , concat ! (
51+
"Alignment of " , stringify ! ( rte_atomic16_t ) ));
5252
assert_eq! (unsafe {
53-
& ( * ( 0 as * const _bindgen_ty_1 ) ) . cnt as * const _ as
53+
& ( * ( 0 as * const rte_atomic16_t ) ) . cnt as * const _ as
5454
usize } , 0usize , concat ! (
55-
"Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::"
55+
"Alignment of field: " , stringify ! ( rte_atomic16_t ) , "::"
5656
, stringify ! ( cnt ) ));
5757
}
58-
impl Clone for _bindgen_ty_1 {
58+
impl Clone for rte_atomic16_t {
5959
fn clone(&self) -> Self { *self }
6060
}
61-
pub type rte_atomic16_t = _bindgen_ty_1;
6261
/**
6362
* The generic rte_mbuf, containing a packet mbuf.
6463
*/
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
7+
#[repr(C)]
8+
#[derive(Debug, Default, Copy)]
9+
pub struct typedef_named_struct {
10+
pub has_name: bool,
11+
}
12+
#[test]
13+
fn bindgen_test_layout_typedef_named_struct() {
14+
assert_eq!(::std::mem::size_of::<typedef_named_struct>() , 1usize , concat
15+
! ( "Size of: " , stringify ! ( typedef_named_struct ) ));
16+
assert_eq! (::std::mem::align_of::<typedef_named_struct>() , 1usize ,
17+
concat ! (
18+
"Alignment of " , stringify ! ( typedef_named_struct ) ));
19+
assert_eq! (unsafe {
20+
& ( * ( 0 as * const typedef_named_struct ) ) . has_name as *
21+
const _ as usize } , 0usize , concat ! (
22+
"Alignment of field: " , stringify ! ( typedef_named_struct )
23+
, "::" , stringify ! ( has_name ) ));
24+
}
25+
impl Clone for typedef_named_struct {
26+
fn clone(&self) -> Self { *self }
27+
}
28+
#[repr(C)]
29+
#[derive(Debug, Copy)]
30+
pub struct _bindgen_ty_1 {
31+
pub no_name: *mut ::std::os::raw::c_void,
32+
}
33+
#[test]
34+
fn bindgen_test_layout__bindgen_ty_1() {
35+
assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 8usize , concat ! (
36+
"Size of: " , stringify ! ( _bindgen_ty_1 ) ));
37+
assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 8usize , concat ! (
38+
"Alignment of " , stringify ! ( _bindgen_ty_1 ) ));
39+
assert_eq! (unsafe {
40+
& ( * ( 0 as * const _bindgen_ty_1 ) ) . no_name as * const _
41+
as usize } , 0usize , concat ! (
42+
"Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::"
43+
, stringify ! ( no_name ) ));
44+
}
45+
impl Clone for _bindgen_ty_1 {
46+
fn clone(&self) -> Self { *self }
47+
}
48+
impl Default for _bindgen_ty_1 {
49+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
50+
}
51+
pub type struct_ptr_t = *mut _bindgen_ty_1;
52+
pub type struct_ptr_ptr_t = *mut *mut _bindgen_ty_1;
53+
#[repr(u32)]
54+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
55+
pub enum typedef_named_enum { ENUM_HAS_NAME = 1, }
56+
pub const ENUM_IS_ANON: _bindgen_ty_2 = _bindgen_ty_2::ENUM_IS_ANON;
57+
#[repr(u32)]
58+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
59+
pub enum _bindgen_ty_2 { ENUM_IS_ANON = 0, }
60+
pub type enum_ptr_t = *mut _bindgen_ty_2;
61+
pub type enum_ptr_ptr_t = *mut *mut _bindgen_ty_2;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
7+
pub mod root {
8+
#[allow(unused_imports)]
9+
use self::super::root;
10+
pub mod whatever {
11+
#[allow(unused_imports)]
12+
use self::super::super::root;
13+
#[repr(C)]
14+
#[derive(Debug, Default, Copy)]
15+
pub struct _bindgen_ty_1 {
16+
pub foo: ::std::os::raw::c_int,
17+
}
18+
#[test]
19+
fn bindgen_test_layout__bindgen_ty_1() {
20+
assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 4usize ,
21+
concat ! ( "Size of: " , stringify ! ( _bindgen_ty_1 )
22+
));
23+
assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 4usize ,
24+
concat ! (
25+
"Alignment of " , stringify ! ( _bindgen_ty_1 ) ));
26+
assert_eq! (unsafe {
27+
& ( * ( 0 as * const _bindgen_ty_1 ) ) . foo as *
28+
const _ as usize } , 0usize , concat ! (
29+
"Alignment of field: " , stringify ! ( _bindgen_ty_1 )
30+
, "::" , stringify ! ( foo ) ));
31+
}
32+
impl Clone for _bindgen_ty_1 {
33+
fn clone(&self) -> Self { *self }
34+
}
35+
pub type typedef_struct = root::whatever::_bindgen_ty_1;
36+
pub const whatever_BAR: root::whatever::_bindgen_ty_2 =
37+
_bindgen_ty_2::BAR;
38+
#[repr(u32)]
39+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
40+
pub enum _bindgen_ty_2 { BAR = 1, }
41+
pub use self::super::super::root::whatever::_bindgen_ty_2 as
42+
typedef_enum;
43+
}
44+
pub mod _bindgen_mod_id_12 {
45+
#[allow(unused_imports)]
46+
use self::super::super::root;
47+
#[repr(C)]
48+
#[derive(Debug, Default, Copy)]
49+
pub struct _bindgen_ty_1 {
50+
pub foo: ::std::os::raw::c_int,
51+
}
52+
#[test]
53+
fn bindgen_test_layout__bindgen_ty_1() {
54+
assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 4usize ,
55+
concat ! ( "Size of: " , stringify ! ( _bindgen_ty_1 )
56+
));
57+
assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 4usize ,
58+
concat ! (
59+
"Alignment of " , stringify ! ( _bindgen_ty_1 ) ));
60+
assert_eq! (unsafe {
61+
& ( * ( 0 as * const _bindgen_ty_1 ) ) . foo as *
62+
const _ as usize } , 0usize , concat ! (
63+
"Alignment of field: " , stringify ! ( _bindgen_ty_1 )
64+
, "::" , stringify ! ( foo ) ));
65+
}
66+
impl Clone for _bindgen_ty_1 {
67+
fn clone(&self) -> Self { *self }
68+
}
69+
pub type typedef_struct = root::_bindgen_mod_id_12::_bindgen_ty_1;
70+
pub const _bindgen_mod_id_12_BAR:
71+
root::_bindgen_mod_id_12::_bindgen_ty_2 =
72+
_bindgen_ty_2::BAR;
73+
#[repr(u32)]
74+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
75+
pub enum _bindgen_ty_2 { BAR = 1, }
76+
pub use self::super::super::root::_bindgen_mod_id_12::_bindgen_ty_2 as
77+
typedef_enum;
78+
}
79+
}

tests/expectations/tests/union_fields.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,34 @@ impl <T> ::std::fmt::Debug for __BindgenUnionField<T> {
3030
}
3131
#[repr(C)]
3232
#[derive(Debug, Default, Copy)]
33-
pub struct _bindgen_ty_1 {
33+
pub struct nsStyleUnion {
3434
pub mInt: __BindgenUnionField<::std::os::raw::c_int>,
3535
pub mFloat: __BindgenUnionField<f32>,
3636
pub mPointer: __BindgenUnionField<*mut ::std::os::raw::c_void>,
3737
pub bindgen_union_field: u64,
3838
}
3939
#[test]
40-
fn bindgen_test_layout__bindgen_ty_1() {
41-
assert_eq!(::std::mem::size_of::<_bindgen_ty_1>() , 8usize , concat ! (
42-
"Size of: " , stringify ! ( _bindgen_ty_1 ) ));
43-
assert_eq! (::std::mem::align_of::<_bindgen_ty_1>() , 8usize , concat ! (
44-
"Alignment of " , stringify ! ( _bindgen_ty_1 ) ));
40+
fn bindgen_test_layout_nsStyleUnion() {
41+
assert_eq!(::std::mem::size_of::<nsStyleUnion>() , 8usize , concat ! (
42+
"Size of: " , stringify ! ( nsStyleUnion ) ));
43+
assert_eq! (::std::mem::align_of::<nsStyleUnion>() , 8usize , concat ! (
44+
"Alignment of " , stringify ! ( nsStyleUnion ) ));
4545
assert_eq! (unsafe {
46-
& ( * ( 0 as * const _bindgen_ty_1 ) ) . mInt as * const _ as
46+
& ( * ( 0 as * const nsStyleUnion ) ) . mInt as * const _ as
4747
usize } , 0usize , concat ! (
48-
"Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::"
49-
, stringify ! ( mInt ) ));
48+
"Alignment of field: " , stringify ! ( nsStyleUnion ) , "::" ,
49+
stringify ! ( mInt ) ));
5050
assert_eq! (unsafe {
51-
& ( * ( 0 as * const _bindgen_ty_1 ) ) . mFloat as * const _
52-
as usize } , 0usize , concat ! (
53-
"Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::"
54-
, stringify ! ( mFloat ) ));
51+
& ( * ( 0 as * const nsStyleUnion ) ) . mFloat as * const _ as
52+
usize } , 0usize , concat ! (
53+
"Alignment of field: " , stringify ! ( nsStyleUnion ) , "::" ,
54+
stringify ! ( mFloat ) ));
5555
assert_eq! (unsafe {
56-
& ( * ( 0 as * const _bindgen_ty_1 ) ) . mPointer as * const _
56+
& ( * ( 0 as * const nsStyleUnion ) ) . mPointer as * const _
5757
as usize } , 0usize , concat ! (
58-
"Alignment of field: " , stringify ! ( _bindgen_ty_1 ) , "::"
59-
, stringify ! ( mPointer ) ));
58+
"Alignment of field: " , stringify ! ( nsStyleUnion ) , "::" ,
59+
stringify ! ( mPointer ) ));
6060
}
61-
impl Clone for _bindgen_ty_1 {
61+
impl Clone for nsStyleUnion {
6262
fn clone(&self) -> Self { *self }
6363
}
64-
pub type nsStyleUnion = _bindgen_ty_1;

0 commit comments

Comments
 (0)