Skip to content

Commit 4ea949f

Browse files
committed
Allow blocklisting anonymous enums
Note: This will also block anonymous enums with no variants, but adding a typedef for such an enum should be useless anyway.
1 parent 97ab915 commit 4ea949f

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

bindgen-tests/tests/expectations/tests/blocklist-anon-enum.rs

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/issue-1025-unknown-enum-repr.rs

+20-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: --blocklist-item 'SHOULD_BE_BLOCKED.*'
2+
3+
enum {
4+
SHOULD_BE_BLOCKED_1,
5+
SHOULD_BE_BLOCKED_2,
6+
SHOULD_BE_BLOCKED_3
7+
};

bindgen-tests/tests/headers/issue-1025-unknown-enum-repr.hpp

+16
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,19 @@
22
template <typename> class a {
33
enum {};
44
};
5+
6+
template <typename> class b {
7+
enum {
8+
SOME_VARIANT
9+
};
10+
};
11+
12+
template <typename> class c {
13+
enum my_enum {
14+
MY_ENUM_SOME_VARIANT
15+
};
16+
};
17+
18+
template <typename> class d {
19+
enum no_variant_enum {};
20+
};

bindgen/codegen/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -3654,6 +3654,18 @@ impl CodeGenerator for Enum {
36543654
let layout = enum_ty.layout(ctx);
36553655
let variation = self.computed_enum_variation(ctx, item);
36563656

3657+
// blocklist anonymous enums if all variants match a regex.
3658+
// Note: This will also block anonymous enums with no variants, but
3659+
// adding a typedef for such an enum should be useless anyway.
3660+
if enum_ty.name().is_none() &&
3661+
self.variants()
3662+
.iter()
3663+
.all(|v| ctx.options().blocklisted_items.matches(v.name()))
3664+
{
3665+
debug!("Blocklisting anonymous enum.");
3666+
return;
3667+
}
3668+
36573669
let repr_translated;
36583670
let repr = match self.repr().map(|repr| ctx.resolve_type(repr)) {
36593671
Some(repr)

bindgen/ir/enum_ty.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ impl Enum {
150150
Ok(Enum::new(repr, variants))
151151
}
152152

153-
fn is_matching_enum(
153+
/// Checks if the enum matches any of the regular expressions in `enums`
154+
///
155+
/// For anonymous enums, returns true if any of the enum variants matches
156+
/// any of the regular expressions in `enums`.
157+
pub(crate) fn is_matching_enum(
154158
&self,
155159
ctx: &BindgenContext,
156160
enums: &RegexSet,

0 commit comments

Comments
 (0)