Skip to content

Commit 24fc13f

Browse files
committed
Reinstate the layout tests for template instantiations
1 parent 46781fd commit 24fc13f

File tree

6 files changed

+134
-2
lines changed

6 files changed

+134
-2
lines changed

src/codegen/mod.rs

+51-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use ir::item_kind::ItemKind;
2020
use ir::layout::Layout;
2121
use ir::module::Module;
2222
use ir::objc::ObjCInterface;
23-
use ir::template::AsNamed;
23+
use ir::template::{AsNamed, TemplateInstantiation};
2424
use ir::ty::{TemplateDeclaration, Type, TypeKind};
2525
use ir::var::Var;
2626

@@ -518,14 +518,16 @@ impl CodeGenerator for Type {
518518
TypeKind::Pointer(..) |
519519
TypeKind::BlockPointer |
520520
TypeKind::Reference(..) |
521-
TypeKind::TemplateInstantiation(..) |
522521
TypeKind::Function(..) |
523522
TypeKind::ResolvedTypeRef(..) |
524523
TypeKind::Named => {
525524
// These items don't need code generation, they only need to be
526525
// converted to rust types in fields, arguments, and such.
527526
return;
528527
}
528+
TypeKind::TemplateInstantiation(ref inst) => {
529+
inst.codegen(ctx, result, whitelisted_items, item)
530+
}
529531
TypeKind::Comp(ref ci) => {
530532
ci.codegen(ctx, result, whitelisted_items, item)
531533
}
@@ -821,6 +823,53 @@ impl<'a> Bitfield<'a> {
821823
}
822824
}
823825

826+
impl CodeGenerator for TemplateInstantiation {
827+
type Extra = Item;
828+
829+
fn codegen<'a>(&self,
830+
ctx: &BindgenContext,
831+
result: &mut CodegenResult<'a>,
832+
_whitelisted_items: &ItemSet,
833+
item: &Item) {
834+
// Although uses of instantiations don't need code generation, and are
835+
// just converted to rust types in fields, vars, etc, we take this
836+
// opportunity to generate tests for their layout here.
837+
838+
let layout = item.kind().expect_type().layout(ctx);
839+
840+
if let Some(layout) = layout {
841+
let size = layout.size;
842+
let align = layout.align;
843+
844+
let name = item.canonical_name(ctx);
845+
let fn_name = format!("__bindgen_test_layout_{}_instantiation_{}",
846+
name,
847+
item.id().as_usize());
848+
let fn_name = ctx.rust_ident_raw(&fn_name);
849+
850+
let prefix = ctx.trait_prefix();
851+
let ident = item.to_rust_ty(ctx);
852+
let size_of_expr = quote_expr!(ctx.ext_cx(),
853+
::$prefix::mem::size_of::<$ident>());
854+
let align_of_expr = quote_expr!(ctx.ext_cx(),
855+
::$prefix::mem::align_of::<$ident>());
856+
857+
let item = quote_item!(
858+
ctx.ext_cx(),
859+
#[test]
860+
fn $fn_name() {
861+
assert_eq!($size_of_expr, $size,
862+
concat!("Size of template specialization: ", stringify!($ident)));
863+
assert_eq!($align_of_expr, $align,
864+
concat!("Alignment of template specialization: ", stringify!($ident)));
865+
})
866+
.unwrap();
867+
868+
result.push(item);
869+
}
870+
}
871+
}
872+
824873
impl CodeGenerator for CompInfo {
825874
type Extra = Item;
826875

tests/expectations/tests/anon_union.rs

+9
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,12 @@ impl Clone for ErrorResult {
7979
impl Default for ErrorResult {
8080
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
8181
}
82+
#[test]
83+
fn __bindgen_test_layout_TErrorResult_instantiation_21() {
84+
assert_eq!(::std::mem::size_of::<TErrorResult>() , 24usize , concat ! (
85+
"Size of template specialization: " , stringify ! (
86+
TErrorResult ) ));
87+
assert_eq!(::std::mem::align_of::<TErrorResult>() , 8usize , concat ! (
88+
"Alignment of template specialization: " , stringify ! (
89+
TErrorResult ) ));
90+
}

tests/expectations/tests/class_nested.rs

+11
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ extern "C" {
7676
#[link_name = "var"]
7777
pub static mut var: A_B;
7878
}
79+
#[test]
80+
fn __bindgen_test_layout_A_D_instantiation_16() {
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+
}
7990
extern "C" {
8091
#[link_name = "baz"]
8192
pub static mut baz: A_D<::std::os::raw::c_int>;

tests/expectations/tests/class_with_dtor.rs

+11
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ fn bindgen_test_layout_WithoutDtor() {
3333
impl Default for WithoutDtor {
3434
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
3535
}
36+
#[test]
37+
fn __bindgen_test_layout_HandleWithDtor_instantiation_10() {
38+
assert_eq!(::std::mem::size_of::<HandleWithDtor<::std::os::raw::c_int>>()
39+
, 8usize , concat ! (
40+
"Size of template specialization: " , stringify ! (
41+
HandleWithDtor<::std::os::raw::c_int> ) ));
42+
assert_eq!(::std::mem::align_of::<HandleWithDtor<::std::os::raw::c_int>>()
43+
, 8usize , concat ! (
44+
"Alignment of template specialization: " , stringify ! (
45+
HandleWithDtor<::std::os::raw::c_int> ) ));
46+
}

tests/expectations/tests/crtp.rs

+19
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,22 @@ fn bindgen_test_layout_DerivedFromBaseWithDestructor() {
5050
impl Default for DerivedFromBaseWithDestructor {
5151
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
5252
}
53+
#[test]
54+
fn __bindgen_test_layout_Base_instantiation_9() {
55+
assert_eq!(::std::mem::size_of::<Base>() , 1usize , concat ! (
56+
"Size of template specialization: " , stringify ! ( Base ) ));
57+
assert_eq!(::std::mem::align_of::<Base>() , 1usize , concat ! (
58+
"Alignment of template specialization: " , stringify ! ( Base )
59+
));
60+
}
61+
#[test]
62+
fn __bindgen_test_layout_BaseWithDestructor_instantiation_12() {
63+
assert_eq!(::std::mem::size_of::<BaseWithDestructor>() , 1usize , concat !
64+
(
65+
"Size of template specialization: " , stringify ! (
66+
BaseWithDestructor ) ));
67+
assert_eq!(::std::mem::align_of::<BaseWithDestructor>() , 1usize , concat
68+
! (
69+
"Alignment of template specialization: " , stringify ! (
70+
BaseWithDestructor ) ));
71+
}

tests/expectations/tests/template.rs

+33
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,36 @@ impl <T> Default for ReplacedWithoutDestructorFwd<T> {
232232
pub struct TemplateWithVar {
233233
pub _address: u8,
234234
}
235+
#[test]
236+
fn __bindgen_test_layout_Foo_instantiation_95() {
237+
assert_eq!(::std::mem::size_of::<Foo<::std::os::raw::c_int>>() , 24usize ,
238+
concat ! (
239+
"Size of template specialization: " , stringify ! (
240+
Foo<::std::os::raw::c_int> ) ));
241+
assert_eq!(::std::mem::align_of::<Foo<::std::os::raw::c_int>>() , 8usize ,
242+
concat ! (
243+
"Alignment of template specialization: " , stringify ! (
244+
Foo<::std::os::raw::c_int> ) ));
245+
}
246+
#[test]
247+
fn __bindgen_test_layout_Rooted_instantiation_106() {
248+
assert_eq!(::std::mem::size_of::<Rooted<*mut ::std::os::raw::c_void>>() ,
249+
24usize , concat ! (
250+
"Size of template specialization: " , stringify ! (
251+
Rooted<*mut ::std::os::raw::c_void> ) ));
252+
assert_eq!(::std::mem::align_of::<Rooted<*mut ::std::os::raw::c_void>>() ,
253+
8usize , concat ! (
254+
"Alignment of template specialization: " , stringify ! (
255+
Rooted<*mut ::std::os::raw::c_void> ) ));
256+
}
257+
#[test]
258+
fn __bindgen_test_layout_WithDtor_instantiation_114() {
259+
assert_eq!(::std::mem::size_of::<WithDtor<::std::os::raw::c_int>>() ,
260+
4usize , concat ! (
261+
"Size of template specialization: " , stringify ! (
262+
WithDtor<::std::os::raw::c_int> ) ));
263+
assert_eq!(::std::mem::align_of::<WithDtor<::std::os::raw::c_int>>() ,
264+
4usize , concat ! (
265+
"Alignment of template specialization: " , stringify ! (
266+
WithDtor<::std::os::raw::c_int> ) ));
267+
}

0 commit comments

Comments
 (0)