From d18faeebfe08ec816ef59c203de0faeb632f75b1 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Fri, 5 Jul 2024 11:41:40 -0700 Subject: [PATCH 1/2] Add FieldInfo::field_type_name The relevance of making a ield private may depend on a field's type. Some fields should be protected against manipulation by Rust code for the same reason `Vec::set_len` is `unsafe`. --- CHANGELOG.md | 1 + bindgen/callbacks.rs | 2 ++ bindgen/codegen/mod.rs | 3 +++ 3 files changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a57f07e6e..04fc283d5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -238,6 +238,7 @@ - Add option to use DST structs for flexible arrays (--flexarray-dst, #2772). - Add option to dynamically load variables (#2812). - Add option in CLI to use rustified non-exhaustive enums (--rustified-non-exhaustive-enum, #2847). +- Add field_type_name to FieldInfo. ## Changed - Remove which and lazy-static dependencies (#2809, #2817). - Generate compile-time layout tests (#2787). diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs index 71ed325405..c824f92bc9 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -280,4 +280,6 @@ pub struct FieldInfo<'a> { pub type_name: &'a str, /// The name of the field. pub field_name: &'a str, + /// The name of the type of the field. + pub field_type_name: Option<&'a str>, } diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index 1561d4d8cd..40168bfb18 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -1544,6 +1544,7 @@ impl FieldCodegen<'_> for FieldData { cb.field_visibility(FieldInfo { type_name: &parent_item.canonical_name(ctx), field_name, + field_type_name: field_ty.name(), }) }), self.annotations(), @@ -1949,6 +1950,7 @@ impl<'a> FieldCodegen<'a> for Bitfield { let bitfield_ty_item = ctx.resolve_item(self.ty()); let bitfield_ty = bitfield_ty_item.expect_type(); + let bitfield_ty_ident = bitfield_ty.name(); let bitfield_ty_layout = bitfield_ty .layout(ctx) @@ -1973,6 +1975,7 @@ impl<'a> FieldCodegen<'a> for Bitfield { cb.field_visibility(FieldInfo { type_name: &parent_item.canonical_name(ctx), field_name, + field_type_name: bitfield_ty_ident, }) }) }); From c973dc9185e3c4740e6893d050a98fa10cb46888 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Sun, 1 Dec 2024 22:57:10 -0500 Subject: [PATCH 2/2] Expose the name of the inner type of an alias --- bindgen/codegen/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index 40168bfb18..2fa0839ac5 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -1118,6 +1118,9 @@ impl CodeGenerator for Type { cb.field_visibility(FieldInfo { type_name: &item.canonical_name(ctx), field_name: "0", + field_type_name: inner_item + .expect_type() + .name(), }) }) .unwrap_or(ctx.options().default_visibility);