diff options
Diffstat (limited to 'bindgen-tests')
-rw-r--r-- | bindgen-tests/Cargo.toml | 2 | ||||
-rw-r--r-- | bindgen-tests/tests/expectations/tests/generated/README.md | 4 | ||||
-rw-r--r-- | bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c | 14 | ||||
-rw-r--r-- | bindgen-tests/tests/expectations/tests/wrap-static-fns.rs | 52 | ||||
-rw-r--r-- | bindgen-tests/tests/headers/wrap-static-fns.h | 33 | ||||
-rw-r--r-- | bindgen-tests/tests/tests.rs | 37 |
6 files changed, 141 insertions, 1 deletions
diff --git a/bindgen-tests/Cargo.toml b/bindgen-tests/Cargo.toml index 0678274f..6df84e8e 100644 --- a/bindgen-tests/Cargo.toml +++ b/bindgen-tests/Cargo.toml @@ -5,7 +5,7 @@ version = "0.1.0" publish = false [dev-dependencies] -bindgen = { path = "../bindgen", features = ["cli"] } +bindgen = { path = "../bindgen", features = ["cli", "experimental"] } diff = "0.1" shlex = "1" clap = { version = "4", features = ["derive"] } diff --git a/bindgen-tests/tests/expectations/tests/generated/README.md b/bindgen-tests/tests/expectations/tests/generated/README.md new file mode 100644 index 00000000..b4e8cabb --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/generated/README.md @@ -0,0 +1,4 @@ +# Generated C, C++, Header files + +This directory contains files for features where extra files are generated +as a part of the feature. For example, `--wrap-static-fns`. diff --git a/bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c b/bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c new file mode 100644 index 00000000..22b2f67f --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c @@ -0,0 +1,14 @@ +int foo__extern(void) asm("foo__extern"); +int foo__extern() { return foo(); } +int bar__extern(void) asm("bar__extern"); +int bar__extern() { 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"); +int takes_fn_ptr__extern(int (*f) (int)) { return takes_fn_ptr(f); } +int takes_fn__extern(int (f) (int)) asm("takes_fn__extern"); +int takes_fn__extern(int (f) (int)) { return takes_fn(f); } +int takes_alias__extern(func f) asm("takes_alias__extern"); +int takes_alias__extern(func f) { return takes_alias(f); } +int takes_qualified__extern(const int *const *arg) asm("takes_qualified__extern"); +int takes_qualified__extern(const int *const *arg) { return takes_qualified(arg); } diff --git a/bindgen-tests/tests/expectations/tests/wrap-static-fns.rs b/bindgen-tests/tests/expectations/tests/wrap-static-fns.rs new file mode 100644 index 00000000..54ed9fd4 --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/wrap-static-fns.rs @@ -0,0 +1,52 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +extern "C" { + #[link_name = "\u{1}foo__extern"] + pub fn foo() -> ::std::os::raw::c_int; +} +extern "C" { + #[link_name = "\u{1}bar__extern"] + pub fn bar() -> ::std::os::raw::c_int; +} +extern "C" { + #[link_name = "\u{1}takes_ptr__extern"] + pub fn takes_ptr(arg: *mut ::std::os::raw::c_int) -> ::std::os::raw::c_int; +} +extern "C" { + #[link_name = "\u{1}takes_fn_ptr__extern"] + pub fn takes_fn_ptr( + f: ::std::option::Option< + unsafe extern "C" fn( + arg1: ::std::os::raw::c_int, + ) -> ::std::os::raw::c_int, + >, + ) -> ::std::os::raw::c_int; +} +extern "C" { + #[link_name = "\u{1}takes_fn__extern"] + pub fn takes_fn( + f: ::std::option::Option< + unsafe extern "C" fn( + arg1: ::std::os::raw::c_int, + ) -> ::std::os::raw::c_int, + >, + ) -> ::std::os::raw::c_int; +} +pub type func = ::std::option::Option< + unsafe extern "C" fn(arg1: ::std::os::raw::c_int) -> ::std::os::raw::c_int, +>; +extern "C" { + #[link_name = "\u{1}takes_alias__extern"] + pub fn takes_alias(f: func) -> ::std::os::raw::c_int; +} +extern "C" { + #[link_name = "\u{1}takes_qualified__extern"] + pub fn takes_qualified( + arg: *const *const ::std::os::raw::c_int, + ) -> ::std::os::raw::c_int; +} diff --git a/bindgen-tests/tests/headers/wrap-static-fns.h b/bindgen-tests/tests/headers/wrap-static-fns.h new file mode 100644 index 00000000..8b90c7bc --- /dev/null +++ b/bindgen-tests/tests/headers/wrap-static-fns.h @@ -0,0 +1,33 @@ +// bindgen-flags: --experimental --wrap-static-fns + +static inline int foo() { + return 11; +} +static int bar() { + return 1; +} +inline int baz() { + return 2; +} + +static inline int takes_ptr(int* arg) { + return *arg + 1; +} + +static inline int takes_fn_ptr(int (*f)(int)) { + return f(1); +} + +static inline int takes_fn(int (f)(int)) { + return f(2); +} + +typedef int (func)(int); + +static inline int takes_alias(func f) { + return f(3); +} + +static inline int takes_qualified(const int *const *arg) { + return **arg; +} diff --git a/bindgen-tests/tests/tests.rs b/bindgen-tests/tests/tests.rs index 25c073cc..ed8566c6 100644 --- a/bindgen-tests/tests/tests.rs +++ b/bindgen-tests/tests/tests.rs @@ -713,3 +713,40 @@ fn commandline_multiple_headers() { .header("tests/headers/16-byte-alignment.h"); build_flags_output_helper(&bindings); } + +#[test] +fn test_wrap_static_fns() { + // This test is for testing diffs of the generated C source and header files + // TODO: If another such feature is added, convert this test into a more generic + // test that looks at `tests/headers/generated` directory. + let expect_path = PathBuf::from("tests/expectations/tests/generated") + .join("wrap_static_fns"); + println!("In path is ::: {}", expect_path.display()); + + let generated_path = + PathBuf::from(env::var("OUT_DIR").unwrap()).join("wrap_static_fns"); + println!("Out path is ::: {}", generated_path.display()); + + let _bindings = Builder::default() + .header("tests/headers/wrap-static-fns.h") + .wrap_static_fns(true) + .wrap_static_fns_path(generated_path.display().to_string()) + .generate() + .expect("Failed to generate bindings"); + + let expected_c = fs::read_to_string(expect_path.with_extension("c")) + .expect("Could not read generated wrap_static_fns.c"); + + let actual_c = fs::read_to_string(generated_path.with_extension("c")) + .expect("Could not read actual wrap_static_fns.c"); + + if expected_c != actual_c { + error_diff_mismatch( + &actual_c, + &expected_c, + None, + &expect_path.with_extension("c"), + ) + .unwrap(); + } +} |