From 58805949e12abaa00c0ddc1f6587bbd371a35a9e Mon Sep 17 00:00:00 2001 From: Adrian Taylor Date: Sun, 16 Feb 2025 15:57:46 +0000 Subject: [PATCH] Report enums in ParseCallbacks. ParseCallbacks previously reported structs but not enums. Enhance it to do so. At the moment, little information is provided about enums - but bindgen doesn't handle (rare) anonymous enums so this seems the right amount of information to report. At the moment, effectively this just provides a mapping between name and DiscoveredItemId. One of a number of PRs I'll be raising for https://github.com/google/autocxx/issues/124. In future PRs I'll be hoping to add further callbacks which report more information based on DiscoveredItemId, so having the DiscoveredItemId for each enum is an important pre-requisite. --- .../header_item_discovery.h | 16 +++++++- .../item_discovery_callback/mod.rs | 40 +++++++++++++++++++ bindgen/callbacks.rs | 9 ++++- bindgen/codegen/mod.rs | 9 +++++ 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/bindgen-tests/tests/parse_callbacks/item_discovery_callback/header_item_discovery.h b/bindgen-tests/tests/parse_callbacks/item_discovery_callback/header_item_discovery.h index 10e97ea480..b2bb04f15f 100644 --- a/bindgen-tests/tests/parse_callbacks/item_discovery_callback/header_item_discovery.h +++ b/bindgen-tests/tests/parse_callbacks/item_discovery_callback/header_item_discovery.h @@ -1,4 +1,4 @@ -// Unions +// Structs void function_using_anonymous_struct(struct {} arg0); struct NamedStruct { @@ -13,4 +13,16 @@ void function_using_anonymous_union(union {} arg0); union NamedUnion { }; -typedef union NamedUnion AliasOfNamedUnion; \ No newline at end of file +typedef union NamedUnion AliasOfNamedUnion; + +// Enums + +// We don't include an anonymous enum because such enums +// are not visible outside the function, and thus tend not +// to be useful - bindgen doesn't handle them for this reason. + +enum NamedEnum { + Fish, +}; + +typedef enum NamedEnum AliasOfNamedEnum; \ No newline at end of file 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 74af110d00..93a2b029d7 100644 --- a/bindgen-tests/tests/parse_callbacks/item_discovery_callback/mod.rs +++ b/bindgen-tests/tests/parse_callbacks/item_discovery_callback/mod.rs @@ -60,6 +60,19 @@ pub fn test_item_discovery_callback() { alias_for: DiscoveredItemId::new(20), }, ), + ( + DiscoveredItemId::new(27), + DiscoveredItem::Alias { + alias_name: "AliasOfNamedEnum".to_string(), + alias_for: DiscoveredItemId::new(24), + }, + ), + ( + DiscoveredItemId::new(24), + DiscoveredItem::Enum { + final_name: "NamedEnum".to_string(), + }, + ), ( DiscoveredItemId::new(30), DiscoveredItem::Struct { @@ -126,6 +139,9 @@ fn compare_item_info( expected, generated, ), + DiscoveredItem::Enum { .. } => { + compare_enum_info(expected_item, generated_item) + } } } @@ -203,6 +219,30 @@ pub fn compare_union_info( } } +pub fn compare_enum_info( + expected_item: &DiscoveredItem, + generated_item: &DiscoveredItem, +) -> bool { + let DiscoveredItem::Enum { + final_name: expected_final_name, + } = expected_item + else { + unreachable!() + }; + + let DiscoveredItem::Enum { + final_name: generated_final_name, + } = generated_item + else { + unreachable!() + }; + + if !compare_names(expected_final_name, generated_final_name) { + return false; + } + true +} + pub fn compare_alias_info( expected_item: &DiscoveredItem, generated_item: &DiscoveredItem, diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs index 8a21e98dea..c2be66828a 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -217,7 +217,14 @@ pub enum DiscoveredItem { /// The identifier of the discovered type alias_for: DiscoveredItemId, - }, // functions, modules, etc. + }, + + /// Represents an enum. + Enum { + /// The final name of the generated binding + final_name: String, + }, + // functions, modules, etc. } /// Relevant information about a type to which new derive attributes will be added using diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index a899ac4de9..f5518e432d 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -3770,6 +3770,15 @@ impl CodeGenerator for Enum { let repr = repr.to_rust_ty_or_opaque(ctx, item); let has_typedef = ctx.is_enum_typedef_combo(item.id()); + ctx.options().for_each_callback(|cb| { + cb.new_item_found( + DiscoveredItemId::new(item.id().as_usize()), + DiscoveredItem::Enum { + final_name: name.to_string(), + }, + ); + }); + let mut builder = EnumBuilder::new(&name, attrs, &repr, variation, has_typedef);