diff --git a/libbindgen/src/codegen/mod.rs b/libbindgen/src/codegen/mod.rs index 6c8a36f733..392ec28a36 100644 --- a/libbindgen/src/codegen/mod.rs +++ b/libbindgen/src/codegen/mod.rs @@ -653,10 +653,32 @@ impl CodeGenerator for CompInfo { // Don't output classes with template parameters that aren't types, and // also don't output template specializations, neither total or partial. - // - // TODO: Generate layout tests for template specializations, yay! - if self.has_non_type_template_params() || - self.is_template_specialization() { + if self.has_non_type_template_params() { + return; + } + + if self.is_template_specialization() { + let layout = item.kind().expect_type().layout(ctx); + + if let Some(layout) = layout { + let fn_name = format!("__bindgen_test_layout_template_{}", item.id().as_usize()); + let fn_name = ctx.rust_ident_raw(&fn_name); + let ident = item.to_rust_ty(ctx); + let prefix = ctx.trait_prefix(); + let size_of_expr = quote_expr!(ctx.ext_cx(), + ::$prefix::mem::size_of::<$ident>()); + let align_of_expr = quote_expr!(ctx.ext_cx(), + ::$prefix::mem::align_of::<$ident>()); + let size = layout.size; + let align = layout.align; + let item = quote_item!(ctx.ext_cx(), + #[test] + fn $fn_name() { + assert_eq!($size_of_expr, $size); + assert_eq!($align_of_expr, $align); + }).unwrap(); + result.push(item); + } return; } diff --git a/libbindgen/tests/expectations/tests/anon_union.rs b/libbindgen/tests/expectations/tests/anon_union.rs index 8af416c32c..915dfbd5b1 100644 --- a/libbindgen/tests/expectations/tests/anon_union.rs +++ b/libbindgen/tests/expectations/tests/anon_union.rs @@ -75,3 +75,10 @@ fn bindgen_test_layout_ErrorResult() { impl Clone for ErrorResult { fn clone(&self) -> Self { *self } } +#[test] +fn __bindgen_test_layout_template_17() { + assert_eq!(::std::mem::size_of::>() , + 24usize); + assert_eq!(::std::mem::align_of::>() , + 8usize); +} diff --git a/libbindgen/tests/expectations/tests/class_with_dtor.rs b/libbindgen/tests/expectations/tests/class_with_dtor.rs index 8ed1ddf9fe..88c036b6a4 100644 --- a/libbindgen/tests/expectations/tests/class_with_dtor.rs +++ b/libbindgen/tests/expectations/tests/class_with_dtor.rs @@ -20,3 +20,10 @@ fn bindgen_test_layout_WithoutDtor() { assert_eq!(::std::mem::size_of::() , 8usize); assert_eq!(::std::mem::align_of::() , 8usize); } +#[test] +fn __bindgen_test_layout_template_11() { + assert_eq!(::std::mem::size_of::>() + , 8usize); + assert_eq!(::std::mem::align_of::>() + , 8usize); +} diff --git a/libbindgen/tests/expectations/tests/crtp.rs b/libbindgen/tests/expectations/tests/crtp.rs index e4a86b2430..a50e05e431 100644 --- a/libbindgen/tests/expectations/tests/crtp.rs +++ b/libbindgen/tests/expectations/tests/crtp.rs @@ -23,6 +23,11 @@ fn bindgen_test_layout_Derived() { impl Clone for Derived { fn clone(&self) -> Self { *self } } +#[test] +fn __bindgen_test_layout_template_5() { + assert_eq!(::std::mem::size_of::>() , 1usize); + assert_eq!(::std::mem::align_of::>() , 1usize); +} #[repr(C)] #[derive(Debug)] pub struct BaseWithDestructor { @@ -41,3 +46,10 @@ fn bindgen_test_layout_DerivedFromBaseWithDestructor() { assert_eq!(::std::mem::align_of::() , 1usize); } +#[test] +fn __bindgen_test_layout_template_12() { + assert_eq!(::std::mem::size_of::>() + , 1usize); + assert_eq!(::std::mem::align_of::>() + , 1usize); +} diff --git a/libbindgen/tests/expectations/tests/template.rs b/libbindgen/tests/expectations/tests/template.rs index 76afc87966..21814baa5d 100644 --- a/libbindgen/tests/expectations/tests/template.rs +++ b/libbindgen/tests/expectations/tests/template.rs @@ -12,6 +12,13 @@ pub struct Foo { pub m_member_arr: [T; 1usize], pub _phantom_1: ::std::marker::PhantomData, } +#[test] +fn __bindgen_test_layout_template_10() { + assert_eq!(::std::mem::size_of::>() + , 24usize); + assert_eq!(::std::mem::align_of::>() + , 8usize); +} extern "C" { #[link_name = "_Z3bar3FooIiiE"] pub fn bar(foo: Foo<::std::os::raw::c_int, ::std::os::raw::c_int>); @@ -168,3 +175,10 @@ pub struct TemplateWithVar { pub _address: u8, pub _phantom_0: ::std::marker::PhantomData, } +#[test] +fn __bindgen_test_layout_template_132() { + assert_eq!(::std::mem::size_of::>() , + 4usize); + assert_eq!(::std::mem::align_of::>() , + 4usize); +}