Skip to content

Commit 94908ea

Browse files
committed
Merge branch 'main' into add-non-exhaustive-enum-cli
2 parents 302dd12 + cf9b02f commit 94908ea

File tree

8 files changed

+113
-6
lines changed

8 files changed

+113
-6
lines changed

bindgen-cli/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn main() {
3838
if verbose {
3939
print_verbose_err()
4040
}
41-
println!("{}", info);
41+
eprintln!("{}", info);
4242
}));
4343

4444
let bindings =
@@ -49,21 +49,21 @@ pub fn main() {
4949
bindings.write(output).expect("Unable to write output");
5050
}
5151
Err(error) => {
52-
println!("{}", error);
52+
eprintln!("{}", error);
5353
std::process::exit(1);
5454
}
5555
};
5656
}
5757

5858
fn print_verbose_err() {
59-
println!("Bindgen unexpectedly panicked");
60-
println!(
59+
eprintln!("Bindgen unexpectedly panicked");
60+
eprintln!(
6161
"This may be caused by one of the known-unsupported \
6262
things (https://rust-lang.github.io/rust-bindgen/cpp.html), \
6363
please modify the bindgen flags to work around it as \
6464
described in https://rust-lang.github.io/rust-bindgen/cpp.html"
6565
);
66-
println!(
66+
eprintln!(
6767
"Otherwise, please file an issue at \
6868
https://github.com/rust-lang/rust-bindgen/issues/new"
6969
);

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)

book/src/using-bitfields.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ As Rust does not support bitfields, Bindgen generates a struct for each with the
88
* For each contiguous block of bitfields, Bindgen emits an opaque physical field that contains one or more logical bitfields
99
* A static constructor ```new_bitfield_{1, 2, ...}``` with a parameter for each bitfield contained within the opaque physical field.
1010

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

1318
For this discussion, we will use the following C type definitions and functions.

0 commit comments

Comments
 (0)