Skip to content

Commit 9409549

Browse files
author
bors-servo
authored
Auto merge of #486 - emilio:unresolved-nasty-thingie, r=fitzgen
ir: Don't parse non-semantic-children cursor as inner structs. r? @fitzgen
2 parents 0ae42f2 + 331ecab commit 9409549

11 files changed

+174
-108
lines changed

src/clang.rs

-9
Original file line numberDiff line numberDiff line change
@@ -1482,9 +1482,6 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult {
14821482
print_cursor(depth,
14831483
String::from(prefix) + "referenced.",
14841484
&refd);
1485-
print_cursor(depth,
1486-
String::from(prefix) + "referenced.",
1487-
&refd);
14881485
}
14891486
}
14901487

@@ -1494,9 +1491,6 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult {
14941491
print_cursor(depth,
14951492
String::from(prefix) + "canonical.",
14961493
&canonical);
1497-
print_cursor(depth,
1498-
String::from(prefix) + "canonical.",
1499-
&canonical);
15001494
}
15011495

15021496
if let Some(specialized) = c.specialized() {
@@ -1505,9 +1499,6 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult {
15051499
print_cursor(depth,
15061500
String::from(prefix) + "specialized.",
15071501
&specialized);
1508-
print_cursor(depth,
1509-
String::from(prefix) + "specialized.",
1510-
&specialized);
15111502
}
15121503
}
15131504
}

src/codegen/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,7 @@ impl ToRustTy for Type {
21902190
.map(|arg| arg.to_rust_ty(ctx))
21912191
.collect::<Vec<_>>();
21922192

2193-
path.segments.last_mut().unwrap().parameters = if
2193+
path.segments.last_mut().unwrap().parameters = if
21942194
template_args.is_empty() {
21952195
None
21962196
} else {

src/ir/comp.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -669,11 +669,20 @@ impl CompInfo {
669669
CXCursor_UnionDecl |
670670
CXCursor_ClassTemplate |
671671
CXCursor_ClassDecl => {
672+
// We can find non-semantic children here, clang uses a
673+
// StructDecl to note incomplete structs that hasn't been
674+
// forward-declared before, see:
675+
//
676+
// https://github.com/servo/rust-bindgen/issues/482
677+
if cur.semantic_parent() != cursor {
678+
return CXChildVisit_Continue;
679+
}
680+
672681
let inner = Item::parse(cur, Some(potential_id), ctx)
673682
.expect("Inner ClassDecl");
674-
if !ci.inner_types.contains(&inner) {
675-
ci.inner_types.push(inner);
676-
}
683+
684+
ci.inner_types.push(inner);
685+
677686
// A declaration of an union or a struct without name could
678687
// also be an unnamed field, unfortunately.
679688
if cur.spelling().is_empty() &&

src/ir/ty.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1026,9 +1026,7 @@ impl Type {
10261026
CXType_MemberPointer |
10271027
CXType_Pointer => {
10281028
let inner = Item::from_ty_or_ref(ty.pointee_type().unwrap(),
1029-
location,
1030-
parent_id,
1031-
ctx);
1029+
location, None, ctx);
10321030
TypeKind::Pointer(inner)
10331031
}
10341032
CXType_BlockPointer => TypeKind::BlockPointer,
@@ -1038,7 +1036,7 @@ impl Type {
10381036
CXType_LValueReference => {
10391037
let inner = Item::from_ty_or_ref(ty.pointee_type().unwrap(),
10401038
location,
1041-
parent_id,
1039+
None,
10421040
ctx);
10431041
TypeKind::Reference(inner)
10441042
}
@@ -1047,15 +1045,15 @@ impl Type {
10471045
CXType_DependentSizedArray => {
10481046
let inner = Item::from_ty(ty.elem_type().as_ref().unwrap(),
10491047
location,
1050-
parent_id,
1048+
None,
10511049
ctx)
10521050
.expect("Not able to resolve array element?");
10531051
TypeKind::Pointer(inner)
10541052
}
10551053
CXType_IncompleteArray => {
10561054
let inner = Item::from_ty(ty.elem_type().as_ref().unwrap(),
10571055
location,
1058-
parent_id,
1056+
None,
10591057
ctx)
10601058
.expect("Not able to resolve array element?");
10611059
TypeKind::Array(inner, 0)
@@ -1070,7 +1068,7 @@ impl Type {
10701068
CXType_Typedef => {
10711069
let inner = cursor.typedef_type().expect("Not valid Type?");
10721070
let inner =
1073-
Item::from_ty_or_ref(inner, location, parent_id, ctx);
1071+
Item::from_ty_or_ref(inner, location, None, ctx);
10741072
TypeKind::Alias(inner)
10751073
}
10761074
CXType_Enum => {
@@ -1092,7 +1090,7 @@ impl Type {
10921090
CXType_ConstantArray => {
10931091
let inner = Item::from_ty(ty.elem_type().as_ref().unwrap(),
10941092
location,
1095-
parent_id,
1093+
None,
10961094
ctx)
10971095
.expect("Not able to resolve array element?");
10981096
TypeKind::Array(inner, ty.num_elements().unwrap())

tests/expectations/tests/class_nested.rs

+43
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,34 @@ fn bindgen_test_layout_A_B() {
2929
impl Clone for A_B {
3030
fn clone(&self) -> Self { *self }
3131
}
32+
#[repr(C)]
33+
#[derive(Debug, Default, Copy)]
34+
pub struct A_C {
35+
pub baz: ::std::os::raw::c_int,
36+
}
37+
#[test]
38+
fn bindgen_test_layout_A_C() {
39+
assert_eq!(::std::mem::size_of::<A_C>() , 4usize , concat ! (
40+
"Size of: " , stringify ! ( A_C ) ));
41+
assert_eq! (::std::mem::align_of::<A_C>() , 4usize , concat ! (
42+
"Alignment of " , stringify ! ( A_C ) ));
43+
assert_eq! (unsafe {
44+
& ( * ( 0 as * const A_C ) ) . baz as * const _ as usize } ,
45+
0usize , concat ! (
46+
"Alignment of field: " , stringify ! ( A_C ) , "::" ,
47+
stringify ! ( baz ) ));
48+
}
49+
impl Clone for A_C {
50+
fn clone(&self) -> Self { *self }
51+
}
52+
#[repr(C)]
53+
#[derive(Debug, Copy, Clone)]
54+
pub struct A_D<T> {
55+
pub foo: T,
56+
}
57+
impl <T> Default for A_D<T> {
58+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
59+
}
3260
#[test]
3361
fn bindgen_test_layout_A() {
3462
assert_eq!(::std::mem::size_of::<A>() , 4usize , concat ! (
@@ -48,6 +76,21 @@ extern "C" {
4876
#[link_name = "var"]
4977
pub static mut var: A_B;
5078
}
79+
#[test]
80+
fn __bindgen_test_layout_template_1() {
81+
assert_eq!(::std::mem::size_of::<A_D<::std::os::raw::c_int>>() , 4usize ,
82+
concat ! (
83+
"Size of template specialization: " , stringify ! (
84+
A_D<::std::os::raw::c_int> ) ));
85+
assert_eq!(::std::mem::align_of::<A_D<::std::os::raw::c_int>>() , 4usize ,
86+
concat ! (
87+
"Alignment of template specialization: " , stringify ! (
88+
A_D<::std::os::raw::c_int> ) ));
89+
}
90+
extern "C" {
91+
#[link_name = "baz"]
92+
pub static mut baz: A_D<::std::os::raw::c_int>;
93+
}
5194
#[repr(C)]
5295
#[derive(Debug, Default, Copy)]
5396
pub struct D {

tests/expectations/tests/layout_array.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,8 @@ pub struct malloc_heap {
200200
#[repr(C)]
201201
#[derive(Debug, Copy)]
202202
pub struct malloc_heap__bindgen_ty_1 {
203-
pub lh_first: *mut malloc_heap__bindgen_ty_1_malloc_elem,
203+
pub lh_first: *mut malloc_elem,
204204
}
205-
#[repr(C)]
206-
#[derive(Debug, Copy, Clone)]
207-
pub struct malloc_heap__bindgen_ty_1_malloc_elem([u8; 0]);
208205
#[test]
209206
fn bindgen_test_layout_malloc_heap__bindgen_ty_1() {
210207
assert_eq!(::std::mem::size_of::<malloc_heap__bindgen_ty_1>() , 8usize ,
@@ -258,3 +255,11 @@ impl Clone for malloc_heap {
258255
impl Default for malloc_heap {
259256
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
260257
}
258+
#[repr(C)]
259+
#[derive(Debug, Default, Copy)]
260+
pub struct malloc_elem {
261+
pub _address: u8,
262+
}
263+
impl Clone for malloc_elem {
264+
fn clone(&self) -> Self { *self }
265+
}

tests/expectations/tests/layout_cmdline_token.rs

+49-58
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,33 @@
1111
#[repr(C)]
1212
#[derive(Debug, Copy)]
1313
pub struct cmdline_token_hdr {
14-
pub ops: *mut cmdline_token_hdr_cmdline_token_ops,
14+
pub ops: *mut cmdline_token_ops,
1515
pub offset: ::std::os::raw::c_uint,
1616
}
17+
#[test]
18+
fn bindgen_test_layout_cmdline_token_hdr() {
19+
assert_eq!(::std::mem::size_of::<cmdline_token_hdr>() , 16usize , concat !
20+
( "Size of: " , stringify ! ( cmdline_token_hdr ) ));
21+
assert_eq! (::std::mem::align_of::<cmdline_token_hdr>() , 8usize , concat
22+
! ( "Alignment of " , stringify ! ( cmdline_token_hdr ) ));
23+
assert_eq! (unsafe {
24+
& ( * ( 0 as * const cmdline_token_hdr ) ) . ops as * const _
25+
as usize } , 0usize , concat ! (
26+
"Alignment of field: " , stringify ! ( cmdline_token_hdr ) ,
27+
"::" , stringify ! ( ops ) ));
28+
assert_eq! (unsafe {
29+
& ( * ( 0 as * const cmdline_token_hdr ) ) . offset as * const
30+
_ as usize } , 8usize , concat ! (
31+
"Alignment of field: " , stringify ! ( cmdline_token_hdr ) ,
32+
"::" , stringify ! ( offset ) ));
33+
}
34+
impl Clone for cmdline_token_hdr {
35+
fn clone(&self) -> Self { *self }
36+
}
37+
impl Default for cmdline_token_hdr {
38+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
39+
}
40+
pub type cmdline_parse_token_hdr_t = cmdline_token_hdr;
1741
/**
1842
* A token is defined by this structure.
1943
*
@@ -35,7 +59,7 @@ pub struct cmdline_token_hdr {
3559
*/
3660
#[repr(C)]
3761
#[derive(Debug, Copy)]
38-
pub struct cmdline_token_hdr_cmdline_token_ops {
62+
pub struct cmdline_token_ops {
3963
/** parse(token ptr, buf, res pts, buf len) */
4064
pub parse: ::std::option::Option<unsafe extern "C" fn(arg1:
4165
*mut cmdline_parse_token_hdr_t,
@@ -70,71 +94,38 @@ pub struct cmdline_token_hdr_cmdline_token_ops {
7094
-> ::std::os::raw::c_int>,
7195
}
7296
#[test]
73-
fn bindgen_test_layout_cmdline_token_hdr_cmdline_token_ops() {
74-
assert_eq!(::std::mem::size_of::<cmdline_token_hdr_cmdline_token_ops>() ,
75-
32usize , concat ! (
76-
"Size of: " , stringify ! ( cmdline_token_hdr_cmdline_token_ops
77-
) ));
78-
assert_eq! (::std::mem::align_of::<cmdline_token_hdr_cmdline_token_ops>()
79-
, 8usize , concat ! (
80-
"Alignment of " , stringify ! (
81-
cmdline_token_hdr_cmdline_token_ops ) ));
97+
fn bindgen_test_layout_cmdline_token_ops() {
98+
assert_eq!(::std::mem::size_of::<cmdline_token_ops>() , 32usize , concat !
99+
( "Size of: " , stringify ! ( cmdline_token_ops ) ));
100+
assert_eq! (::std::mem::align_of::<cmdline_token_ops>() , 8usize , concat
101+
! ( "Alignment of " , stringify ! ( cmdline_token_ops ) ));
82102
assert_eq! (unsafe {
83-
& ( * ( 0 as * const cmdline_token_hdr_cmdline_token_ops ) ) .
84-
parse as * const _ as usize } , 0usize , concat ! (
85-
"Alignment of field: " , stringify ! (
86-
cmdline_token_hdr_cmdline_token_ops ) , "::" , stringify ! (
87-
parse ) ));
103+
& ( * ( 0 as * const cmdline_token_ops ) ) . parse as * const
104+
_ as usize } , 0usize , concat ! (
105+
"Alignment of field: " , stringify ! ( cmdline_token_ops ) ,
106+
"::" , stringify ! ( parse ) ));
88107
assert_eq! (unsafe {
89-
& ( * ( 0 as * const cmdline_token_hdr_cmdline_token_ops ) ) .
90-
complete_get_nb as * const _ as usize } , 8usize , concat ! (
91-
"Alignment of field: " , stringify ! (
92-
cmdline_token_hdr_cmdline_token_ops ) , "::" , stringify ! (
93-
complete_get_nb ) ));
108+
& ( * ( 0 as * const cmdline_token_ops ) ) . complete_get_nb
109+
as * const _ as usize } , 8usize , concat ! (
110+
"Alignment of field: " , stringify ! ( cmdline_token_ops ) ,
111+
"::" , stringify ! ( complete_get_nb ) ));
94112
assert_eq! (unsafe {
95-
& ( * ( 0 as * const cmdline_token_hdr_cmdline_token_ops ) ) .
96-
complete_get_elt as * const _ as usize } , 16usize , concat !
97-
(
98-
"Alignment of field: " , stringify ! (
99-
cmdline_token_hdr_cmdline_token_ops ) , "::" , stringify ! (
100-
complete_get_elt ) ));
113+
& ( * ( 0 as * const cmdline_token_ops ) ) . complete_get_elt
114+
as * const _ as usize } , 16usize , concat ! (
115+
"Alignment of field: " , stringify ! ( cmdline_token_ops ) ,
116+
"::" , stringify ! ( complete_get_elt ) ));
101117
assert_eq! (unsafe {
102-
& ( * ( 0 as * const cmdline_token_hdr_cmdline_token_ops ) ) .
103-
get_help as * const _ as usize } , 24usize , concat ! (
104-
"Alignment of field: " , stringify ! (
105-
cmdline_token_hdr_cmdline_token_ops ) , "::" , stringify ! (
106-
get_help ) ));
118+
& ( * ( 0 as * const cmdline_token_ops ) ) . get_help as *
119+
const _ as usize } , 24usize , concat ! (
120+
"Alignment of field: " , stringify ! ( cmdline_token_ops ) ,
121+
"::" , stringify ! ( get_help ) ));
107122
}
108-
impl Clone for cmdline_token_hdr_cmdline_token_ops {
123+
impl Clone for cmdline_token_ops {
109124
fn clone(&self) -> Self { *self }
110125
}
111-
impl Default for cmdline_token_hdr_cmdline_token_ops {
126+
impl Default for cmdline_token_ops {
112127
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
113128
}
114-
#[test]
115-
fn bindgen_test_layout_cmdline_token_hdr() {
116-
assert_eq!(::std::mem::size_of::<cmdline_token_hdr>() , 16usize , concat !
117-
( "Size of: " , stringify ! ( cmdline_token_hdr ) ));
118-
assert_eq! (::std::mem::align_of::<cmdline_token_hdr>() , 8usize , concat
119-
! ( "Alignment of " , stringify ! ( cmdline_token_hdr ) ));
120-
assert_eq! (unsafe {
121-
& ( * ( 0 as * const cmdline_token_hdr ) ) . ops as * const _
122-
as usize } , 0usize , concat ! (
123-
"Alignment of field: " , stringify ! ( cmdline_token_hdr ) ,
124-
"::" , stringify ! ( ops ) ));
125-
assert_eq! (unsafe {
126-
& ( * ( 0 as * const cmdline_token_hdr ) ) . offset as * const
127-
_ as usize } , 8usize , concat ! (
128-
"Alignment of field: " , stringify ! ( cmdline_token_hdr ) ,
129-
"::" , stringify ! ( offset ) ));
130-
}
131-
impl Clone for cmdline_token_hdr {
132-
fn clone(&self) -> Self { *self }
133-
}
134-
impl Default for cmdline_token_hdr {
135-
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
136-
}
137-
pub type cmdline_parse_token_hdr_t = cmdline_token_hdr;
138129
#[repr(u32)]
139130
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
140131
pub enum cmdline_numtype {

tests/expectations/tests/layout_mbuf.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub struct rte_mbuf {
9898
pub cacheline1: MARKER,
9999
pub __bindgen_anon_3: rte_mbuf__bindgen_ty_4,
100100
/**< Pool from which mbuf was allocated. */
101-
pub pool: *mut rte_mbuf_rte_mempool,
101+
pub pool: *mut rte_mempool,
102102
/**< Next segment of scattered packet. */
103103
pub next: *mut rte_mbuf,
104104
pub __bindgen_anon_4: rte_mbuf__bindgen_ty_5,
@@ -490,9 +490,6 @@ impl Clone for rte_mbuf__bindgen_ty_4 {
490490
fn clone(&self) -> Self { *self }
491491
}
492492
#[repr(C)]
493-
#[derive(Debug, Copy, Clone)]
494-
pub struct rte_mbuf_rte_mempool([u8; 0]);
495-
#[repr(C)]
496493
#[derive(Debug, Default, Copy)]
497494
pub struct rte_mbuf__bindgen_ty_5 {
498495
/**< combined for easy fetch */
@@ -734,3 +731,12 @@ impl Clone for rte_mbuf {
734731
impl Default for rte_mbuf {
735732
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
736733
}
734+
/**< Pool from which mbuf was allocated. */
735+
#[repr(C)]
736+
#[derive(Debug, Default, Copy)]
737+
pub struct rte_mempool {
738+
pub _address: u8,
739+
}
740+
impl Clone for rte_mempool {
741+
fn clone(&self) -> Self { *self }
742+
}

0 commit comments

Comments
 (0)