Skip to content

Commit

Permalink
Use void in parameterless functions (#2633)
Browse files Browse the repository at this point in the history
* Use void for parameterless function arguments

* Update tests

* Update CHANGELOG.md
  • Loading branch information
pvdrz authored Sep 11, 2023
1 parent 53c01d2 commit 1a09f45
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) {
Expand Down
6 changes: 6 additions & 0 deletions bindgen-tests/tests/expectations/tests/wrap-static-fns.rs

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

4 changes: 4 additions & 0 deletions bindgen-tests/tests/headers/wrap-static-fns.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
35 changes: 20 additions & 15 deletions bindgen/codegen/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 1a09f45

Please sign in to comment.