Skip to content

codegen: Improve the assertion message of failing layout tests. #487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,10 @@ impl CodeGenerator for CompInfo {
let item = quote_item!(ctx.ext_cx(),
#[test]
fn $fn_name() {
assert_eq!($size_of_expr, $size);
assert_eq!($align_of_expr, $align);
assert_eq!($size_of_expr, $size,
concat!("Size of template specialization: ", stringify!($ident)));
assert_eq!($align_of_expr, $align,
concat!("Alignment of template specialization: ", stringify!($ident)));
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch is good to me, something else observed.

Do we need check the field offset for the template specialization?

It seems be different to VTable case, should have potential layout issue. Anyway, I haven't user cases in wild

.unwrap();
result.push(item);
Expand Down Expand Up @@ -1339,12 +1341,12 @@ impl CodeGenerator for CompInfo {
if let Some(layout) = layout {
let fn_name = format!("bindgen_test_layout_{}", canonical_name);
let fn_name = ctx.rust_ident_raw(&fn_name);
let ident = ctx.rust_ident_raw(&canonical_name);
let type_name = ctx.rust_ident_raw(&canonical_name);
let prefix = ctx.trait_prefix();
let size_of_expr = quote_expr!(ctx.ext_cx(),
::$prefix::mem::size_of::<$ident>());
::$prefix::mem::size_of::<$type_name>());
let align_of_expr = quote_expr!(ctx.ext_cx(),
::$prefix::mem::align_of::<$ident>());
::$prefix::mem::align_of::<$type_name>());
let size = layout.size;
let align = layout.align;

Expand All @@ -1353,7 +1355,9 @@ impl CodeGenerator for CompInfo {
None
} else {
quote_item!(ctx.ext_cx(),
assert_eq!($align_of_expr, $align);
assert_eq!($align_of_expr,
$align,
concat!("Alignment of ", stringify!($type_name)));
)
};

Expand All @@ -1370,8 +1374,6 @@ impl CodeGenerator for CompInfo {
let check_field_offset = if should_skip_field_offset_checks {
None
} else {
let type_name = ctx.rust_ident(&canonical_name);

let asserts = self.fields()
.iter()
.filter(|field| field.bitfield().is_none())
Expand All @@ -1382,7 +1384,9 @@ impl CodeGenerator for CompInfo {
let field_name = ctx.rust_ident(name);

quote_item!(ctx.ext_cx(),
assert_eq!(unsafe { &(*(0 as *const $type_name)).$field_name as *const _ as usize }, $field_offset);
assert_eq!(unsafe { &(*(0 as *const $type_name)).$field_name as *const _ as usize },
$field_offset,
concat!("Alignment of field: ", stringify!($type_name), "::", stringify!($field_name)));
)
})
})
Expand All @@ -1394,7 +1398,9 @@ impl CodeGenerator for CompInfo {
let item = quote_item!(ctx.ext_cx(),
#[test]
fn $fn_name() {
assert_eq!($size_of_expr, $size);
assert_eq!($size_of_expr,
$size,
concat!("Size of: ", stringify!($type_name)));

$check_struct_align
$check_field_offset
Expand Down Expand Up @@ -2137,7 +2143,7 @@ impl ToRustTy for Type {
.map(|arg| arg.to_rust_ty(ctx))
.collect::<Vec<_>>();

path.segments.last_mut().unwrap().parameters = if
path.segments.last_mut().unwrap().parameters = if
template_args.is_empty() {
None
} else {
Expand Down Expand Up @@ -2458,12 +2464,12 @@ mod utils {
use aster;
use ir::context::{BindgenContext, ItemId};
use ir::item::{Item, ItemCanonicalPath};
use ir::function::FunctionSig;
use ir::ty::TypeKind;
use std::mem;
use syntax::ast;
use syntax::ptr::P;


pub fn prepend_objc_header(ctx: &BindgenContext,
result: &mut Vec<P<ast::Item>>) {
let use_objc = if ctx.options().objc_extern_crate {
Expand Down Expand Up @@ -2745,7 +2751,7 @@ mod utils {
}

pub fn fnsig_return_ty(ctx: &BindgenContext,
sig: &super::FunctionSig)
sig: &FunctionSig)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to support older rustc, we should add the oldest supported version to our travis ci matrix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only temporary until Gecko updates is rustc version anyway, should be <1week

-> ast::FunctionRetTy {
let return_item = ctx.resolve_item(sig.return_type());
if let TypeKind::Void = *return_item.kind().expect_type().kind() {
Expand All @@ -2756,7 +2762,7 @@ mod utils {
}

pub fn fnsig_arguments(ctx: &BindgenContext,
sig: &super::FunctionSig)
sig: &FunctionSig)
-> Vec<ast::Arg> {
use super::ToPtr;
let mut unnamed_arguments = 0;
Expand Down
103 changes: 76 additions & 27 deletions tests/expectations/tests/16-byte-alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,44 +51,66 @@ pub struct rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 {
#[test]
fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1() {
assert_eq!(::std::mem::size_of::<rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1>()
, 4usize);
, 4usize , concat ! (
"Size of: " , stringify ! (
rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 ) ));
assert_eq! (::std::mem::align_of::<rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1>()
, 2usize);
, 2usize , concat ! (
"Alignment of " , stringify ! (
rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 ) ));
assert_eq! (unsafe {
& (
* ( 0 as * const rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 )
) . dport as * const _ as usize } , 0usize);
) . dport as * const _ as usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! (
rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 ) , "::" ,
stringify ! ( dport ) ));
assert_eq! (unsafe {
& (
* ( 0 as * const rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 )
) . sport as * const _ as usize } , 2usize);
) . sport as * const _ as usize } , 2usize , concat ! (
"Alignment of field: " , stringify ! (
rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 ) , "::" ,
stringify ! ( sport ) ));
}
impl Clone for rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 {
fn clone(&self) -> Self { *self }
}
#[test]
fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1() {
assert_eq!(::std::mem::size_of::<rte_ipv4_tuple__bindgen_ty_1>() ,
4usize);
assert_eq!(::std::mem::size_of::<rte_ipv4_tuple__bindgen_ty_1>() , 4usize
, concat ! (
"Size of: " , stringify ! ( rte_ipv4_tuple__bindgen_ty_1 ) ));
assert_eq! (::std::mem::align_of::<rte_ipv4_tuple__bindgen_ty_1>() ,
4usize);
4usize , concat ! (
"Alignment of " , stringify ! ( rte_ipv4_tuple__bindgen_ty_1 )
));
assert_eq! (unsafe {
& ( * ( 0 as * const rte_ipv4_tuple__bindgen_ty_1 ) ) .
sctp_tag as * const _ as usize } , 0usize);
sctp_tag as * const _ as usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! (
rte_ipv4_tuple__bindgen_ty_1 ) , "::" , stringify ! ( sctp_tag
) ));
}
impl Clone for rte_ipv4_tuple__bindgen_ty_1 {
fn clone(&self) -> Self { *self }
}
#[test]
fn bindgen_test_layout_rte_ipv4_tuple() {
assert_eq!(::std::mem::size_of::<rte_ipv4_tuple>() , 12usize);
assert_eq! (::std::mem::align_of::<rte_ipv4_tuple>() , 4usize);
assert_eq!(::std::mem::size_of::<rte_ipv4_tuple>() , 12usize , concat ! (
"Size of: " , stringify ! ( rte_ipv4_tuple ) ));
assert_eq! (::std::mem::align_of::<rte_ipv4_tuple>() , 4usize , concat ! (
"Alignment of " , stringify ! ( rte_ipv4_tuple ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const rte_ipv4_tuple ) ) . src_addr as * const
_ as usize } , 0usize);
_ as usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! ( rte_ipv4_tuple ) , "::"
, stringify ! ( src_addr ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const rte_ipv4_tuple ) ) . dst_addr as * const
_ as usize } , 4usize);
_ as usize } , 4usize , concat ! (
"Alignment of field: " , stringify ! ( rte_ipv4_tuple ) , "::"
, stringify ! ( dst_addr ) ));
}
impl Clone for rte_ipv4_tuple {
fn clone(&self) -> Self { *self }
Expand Down Expand Up @@ -116,44 +138,66 @@ pub struct rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 {
#[test]
fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1() {
assert_eq!(::std::mem::size_of::<rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1>()
, 4usize);
, 4usize , concat ! (
"Size of: " , stringify ! (
rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 ) ));
assert_eq! (::std::mem::align_of::<rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1>()
, 2usize);
, 2usize , concat ! (
"Alignment of " , stringify ! (
rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 ) ));
assert_eq! (unsafe {
& (
* ( 0 as * const rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 )
) . dport as * const _ as usize } , 0usize);
) . dport as * const _ as usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! (
rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 ) , "::" ,
stringify ! ( dport ) ));
assert_eq! (unsafe {
& (
* ( 0 as * const rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 )
) . sport as * const _ as usize } , 2usize);
) . sport as * const _ as usize } , 2usize , concat ! (
"Alignment of field: " , stringify ! (
rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 ) , "::" ,
stringify ! ( sport ) ));
}
impl Clone for rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 {
fn clone(&self) -> Self { *self }
}
#[test]
fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1() {
assert_eq!(::std::mem::size_of::<rte_ipv6_tuple__bindgen_ty_1>() ,
4usize);
assert_eq!(::std::mem::size_of::<rte_ipv6_tuple__bindgen_ty_1>() , 4usize
, concat ! (
"Size of: " , stringify ! ( rte_ipv6_tuple__bindgen_ty_1 ) ));
assert_eq! (::std::mem::align_of::<rte_ipv6_tuple__bindgen_ty_1>() ,
4usize);
4usize , concat ! (
"Alignment of " , stringify ! ( rte_ipv6_tuple__bindgen_ty_1 )
));
assert_eq! (unsafe {
& ( * ( 0 as * const rte_ipv6_tuple__bindgen_ty_1 ) ) .
sctp_tag as * const _ as usize } , 0usize);
sctp_tag as * const _ as usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! (
rte_ipv6_tuple__bindgen_ty_1 ) , "::" , stringify ! ( sctp_tag
) ));
}
impl Clone for rte_ipv6_tuple__bindgen_ty_1 {
fn clone(&self) -> Self { *self }
}
#[test]
fn bindgen_test_layout_rte_ipv6_tuple() {
assert_eq!(::std::mem::size_of::<rte_ipv6_tuple>() , 36usize);
assert_eq! (::std::mem::align_of::<rte_ipv6_tuple>() , 4usize);
assert_eq!(::std::mem::size_of::<rte_ipv6_tuple>() , 36usize , concat ! (
"Size of: " , stringify ! ( rte_ipv6_tuple ) ));
assert_eq! (::std::mem::align_of::<rte_ipv6_tuple>() , 4usize , concat ! (
"Alignment of " , stringify ! ( rte_ipv6_tuple ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const rte_ipv6_tuple ) ) . src_addr as * const
_ as usize } , 0usize);
_ as usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! ( rte_ipv6_tuple ) , "::"
, stringify ! ( src_addr ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const rte_ipv6_tuple ) ) . dst_addr as * const
_ as usize } , 16usize);
_ as usize } , 16usize , concat ! (
"Alignment of field: " , stringify ! ( rte_ipv6_tuple ) , "::"
, stringify ! ( dst_addr ) ));
}
impl Clone for rte_ipv6_tuple {
fn clone(&self) -> Self { *self }
Expand All @@ -167,13 +211,18 @@ pub struct rte_thash_tuple {
}
#[test]
fn bindgen_test_layout_rte_thash_tuple() {
assert_eq!(::std::mem::size_of::<rte_thash_tuple>() , 48usize);
assert_eq!(::std::mem::size_of::<rte_thash_tuple>() , 48usize , concat ! (
"Size of: " , stringify ! ( rte_thash_tuple ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const rte_thash_tuple ) ) . v4 as * const _ as
usize } , 0usize);
usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! ( rte_thash_tuple ) ,
"::" , stringify ! ( v4 ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const rte_thash_tuple ) ) . v6 as * const _ as
usize } , 0usize);
usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! ( rte_thash_tuple ) ,
"::" , stringify ! ( v6 ) ));
}
impl Clone for rte_thash_tuple {
fn clone(&self) -> Self { *self }
Expand Down
Loading