rust-bindgen
rust-bindgen copied to clipboard
`#pragma pack(1)` struct containing another packed struct is not packed itself.
Input C/C++ Header
#include <stdint.h>
#pragma pack(1)
typedef struct {
uint64_t value;
} first_t;
typedef struct {
first_t value;
} second_t;
#pragma pack()
Bindgen Invocation
$ bindgen input.h
Actual Results
#[repr(C, packed)]
#[derive(Debug, Copy, Clone)]
pub struct first_t {
pub value: u64,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct second_t {
pub value: first_t,
}
Expected Results
The second_t struct should be #[repr(C, packed)], not just #[repr(C)].
#[repr(C, packed)]
#[derive(Debug, Copy, Clone)]
pub struct first_t {
pub value: u64,
}
#[repr(C, packed)]
#[derive(Debug, Copy, Clone)]
pub struct second_t {
pub value: first_t,
}
The way we detect packed structs with #pragma pack() is a bit hacky, and fails for this case:
https://github.com/rust-lang/rust-bindgen/blob/4d18f7606a2bc8a18b181b3e9639c9464a9dc896/src/ir/comp.rs#L1621-L1636
It's not particularly trivial to do better I think, but maybe libclang now exposes the relevant attributes?