Repository: rust-lang/rust-bindgen
Pull request #2443 (2023-03-15 21:59:46)

Avoid emitting old-style C declarations
Fixes https://github.com/rust-lang/rust-bindgen/issues/2442


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 037755ca..43cff3d9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -164,7 +164,8 @@
 ## Added
 
 ## Changed
-
+ * Static functions with no arguments use `void` as their single argument
+   instead of having no arguments when the `--wrap-static-fns` flag is used.
 ## Removed
  * The following deprecated flags were removed: `--use-msvc-mangling`,
    `--rustfmt-bindings` and `--size_t-is-usize`.
diff --git a/bindgen/codegen/serialize.rs b/bindgen/codegen/serialize.rs
index 13106484..dedf2568 100644
--- a/bindgen/codegen/serialize.rs
+++ b/bindgen/codegen/serialize.rs
@@ -108,33 +108,13 @@ impl<'a> CSerialize<'a> for Function {
         // Write `ret_ty wrap_name(args) asm("wrap_name");`
         ret_ty.serialize(ctx, (), stack, writer)?;
         write!(writer, " {}(", wrap_name)?;
-        if args.is_empty() {
-            write!(writer, "void")?;
-        } else {
-            serialize_sep(
-                ", ",
-                args.iter(),
-                ctx,
-                writer,
-                |(name, type_id), ctx, buf| {
-                    type_id.serialize(ctx, (), &mut vec![name.clone()], buf)
-                },
-            )?;
-        }
+        serialize_args(&args, ctx, writer)?;
         writeln!(writer, ") asm(\"{}\");", wrap_name)?;
 
         // Write `ret_ty wrap_name(args) { return name(arg_names)' }`
         ret_ty.serialize(ctx, (), stack, writer)?;
         write!(writer, " {}(", wrap_name)?;
-        serialize_sep(
-            ", ",
-            args.iter(),
-            ctx,
-            writer,
-            |(name, type_id), _, buf| {
-                type_id.serialize(ctx, (), &mut vec![name.clone()], buf)
-            },
-        )?;
+        serialize_args(&args, ctx, writer)?;
         write!(writer, ") {{ return {}(", name)?;
         serialize_sep(", ", args.iter(), ctx, writer, |(name, _), _, buf| {
             write!(buf, "{}", name).map_err(From::from)
@@ -338,6 +318,28 @@ impl<'a> CSerialize<'a> for Type {
     }
 }
 
+fn serialize_args<W: Write>(
+    args: &[(String, TypeId)],
+    ctx: &BindgenContext,
+    writer: &mut W,
+) -> Result<(), CodegenError> {
+    if args.is_empty() {
+        write!(writer, "void")?;
+    } else {
+        serialize_sep(
+            ", ",
+            args.iter(),
+            ctx,
+            writer,
+            |(name, type_id), ctx, buf| {
+                type_id.serialize(ctx, (), &mut vec![name.clone()], buf)
+            },
+        )?;
+    }
+
+    Ok(())
+}
+
 fn serialize_sep<
     W: Write,
     F: FnMut(I::Item, &BindgenContext, &mut W) -> Result<(), CodegenError>,


Test patch:
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 f5f33f84..60c1685e 100644
--- a/bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c
+++ b/bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c
@@ -1,7 +1,7 @@
 int foo__extern(void) asm("foo__extern");
-int foo__extern() { return foo(); }
+int foo__extern(void) { return foo(); }
 int bar__extern(void) asm("bar__extern");
-int bar__extern() { return bar(); }
+int bar__extern(void) { return bar(); }
 int takes_ptr__extern(int *arg) asm("takes_ptr__extern");
 int takes_ptr__extern(int *arg) { return takes_ptr(arg); }
 int takes_fn_ptr__extern(int (*f) (int)) asm("takes_fn_ptr__extern");