summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libbindgen/src/codegen/mod.rs30
-rw-r--r--libbindgen/tests/expectations/tests/anon_union.rs7
-rw-r--r--libbindgen/tests/expectations/tests/class_with_dtor.rs7
-rw-r--r--libbindgen/tests/expectations/tests/crtp.rs12
-rw-r--r--libbindgen/tests/expectations/tests/template.rs14
5 files changed, 66 insertions, 4 deletions
diff --git a/libbindgen/src/codegen/mod.rs b/libbindgen/src/codegen/mod.rs
index c8cd4cdf..a3fefcc7 100644
--- a/libbindgen/src/codegen/mod.rs
+++ b/libbindgen/src/codegen/mod.rs
@@ -670,10 +670,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 8af416c3..915dfbd5 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::<TErrorResult<::std::os::raw::c_int>>() ,
+ 24usize);
+ assert_eq!(::std::mem::align_of::<TErrorResult<::std::os::raw::c_int>>() ,
+ 8usize);
+}
diff --git a/libbindgen/tests/expectations/tests/class_with_dtor.rs b/libbindgen/tests/expectations/tests/class_with_dtor.rs
index 8ed1ddf9..88c036b6 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::<WithoutDtor>() , 8usize);
assert_eq!(::std::mem::align_of::<WithoutDtor>() , 8usize);
}
+#[test]
+fn __bindgen_test_layout_template_11() {
+ assert_eq!(::std::mem::size_of::<HandleWithDtor<::std::os::raw::c_int>>()
+ , 8usize);
+ assert_eq!(::std::mem::align_of::<HandleWithDtor<::std::os::raw::c_int>>()
+ , 8usize);
+}
diff --git a/libbindgen/tests/expectations/tests/crtp.rs b/libbindgen/tests/expectations/tests/crtp.rs
index e4a86b24..a50e05e4 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::<Base<Derived>>() , 1usize);
+ assert_eq!(::std::mem::align_of::<Base<Derived>>() , 1usize);
+}
#[repr(C)]
#[derive(Debug)]
pub struct BaseWithDestructor<T> {
@@ -41,3 +46,10 @@ fn bindgen_test_layout_DerivedFromBaseWithDestructor() {
assert_eq!(::std::mem::align_of::<DerivedFromBaseWithDestructor>() ,
1usize);
}
+#[test]
+fn __bindgen_test_layout_template_12() {
+ assert_eq!(::std::mem::size_of::<BaseWithDestructor<DerivedFromBaseWithDestructor>>()
+ , 1usize);
+ assert_eq!(::std::mem::align_of::<BaseWithDestructor<DerivedFromBaseWithDestructor>>()
+ , 1usize);
+}
diff --git a/libbindgen/tests/expectations/tests/template.rs b/libbindgen/tests/expectations/tests/template.rs
index 76afc879..21814baa 100644
--- a/libbindgen/tests/expectations/tests/template.rs
+++ b/libbindgen/tests/expectations/tests/template.rs
@@ -12,6 +12,13 @@ pub struct Foo<T, U> {
pub m_member_arr: [T; 1usize],
pub _phantom_1: ::std::marker::PhantomData<U>,
}
+#[test]
+fn __bindgen_test_layout_template_10() {
+ assert_eq!(::std::mem::size_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
+ , 24usize);
+ assert_eq!(::std::mem::align_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
+ , 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<T> {
pub _address: u8,
pub _phantom_0: ::std::marker::PhantomData<T>,
}
+#[test]
+fn __bindgen_test_layout_template_132() {
+ assert_eq!(::std::mem::size_of::<WithDtor<::std::os::raw::c_int>>() ,
+ 4usize);
+ assert_eq!(::std::mem::align_of::<WithDtor<::std::os::raw::c_int>>() ,
+ 4usize);
+}