-
Notifications
You must be signed in to change notification settings - Fork 711
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
228 changes: 228 additions & 0 deletions
228
tests/expectations/tests/inherit-from-template-instantiation-with-vtable.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
/* automatically generated by rust-bindgen */ | ||
|
||
|
||
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] | ||
|
||
|
||
#[repr(C)] | ||
pub struct BaseWithVtable__bindgen_vtable(::std::os::raw::c_void); | ||
/// This should have an explicit vtable. | ||
#[repr(C)] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct BaseWithVtable<T> { | ||
pub vtable_: *const BaseWithVtable__bindgen_vtable, | ||
pub t: T, | ||
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>, | ||
} | ||
impl<T> Default for BaseWithVtable<T> { | ||
fn default() -> Self { | ||
unsafe { ::std::mem::zeroed() } | ||
} | ||
} | ||
/// This should not have an explicit vtable. | ||
#[repr(C)] | ||
#[derive(Debug, Copy)] | ||
pub struct DerivedWithNoVirtualMethods { | ||
pub _base: BaseWithVtable<*mut ::std::os::raw::c_char>, | ||
} | ||
#[test] | ||
fn bindgen_test_layout_DerivedWithNoVirtualMethods() { | ||
assert_eq!( | ||
::std::mem::size_of::<DerivedWithNoVirtualMethods>(), | ||
16usize, | ||
concat!("Size of: ", stringify!(DerivedWithNoVirtualMethods)) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<DerivedWithNoVirtualMethods>(), | ||
8usize, | ||
concat!("Alignment of ", stringify!(DerivedWithNoVirtualMethods)) | ||
); | ||
} | ||
impl Clone for DerivedWithNoVirtualMethods { | ||
fn clone(&self) -> Self { | ||
*self | ||
} | ||
} | ||
impl Default for DerivedWithNoVirtualMethods { | ||
fn default() -> Self { | ||
unsafe { ::std::mem::zeroed() } | ||
} | ||
} | ||
/// This should not have an explicit vtable. | ||
#[repr(C)] | ||
#[derive(Debug, Copy)] | ||
pub struct DerivedWithVirtualMethods { | ||
pub _base: BaseWithVtable<*mut ::std::os::raw::c_char>, | ||
} | ||
#[test] | ||
fn bindgen_test_layout_DerivedWithVirtualMethods() { | ||
assert_eq!( | ||
::std::mem::size_of::<DerivedWithVirtualMethods>(), | ||
16usize, | ||
concat!("Size of: ", stringify!(DerivedWithVirtualMethods)) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<DerivedWithVirtualMethods>(), | ||
8usize, | ||
concat!("Alignment of ", stringify!(DerivedWithVirtualMethods)) | ||
); | ||
} | ||
impl Clone for DerivedWithVirtualMethods { | ||
fn clone(&self) -> Self { | ||
*self | ||
} | ||
} | ||
impl Default for DerivedWithVirtualMethods { | ||
fn default() -> Self { | ||
unsafe { ::std::mem::zeroed() } | ||
} | ||
} | ||
/// This should not have any vtable. | ||
#[repr(C)] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct BaseWithoutVtable<U> { | ||
pub u: U, | ||
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>, | ||
} | ||
impl<U> Default for BaseWithoutVtable<U> { | ||
fn default() -> Self { | ||
unsafe { ::std::mem::zeroed() } | ||
} | ||
} | ||
#[repr(C)] | ||
pub struct DerivedWithVtable__bindgen_vtable(::std::os::raw::c_void); | ||
/// This should have an explicit vtable. | ||
#[repr(C)] | ||
#[derive(Debug, Copy)] | ||
pub struct DerivedWithVtable { | ||
pub vtable_: *const DerivedWithVtable__bindgen_vtable, | ||
pub _base: BaseWithoutVtable<*mut ::std::os::raw::c_char>, | ||
} | ||
#[test] | ||
fn bindgen_test_layout_DerivedWithVtable() { | ||
assert_eq!( | ||
::std::mem::size_of::<DerivedWithVtable>(), | ||
16usize, | ||
concat!("Size of: ", stringify!(DerivedWithVtable)) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<DerivedWithVtable>(), | ||
8usize, | ||
concat!("Alignment of ", stringify!(DerivedWithVtable)) | ||
); | ||
} | ||
impl Clone for DerivedWithVtable { | ||
fn clone(&self) -> Self { | ||
*self | ||
} | ||
} | ||
impl Default for DerivedWithVtable { | ||
fn default() -> Self { | ||
unsafe { ::std::mem::zeroed() } | ||
} | ||
} | ||
/// This should not have any vtable. | ||
#[repr(C)] | ||
#[derive(Debug, Copy)] | ||
pub struct DerivedWithoutVtable { | ||
pub _base: BaseWithoutVtable<*mut ::std::os::raw::c_char>, | ||
} | ||
#[test] | ||
fn bindgen_test_layout_DerivedWithoutVtable() { | ||
assert_eq!( | ||
::std::mem::size_of::<DerivedWithoutVtable>(), | ||
8usize, | ||
concat!("Size of: ", stringify!(DerivedWithoutVtable)) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<DerivedWithoutVtable>(), | ||
8usize, | ||
concat!("Alignment of ", stringify!(DerivedWithoutVtable)) | ||
); | ||
} | ||
impl Clone for DerivedWithoutVtable { | ||
fn clone(&self) -> Self { | ||
*self | ||
} | ||
} | ||
impl Default for DerivedWithoutVtable { | ||
fn default() -> Self { | ||
unsafe { ::std::mem::zeroed() } | ||
} | ||
} | ||
#[test] | ||
fn __bindgen_test_layout_BaseWithVtable_open0_ptr_char_close0_instantiation() { | ||
assert_eq!( | ||
::std::mem::size_of::<BaseWithVtable<*mut ::std::os::raw::c_char>>(), | ||
16usize, | ||
concat!( | ||
"Size of template specialization: ", | ||
stringify!(BaseWithVtable<*mut ::std::os::raw::c_char>) | ||
) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<BaseWithVtable<*mut ::std::os::raw::c_char>>(), | ||
8usize, | ||
concat!( | ||
"Alignment of template specialization: ", | ||
stringify!(BaseWithVtable<*mut ::std::os::raw::c_char>) | ||
) | ||
); | ||
} | ||
#[test] | ||
fn __bindgen_test_layout_BaseWithVtable_open0_ptr_char_close0_instantiation_1() { | ||
assert_eq!( | ||
::std::mem::size_of::<BaseWithVtable<*mut ::std::os::raw::c_char>>(), | ||
16usize, | ||
concat!( | ||
"Size of template specialization: ", | ||
stringify!(BaseWithVtable<*mut ::std::os::raw::c_char>) | ||
) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<BaseWithVtable<*mut ::std::os::raw::c_char>>(), | ||
8usize, | ||
concat!( | ||
"Alignment of template specialization: ", | ||
stringify!(BaseWithVtable<*mut ::std::os::raw::c_char>) | ||
) | ||
); | ||
} | ||
#[test] | ||
fn __bindgen_test_layout_BaseWithoutVtable_open0_ptr_char_close0_instantiation() { | ||
assert_eq!( | ||
::std::mem::size_of::<BaseWithoutVtable<*mut ::std::os::raw::c_char>>(), | ||
8usize, | ||
concat!( | ||
"Size of template specialization: ", | ||
stringify!(BaseWithoutVtable<*mut ::std::os::raw::c_char>) | ||
) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<BaseWithoutVtable<*mut ::std::os::raw::c_char>>(), | ||
8usize, | ||
concat!( | ||
"Alignment of template specialization: ", | ||
stringify!(BaseWithoutVtable<*mut ::std::os::raw::c_char>) | ||
) | ||
); | ||
} | ||
#[test] | ||
fn __bindgen_test_layout_BaseWithoutVtable_open0_ptr_char_close0_instantiation_1() { | ||
assert_eq!( | ||
::std::mem::size_of::<BaseWithoutVtable<*mut ::std::os::raw::c_char>>(), | ||
8usize, | ||
concat!( | ||
"Size of template specialization: ", | ||
stringify!(BaseWithoutVtable<*mut ::std::os::raw::c_char>) | ||
) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<BaseWithoutVtable<*mut ::std::os::raw::c_char>>(), | ||
8usize, | ||
concat!( | ||
"Alignment of template specialization: ", | ||
stringify!(BaseWithoutVtable<*mut ::std::os::raw::c_char>) | ||
) | ||
); | ||
} |
37 changes: 37 additions & 0 deletions
37
tests/headers/inherit-from-template-instantiation-with-vtable.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// bindgen-flags: -- -std=c++14 | ||
|
||
// Small test that we handle virtual tables correctly when deriving from a | ||
// template instantiation. This wasn't previously handled at all. Note that when | ||
// inheriting from a template parameter, the type that is instantiated might or | ||
// might not have a virtual table, and we have no way of knowing. We don't | ||
// handle that yet, so no test for it here. | ||
|
||
/// This should have an explicit vtable. | ||
template<class T> | ||
class BaseWithVtable { | ||
T t; | ||
|
||
virtual void hello(); | ||
}; | ||
|
||
/// This should not have an explicit vtable. | ||
class DerivedWithNoVirtualMethods : public BaseWithVtable<char*> {}; | ||
|
||
/// This should not have an explicit vtable. | ||
class DerivedWithVirtualMethods : public BaseWithVtable<char*> { | ||
virtual void zoidberg(); | ||
}; | ||
|
||
/// This should not have any vtable. | ||
template<class U> | ||
class BaseWithoutVtable { | ||
U u; | ||
}; | ||
|
||
/// This should have an explicit vtable. | ||
class DerivedWithVtable : public BaseWithoutVtable<char*> { | ||
virtual void leela(); | ||
}; | ||
|
||
/// This should not have any vtable. | ||
class DerivedWithoutVtable : public BaseWithoutVtable<char*> {}; |