Skip to content

Commit

Permalink
codegen: Be consistent about variadic signatures.
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio committed Jan 6, 2018
1 parent af78302 commit ac3faac
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3249,21 +3249,13 @@ impl CodeGenerator for Function {
abi => abi,
};

let variadic = if signature.is_variadic() {
quote! { ... }
} else {
quote! {}
};

let ident = ctx.rust_ident(canonical_name);
let mut tokens = quote! { extern #abi };
tokens.append("{\n");
if !attributes.is_empty() {
tokens.append_separated(attributes, "\n");
tokens.append("\n");
}
let mut args = args;
args.push(variadic);
tokens.append(quote! {
pub fn #ident ( #( #args ),* ) #ret;
});
Expand Down Expand Up @@ -3719,7 +3711,7 @@ mod utils {
use super::ToPtr;

let mut unnamed_arguments = 0;
sig.argument_types().iter().map(|&(ref name, ty)| {
let mut args = sig.argument_types().iter().map(|&(ref name, ty)| {
let arg_item = ctx.resolve_item(ty);
let arg_ty = arg_item.kind().expect_type();

Expand Down Expand Up @@ -3766,6 +3758,12 @@ mod utils {
quote! {
#arg_name : #arg_ty
}
}).collect()
}).collect::<Vec<_>>();

if sig.is_variadic() {
args.push(quote! { ... })
}

args
}
}
42 changes: 42 additions & 0 deletions tests/expectations/tests/issue-1216-variadic-member.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* automatically generated by rust-bindgen */

#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]

extern "C" {
pub fn f(a: ::std::os::raw::c_int, ...);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Foo {
pub f: ::std::option::Option<
unsafe extern "C" fn(
p: *mut ::std::os::raw::c_void,
obj: *mut ::std::os::raw::c_void,
a: ::std::os::raw::c_int,
...
),
>,
}
#[test]
fn bindgen_test_layout_Foo() {
assert_eq!(
::std::mem::size_of::<Foo>(),
8usize,
concat!("Size of: ", stringify!(Foo))
);
assert_eq!(
::std::mem::align_of::<Foo>(),
8usize,
concat!("Alignment of ", stringify!(Foo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<Foo>())).f as *const _ as usize },
0usize,
concat!("Offset of field: ", stringify!(Foo), "::", stringify!(f))
);
}
impl Default for Foo {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
4 changes: 4 additions & 0 deletions tests/headers/issue-1216-variadic-member.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
void f(int a, ...);
struct Foo {
void (*f)(void *p, void *obj, int a, ...);
};

0 comments on commit ac3faac

Please sign in to comment.