summaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
authorNick Fitzgerald <fitzgen@gmail.com>2017-11-01 14:36:08 -0700
committerNick Fitzgerald <fitzgen@gmail.com>2017-11-02 09:51:09 -0700
commit79dbe8b8062076f8ac3f76c0eaf4846dcb8c4638 (patch)
treec7fbf6bbb0134e13a1ebaea38c342f02daaaa69c /src/codegen
parentf059edee4a6b165323623af0c993d3c3889ed2c1 (diff)
Detect `#pragma pack(...)` and make `pack(n)` where `n > 1` opaque
This is a bandaid for #537. It does *not* fix the underlying issue, which requires `#[repr(packed = "N")]` support in Rust. However, it does make sure that we don't generate type definitions with the wrong layout, or fail our generated layout tests.
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/mod.rs8
-rw-r--r--src/codegen/struct_layout.rs9
2 files changed, 11 insertions, 6 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index 2cbde732..50de296e 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -1394,7 +1394,9 @@ impl CodeGenerator for CompInfo {
let used_template_params = item.used_template_params(ctx);
- let mut packed = self.packed();
+ let ty = item.expect_type();
+ let layout = ty.layout(ctx);
+ let mut packed = self.is_packed(ctx, &layout);
// generate tuple struct if struct or union is a forward declaration,
// skip for now if template parameters are needed.
@@ -1431,7 +1433,7 @@ impl CodeGenerator for CompInfo {
let is_opaque = item.is_opaque(ctx, &());
let mut fields = vec![];
let mut struct_layout =
- StructLayoutTracker::new(ctx, self, &canonical_name);
+ StructLayoutTracker::new(ctx, self, ty, &canonical_name);
if !is_opaque {
if item.has_vtable_ptr(ctx) {
@@ -1618,7 +1620,7 @@ impl CodeGenerator for CompInfo {
if let Some(comment) = item.comment(ctx) {
attributes.push(attributes::doc(comment));
}
- if packed {
+ if packed && !is_opaque {
attributes.push(attributes::repr_list(&["C", "packed"]));
} else {
attributes.push(attributes::repr("C"));
diff --git a/src/codegen/struct_layout.rs b/src/codegen/struct_layout.rs
index ca46947d..32b48965 100644
--- a/src/codegen/struct_layout.rs
+++ b/src/codegen/struct_layout.rs
@@ -16,6 +16,7 @@ pub struct StructLayoutTracker<'a> {
name: &'a str,
ctx: &'a BindgenContext,
comp: &'a CompInfo,
+ is_packed: bool,
latest_offset: usize,
padding_count: usize,
latest_field_layout: Option<Layout>,
@@ -81,12 +82,14 @@ impl<'a> StructLayoutTracker<'a> {
pub fn new(
ctx: &'a BindgenContext,
comp: &'a CompInfo,
+ ty: &'a Type,
name: &'a str,
) -> Self {
StructLayoutTracker {
name: name,
ctx: ctx,
comp: comp,
+ is_packed: comp.is_packed(ctx, &ty.layout(ctx)),
latest_offset: 0,
padding_count: 0,
latest_field_layout: None,
@@ -180,7 +183,7 @@ impl<'a> StructLayoutTracker<'a> {
let will_merge_with_bitfield = self.align_to_latest_field(field_layout);
- let padding_layout = if self.comp.packed() {
+ let padding_layout = if self.is_packed {
None
} else {
let padding_bytes = match field_offset {
@@ -269,7 +272,7 @@ impl<'a> StructLayoutTracker<'a> {
self.latest_field_layout.unwrap().align) ||
layout.align > mem::size_of::<*mut ()>())
{
- let layout = if self.comp.packed() {
+ let layout = if self.is_packed {
Layout::new(padding_bytes, 1)
} else if self.last_field_was_bitfield ||
layout.align > mem::size_of::<*mut ()>()
@@ -316,7 +319,7 @@ impl<'a> StructLayoutTracker<'a> {
///
/// This is just to avoid doing the same check also in pad_field.
fn align_to_latest_field(&mut self, new_field_layout: Layout) -> bool {
- if self.comp.packed() {
+ if self.is_packed {
// Skip to align fields when packed.
return false;
}