Skip to content
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

Report enums in ParseCallbacks. #3133

Merged
merged 1 commit into from
Feb 17, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Unions
// Structs
void function_using_anonymous_struct(struct {} arg0);

struct NamedStruct {
Expand All @@ -13,4 +13,16 @@ void function_using_anonymous_union(union {} arg0);
union NamedUnion {
};

typedef union NamedUnion AliasOfNamedUnion;
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;
40 changes: 40 additions & 0 deletions bindgen-tests/tests/parse_callbacks/item_discovery_callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -126,6 +139,9 @@ fn compare_item_info(
expected,
generated,
),
DiscoveredItem::Enum { .. } => {
compare_enum_info(expected_item, generated_item)
}
}
}

Expand Down Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion bindgen/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre-existing but all these string allocations make me sad, maybe we should just hold an &'a str, but that's a behavior change so never mind for this patch :)

);
});

let mut builder =
EnumBuilder::new(&name, attrs, &repr, variation, has_typedef);

Expand Down
Loading