Skip to content

Commit

Permalink
Avoid emitting old-style C declarations (#2443)
Browse files Browse the repository at this point in the history
* Avoid emiting old-style C declarations

* Update Changelog
  • Loading branch information
pvdrz committed Mar 15, 2023
1 parent 33c9fe4 commit 9f90b32
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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`.
Expand Down
@@ -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");
Expand Down
46 changes: 24 additions & 22 deletions bindgen/codegen/serialize.rs
Expand Up @@ -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)
Expand Down Expand Up @@ -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>,
Expand Down

0 comments on commit 9f90b32

Please sign in to comment.