Skip to content

Commit ec9852a

Browse files
author
bors-servo
authored
Auto merge of #282 - impowski:layout_template_specializations, r=emilio
First steps to fix issue #57 This should generate tests for fully specialized templates. TODO: - [x] Tests r? @emilio
2 parents 399f590 + 5c76483 commit ec9852a

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

libbindgen/src/codegen/mod.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,10 +670,32 @@ impl CodeGenerator for CompInfo {
670670

671671
// Don't output classes with template parameters that aren't types, and
672672
// also don't output template specializations, neither total or partial.
673-
//
674-
// TODO: Generate layout tests for template specializations, yay!
675-
if self.has_non_type_template_params() ||
676-
self.is_template_specialization() {
673+
if self.has_non_type_template_params() {
674+
return;
675+
}
676+
677+
if self.is_template_specialization() {
678+
let layout = item.kind().expect_type().layout(ctx);
679+
680+
if let Some(layout) = layout {
681+
let fn_name = format!("__bindgen_test_layout_template_{}", item.id().as_usize());
682+
let fn_name = ctx.rust_ident_raw(&fn_name);
683+
let ident = item.to_rust_ty(ctx);
684+
let prefix = ctx.trait_prefix();
685+
let size_of_expr = quote_expr!(ctx.ext_cx(),
686+
::$prefix::mem::size_of::<$ident>());
687+
let align_of_expr = quote_expr!(ctx.ext_cx(),
688+
::$prefix::mem::align_of::<$ident>());
689+
let size = layout.size;
690+
let align = layout.align;
691+
let item = quote_item!(ctx.ext_cx(),
692+
#[test]
693+
fn $fn_name() {
694+
assert_eq!($size_of_expr, $size);
695+
assert_eq!($align_of_expr, $align);
696+
}).unwrap();
697+
result.push(item);
698+
}
677699
return;
678700
}
679701

libbindgen/tests/expectations/tests/anon_union.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,10 @@ fn bindgen_test_layout_ErrorResult() {
7575
impl Clone for ErrorResult {
7676
fn clone(&self) -> Self { *self }
7777
}
78+
#[test]
79+
fn __bindgen_test_layout_template_17() {
80+
assert_eq!(::std::mem::size_of::<TErrorResult<::std::os::raw::c_int>>() ,
81+
24usize);
82+
assert_eq!(::std::mem::align_of::<TErrorResult<::std::os::raw::c_int>>() ,
83+
8usize);
84+
}

libbindgen/tests/expectations/tests/class_with_dtor.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ fn bindgen_test_layout_WithoutDtor() {
2020
assert_eq!(::std::mem::size_of::<WithoutDtor>() , 8usize);
2121
assert_eq!(::std::mem::align_of::<WithoutDtor>() , 8usize);
2222
}
23+
#[test]
24+
fn __bindgen_test_layout_template_11() {
25+
assert_eq!(::std::mem::size_of::<HandleWithDtor<::std::os::raw::c_int>>()
26+
, 8usize);
27+
assert_eq!(::std::mem::align_of::<HandleWithDtor<::std::os::raw::c_int>>()
28+
, 8usize);
29+
}

libbindgen/tests/expectations/tests/crtp.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ fn bindgen_test_layout_Derived() {
2323
impl Clone for Derived {
2424
fn clone(&self) -> Self { *self }
2525
}
26+
#[test]
27+
fn __bindgen_test_layout_template_5() {
28+
assert_eq!(::std::mem::size_of::<Base<Derived>>() , 1usize);
29+
assert_eq!(::std::mem::align_of::<Base<Derived>>() , 1usize);
30+
}
2631
#[repr(C)]
2732
#[derive(Debug)]
2833
pub struct BaseWithDestructor<T> {
@@ -41,3 +46,10 @@ fn bindgen_test_layout_DerivedFromBaseWithDestructor() {
4146
assert_eq!(::std::mem::align_of::<DerivedFromBaseWithDestructor>() ,
4247
1usize);
4348
}
49+
#[test]
50+
fn __bindgen_test_layout_template_12() {
51+
assert_eq!(::std::mem::size_of::<BaseWithDestructor<DerivedFromBaseWithDestructor>>()
52+
, 1usize);
53+
assert_eq!(::std::mem::align_of::<BaseWithDestructor<DerivedFromBaseWithDestructor>>()
54+
, 1usize);
55+
}

libbindgen/tests/expectations/tests/template.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ pub struct Foo<T, U> {
1212
pub m_member_arr: [T; 1usize],
1313
pub _phantom_1: ::std::marker::PhantomData<U>,
1414
}
15+
#[test]
16+
fn __bindgen_test_layout_template_10() {
17+
assert_eq!(::std::mem::size_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
18+
, 24usize);
19+
assert_eq!(::std::mem::align_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
20+
, 8usize);
21+
}
1522
extern "C" {
1623
#[link_name = "_Z3bar3FooIiiE"]
1724
pub fn bar(foo: Foo<::std::os::raw::c_int, ::std::os::raw::c_int>);
@@ -168,3 +175,10 @@ pub struct TemplateWithVar<T> {
168175
pub _address: u8,
169176
pub _phantom_0: ::std::marker::PhantomData<T>,
170177
}
178+
#[test]
179+
fn __bindgen_test_layout_template_132() {
180+
assert_eq!(::std::mem::size_of::<WithDtor<::std::os::raw::c_int>>() ,
181+
4usize);
182+
assert_eq!(::std::mem::align_of::<WithDtor<::std::os::raw::c_int>>() ,
183+
4usize);
184+
}

0 commit comments

Comments
 (0)