Skip to content

Commit

Permalink
Test struct field types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Sep 16, 2015
1 parent dbc5d24 commit 8e619cd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
21 changes: 16 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,31 +428,42 @@ impl<'a> Generator<'a> {
};

let cfield = self.rust2cfield(ty, &name.to_string());
let cfieldty = self.ty2name(&field.node.ty, false);
let rust_fieldty = self.ty2name(&field.node.ty, true);

t!(writeln!(self.c, r#"
uint64_t __test_offset_{ty}_{rust_field}(void) {{
return offsetof({cty}, {c_field});
return offsetof({cstructty}, {c_field});
}}
uint64_t __test_size_{ty}_{rust_field}(void) {{
{cty}* foo = NULL;
{cstructty}* foo = NULL;
return sizeof(foo->{c_field});
}}
"#, ty = ty, cty = cty, rust_field = name, c_field = cfield));
{cfieldty}* __test_field_type_{ty}_{rust_field}({cstructty}* b) {{
return &b->{c_field};
}}
"#, ty = ty, cstructty = cty, rust_field = name, c_field = cfield,
cfieldty = cfieldty));
t!(writeln!(self.rust, r#"
extern {{
fn __test_offset_{ty}_{field}() -> u64;
fn __test_size_{ty}_{field}() -> u64;
fn __test_field_type_{ty}_{field}(a: *mut {ty})
-> *mut {field_ty};
}}
unsafe {{
let foo = 0 as *const {ty};
let foo = 0 as *mut {ty};
same(offset_of!({ty}, {field}),
__test_offset_{ty}_{field}(),
"field offset {field} of {ty}");
same(mem::size_of_val(&(*foo).{field}) as u64,
__test_size_{ty}_{field}(),
"field size {field} of {ty}");
same(&(*foo).{field} as *const _ as *mut _,
__test_field_type_{ty}_{field}(foo),
"field type {field} of {ty}");
}}
"#, ty = ty, field = name));
"#, ty = ty, field = name, field_ty = rust_fieldty));
}
t!(writeln!(self.rust, r#"
}}
Expand Down
1 change: 1 addition & 0 deletions testcrate/src/t2.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ typedef int32_t T2Foo;
typedef int8_t T2Bar;

struct T2Baz {
int8_t _a;
int64_t a;
uint32_t b;
};
Expand Down
4 changes: 2 additions & 2 deletions testcrate/src/t2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pub type T2Bar = u32;

#[repr(C)]
pub struct T2Baz {
pub a: u8,
pub b: i32,
pub a: i64,
pub b: u32,
}

pub const T2C: i32 = 5;
Expand Down
5 changes: 3 additions & 2 deletions testcrate/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ fn t2() {
"bad T2Bar align",
"bad T2Bar signed",
"bad T2Baz size",
"bad T2Baz align",
"bad field size a of T2Baz",
"bad field offset a of T2Baz",
"bad field type a of T2Baz",
"bad field offset b of T2Baz",
"bad field type b of T2Baz",
"bad T2a function pointer",
"bad T2C value",
];
Expand Down

0 comments on commit 8e619cd

Please sign in to comment.