Skip to content

Add option to avoid wrapping function pointer fields or arguments #2370

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@
# Unreleased

## Added
* Added the `--non-null-fn-ptr=<PATH>` flag and its equivalent builder
method to avoid wrapping function pointer arguments or fields in `Option` if
their path matches `<PATH>`.

## Changed

Expand Down
9 changes: 9 additions & 0 deletions bindgen-cli/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ struct BindgenCommand {
/// inline` functions.
#[arg(long, requires = "experimental", value_name = "SUFFIX")]
wrap_static_fns_suffix: Option<String>,
/// Do not wrap function pointers in `Option` if they are used as fields/arguments whose path
/// matches <PATH>.
#[arg(long, value_name = "PATH")]
non_null_fn_ptr: Vec<String>,
/// Enables experimental features.
#[arg(long)]
experimental: bool,
Expand Down Expand Up @@ -478,6 +482,7 @@ where
wrap_static_fns,
wrap_static_fns_path,
wrap_static_fns_suffix,
non_null_fn_ptr,
experimental: _,
version,
clang_args,
Expand Down Expand Up @@ -989,5 +994,9 @@ where
builder = builder.wrap_static_fns_suffix(suffix);
}

for path in non_null_fn_ptr {
builder = builder.non_null_fn_ptr(path);
}

Ok((builder, output, verbose))
}
177 changes: 177 additions & 0 deletions bindgen-tests/tests/expectations/tests/non_null_fn_ptr.rs

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

27 changes: 27 additions & 0 deletions bindgen-tests/tests/headers/non_null_fn_ptr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// bindgen-flags: --non-null-fn-ptr=".*foo::.*" --no-default=".*foo"

typedef struct foo {
int (*bar)();
} foo;

foo new_foo(int (*arg)());

typedef struct baz {
int (*foo)();
} baz;

baz new_baz(int (*foo)());

typedef union union_foo {
int (*fst)();
float (*snd)();
} union_foo;

union_foo new_union_foo(int (*arg)());

typedef union union_baz {
int (*foo)();
float (*bar)();
} union_baz;

union_baz new_union_baz(int (*foo)());
5 changes: 1 addition & 4 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3784,11 +3784,8 @@ impl TryToRustTy for Type {
// they aren't NonZero), so don't *ever* use an or_opaque
// variant here.
let ty = fs.try_to_rust_ty(ctx, &())?;

let prefix = ctx.trait_prefix();
Ok(quote! {
::#prefix::option::Option<#ty>
})
Ok(quote!(::#prefix::option::Option<#ty>))
}
TypeKind::Array(item, len) | TypeKind::Vector(item, len) => {
let ty = item.try_to_rust_ty(ctx, &())?;
Expand Down
7 changes: 6 additions & 1 deletion bindgen/codegen/postprocessing/merge_extern_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ use syn::{
Item, ItemForeignMod, ItemMod,
};

pub(super) fn merge_extern_blocks(item_mod: &mut ItemMod) {
use crate::BindgenOptions;

pub(super) fn merge_extern_blocks(
item_mod: &mut ItemMod,
_options: &BindgenOptions,
) {
Visitor.visit_item_mod_mut(item_mod)
}

Expand Down
18 changes: 13 additions & 5 deletions bindgen/codegen/postprocessing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ use syn::{parse2, ItemMod};
use crate::BindgenOptions;

mod merge_extern_blocks;
mod non_null_fn_ptr;
mod sort_semantically;

use merge_extern_blocks::merge_extern_blocks;
use non_null_fn_ptr::non_null_fn_ptr;
use sort_semantically::sort_semantically;

struct PostProcessingPass {
should_run: fn(&BindgenOptions) -> bool,
run: fn(&mut ItemMod),
run: fn(&mut ItemMod, &BindgenOptions),
}

// TODO: This can be a const fn when mutable references are allowed in const
Expand All @@ -21,13 +23,19 @@ macro_rules! pass {
($pass:ident) => {
PostProcessingPass {
should_run: |options| options.$pass,
run: |item_mod| $pass(item_mod),
run: |item_mod, options| $pass(item_mod, options),
}
};
}

const PASSES: &[PostProcessingPass] =
&[pass!(merge_extern_blocks), pass!(sort_semantically)];
const PASSES: &[PostProcessingPass] = &[
pass!(merge_extern_blocks),
pass!(sort_semantically),
PostProcessingPass {
should_run: |options| !options.non_null_fn_ptr.is_empty(),
run: non_null_fn_ptr,
},
];

pub(crate) fn postprocessing(
items: Vec<TokenStream>,
Expand All @@ -51,7 +59,7 @@ pub(crate) fn postprocessing(

for pass in PASSES {
if (pass.should_run)(options) {
(pass.run)(&mut item_mod);
(pass.run)(&mut item_mod, options);
}
}

Expand Down
Loading