Skip to content

Commit 5c76483

Browse files
committed
Apply fixes due code review for issue #57
1 parent 5ee3601 commit 5c76483

File tree

5 files changed

+31
-43
lines changed

5 files changed

+31
-43
lines changed

libbindgen/src/codegen/mod.rs

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -653,45 +653,33 @@ impl CodeGenerator for CompInfo {
653653

654654
// Don't output classes with template parameters that aren't types, and
655655
// also don't output template specializations, neither total or partial.
656-
//
657-
// TODO: Generate layout tests for template specializations, yay!
658-
659-
// TODO (imp): I will keep it like that right now and move it to function later
660-
if self.has_non_type_template_params() ||
661-
self.is_template_specialization() {
662-
let layout = item.kind().expect_type().layout(ctx);
663-
let canonical_name = item.canonical_name(ctx);
664-
665-
if let Some(layout) = layout {
656+
if self.has_non_type_template_params() {
657+
return;
658+
}
666659

667-
let mut types = String::new();
660+
if self.is_template_specialization() {
661+
let layout = item.kind().expect_type().layout(ctx);
668662

669-
for arg in self.template_args() {
670-
if let Some(name) = ctx.resolve_type(*arg).name() {
671-
// hope this isn't bad
672-
types.push_str(format!("_{}", name).as_str());
673-
}
674-
}
675-
676-
let fn_name = format!("bindgen_test_layout_template_{}{}", canonical_name, types);
677-
let fn_name = ctx.rust_ident_raw(&fn_name);
678-
let ident = item.to_rust_ty(ctx);
679-
let prefix = ctx.trait_prefix();
680-
let size_of_expr = quote_expr!(ctx.ext_cx(),
681-
::$prefix::mem::size_of::<$ident>());
682-
let align_of_expr = quote_expr!(ctx.ext_cx(),
683-
::$prefix::mem::align_of::<$ident>());
684-
let size = layout.size;
685-
let align = layout.align;
686-
let item = quote_item!(ctx.ext_cx(),
687-
#[test]
688-
fn $fn_name() {
689-
assert_eq!($size_of_expr, $size);
690-
assert_eq!($align_of_expr, $align);
691-
}).unwrap();
692-
result.push(item);
693-
}
694-
return;
663+
if let Some(layout) = layout {
664+
let fn_name = format!("__bindgen_test_layout_template_{}", item.id().as_usize());
665+
let fn_name = ctx.rust_ident_raw(&fn_name);
666+
let ident = item.to_rust_ty(ctx);
667+
let prefix = ctx.trait_prefix();
668+
let size_of_expr = quote_expr!(ctx.ext_cx(),
669+
::$prefix::mem::size_of::<$ident>());
670+
let align_of_expr = quote_expr!(ctx.ext_cx(),
671+
::$prefix::mem::align_of::<$ident>());
672+
let size = layout.size;
673+
let align = layout.align;
674+
let item = quote_item!(ctx.ext_cx(),
675+
#[test]
676+
fn $fn_name() {
677+
assert_eq!($size_of_expr, $size);
678+
assert_eq!($align_of_expr, $align);
679+
}).unwrap();
680+
result.push(item);
681+
}
682+
return;
695683
}
696684

697685
let applicable_template_args = item.applicable_template_args(ctx);

libbindgen/tests/expectations/tests/anon_union.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl Clone for ErrorResult {
7676
fn clone(&self) -> Self { *self }
7777
}
7878
#[test]
79-
fn bindgen_test_layout_template_TErrorResult_int() {
79+
fn __bindgen_test_layout_template_17() {
8080
assert_eq!(::std::mem::size_of::<TErrorResult<::std::os::raw::c_int>>() ,
8181
24usize);
8282
assert_eq!(::std::mem::align_of::<TErrorResult<::std::os::raw::c_int>>() ,

libbindgen/tests/expectations/tests/class_with_dtor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn bindgen_test_layout_WithoutDtor() {
2121
assert_eq!(::std::mem::align_of::<WithoutDtor>() , 8usize);
2222
}
2323
#[test]
24-
fn bindgen_test_layout_template_HandleWithDtor_int() {
24+
fn __bindgen_test_layout_template_11() {
2525
assert_eq!(::std::mem::size_of::<HandleWithDtor<::std::os::raw::c_int>>()
2626
, 8usize);
2727
assert_eq!(::std::mem::align_of::<HandleWithDtor<::std::os::raw::c_int>>()

libbindgen/tests/expectations/tests/crtp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl Clone for Derived {
2424
fn clone(&self) -> Self { *self }
2525
}
2626
#[test]
27-
fn bindgen_test_layout_template_Base() {
27+
fn __bindgen_test_layout_template_5() {
2828
assert_eq!(::std::mem::size_of::<Base<Derived>>() , 1usize);
2929
assert_eq!(::std::mem::align_of::<Base<Derived>>() , 1usize);
3030
}
@@ -47,7 +47,7 @@ fn bindgen_test_layout_DerivedFromBaseWithDestructor() {
4747
1usize);
4848
}
4949
#[test]
50-
fn bindgen_test_layout_template_BaseWithDestructor() {
50+
fn __bindgen_test_layout_template_12() {
5151
assert_eq!(::std::mem::size_of::<BaseWithDestructor<DerivedFromBaseWithDestructor>>()
5252
, 1usize);
5353
assert_eq!(::std::mem::align_of::<BaseWithDestructor<DerivedFromBaseWithDestructor>>()

libbindgen/tests/expectations/tests/template.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct Foo<T, U> {
1313
pub _phantom_1: ::std::marker::PhantomData<U>,
1414
}
1515
#[test]
16-
fn bindgen_test_layout_template_Foo_int_int() {
16+
fn __bindgen_test_layout_template_10() {
1717
assert_eq!(::std::mem::size_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
1818
, 24usize);
1919
assert_eq!(::std::mem::align_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
@@ -176,7 +176,7 @@ pub struct TemplateWithVar<T> {
176176
pub _phantom_0: ::std::marker::PhantomData<T>,
177177
}
178178
#[test]
179-
fn bindgen_test_layout_template_WithDtor_int() {
179+
fn __bindgen_test_layout_template_132() {
180180
assert_eq!(::std::mem::size_of::<WithDtor<::std::os::raw::c_int>>() ,
181181
4usize);
182182
assert_eq!(::std::mem::align_of::<WithDtor<::std::os::raw::c_int>>() ,

0 commit comments

Comments
 (0)