diff --git a/bindgen-tests/tests/parse_callbacks/item_discovery_callback/mod.rs b/bindgen-tests/tests/parse_callbacks/item_discovery_callback/mod.rs index 93a2b029d7..ce458f1a2f 100644 --- a/bindgen-tests/tests/parse_callbacks/item_discovery_callback/mod.rs +++ b/bindgen-tests/tests/parse_callbacks/item_discovery_callback/mod.rs @@ -4,7 +4,9 @@ use std::rc::Rc; use regex::Regex; -use bindgen::callbacks::{DiscoveredItem, DiscoveredItemId, ParseCallbacks}; +use bindgen::callbacks::{ + DiscoveredItem, DiscoveredItemId, Layout, ParseCallbacks, +}; use bindgen::Builder; #[derive(Debug, Default)] @@ -37,6 +39,11 @@ pub fn test_item_discovery_callback() { DiscoveredItem::Struct { original_name: Some("NamedStruct".to_string()), final_name: "NamedStruct".to_string(), + layout: Some(Layout { + size: 0, + align: 1, + packed: false, + }), }, ), ( @@ -78,6 +85,11 @@ pub fn test_item_discovery_callback() { DiscoveredItem::Struct { original_name: None, final_name: "_bindgen_ty_*".to_string(), + layout: Some(Layout { + size: 0, + align: 1, + packed: false, + }), }, ), ( @@ -160,6 +172,7 @@ pub fn compare_struct_info( let DiscoveredItem::Struct { original_name: expected_original_name, final_name: expected_final_name, + layout: expected_layout, } = expected_item else { unreachable!() @@ -168,6 +181,7 @@ pub fn compare_struct_info( let DiscoveredItem::Struct { original_name: generated_original_name, final_name: generated_final_name, + layout: generated_layout, } = generated_item else { unreachable!() @@ -177,12 +191,22 @@ pub fn compare_struct_info( return false; } - match (expected_original_name, generated_original_name) { + if !match (expected_original_name, generated_original_name) { (None, None) => true, (Some(expected_original_name), Some(generated_original_name)) => { compare_names(expected_original_name, generated_original_name) } _ => false, + } { + return false; + } + + match (expected_layout, generated_layout) { + (None, None) => true, + (Some(expected_layout), Some(actual_layout)) => { + expected_layout == actual_layout + } + _ => false, } } diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs index c2be66828a..d534b407c8 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -4,6 +4,7 @@ pub use crate::ir::analysis::DeriveTrait; pub use crate::ir::derive::CanDerive as ImplementsTrait; pub use crate::ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue}; pub use crate::ir::int::IntKind; +pub use crate::ir::layout::Layout; use std::fmt; /// An enum to allow ignoring parsing of macros. @@ -192,6 +193,10 @@ pub enum DiscoveredItem { /// The name of the generated binding final_name: String, + + /// The layout of the structure in memory (size, alignment, etc.) if + /// known. + layout: Option, }, /// Represents a union with its original name in C and its generated binding name diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index f5518e432d..69fcfdf118 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -2490,6 +2490,7 @@ impl CodeGenerator for CompInfo { .name() .map(String::from), final_name: canonical_ident.to_string(), + layout, }, CompKind::Union => DiscoveredItem::Union { original_name: item diff --git a/bindgen/ir/layout.rs b/bindgen/ir/layout.rs index 905e47c732..2920fa5a72 100644 --- a/bindgen/ir/layout.rs +++ b/bindgen/ir/layout.rs @@ -7,14 +7,14 @@ use crate::ir::context::BindgenContext; use std::cmp; /// A type that represents the struct layout of a type. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) struct Layout { +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] +pub struct Layout { /// The size (in bytes) of this layout. - pub(crate) size: usize, + pub size: usize, /// The alignment (in bytes) of this layout. - pub(crate) align: usize, + pub align: usize, /// Whether this layout's members are packed or not. - pub(crate) packed: bool, + pub packed: bool, } #[test]