-
Notifications
You must be signed in to change notification settings - Fork 741
Allow blocklisting anonymous enums #3067
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// bindgen-flags: --blocklist-item 'SHOULD_BE_BLOCKED.*' | ||
|
||
enum { | ||
SHOULD_BE_BLOCKED_1, | ||
SHOULD_BE_BLOCKED_2, | ||
SHOULD_BE_BLOCKED_3 | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this only block the enum if all variants, and not any are blocklisted? If not, can we document this behavior and why is it useful? thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The behavior of using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But you can individually hide variants (via annotations or callbacks), right... I'd expect allowlisting to work with any and blocklisting to work with all, if that makes sense... But let me know if you disagree, maybe my expectation is flawed somehow? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I think that's perfectly reasonable. I changed this PR to use |
||
|
||
let repr_translated; | ||
let repr = match self.repr().map(|repr| ctx.resolve_type(repr)) { | ||
Some(repr) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into #1025 and I'm assuming that the anonymous enum with no variants was just an example and nobody actually needs a typedef for an anonymous enum with no variants (which would be blocked by this PR, unless we specifically want to preserve existing behavior, since
.all()
in Rust returns true if there is nothing to iterate over).I added some cases for the other possibilities and the typedef is still generated correctly for all the enum definitions that could be used in some way.
CC @ctaggart who reported #1025, just in case I'm missing something and they had an actual usecase for a typedef for an anonymous enum with no variants.