Skip to content

Add option to avoid wrapping function pointer fields #2361

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
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@
# Unreleased

## Added
* Do not wrap function pointer fields in `Option` if the new
`--dont-wrap-fn-ptr-fields` option is enabled.

## Changed

Expand Down
7 changes: 7 additions & 0 deletions bindgen-cli/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,9 @@ where
Arg::new("wrap-unsafe-ops")
.long("wrap-unsafe-ops")
.help("Wrap unsafe operations in unsafe blocks."),
Arg::new("dont-wrap-fn-ptr-fields")
.long("dont-wrap-fn-ptr-fields")
.help("Do not wrap function pointer fields in `Option`."),
Arg::new("V")
.long("version")
.help("Prints the version, and exits"),
Expand Down Expand Up @@ -1092,5 +1095,9 @@ where
builder = builder.wrap_unsafe_ops(true);
}

if matches.is_present("dont-wrap-fn-ptr-fields") {
builder = builder.wrap_fn_ptr_fields(false);
}

Ok((builder, output, verbose))
}
33 changes: 33 additions & 0 deletions bindgen-tests/tests/expectations/tests/dont-wrap-fn-ptr-fields.rs

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

5 changes: 5 additions & 0 deletions bindgen-tests/tests/headers/dont-wrap-fn-ptr-fields.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// bindgen-flags: --dont-wrap-fn-ptr-fields --no-default="foo"

typedef struct foo {
int (*bar)();
} foo;
10 changes: 6 additions & 4 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3762,10 +3762,12 @@ impl TryToRustTy for Type {
// variant here.
let ty = fs.try_to_rust_ty(ctx, &())?;

let prefix = ctx.trait_prefix();
Ok(quote! {
::#prefix::option::Option<#ty>
})
if ctx.options().wrap_fn_ptr_fields {
let prefix = ctx.trait_prefix();
Ok(quote!(::#prefix::option::Option<#ty>))
} else {
Ok(quote!(#ty))
}
}
TypeKind::Array(item, len) | TypeKind::Vector(item, len) => {
let ty = item.try_to_rust_ty(ctx, &())?;
Expand Down
14 changes: 14 additions & 0 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,10 @@ impl Builder {
output_vector.push("--wrap-unsafe-ops".into());
}

if !self.options.wrap_fn_ptr_fields {
output_vector.push("--dont-wrap-fn-ptr-fields".into());
}

// Add clang arguments

output_vector.push("--".into());
Expand Down Expand Up @@ -1787,6 +1791,12 @@ impl Builder {
self.options.wrap_unsafe_ops = doit;
self
}

/// If true, wraps function pointer fields in `Option`.
pub fn wrap_fn_ptr_fields(mut self, doit: bool) -> Self {
self.options.wrap_fn_ptr_fields = doit;
self
}
}

/// Configuration options for generated bindings.
Expand Down Expand Up @@ -2127,6 +2137,9 @@ struct BindgenOptions {

/// Whether to wrap unsafe operations in unsafe blocks or not.
wrap_unsafe_ops: bool,

/// Whether to wrap function pointer fields in `Option` or not.
wrap_fn_ptr_fields: bool,
}

impl BindgenOptions {
Expand Down Expand Up @@ -2238,6 +2251,7 @@ impl Default for BindgenOptions {
record_matches: true,
rustfmt_bindings: true,
size_t_is_usize: true,
wrap_fn_ptr_fields: true,

--default-fields--
blocklisted_types,
Expand Down