From 1a09f450c874d57883f56a7a0c298be5db183569 Mon Sep 17 00:00:00 2001 From: Christian Poveda Ruiz <31802960+pvdrz@users.noreply.github.com> Date: Mon, 11 Sep 2023 16:48:04 -0500 Subject: [PATCH] Use void in parameterless functions (#2633) * Use void for parameterless function arguments * Update tests * Update CHANGELOG.md --- CHANGELOG.md | 2 ++ .../tests/generated/wrap_static_fns.c | 1 + .../expectations/tests/wrap-static-fns.rs | 6 ++++ bindgen-tests/tests/headers/wrap-static-fns.h | 4 +++ bindgen/codegen/serialize.rs | 35 +++++++++++-------- 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa5af68580..9b536800fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -179,6 +179,8 @@ ## Added ## Changed +- The `--wrap-static-fns` feature was updated so function types that has no + argument use `void` as its sole argument. ## Removed ## Fixed ## Security diff --git a/bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c b/bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c index cf1106ec76..a8fd5045d2 100644 --- a/bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c +++ b/bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c @@ -11,6 +11,7 @@ int takes_alias__extern(func f) { return takes_alias(f); } int takes_qualified__extern(const int *const *arg) { return takes_qualified(arg); } enum foo takes_enum__extern(const enum foo f) { return takes_enum(f); } void nevermore__extern(void) { nevermore(); } +int takes_fn_with_no_args__extern(int (f) (void)) { return takes_fn_with_no_args(f); } void no_extra_argument__extern(__builtin_va_list va) { no_extra_argument(va); } int many_va_list__extern(int i, __builtin_va_list va1, __builtin_va_list va2) { return many_va_list(i, va1, va2); } int wrap_as_variadic_fn1__extern(int i, ...) { diff --git a/bindgen-tests/tests/expectations/tests/wrap-static-fns.rs b/bindgen-tests/tests/expectations/tests/wrap-static-fns.rs index 03f3907ed2..46b369b2f4 100644 --- a/bindgen-tests/tests/expectations/tests/wrap-static-fns.rs +++ b/bindgen-tests/tests/expectations/tests/wrap-static-fns.rs @@ -50,6 +50,12 @@ extern "C" { #[link_name = "nevermore__extern"] pub fn nevermore(); } +extern "C" { + #[link_name = "takes_fn_with_no_args__extern"] + pub fn takes_fn_with_no_args( + f: ::std::option::Option ::std::os::raw::c_int>, + ) -> ::std::os::raw::c_int; +} extern "C" { #[link_name = "no_extra_argument__extern"] pub fn no_extra_argument(va: *mut __va_list_tag); diff --git a/bindgen-tests/tests/headers/wrap-static-fns.h b/bindgen-tests/tests/headers/wrap-static-fns.h index 8dcabe7b30..131b7ab15f 100644 --- a/bindgen-tests/tests/headers/wrap-static-fns.h +++ b/bindgen-tests/tests/headers/wrap-static-fns.h @@ -52,6 +52,10 @@ static inline void nevermore() { while (1) { } } +static inline int takes_fn_with_no_args(int(f)(void)) { + return f(); +} + static inline int variadic(int x, ...) { return x; } diff --git a/bindgen/codegen/serialize.rs b/bindgen/codegen/serialize.rs index 58e0882faf..02c4680263 100644 --- a/bindgen/codegen/serialize.rs +++ b/bindgen/codegen/serialize.rs @@ -322,21 +322,26 @@ impl<'a> CSerialize<'a> for Type { } write!(writer, ")")?; - write!(writer, " (")?; - serialize_sep( - ", ", - signature.argument_types().iter(), - ctx, - writer, - |(name, type_id), ctx, buf| { - let mut stack = vec![]; - if let Some(name) = name { - stack.push(name.clone()); - } - type_id.serialize(ctx, (), &mut stack, buf) - }, - )?; - write!(writer, ")")? + let args = signature.argument_types(); + if args.is_empty() { + write!(writer, " (void)")?; + } else { + write!(writer, " (")?; + serialize_sep( + ", ", + args.iter(), + ctx, + writer, + |(name, type_id), ctx, buf| { + let mut stack = vec![]; + if let Some(name) = name { + stack.push(name.clone()); + } + type_id.serialize(ctx, (), &mut stack, buf) + }, + )?; + write!(writer, ")")? + } } TypeKind::ResolvedTypeRef(type_id) => { if self.is_const() {