Skip to content

Commit 6f87f0b

Browse files
committed
Add a test for inheriting from template instantiations with vtables
We didn't previously handle this case until the `HasVtableAnalysis` was updated to determine which items have explicit vtable pointers.
1 parent 02dc02b commit 6f87f0b

File tree

2 files changed

+265
-0
lines changed

2 files changed

+265
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
#[repr(C)]
8+
pub struct BaseWithVtable__bindgen_vtable(::std::os::raw::c_void);
9+
/// This should have an explicit vtable.
10+
#[repr(C)]
11+
#[derive(Debug, Copy, Clone)]
12+
pub struct BaseWithVtable<T> {
13+
pub vtable_: *const BaseWithVtable__bindgen_vtable,
14+
pub t: T,
15+
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
16+
}
17+
impl<T> Default for BaseWithVtable<T> {
18+
fn default() -> Self {
19+
unsafe { ::std::mem::zeroed() }
20+
}
21+
}
22+
/// This should not have an explicit vtable.
23+
#[repr(C)]
24+
#[derive(Debug, Copy)]
25+
pub struct DerivedWithNoVirtualMethods {
26+
pub _base: BaseWithVtable<*mut ::std::os::raw::c_char>,
27+
}
28+
#[test]
29+
fn bindgen_test_layout_DerivedWithNoVirtualMethods() {
30+
assert_eq!(
31+
::std::mem::size_of::<DerivedWithNoVirtualMethods>(),
32+
16usize,
33+
concat!("Size of: ", stringify!(DerivedWithNoVirtualMethods))
34+
);
35+
assert_eq!(
36+
::std::mem::align_of::<DerivedWithNoVirtualMethods>(),
37+
8usize,
38+
concat!("Alignment of ", stringify!(DerivedWithNoVirtualMethods))
39+
);
40+
}
41+
impl Clone for DerivedWithNoVirtualMethods {
42+
fn clone(&self) -> Self {
43+
*self
44+
}
45+
}
46+
impl Default for DerivedWithNoVirtualMethods {
47+
fn default() -> Self {
48+
unsafe { ::std::mem::zeroed() }
49+
}
50+
}
51+
/// This should not have an explicit vtable.
52+
#[repr(C)]
53+
#[derive(Debug, Copy)]
54+
pub struct DerivedWithVirtualMethods {
55+
pub _base: BaseWithVtable<*mut ::std::os::raw::c_char>,
56+
}
57+
#[test]
58+
fn bindgen_test_layout_DerivedWithVirtualMethods() {
59+
assert_eq!(
60+
::std::mem::size_of::<DerivedWithVirtualMethods>(),
61+
16usize,
62+
concat!("Size of: ", stringify!(DerivedWithVirtualMethods))
63+
);
64+
assert_eq!(
65+
::std::mem::align_of::<DerivedWithVirtualMethods>(),
66+
8usize,
67+
concat!("Alignment of ", stringify!(DerivedWithVirtualMethods))
68+
);
69+
}
70+
impl Clone for DerivedWithVirtualMethods {
71+
fn clone(&self) -> Self {
72+
*self
73+
}
74+
}
75+
impl Default for DerivedWithVirtualMethods {
76+
fn default() -> Self {
77+
unsafe { ::std::mem::zeroed() }
78+
}
79+
}
80+
/// This should not have any vtable.
81+
#[repr(C)]
82+
#[derive(Debug, Copy, Clone)]
83+
pub struct BaseWithoutVtable<U> {
84+
pub u: U,
85+
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,
86+
}
87+
impl<U> Default for BaseWithoutVtable<U> {
88+
fn default() -> Self {
89+
unsafe { ::std::mem::zeroed() }
90+
}
91+
}
92+
#[repr(C)]
93+
pub struct DerivedWithVtable__bindgen_vtable(::std::os::raw::c_void);
94+
/// This should have an explicit vtable.
95+
#[repr(C)]
96+
#[derive(Debug, Copy)]
97+
pub struct DerivedWithVtable {
98+
pub vtable_: *const DerivedWithVtable__bindgen_vtable,
99+
pub _base: BaseWithoutVtable<*mut ::std::os::raw::c_char>,
100+
}
101+
#[test]
102+
fn bindgen_test_layout_DerivedWithVtable() {
103+
assert_eq!(
104+
::std::mem::size_of::<DerivedWithVtable>(),
105+
16usize,
106+
concat!("Size of: ", stringify!(DerivedWithVtable))
107+
);
108+
assert_eq!(
109+
::std::mem::align_of::<DerivedWithVtable>(),
110+
8usize,
111+
concat!("Alignment of ", stringify!(DerivedWithVtable))
112+
);
113+
}
114+
impl Clone for DerivedWithVtable {
115+
fn clone(&self) -> Self {
116+
*self
117+
}
118+
}
119+
impl Default for DerivedWithVtable {
120+
fn default() -> Self {
121+
unsafe { ::std::mem::zeroed() }
122+
}
123+
}
124+
/// This should not have any vtable.
125+
#[repr(C)]
126+
#[derive(Debug, Copy)]
127+
pub struct DerivedWithoutVtable {
128+
pub _base: BaseWithoutVtable<*mut ::std::os::raw::c_char>,
129+
}
130+
#[test]
131+
fn bindgen_test_layout_DerivedWithoutVtable() {
132+
assert_eq!(
133+
::std::mem::size_of::<DerivedWithoutVtable>(),
134+
8usize,
135+
concat!("Size of: ", stringify!(DerivedWithoutVtable))
136+
);
137+
assert_eq!(
138+
::std::mem::align_of::<DerivedWithoutVtable>(),
139+
8usize,
140+
concat!("Alignment of ", stringify!(DerivedWithoutVtable))
141+
);
142+
}
143+
impl Clone for DerivedWithoutVtable {
144+
fn clone(&self) -> Self {
145+
*self
146+
}
147+
}
148+
impl Default for DerivedWithoutVtable {
149+
fn default() -> Self {
150+
unsafe { ::std::mem::zeroed() }
151+
}
152+
}
153+
#[test]
154+
fn __bindgen_test_layout_BaseWithVtable_open0_ptr_char_close0_instantiation() {
155+
assert_eq!(
156+
::std::mem::size_of::<BaseWithVtable<*mut ::std::os::raw::c_char>>(),
157+
16usize,
158+
concat!(
159+
"Size of template specialization: ",
160+
stringify!(BaseWithVtable<*mut ::std::os::raw::c_char>)
161+
)
162+
);
163+
assert_eq!(
164+
::std::mem::align_of::<BaseWithVtable<*mut ::std::os::raw::c_char>>(),
165+
8usize,
166+
concat!(
167+
"Alignment of template specialization: ",
168+
stringify!(BaseWithVtable<*mut ::std::os::raw::c_char>)
169+
)
170+
);
171+
}
172+
#[test]
173+
fn __bindgen_test_layout_BaseWithVtable_open0_ptr_char_close0_instantiation_1() {
174+
assert_eq!(
175+
::std::mem::size_of::<BaseWithVtable<*mut ::std::os::raw::c_char>>(),
176+
16usize,
177+
concat!(
178+
"Size of template specialization: ",
179+
stringify!(BaseWithVtable<*mut ::std::os::raw::c_char>)
180+
)
181+
);
182+
assert_eq!(
183+
::std::mem::align_of::<BaseWithVtable<*mut ::std::os::raw::c_char>>(),
184+
8usize,
185+
concat!(
186+
"Alignment of template specialization: ",
187+
stringify!(BaseWithVtable<*mut ::std::os::raw::c_char>)
188+
)
189+
);
190+
}
191+
#[test]
192+
fn __bindgen_test_layout_BaseWithoutVtable_open0_ptr_char_close0_instantiation() {
193+
assert_eq!(
194+
::std::mem::size_of::<BaseWithoutVtable<*mut ::std::os::raw::c_char>>(),
195+
8usize,
196+
concat!(
197+
"Size of template specialization: ",
198+
stringify!(BaseWithoutVtable<*mut ::std::os::raw::c_char>)
199+
)
200+
);
201+
assert_eq!(
202+
::std::mem::align_of::<BaseWithoutVtable<*mut ::std::os::raw::c_char>>(),
203+
8usize,
204+
concat!(
205+
"Alignment of template specialization: ",
206+
stringify!(BaseWithoutVtable<*mut ::std::os::raw::c_char>)
207+
)
208+
);
209+
}
210+
#[test]
211+
fn __bindgen_test_layout_BaseWithoutVtable_open0_ptr_char_close0_instantiation_1() {
212+
assert_eq!(
213+
::std::mem::size_of::<BaseWithoutVtable<*mut ::std::os::raw::c_char>>(),
214+
8usize,
215+
concat!(
216+
"Size of template specialization: ",
217+
stringify!(BaseWithoutVtable<*mut ::std::os::raw::c_char>)
218+
)
219+
);
220+
assert_eq!(
221+
::std::mem::align_of::<BaseWithoutVtable<*mut ::std::os::raw::c_char>>(),
222+
8usize,
223+
concat!(
224+
"Alignment of template specialization: ",
225+
stringify!(BaseWithoutVtable<*mut ::std::os::raw::c_char>)
226+
)
227+
);
228+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// bindgen-flags: -- -std=c++14
2+
3+
// Small test that we handle virtual tables correctly when deriving from a
4+
// template instantiation. This wasn't previously handled at all. Note that when
5+
// inheriting from a template parameter, the type that is instantiated might or
6+
// might not have a virtual table, and we have no way of knowing. We don't
7+
// handle that yet, so no test for it here.
8+
9+
/// This should have an explicit vtable.
10+
template<class T>
11+
class BaseWithVtable {
12+
T t;
13+
14+
virtual void hello();
15+
};
16+
17+
/// This should not have an explicit vtable.
18+
class DerivedWithNoVirtualMethods : public BaseWithVtable<char*> {};
19+
20+
/// This should not have an explicit vtable.
21+
class DerivedWithVirtualMethods : public BaseWithVtable<char*> {
22+
virtual void zoidberg();
23+
};
24+
25+
/// This should not have any vtable.
26+
template<class U>
27+
class BaseWithoutVtable {
28+
U u;
29+
};
30+
31+
/// This should have an explicit vtable.
32+
class DerivedWithVtable : public BaseWithoutVtable<char*> {
33+
virtual void leela();
34+
};
35+
36+
/// This should not have any vtable.
37+
class DerivedWithoutVtable : public BaseWithoutVtable<char*> {};

0 commit comments

Comments
 (0)