Skip to content

Commit

Permalink
Ignore blocklisted enum variants.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Feb 21, 2024
1 parent 9aa4ae1 commit 57ef7cf
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions bindgen/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ pub(crate) struct BindgenContext {

/// A map of all enum variants, mapping the variant name to
/// the generated constant name and/or path.
enum_variants: HashMap<String, syn::Expr>,
enum_variants: HashMap<String, (ItemId, syn::Expr)>,

/// The set of (`ItemId`s of) types that can't derive debug.
///
Expand Down Expand Up @@ -2826,15 +2826,19 @@ If you encounter an error missing from this list, please file an issue or a PR!"

use crate::EnumVariation;

let enum_canonical_name = if ty.name().is_some() {
Some(self.rust_ident(item.canonical_name(self)))
} else {
None
};

for variant in enum_ty.variants() {
let variant_name = self.rust_ident(variant.name());

let variant_expr = if ty.name().is_some() {
let enum_canonical_name =
item.canonical_name(self);
let enum_canonical_name =
self.rust_ident(enum_canonical_name);

let variant_expr = if let Some(
enum_canonical_name,
) = &enum_canonical_name
{
match variation {
EnumVariation::Rust { .. } |
EnumVariation::NewType {
Expand All @@ -2851,7 +2855,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
EnumVariation::Consts { .. } => {
let constant_name = self
.enum_variant_const_name(
Some(&enum_canonical_name),
Some(enum_canonical_name),
&variant_name,
);
syn::parse_quote! { #constant_name }
Expand All @@ -2868,7 +2872,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"

self.enum_variants.insert(
variant.name().to_owned(),
variant_expr,
(item.id(), variant_expr),
);
}
}
Expand Down Expand Up @@ -2932,12 +2936,17 @@ If you encounter an error missing from this list, please file an issue or a PR!"
}

/// Get the generated name of an enum variant.
pub(crate) fn enum_variant(&self, variant: &str) -> Option<&syn::Expr> {
pub(crate) fn enum_variant(
&self,
variant: &str,
) -> Option<(ItemId, &syn::Expr)> {
assert!(
self.in_codegen_phase(),
"We only compute enum_variants when we enter codegen",
);
self.enum_variants.get(variant)
self.enum_variants
.get(variant)
.map(|(item_id, variant)| (*item_id, variant))
}

/// Look up whether `id` refers to an `enum` whose underlying type is
Expand Down Expand Up @@ -3230,7 +3239,14 @@ impl cmacro::CodegenContext for BindgenContext {
}

fn resolve_enum_variant(&self, variant: &str) -> Option<syn::Expr> {
self.enum_variant(variant).cloned()
let (item_id, enum_variant) = self.enum_variant(variant)?;

let item = self.resolve_item(item_id);
if item.is_blocklisted(self) {
return None;
}

Some(enum_variant.clone())
}

fn resolve_ty(&self, ty: &str) -> Option<syn::Type> {
Expand Down

0 comments on commit 57ef7cf

Please sign in to comment.