Skip to content

Commit 0e4a5cc

Browse files
ian-h-chamberlainemilio
authored andcommitted
Support blocklisting __BindgenBitfieldUnit
Simply skip emitting it if the list of blocklisted types/items matches its name. Also add a test that verifies blocklisting anonymous types by `_bindgen_ty_*` works as expected.
1 parent 4b3cd6c commit 0e4a5cc

File tree

6 files changed

+103
-1
lines changed

6 files changed

+103
-1
lines changed

bindgen-tests/tests/expectations/tests/anon_enum_blocklist.rs

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/blocklist_bitfield_unit.rs

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// bindgen-flags: --blocklist-type "_bindgen_ty_1"
2+
3+
enum {
4+
FLAG_X,
5+
FLAG_Y,
6+
};
7+
8+
enum {
9+
FLAG_Z,
10+
FLAG_W,
11+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: --blocklist-type "__BindgenBitfieldUnit" --raw-line '#[path = "./struct_with_bitfields.rs"] mod bitfields;' --raw-line 'use bitfields::*;'
2+
struct C {
3+
unsigned char x;
4+
unsigned b1 : 1;
5+
unsigned b2 : 1;
6+
unsigned baz;
7+
};

bindgen/codegen/helpers.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Helpers for code generation that don't need macro expansion.
22
3+
use proc_macro2::{Ident, Span};
4+
35
use crate::ir::context::BindgenContext;
46
use crate::ir::layout::Layout;
57

@@ -109,10 +111,13 @@ pub(crate) fn integer_type(
109111
Layout::known_type_for_size(ctx, layout.size)
110112
}
111113

114+
pub(crate) const BITFIELD_UNIT: &str = "__BindgenBitfieldUnit";
115+
112116
/// Generates a bitfield allocation unit type for a type with the given `Layout`.
113117
pub(crate) fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> syn::Type {
114118
let size = layout.size;
115-
let ty = syn::parse_quote! { __BindgenBitfieldUnit<[u8; #size]> };
119+
let bitfield_unit_name = Ident::new(BITFIELD_UNIT, Span::call_site());
120+
let ty = syn::parse_quote! { #bitfield_unit_name<[u8; #size]> };
116121

117122
if ctx.options().enable_cxx_namespaces {
118123
return syn::parse_quote! { root::#ty };

bindgen/codegen/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5017,6 +5017,7 @@ pub(crate) fn codegen(
50175017
}
50185018

50195019
pub(crate) mod utils {
5020+
use super::helpers::BITFIELD_UNIT;
50205021
use super::serialize::CSerialize;
50215022
use super::{error, CodegenError, CodegenResult, ToRustTyOrOpaque};
50225023
use crate::ir::context::BindgenContext;
@@ -5153,6 +5154,12 @@ pub(crate) mod utils {
51535154
ctx: &BindgenContext,
51545155
result: &mut Vec<proc_macro2::TokenStream>,
51555156
) {
5157+
if ctx.options().blocklisted_items.matches(BITFIELD_UNIT) ||
5158+
ctx.options().blocklisted_types.matches(BITFIELD_UNIT)
5159+
{
5160+
return;
5161+
}
5162+
51565163
let bitfield_unit_src = include_str!("./bitfield_unit.rs");
51575164
let bitfield_unit_src = if ctx.options().rust_features().min_const_fn {
51585165
Cow::Borrowed(bitfield_unit_src)

0 commit comments

Comments
 (0)