Skip to content

Commit ea39b98

Browse files
committed
ir: Parse cursor definitions in unions as children of the union.
1 parent 0063bf3 commit ea39b98

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

src/ir/comp.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,13 @@ impl CompInfo {
630630
// anonymous, let's just assume that if the cursor we've
631631
// found is a definition it's a valid inner type.
632632
//
633+
// Note that doing this could be always ok, but let's just
634+
// keep the union check for now.
635+
//
633636
// https://github.com/servo/rust-bindgen/issues/482
634637
let is_inner_struct = cur.semantic_parent() == cursor ||
635-
cur.is_definition();
638+
(kind == CompKind::Union &&
639+
cur.is_definition());
636640
if !is_inner_struct {
637641
return CXChildVisit_Continue;
638642
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
7+
#[repr(C)]
8+
pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>);
9+
impl <T> __BindgenUnionField<T> {
10+
#[inline]
11+
pub fn new() -> Self { __BindgenUnionField(::std::marker::PhantomData) }
12+
#[inline]
13+
pub unsafe fn as_ref(&self) -> &T { ::std::mem::transmute(self) }
14+
#[inline]
15+
pub unsafe fn as_mut(&mut self) -> &mut T { ::std::mem::transmute(self) }
16+
}
17+
impl <T> ::std::default::Default for __BindgenUnionField<T> {
18+
#[inline]
19+
fn default() -> Self { Self::new() }
20+
}
21+
impl <T> ::std::clone::Clone for __BindgenUnionField<T> {
22+
#[inline]
23+
fn clone(&self) -> Self { Self::new() }
24+
}
25+
impl <T> ::std::marker::Copy for __BindgenUnionField<T> { }
26+
impl <T> ::std::fmt::Debug for __BindgenUnionField<T> {
27+
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
28+
fmt.write_str("__BindgenUnionField")
29+
}
30+
}
31+
#[repr(C)]
32+
#[derive(Debug, Default, Copy)]
33+
pub struct s {
34+
pub u: s__bindgen_ty_1,
35+
}
36+
#[repr(C)]
37+
#[derive(Debug, Default, Copy)]
38+
pub struct s__bindgen_ty_1 {
39+
pub field: __BindgenUnionField<s__bindgen_ty_1_inner>,
40+
pub bindgen_union_field: u32,
41+
}
42+
#[repr(C)]
43+
#[derive(Debug, Default, Copy)]
44+
pub struct s__bindgen_ty_1_inner {
45+
pub b: ::std::os::raw::c_int,
46+
}
47+
#[test]
48+
fn bindgen_test_layout_s__bindgen_ty_1_inner() {
49+
assert_eq!(::std::mem::size_of::<s__bindgen_ty_1_inner>() , 4usize ,
50+
concat ! ( "Size of: " , stringify ! ( s__bindgen_ty_1_inner )
51+
));
52+
assert_eq! (::std::mem::align_of::<s__bindgen_ty_1_inner>() , 4usize ,
53+
concat ! (
54+
"Alignment of " , stringify ! ( s__bindgen_ty_1_inner ) ));
55+
assert_eq! (unsafe {
56+
& ( * ( 0 as * const s__bindgen_ty_1_inner ) ) . b as * const
57+
_ as usize } , 0usize , concat ! (
58+
"Alignment of field: " , stringify ! ( s__bindgen_ty_1_inner )
59+
, "::" , stringify ! ( b ) ));
60+
}
61+
impl Clone for s__bindgen_ty_1_inner {
62+
fn clone(&self) -> Self { *self }
63+
}
64+
#[test]
65+
fn bindgen_test_layout_s__bindgen_ty_1() {
66+
assert_eq!(::std::mem::size_of::<s__bindgen_ty_1>() , 4usize , concat ! (
67+
"Size of: " , stringify ! ( s__bindgen_ty_1 ) ));
68+
assert_eq! (::std::mem::align_of::<s__bindgen_ty_1>() , 4usize , concat !
69+
( "Alignment of " , stringify ! ( s__bindgen_ty_1 ) ));
70+
assert_eq! (unsafe {
71+
& ( * ( 0 as * const s__bindgen_ty_1 ) ) . field as * const _
72+
as usize } , 0usize , concat ! (
73+
"Alignment of field: " , stringify ! ( s__bindgen_ty_1 ) ,
74+
"::" , stringify ! ( field ) ));
75+
}
76+
impl Clone for s__bindgen_ty_1 {
77+
fn clone(&self) -> Self { *self }
78+
}
79+
#[test]
80+
fn bindgen_test_layout_s() {
81+
assert_eq!(::std::mem::size_of::<s>() , 4usize , concat ! (
82+
"Size of: " , stringify ! ( s ) ));
83+
assert_eq! (::std::mem::align_of::<s>() , 4usize , concat ! (
84+
"Alignment of " , stringify ! ( s ) ));
85+
assert_eq! (unsafe { & ( * ( 0 as * const s ) ) . u as * const _ as usize
86+
} , 0usize , concat ! (
87+
"Alignment of field: " , stringify ! ( s ) , "::" , stringify
88+
! ( u ) ));
89+
}
90+
impl Clone for s {
91+
fn clone(&self) -> Self { *self }
92+
}

tests/headers/anon_struct_in_union.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct s {
2+
union {
3+
struct inner {
4+
int b;
5+
} field;
6+
} u;
7+
};

0 commit comments

Comments
 (0)