Skip to content

Support blocklisting __BindgenBitfieldUnit #2836

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

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bindgen-tests/tests/expectations/tests/anon_enum_blocklist.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions bindgen-tests/tests/expectations/tests/blocklist_bitfield_unit.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions bindgen-tests/tests/headers/anon_enum_blocklist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// bindgen-flags: --blocklist-type "_bindgen_ty_1"

enum {
FLAG_X,
FLAG_Y,
};

enum {
FLAG_Z,
FLAG_W,
};
7 changes: 7 additions & 0 deletions bindgen-tests/tests/headers/blocklist_bitfield_unit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// bindgen-flags: --blocklist-type "__BindgenBitfieldUnit" --raw-line '#[path = "./struct_with_bitfields.rs"] mod bitfields;' --raw-line 'use bitfields::*;'
struct C {
unsigned char x;
unsigned b1 : 1;
unsigned b2 : 1;
unsigned baz;
};
7 changes: 6 additions & 1 deletion bindgen/codegen/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Helpers for code generation that don't need macro expansion.

use proc_macro2::{Ident, Span};

use crate::ir::context::BindgenContext;
use crate::ir::layout::Layout;

Expand Down Expand Up @@ -109,10 +111,13 @@ pub(crate) fn integer_type(
Layout::known_type_for_size(ctx, layout.size)
}

pub(crate) const BITFIELD_UNIT: &str = "__BindgenBitfieldUnit";

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

if ctx.options().enable_cxx_namespaces {
return syn::parse_quote! { root::#ty };
Expand Down
7 changes: 7 additions & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,13 +1658,13 @@

/// Compute a fields or structs visibility based on multiple conditions.
/// 1. If the element was declared public, and we respect such CXX accesses specs
/// (context option) => By default Public, but this can be overruled by an `annotation`.

Check warning on line 1661 in bindgen/codegen/mod.rs

View workflow job for this annotation

GitHub Actions / rustfmt-clippy

doc list item missing indentation
///
/// 2. If the element was declared private, and we respect such CXX accesses specs
/// (context option) => By default Private, but this can be overruled by an `annotation`.

Check warning on line 1664 in bindgen/codegen/mod.rs

View workflow job for this annotation

GitHub Actions / rustfmt-clippy

doc list item missing indentation
///
/// 3. If we do not respect visibility modifiers, the result depends on the `annotation`,
/// if any, or the passed `default_kind`.

Check warning on line 1667 in bindgen/codegen/mod.rs

View workflow job for this annotation

GitHub Actions / rustfmt-clippy

doc list item missing indentation
///
fn compute_visibility(
ctx: &BindgenContext,
Expand Down Expand Up @@ -5017,6 +5017,7 @@
}

pub(crate) mod utils {
use super::helpers::BITFIELD_UNIT;
use super::serialize::CSerialize;
use super::{error, CodegenError, CodegenResult, ToRustTyOrOpaque};
use crate::ir::context::BindgenContext;
Expand Down Expand Up @@ -5153,6 +5154,12 @@
ctx: &BindgenContext,
result: &mut Vec<proc_macro2::TokenStream>,
) {
if ctx.options().blocklisted_items.matches(BITFIELD_UNIT) ||
ctx.options().blocklisted_types.matches(BITFIELD_UNIT)
{
return;
}

let bitfield_unit_src = include_str!("./bitfield_unit.rs");
let bitfield_unit_src = if ctx.options().rust_features().min_const_fn {
Cow::Borrowed(bitfield_unit_src)
Expand Down
5 changes: 5 additions & 0 deletions book/src/using-bitfields.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ As Rust does not support bitfields, Bindgen generates a struct for each with the
* For each contiguous block of bitfields, Bindgen emits an opaque physical field that contains one or more logical bitfields
* A static constructor ```new_bitfield_{1, 2, ...}``` with a parameter for each bitfield contained within the opaque physical field.

To keep bindgen from generating the bitfield unit struct, it can be blocklisted like any
other type, i.e. `--blocklist-type "__BindgenBitfieldUnit"`. This may be useful if
you want to define a custom implementation, or your generated bindings import a
pre-existing definition for the bitfield unit type.

## Bitfield examples

For this discussion, we will use the following C type definitions and functions.
Expand Down
Loading