Skip to content

Commit 23d9229

Browse files
committed
codegen: Properly track alignment of unions.
This makes us not unnecessarily add repr(align) to unions. Closes #1498.
1 parent 3dd3ce7 commit 23d9229

36 files changed

+179
-54
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ Released 2019/03/04
101101
* Default rust target was changed to 1.33, which means that bindgen can get much
102102
more often the layout of structs right. [#1529][]
103103

104+
## Fixed
105+
106+
* Bindgen will output repr(align) just when needed for unions. [#1498][]
107+
104108
[#1529]: https://github.com/rust-lang-nursery/rust-bindgen/issues/1529
109+
[#1498]: https://github.com/rust-lang-nursery/rust-bindgen/issues/1498
105110

106111
--------------------------------------------------------------------------------
107112

src/codegen/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,7 @@ impl CodeGenerator for CompInfo {
15891589
// TODO(emilio): It'd be nice to unify this with the struct path
15901590
// above somehow.
15911591
let layout = layout.expect("Unable to get layout information?");
1592+
struct_layout.saw_union(layout);
15921593

15931594
if struct_layout.requires_explicit_align(layout) {
15941595
explicit_align = Some(layout.align);
@@ -1600,8 +1601,6 @@ impl CodeGenerator for CompInfo {
16001601
_bindgen_union_align: #ty ,
16011602
}
16021603
} else {
1603-
struct_layout.saw_union(layout);
1604-
16051604
quote! {
16061605
pub bindgen_union_field: #ty ,
16071606
}

src/codegen/struct_layout.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ impl<'a> StructLayoutTracker<'a> {
8585
name: &'a str,
8686
) -> Self {
8787
StructLayoutTracker {
88-
name: name,
89-
ctx: ctx,
90-
comp: comp,
88+
name,
89+
ctx,
90+
comp,
9191
is_packed: comp.is_packed(ctx, &ty.layout(ctx)),
9292
latest_offset: 0,
9393
padding_count: 0,

tests/expectations/tests/16-byte-alignment.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub struct rte_ipv4_tuple {
1515
pub __bindgen_anon_1: rte_ipv4_tuple__bindgen_ty_1,
1616
}
1717
#[repr(C)]
18-
#[repr(align(4))]
1918
#[derive(Copy, Clone)]
2019
pub union rte_ipv4_tuple__bindgen_ty_1 {
2120
pub __bindgen_anon_1: rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1,
@@ -149,7 +148,6 @@ pub struct rte_ipv6_tuple {
149148
pub __bindgen_anon_1: rte_ipv6_tuple__bindgen_ty_1,
150149
}
151150
#[repr(C)]
152-
#[repr(align(4))]
153151
#[derive(Copy, Clone)]
154152
pub union rte_ipv6_tuple__bindgen_ty_1 {
155153
pub __bindgen_anon_1: rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1,

tests/expectations/tests/anon_struct_in_union.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub struct s {
1313
pub u: s__bindgen_ty_1,
1414
}
1515
#[repr(C)]
16-
#[repr(align(4))]
1716
#[derive(Copy, Clone)]
1817
pub union s__bindgen_ty_1 {
1918
pub field: s__bindgen_ty_1_inner,

tests/expectations/tests/anon_union.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ pub struct TErrorResult_DOMExceptionInfo {
3333
_unused: [u8; 0],
3434
}
3535
#[repr(C)]
36-
#[repr(align(8))]
3736
pub union TErrorResult__bindgen_ty_1 {
3837
pub mMessage: *mut TErrorResult_Message,
3938
pub mDOMExceptionInfo: *mut TErrorResult_DOMExceptionInfo,

tests/expectations/tests/class.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ impl Default for IncompleteArrayNonCopiable {
335335
}
336336
}
337337
#[repr(C)]
338-
#[repr(align(4))]
339338
#[derive(Copy, Clone)]
340339
pub union Union {
341340
pub d: f32,

tests/expectations/tests/class_with_inner_struct.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ fn bindgen_test_layout_A_Segment() {
5454
);
5555
}
5656
#[repr(C)]
57-
#[repr(align(4))]
5857
#[derive(Copy, Clone)]
5958
pub union A__bindgen_ty_1 {
6059
pub f: ::std::os::raw::c_int,
@@ -89,7 +88,6 @@ impl Default for A__bindgen_ty_1 {
8988
}
9089
}
9190
#[repr(C)]
92-
#[repr(align(4))]
9391
#[derive(Copy, Clone)]
9492
pub union A__bindgen_ty_2 {
9593
pub d: ::std::os::raw::c_int,
@@ -233,7 +231,6 @@ pub struct C {
233231
pub __bindgen_anon_1: C__bindgen_ty_1,
234232
}
235233
#[repr(C)]
236-
#[repr(align(4))]
237234
#[derive(Copy, Clone)]
238235
pub union C__bindgen_ty_1 {
239236
pub mFunc: C__bindgen_ty_1__bindgen_ty_1,

tests/expectations/tests/derive-debug-mangle-name.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub struct perf_event_attr {
1515
pub __bindgen_anon_1: perf_event_attr__bindgen_ty_1,
1616
}
1717
#[repr(C)]
18-
#[repr(align(4))]
1918
#[derive(Copy, Clone)]
2019
pub union perf_event_attr__bindgen_ty_1 {
2120
pub b: ::std::os::raw::c_int,

tests/expectations/tests/derive-partialeq-anonfield.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub struct rte_mbuf {
1414
pub __bindgen_anon_1: rte_mbuf__bindgen_ty_1,
1515
}
1616
#[repr(C)]
17-
#[repr(align(1))]
1817
#[derive(Copy, Clone)]
1918
pub union rte_mbuf__bindgen_ty_1 {
2019
_bindgen_union_align: [u8; 0usize],

tests/expectations/tests/derive-partialeq-pointer.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ pub struct c {
4141
pub __bindgen_anon_1: c__bindgen_ty_1,
4242
}
4343
#[repr(C)]
44-
#[repr(align(1))]
4544
#[derive(Copy, Clone)]
4645
pub union c__bindgen_ty_1 {
4746
_bindgen_union_align: u8,

tests/expectations/tests/derive-partialeq-union.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
/// Deriving PartialEq for rust unions is not supported.
1111
#[repr(C)]
12-
#[repr(align(4))]
1312
#[derive(Copy, Clone)]
1413
pub union ShouldNotDerivePartialEq {
1514
pub a: ::std::os::raw::c_char,

tests/expectations/tests/issue-1285.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub struct foo {
1212
pub bar: foo__bindgen_ty_1,
1313
}
1414
#[repr(C)]
15-
#[repr(align(4))]
1615
pub union foo__bindgen_ty_1 {
1716
pub a: ::std::os::raw::c_uint,
1817
pub b: ::std::os::raw::c_ushort,
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
10+
#[repr(C, packed)]
11+
#[derive(Copy, Clone)]
12+
pub struct rte_memseg {
13+
///< Start physical address.
14+
pub phys_addr: u64,
15+
pub __bindgen_anon_1: rte_memseg__bindgen_ty_1,
16+
///< Length of the segment.
17+
pub len: usize,
18+
///< The pagesize of underlying memory
19+
pub hugepage_sz: u64,
20+
///< NUMA socket ID.
21+
pub socket_id: i32,
22+
///< Number of channels.
23+
pub nchannel: u32,
24+
///< Number of ranks.
25+
pub nrank: u32,
26+
}
27+
#[repr(C)]
28+
#[derive(Copy, Clone)]
29+
pub union rte_memseg__bindgen_ty_1 {
30+
///< Start virtual address.
31+
pub addr: *mut ::std::os::raw::c_void,
32+
///< Makes sure addr is always 64 bits
33+
pub addr_64: u64,
34+
_bindgen_union_align: u64,
35+
}
36+
#[test]
37+
fn bindgen_test_layout_rte_memseg__bindgen_ty_1() {
38+
assert_eq!(
39+
::std::mem::size_of::<rte_memseg__bindgen_ty_1>(),
40+
8usize,
41+
concat!("Size of: ", stringify!(rte_memseg__bindgen_ty_1))
42+
);
43+
assert_eq!(
44+
::std::mem::align_of::<rte_memseg__bindgen_ty_1>(),
45+
8usize,
46+
concat!("Alignment of ", stringify!(rte_memseg__bindgen_ty_1))
47+
);
48+
assert_eq!(
49+
unsafe { &(*(::std::ptr::null::<rte_memseg__bindgen_ty_1>())).addr as *const _ as usize },
50+
0usize,
51+
concat!(
52+
"Offset of field: ",
53+
stringify!(rte_memseg__bindgen_ty_1),
54+
"::",
55+
stringify!(addr)
56+
)
57+
);
58+
assert_eq!(
59+
unsafe {
60+
&(*(::std::ptr::null::<rte_memseg__bindgen_ty_1>())).addr_64 as *const _ as usize
61+
},
62+
0usize,
63+
concat!(
64+
"Offset of field: ",
65+
stringify!(rte_memseg__bindgen_ty_1),
66+
"::",
67+
stringify!(addr_64)
68+
)
69+
);
70+
}
71+
impl Default for rte_memseg__bindgen_ty_1 {
72+
fn default() -> Self {
73+
unsafe { ::std::mem::zeroed() }
74+
}
75+
}
76+
#[test]
77+
fn bindgen_test_layout_rte_memseg() {
78+
assert_eq!(
79+
::std::mem::size_of::<rte_memseg>(),
80+
44usize,
81+
concat!("Size of: ", stringify!(rte_memseg))
82+
);
83+
assert_eq!(
84+
::std::mem::align_of::<rte_memseg>(),
85+
1usize,
86+
concat!("Alignment of ", stringify!(rte_memseg))
87+
);
88+
assert_eq!(
89+
unsafe { &(*(::std::ptr::null::<rte_memseg>())).phys_addr as *const _ as usize },
90+
0usize,
91+
concat!(
92+
"Offset of field: ",
93+
stringify!(rte_memseg),
94+
"::",
95+
stringify!(phys_addr)
96+
)
97+
);
98+
assert_eq!(
99+
unsafe { &(*(::std::ptr::null::<rte_memseg>())).len as *const _ as usize },
100+
16usize,
101+
concat!(
102+
"Offset of field: ",
103+
stringify!(rte_memseg),
104+
"::",
105+
stringify!(len)
106+
)
107+
);
108+
assert_eq!(
109+
unsafe { &(*(::std::ptr::null::<rte_memseg>())).hugepage_sz as *const _ as usize },
110+
24usize,
111+
concat!(
112+
"Offset of field: ",
113+
stringify!(rte_memseg),
114+
"::",
115+
stringify!(hugepage_sz)
116+
)
117+
);
118+
assert_eq!(
119+
unsafe { &(*(::std::ptr::null::<rte_memseg>())).socket_id as *const _ as usize },
120+
32usize,
121+
concat!(
122+
"Offset of field: ",
123+
stringify!(rte_memseg),
124+
"::",
125+
stringify!(socket_id)
126+
)
127+
);
128+
assert_eq!(
129+
unsafe { &(*(::std::ptr::null::<rte_memseg>())).nchannel as *const _ as usize },
130+
36usize,
131+
concat!(
132+
"Offset of field: ",
133+
stringify!(rte_memseg),
134+
"::",
135+
stringify!(nchannel)
136+
)
137+
);
138+
assert_eq!(
139+
unsafe { &(*(::std::ptr::null::<rte_memseg>())).nrank as *const _ as usize },
140+
40usize,
141+
concat!(
142+
"Offset of field: ",
143+
stringify!(rte_memseg),
144+
"::",
145+
stringify!(nrank)
146+
)
147+
);
148+
}
149+
impl Default for rte_memseg {
150+
fn default() -> Self {
151+
unsafe { ::std::mem::zeroed() }
152+
}
153+
}

tests/expectations/tests/issue-493.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ pub struct basic_string___short {
8383
pub __data_: *mut basic_string_value_type,
8484
}
8585
#[repr(C)]
86-
#[repr(align(1))]
8786
pub union basic_string___short__bindgen_ty_1 {
8887
pub __size_: ::std::os::raw::c_uchar,
8988
pub __lx: basic_string_value_type,

tests/expectations/tests/jsval_layout_opaque.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ pub enum JSWhyMagic {
176176
JS_WHY_MAGIC_COUNT = 18,
177177
}
178178
#[repr(C)]
179-
#[repr(align(8))]
180179
#[derive(Copy, Clone)]
181180
pub union jsval_layout {
182181
pub asBits: u64,
@@ -259,7 +258,6 @@ pub struct jsval_layout__bindgen_ty_2 {
259258
pub payload: jsval_layout__bindgen_ty_2__bindgen_ty_1,
260259
}
261260
#[repr(C)]
262-
#[repr(align(4))]
263261
#[derive(Copy, Clone)]
264262
pub union jsval_layout__bindgen_ty_2__bindgen_ty_1 {
265263
pub i32: i32,

tests/expectations/tests/layout_eth_conf.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,6 @@ impl Default for rte_eth_conf__bindgen_ty_1 {
18301830
}
18311831
}
18321832
#[repr(C)]
1833-
#[repr(align(4))]
18341833
#[derive(Copy, Clone)]
18351834
pub union rte_eth_conf__bindgen_ty_2 {
18361835
pub vmdq_dcb_tx_conf: rte_eth_vmdq_dcb_tx_conf,

tests/expectations/tests/layout_mbuf.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ pub struct rte_mbuf {
180180
/// or non-atomic) is controlled by the CONFIG_RTE_MBUF_REFCNT_ATOMIC
181181
/// config option.
182182
#[repr(C)]
183-
#[repr(align(2))]
184183
#[derive(Copy, Clone)]
185184
pub union rte_mbuf__bindgen_ty_1 {
186185
///< Atomically accessed refcnt
@@ -230,7 +229,6 @@ impl Default for rte_mbuf__bindgen_ty_1 {
230229
}
231230
}
232231
#[repr(C)]
233-
#[repr(align(4))]
234232
#[derive(Copy, Clone)]
235233
pub union rte_mbuf__bindgen_ty_2 {
236234
///< L2/L3/L4 and tunnel information.
@@ -415,7 +413,6 @@ impl Default for rte_mbuf__bindgen_ty_2 {
415413
}
416414
}
417415
#[repr(C)]
418-
#[repr(align(4))]
419416
#[derive(Copy, Clone)]
420417
pub union rte_mbuf__bindgen_ty_3 {
421418
///< RSS hash result if RSS enabled
@@ -435,7 +432,6 @@ pub struct rte_mbuf__bindgen_ty_3__bindgen_ty_1 {
435432
pub hi: u32,
436433
}
437434
#[repr(C)]
438-
#[repr(align(4))]
439435
#[derive(Copy, Clone)]
440436
pub union rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1 {
441437
pub __bindgen_anon_1: rte_mbuf__bindgen_ty_3__bindgen_ty_1__bindgen_ty_1__bindgen_ty_1,
@@ -672,7 +668,6 @@ impl Default for rte_mbuf__bindgen_ty_3 {
672668
}
673669
}
674670
#[repr(C)]
675-
#[repr(align(8))]
676671
#[derive(Copy, Clone)]
677672
pub union rte_mbuf__bindgen_ty_4 {
678673
///< Can be used for external metadata
@@ -720,7 +715,6 @@ impl Default for rte_mbuf__bindgen_ty_4 {
720715
}
721716
}
722717
#[repr(C)]
723-
#[repr(align(8))]
724718
#[derive(Copy, Clone)]
725719
pub union rte_mbuf__bindgen_ty_5 {
726720
///< combined for easy fetch

tests/expectations/tests/struct_with_anon_union.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub struct foo {
1313
pub bar: foo__bindgen_ty_1,
1414
}
1515
#[repr(C)]
16-
#[repr(align(4))]
1716
#[derive(Copy, Clone)]
1817
pub union foo__bindgen_ty_1 {
1918
pub a: ::std::os::raw::c_uint,

tests/expectations/tests/struct_with_anon_unnamed_union.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub struct foo {
1313
pub __bindgen_anon_1: foo__bindgen_ty_1,
1414
}
1515
#[repr(C)]
16-
#[repr(align(4))]
1716
#[derive(Copy, Clone)]
1817
pub union foo__bindgen_ty_1 {
1918
pub a: ::std::os::raw::c_uint,

0 commit comments

Comments
 (0)