Skip to content
Closed
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
126 changes: 126 additions & 0 deletions bindgen-tests/tests/expectations/tests/packed_embeds_aligned.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions bindgen-tests/tests/headers/packed_embeds_aligned.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
struct inner1 {
char a;
char b;
} __attribute__((aligned(2)));

struct inner2 {
struct inner1 a;
struct inner1 b;
} __attribute__((aligned(2)));

struct outer1 {
short a;
struct inner1 b;
} __attribute__((packed, aligned(2)));

struct outer2 {
short a;
struct inner2 b;
} __attribute__((packed, aligned(2)));
8 changes: 4 additions & 4 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2124,7 +2124,7 @@ impl CodeGenerator for CompInfo {
}

if let Some(layout) = layout {
if struct_layout.requires_explicit_align(layout) {
if self.needs_explicit_align() {
if layout.align == 1 {
packed = true;
} else {
Expand All @@ -2145,7 +2145,7 @@ impl CodeGenerator for CompInfo {
// TODO(emilio): It'd be nice to unify this with the struct path
// above somehow.
let layout = layout.expect("Unable to get layout information?");
if struct_layout.requires_explicit_align(layout) {
if self.needs_explicit_align() {
explicit_align = Some(layout.align);
}

Expand Down Expand Up @@ -2202,8 +2202,8 @@ impl CodeGenerator for CompInfo {
// "packed" attr is redundant, and do not include it if so.
if packed &&
!is_opaque &&
!(explicit_align.is_some() &&
self.already_packed(ctx).unwrap_or(false))
!((explicit_align.is_some() || self.contains_aligned_type(ctx)) &&
self.already_packed(ctx, layout).unwrap_or(false))
{
let n = layout.map_or(1, |l| l.align);
assert!(ctx.options().rust_features().repr_packed_n || n == 1);
Expand Down
29 changes: 0 additions & 29 deletions bindgen/codegen/struct_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub(crate) struct StructLayoutTracker<'a> {
latest_offset: usize,
padding_count: usize,
latest_field_layout: Option<Layout>,
max_field_align: usize,
last_field_was_bitfield: bool,
visibility: FieldVisibilityKind,
}
Expand Down Expand Up @@ -108,7 +107,6 @@ impl<'a> StructLayoutTracker<'a> {
latest_offset: 0,
padding_count: 0,
latest_field_layout: None,
max_field_align: 0,
last_field_was_bitfield: false,
}
}
Expand All @@ -127,7 +125,6 @@ impl<'a> StructLayoutTracker<'a> {
let ptr_size = self.ctx.target_pointer_size();
self.latest_offset += ptr_size;
self.latest_field_layout = Some(Layout::new(ptr_size, ptr_size));
self.max_field_align = ptr_size;
}

pub(crate) fn saw_base(&mut self, base_ty: &Type) {
Expand All @@ -137,7 +134,6 @@ impl<'a> StructLayoutTracker<'a> {

self.latest_offset += self.padding_bytes(layout) + layout.size;
self.latest_field_layout = Some(layout);
self.max_field_align = cmp::max(self.max_field_align, layout.align);
}
}

Expand All @@ -156,7 +152,6 @@ impl<'a> StructLayoutTracker<'a> {

self.latest_field_layout = Some(layout);
self.last_field_was_bitfield = true;
self.max_field_align = cmp::max(self.max_field_align, layout.align);
}

/// Returns a padding field if necessary for a given new field _before_
Expand Down Expand Up @@ -265,8 +260,6 @@ impl<'a> StructLayoutTracker<'a> {

self.latest_offset += field_layout.size;
self.latest_field_layout = Some(field_layout);
self.max_field_align =
cmp::max(self.max_field_align, field_layout.align);
self.last_field_was_bitfield = false;

debug!(
Expand Down Expand Up @@ -365,26 +358,6 @@ impl<'a> StructLayoutTracker<'a> {
}
}

pub(crate) fn requires_explicit_align(&self, layout: Layout) -> bool {
let repr_align = self.ctx.options().rust_features().repr_align;

// Always force explicit repr(align) for stuff more than 16-byte aligned
// to work-around https://github.com/rust-lang/rust/issues/54341.
//
// Worst-case this just generates redundant alignment attributes.
if repr_align && self.max_field_align >= 16 {
return true;
}

if self.max_field_align >= layout.align {
return false;
}

// We can only generate up-to a 8-bytes of alignment unless we support
// repr(align).
repr_align || layout.align <= MAX_GUARANTEED_ALIGN
}

fn padding_bytes(&self, layout: Layout) -> usize {
align_to(self.latest_offset, layout.align) - self.latest_offset
}
Expand All @@ -400,8 +373,6 @@ impl<'a> StructLayoutTracker<'a> {
Span::call_site(),
);

self.max_field_align = cmp::max(self.max_field_align, layout.align);

let vis = super::access_specifier(self.visibility);

quote! {
Expand Down
Loading