diff --git a/bindgen-tests/tests/expectations/tests/blocklist-anon-enum.rs b/bindgen-tests/tests/expectations/tests/blocklist-anon-enum.rs new file mode 100644 index 0000000000..fe64295a68 --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/blocklist-anon-enum.rs @@ -0,0 +1 @@ +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] \ No newline at end of file diff --git a/bindgen-tests/tests/expectations/tests/issue-1025-unknown-enum-repr.rs b/bindgen-tests/tests/expectations/tests/issue-1025-unknown-enum-repr.rs index 6e27b94ca9..48ec9674d9 100644 --- a/bindgen-tests/tests/expectations/tests/issue-1025-unknown-enum-repr.rs +++ b/bindgen-tests/tests/expectations/tests/issue-1025-unknown-enum-repr.rs @@ -4,4 +4,23 @@ pub struct a { pub _address: u8, } -pub type a__bindgen_ty_1 = i32; +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct b { + pub _address: u8, +} +pub const b_SOME_VARIANT: b__bindgen_ty_1 = 0; +pub type b__bindgen_ty_1 = i32; +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct c { + pub _address: u8, +} +pub const c_my_enum_MY_ENUM_SOME_VARIANT: c_my_enum = 0; +pub type c_my_enum = i32; +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct d { + pub _address: u8, +} +pub type d_no_variant_enum = i32; diff --git a/bindgen-tests/tests/headers/blocklist-anon-enum.hpp b/bindgen-tests/tests/headers/blocklist-anon-enum.hpp new file mode 100644 index 0000000000..62ca89b123 --- /dev/null +++ b/bindgen-tests/tests/headers/blocklist-anon-enum.hpp @@ -0,0 +1,7 @@ +// bindgen-flags: --blocklist-item 'SHOULD_BE_BLOCKED.*' + +enum { + SHOULD_BE_BLOCKED_1, + SHOULD_BE_BLOCKED_2, + SHOULD_BE_BLOCKED_3 +}; diff --git a/bindgen-tests/tests/headers/issue-1025-unknown-enum-repr.hpp b/bindgen-tests/tests/headers/issue-1025-unknown-enum-repr.hpp index 589b3c25f3..b38df4e521 100644 --- a/bindgen-tests/tests/headers/issue-1025-unknown-enum-repr.hpp +++ b/bindgen-tests/tests/headers/issue-1025-unknown-enum-repr.hpp @@ -2,3 +2,19 @@ template class a { enum {}; }; + +template class b { + enum { + SOME_VARIANT + }; +}; + +template class c { + enum my_enum { + MY_ENUM_SOME_VARIANT + }; +}; + +template class d { + enum no_variant_enum {}; +}; \ No newline at end of file diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index 8da10ff051..3d834771f5 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -3654,6 +3654,18 @@ impl CodeGenerator for Enum { let layout = enum_ty.layout(ctx); let variation = self.computed_enum_variation(ctx, item); + // blocklist anonymous enums if all variants match a regex. + // Note: This will also block anonymous enums with no variants, but + // adding a typedef for such an enum should be useless anyway. + if enum_ty.name().is_none() && + self.variants() + .iter() + .all(|v| ctx.options().blocklisted_items.matches(v.name())) + { + debug!("Blocklisting anonymous enum."); + return; + } + let repr_translated; let repr = match self.repr().map(|repr| ctx.resolve_type(repr)) { Some(repr) diff --git a/bindgen/ir/enum_ty.rs b/bindgen/ir/enum_ty.rs index 9b08da3bce..2f0fff118d 100644 --- a/bindgen/ir/enum_ty.rs +++ b/bindgen/ir/enum_ty.rs @@ -150,7 +150,11 @@ impl Enum { Ok(Enum::new(repr, variants)) } - fn is_matching_enum( + /// Checks if the enum matches any of the regular expressions in `enums` + /// + /// For anonymous enums, returns true if any of the enum variants matches + /// any of the regular expressions in `enums`. + pub(crate) fn is_matching_enum( &self, ctx: &BindgenContext, enums: &RegexSet,