Skip to content

Include struct layout in callbacks. #3143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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
28 changes: 26 additions & 2 deletions bindgen-tests/tests/parse_callbacks/item_discovery_callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
}),
},
),
(
Expand Down Expand Up @@ -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,
}),
},
),
(
Expand Down Expand Up @@ -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!()
Expand All @@ -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!()
Expand All @@ -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,
}
}

Expand Down
5 changes: 5 additions & 0 deletions bindgen/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Layout>,
},

/// Represents a union with its original name in C and its generated binding name
Expand Down
1 change: 1 addition & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions bindgen/ir/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading