Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Va list binding compatibility #970

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/bindgen/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,18 @@ impl SynFnArgHelpers for syn::FnArg {
syn::FnArg::Typed(syn::PatType {
ref pat, ref ty, ..
}) => {
let ty = match Type::load(ty)? {
Some(x) => x,
None => return Ok(None),
};
let name = match **pat {
syn::Pat::Wild(..) => None,
syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => {
Some(ident.unraw().to_string())
if ty == Type::Primitive(super::PrimitiveType::VaList) {
None
} else {
Some(ident.unraw().to_string())
}
}
_ => {
return Err(format!(
Expand All @@ -251,10 +259,6 @@ impl SynFnArgHelpers for syn::FnArg {
))
}
};
let ty = match Type::load(ty)? {
Some(x) => x,
None => return Ok(None),
};
if let Type::Array(..) = ty {
return Err("Array as function arguments are not supported".to_owned());
}
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl PrimitiveType {
PrimitiveType::Float => "float",
PrimitiveType::Double => "double",
PrimitiveType::PtrDiffT => "ptrdiff_t",
PrimitiveType::VaList => "va_list",
PrimitiveType::VaList => "...",
}
}

Expand Down
23 changes: 21 additions & 2 deletions tests/expectations/va_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
#include <stdint.h>
#include <stdlib.h>

int32_t va_list_test(va_list ap);
typedef int32_t (*VaListFnPtr)(int32_t count, ...);

int32_t va_list_test2(va_list ap);
typedef int32_t (*VaListFnPtr2)(int32_t count);

typedef struct {
int32_t (*fn1)(int32_t count, ...);
} Interface_______i32_______i32_______va_list;

typedef struct {
int32_t (*fn1)(int32_t count);
} Interface_______i32_______i32;

int32_t va_list_test(int32_t count, ...);

int32_t va_list_test2(int32_t count, ...);

void va_list_fn_ptrs(int32_t (*fn1)(int32_t count, ...),
int32_t (*fn2)(int32_t count),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like when used in function pointer the dotdotdot version does not emit the va args dots in the C version. Is this intended?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #971

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this test to highlight the existing limitation.
I had attempted to address it with #969 but there appears to have been some miscommunication.

VaListFnPtr fn3,
VaListFnPtr2 fn4,
Interface_______i32_______i32_______va_list fn5,
Interface_______i32_______i32 fn6);
23 changes: 21 additions & 2 deletions tests/expectations/va_list.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,32 @@
#include <stdint.h>
#include <stdlib.h>

typedef int32_t (*VaListFnPtr)(int32_t count, ...);

typedef int32_t (*VaListFnPtr2)(int32_t count);

typedef struct {
int32_t (*fn1)(int32_t count, ...);
} Interface_______i32_______i32_______va_list;

typedef struct {
int32_t (*fn1)(int32_t count);
} Interface_______i32_______i32;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

int32_t va_list_test(va_list ap);
int32_t va_list_test(int32_t count, ...);

int32_t va_list_test2(int32_t count, ...);

int32_t va_list_test2(va_list ap);
void va_list_fn_ptrs(int32_t (*fn1)(int32_t count, ...),
int32_t (*fn2)(int32_t count),
VaListFnPtr fn3,
VaListFnPtr2 fn4,
Interface_______i32_______i32_______va_list fn5,
Interface_______i32_______i32 fn6);

#ifdef __cplusplus
} // extern "C"
Expand Down
20 changes: 18 additions & 2 deletions tests/expectations/va_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@
#include <ostream>
#include <new>

using VaListFnPtr = int32_t(*)(int32_t count, ...);

using VaListFnPtr2 = int32_t(*)(int32_t count);

template<typename T>
struct Interface {
T fn1;
};

extern "C" {

int32_t va_list_test(va_list ap);
int32_t va_list_test(int32_t count, ...);

int32_t va_list_test2(int32_t count, ...);

int32_t va_list_test2(va_list ap);
void va_list_fn_ptrs(int32_t (*fn1)(int32_t count, ...),
int32_t (*fn2)(int32_t count),
VaListFnPtr fn3,
VaListFnPtr2 fn4,
Interface<int32_t(*)(int32_t count, ...)> fn5,
Interface<int32_t(*)(int32_t count)> fn6);

} // extern "C"
21 changes: 19 additions & 2 deletions tests/expectations/va_list.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ cdef extern from *:

cdef extern from *:

int32_t va_list_test(va_list ap);
ctypedef int32_t (*VaListFnPtr)(int32_t count, ...);

int32_t va_list_test2(va_list ap);
ctypedef int32_t (*VaListFnPtr2)(int32_t count);

ctypedef struct Interface_______i32_______i32_______va_list:
int32_t (*fn1)(int32_t count, ...);

ctypedef struct Interface_______i32_______i32:
int32_t (*fn1)(int32_t count);

int32_t va_list_test(int32_t count, ...);

int32_t va_list_test2(int32_t count, ...);

void va_list_fn_ptrs(int32_t (*fn1)(int32_t count, ...),
int32_t (*fn2)(int32_t count),
VaListFnPtr fn3,
VaListFnPtr2 fn4,
Interface_______i32_______i32_______va_list fn5,
Interface_______i32_______i32 fn6);
27 changes: 27 additions & 0 deletions tests/expectations/va_list_both.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef int32_t (*VaListFnPtr)(int32_t count, ...);

typedef int32_t (*VaListFnPtr2)(int32_t count);

typedef struct Interface_______i32_______i32_______va_list {
int32_t (*fn1)(int32_t count, ...);
} Interface_______i32_______i32_______va_list;

typedef struct Interface_______i32_______i32 {
int32_t (*fn1)(int32_t count);
} Interface_______i32_______i32;

int32_t va_list_test(int32_t count, ...);

int32_t va_list_test2(int32_t count, ...);

void va_list_fn_ptrs(int32_t (*fn1)(int32_t count, ...),
int32_t (*fn2)(int32_t count),
VaListFnPtr fn3,
VaListFnPtr2 fn4,
struct Interface_______i32_______i32_______va_list fn5,
struct Interface_______i32_______i32 fn6);
35 changes: 35 additions & 0 deletions tests/expectations/va_list_both.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef int32_t (*VaListFnPtr)(int32_t count, ...);

typedef int32_t (*VaListFnPtr2)(int32_t count);

typedef struct Interface_______i32_______i32_______va_list {
int32_t (*fn1)(int32_t count, ...);
} Interface_______i32_______i32_______va_list;

typedef struct Interface_______i32_______i32 {
int32_t (*fn1)(int32_t count);
} Interface_______i32_______i32;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

int32_t va_list_test(int32_t count, ...);

int32_t va_list_test2(int32_t count, ...);

void va_list_fn_ptrs(int32_t (*fn1)(int32_t count, ...),
int32_t (*fn2)(int32_t count),
VaListFnPtr fn3,
VaListFnPtr2 fn4,
struct Interface_______i32_______i32_______va_list fn5,
struct Interface_______i32_______i32 fn6);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
27 changes: 27 additions & 0 deletions tests/expectations/va_list_tag.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef int32_t (*VaListFnPtr)(int32_t count, ...);

typedef int32_t (*VaListFnPtr2)(int32_t count);

struct Interface_______i32_______i32_______va_list {
int32_t (*fn1)(int32_t count, ...);
};

struct Interface_______i32_______i32 {
int32_t (*fn1)(int32_t count);
};

int32_t va_list_test(int32_t count, ...);

int32_t va_list_test2(int32_t count, ...);

void va_list_fn_ptrs(int32_t (*fn1)(int32_t count, ...),
int32_t (*fn2)(int32_t count),
VaListFnPtr fn3,
VaListFnPtr2 fn4,
struct Interface_______i32_______i32_______va_list fn5,
struct Interface_______i32_______i32 fn6);
35 changes: 35 additions & 0 deletions tests/expectations/va_list_tag.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef int32_t (*VaListFnPtr)(int32_t count, ...);

typedef int32_t (*VaListFnPtr2)(int32_t count);

struct Interface_______i32_______i32_______va_list {
int32_t (*fn1)(int32_t count, ...);
};

struct Interface_______i32_______i32 {
int32_t (*fn1)(int32_t count);
};

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

int32_t va_list_test(int32_t count, ...);

int32_t va_list_test2(int32_t count, ...);

void va_list_fn_ptrs(int32_t (*fn1)(int32_t count, ...),
int32_t (*fn2)(int32_t count),
VaListFnPtr fn3,
VaListFnPtr2 fn4,
struct Interface_______i32_______i32_______va_list fn5,
struct Interface_______i32_______i32 fn6);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
28 changes: 28 additions & 0 deletions tests/expectations/va_list_tag.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list

cdef extern from *:

ctypedef int32_t (*VaListFnPtr)(int32_t count, ...);

ctypedef int32_t (*VaListFnPtr2)(int32_t count);

cdef struct Interface_______i32_______i32_______va_list:
int32_t (*fn1)(int32_t count, ...);

cdef struct Interface_______i32_______i32:
int32_t (*fn1)(int32_t count);

int32_t va_list_test(int32_t count, ...);

int32_t va_list_test2(int32_t count, ...);

void va_list_fn_ptrs(int32_t (*fn1)(int32_t count, ...),
int32_t (*fn2)(int32_t count),
VaListFnPtr fn3,
VaListFnPtr2 fn4,
Interface_______i32_______i32_______va_list fn5,
Interface_______i32_______i32 fn6);
23 changes: 21 additions & 2 deletions tests/rust/va_list.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
use std::ffi::VaList;

#[no_mangle]
pub unsafe extern "C" fn va_list_test(mut ap: VaList) -> int32_t {
pub unsafe extern "C" fn va_list_test(count: int32_t, mut ap: VaList) -> int32_t {
ap.arg()
}

#[no_mangle]
pub unsafe extern "C" fn va_list_test2(mut ap: ...) -> int32_t {
pub unsafe extern "C" fn va_list_test2(count: int32_t, mut ap: ...) -> int32_t {
ap.arg()
}

type VaListFnPtr = Option<unsafe extern "C" fn(count: int32_t, VaList) -> int32_t>;
type VaListFnPtr2 = Option<unsafe extern "C" fn(count: int32_t, ...) -> int32_t>;

#[repr(C)]
struct Interface<T> {
fn1: T,
}

#[no_mangle]
pub extern "C" fn va_list_fn_ptrs(
fn1: Option<unsafe extern "C" fn(count: int32_t, VaList) -> int32_t>,
fn2: Option<unsafe extern "C" fn(count: int32_t, ...) -> int32_t>,
fn3: VaListFnPtr,
fn4: VaListFnPtr2,
fn5: Interface<Option<unsafe extern "C" fn(count: int32_t, VaList) -> int32_t>>,
fn6: Interface<Option<unsafe extern "C" fn(count: int32_t, ...) -> int32_t>>,
) {
}
Loading