Skip to content

Commit

Permalink
Report enums in ParseCallbacks.
Browse files Browse the repository at this point in the history
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 google/autocxx#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.
  • Loading branch information
adetaylor authored and emilio committed Feb 17, 2025
1 parent a10bcfd commit 5880594
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
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(),
},
);
});

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

Expand Down

0 comments on commit 5880594

Please sign in to comment.