Skip to content

Commit e1a1eed

Browse files
committed
test: add test for wrap-static-fns in C++ mode
1 parent 4ff0c52 commit e1a1eed

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "tests/headers/wrap-static-fns.hpp"
2+
3+
// Static wrappers
4+
5+
int foo__extern(void) { return foo(); }
6+
int bar__extern(void) { return bar(); }
7+
int takes_ptr__extern(int *arg) { return takes_ptr(arg); }
8+
int takes_fn_ptr__extern(int (*f) (int)) { return takes_fn_ptr(f); }
9+
int takes_fn__extern(int (f) (int)) { return takes_fn(f); }
10+
int takes_alias__extern(func f) { return takes_alias(f); }
11+
int takes_qualified__extern(const int *const *arg) { return takes_qualified(arg); }
12+
enum foo takes_enum__extern(const enum foo f) { return takes_enum(f); }
13+
void nevermore__extern(void) { nevermore(); }
14+
int takes_fn_with_no_args__extern(int (f) (void)) { return takes_fn_with_no_args(f); }
15+
void no_extra_argument__extern(__builtin_va_list va) { no_extra_argument(va); }
16+
int many_va_list__extern(int i, __builtin_va_list va1, __builtin_va_list va2) { return many_va_list(i, va1, va2); }
17+
int wrap_as_variadic_fn1__extern(int i, ...) {
18+
int ret;
19+
va_list ap;
20+
21+
va_start(ap, i);
22+
ret = wrap_as_variadic_fn1(i, ap);
23+
va_end(ap);
24+
return ret;
25+
}
26+
void wrap_as_variadic_fn2__extern(int i, ...) {
27+
va_list ap;
28+
29+
va_start(ap, i);
30+
wrap_as_variadic_fn2(i, ap);
31+
va_end(ap);
32+
}
33+
int foo__extern(void) { return foo(); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// bindgen-flags: --wrap-static-fns
2+
// bindgen-parse-callbacks: wrap-as-variadic-fn
3+
4+
// to avoid polluting the expectation tests we put the stdarg.h behind a conditional
5+
// variable only used in bindgen-integration
6+
#ifdef USE_VA_HEADER
7+
#include <stdarg.h>
8+
#endif
9+
10+
static inline int foo() {
11+
return 11;
12+
}
13+
static int bar();
14+
static int bar() {
15+
return 1;
16+
}
17+
inline int baz() {
18+
return 2;
19+
}
20+
21+
static inline int takes_ptr(int* arg) {
22+
return *arg + 1;
23+
}
24+
25+
static inline int takes_fn_ptr(int (*f)(int)) {
26+
return f(1);
27+
}
28+
29+
static inline int takes_fn(int (f)(int)) {
30+
return f(2);
31+
}
32+
33+
typedef int (func)(int);
34+
35+
static inline int takes_alias(func f) {
36+
return f(3);
37+
}
38+
39+
static inline int takes_qualified(const int *const *arg) {
40+
return **arg;
41+
}
42+
43+
enum foo {
44+
BAR = 0x0,
45+
};
46+
47+
static inline enum foo takes_enum(const enum foo f) {
48+
return f;
49+
}
50+
51+
static inline void nevermore() {
52+
while (1) { }
53+
}
54+
55+
static inline int takes_fn_with_no_args(int(f)(void)) {
56+
return f();
57+
}
58+
59+
static inline int variadic(int x, ...) {
60+
return x;
61+
}
62+
63+
static inline void no_extra_argument(__builtin_va_list va) {}
64+
65+
static inline int many_va_list(int i, __builtin_va_list va1, __builtin_va_list va2) {
66+
return i;
67+
}
68+
69+
#ifndef USE_VA_HEADER
70+
static inline int wrap_as_variadic_fn1(int i, __builtin_va_list va) {
71+
return i;
72+
}
73+
74+
static inline void wrap_as_variadic_fn2(int i, __builtin_va_list va) {}
75+
#else
76+
static inline int wrap_as_variadic_fn1(int i, va_list va) {
77+
int res = 0;
78+
79+
for (int j = 0; j < i; j++)
80+
res += (int) va_arg(va, int);
81+
82+
return res;
83+
}
84+
85+
static inline void wrap_as_variadic_fn2(int i, va_list va) {}
86+
#endif
87+
88+
namespace qux {
89+
static int foo();
90+
}

bindgen-tests/tests/tests.rs

+38
Original file line numberDiff line numberDiff line change
@@ -757,3 +757,41 @@ fn test_wrap_static_fns() {
757757
.unwrap();
758758
}
759759
}
760+
761+
#[test]
762+
fn test_wrap_static_fns_cpp() {
763+
// This test is for testing diffs of the generated C source and header files
764+
// TODO: If another such feature is added, convert this test into a more generic
765+
// test that looks at `tests/headers/generated` directory.
766+
let expect_path = PathBuf::from("tests/expectations/tests/generated")
767+
.join("wrap_static_fns");
768+
println!("In path is ::: {}", expect_path.display());
769+
770+
let generated_path =
771+
PathBuf::from(env::var("OUT_DIR").unwrap()).join("wrap_static_fns");
772+
println!("Out path is ::: {}", generated_path.display());
773+
774+
let _bindings = Builder::default()
775+
.header("tests/headers/wrap-static-fns.hpp")
776+
.wrap_static_fns(true)
777+
.wrap_static_fns_path(generated_path.display().to_string())
778+
.parse_callbacks(Box::new(parse_callbacks::WrapAsVariadicFn))
779+
.generate()
780+
.expect("Failed to generate bindings");
781+
782+
let expected_cpp = fs::read_to_string(expect_path.with_extension("cpp"))
783+
.expect("Could not read generated wrap_static_fns.cpp");
784+
785+
let actual_cpp = fs::read_to_string(generated_path.with_extension("cpp"))
786+
.expect("Could not read actual wrap_static_fns.cpp");
787+
788+
if expected_cpp != actual_cpp {
789+
error_diff_mismatch(
790+
&actual_cpp,
791+
&expected_cpp,
792+
None,
793+
&expect_path.with_extension("cpp"),
794+
)
795+
.unwrap();
796+
}
797+
}

0 commit comments

Comments
 (0)