Skip to content

Commit d62e9d4

Browse files
committed
Add tests for macro wrap function generation
1 parent 88e6e31 commit d62e9d4

File tree

5 files changed

+98
-1
lines changed

5 files changed

+98
-1
lines changed

bindgen-tests/tests/expectations/tests/function_macros.rs

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdint.h>
2+
#include <stdbool.h>
3+
#include "tests/headers/function_macros.h"
4+
5+
// Macro function wrappers
6+
7+
void SIMPLE__macro(void) { SIMPLE; }
8+
void INDIRECT_SIMPLE__macro(void) { INDIRECT_SIMPLE; }
9+
float COMPLEX__macro(uint32_t x) { return COMPLEX(x); }
10+
float INDIRECT_COMPLEX__macro(uint32_t x) { return INDIRECT_COMPLEX(x); }
11+
float CONDITIONAL_COMPLEX__macro(bool condition, uint32_t x) { return CONDITIONAL_COMPLEX(condition, x); }

bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <stdint.h>
2+
#include <stdbool.h>
13
#include "tests/headers/wrap-static-fns.h"
24

35
// Static wrappers
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// bindgen-flags: --macro-function "SIMPLE" --macro-function "INDIRECT_SIMPLE" --macro-function "f32 COMPLEX(u32)" --macro-function "f32 INDIRECT_COMPLEX(u32)" --macro-function "f32 CONDITIONAL_COMPLEX(bool, u32)"
2+
3+
void simple(void);
4+
5+
#define SIMPLE simple()
6+
#define INDIRECT_SIMPLE SIMPLE
7+
8+
float complex(int);
9+
10+
#define COMPLEX(x) complex(x)
11+
#define INDIRECT_COMPLEX(x) COMPLEX(x)
12+
13+
#define CONDITIONAL_COMPLEX(condition, x) ((condition) ? COMPLEX(x) : 0)
14+
15+
#define SIMPLE_NOT_CONFIGURED simple()

bindgen-tests/tests/tests.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate clap;
44
extern crate env_logger;
55
extern crate shlex;
66

7-
use bindgen::{clang_version, Builder};
7+
use bindgen::{clang_version, function_types::FunctionType, Builder};
88
use owo_colors::{OwoColorize, Style};
99
use similar::{ChangeTag, TextDiff};
1010
use std::env;
@@ -677,3 +677,50 @@ fn test_wrap_static_fns() {
677677
.unwrap();
678678
}
679679
}
680+
681+
#[test]
682+
fn test_function_macros() {
683+
if env::current_dir().unwrap().ends_with("rust-bindgen") {
684+
env::set_current_dir(Path::new("bindgen-tests")).unwrap();
685+
}
686+
if env::var("OUT_DIR").is_err() {
687+
env::set_var("OUT_DIR", PathBuf::from("target/out"));
688+
}
689+
let expect_path = PathBuf::from("tests/expectations/tests/generated")
690+
.join("function_macros");
691+
println!("In path is ::: {}", expect_path.display());
692+
693+
let generated_path =
694+
PathBuf::from(env::var("OUT_DIR").unwrap()).join("function_macros");
695+
println!("Out path is ::: {}", generated_path.display());
696+
697+
let _bindings = Builder::default()
698+
.header("tests/headers/function_macros.h")
699+
.macro_function("SIMPLE", FunctionType::new::<(), ()>())
700+
.macro_function("INDIRECT_SIMPLE", FunctionType::new::<(), ()>())
701+
.macro_function("COMPLEX", FunctionType::new::<f32, u32>())
702+
.macro_function("INDIRECT_COMPLEX", FunctionType::new::<f32, u32>())
703+
.macro_function(
704+
"CONDITIONAL_COMPLEX",
705+
FunctionType::new::<f32, (bool, u32)>(),
706+
)
707+
.native_code_generation_path(generated_path.display().to_string())
708+
.generate()
709+
.expect("Failed to generate bindings");
710+
711+
let expected_c = fs::read_to_string(expect_path.with_extension("c"))
712+
.expect("Could not read generated function_macros.c");
713+
714+
let actual_c = fs::read_to_string(generated_path.with_extension("c"))
715+
.expect("Could not read actual function_macros.c");
716+
717+
if expected_c != actual_c {
718+
error_diff_mismatch(
719+
&actual_c,
720+
&expected_c,
721+
None,
722+
&expect_path.with_extension("c"),
723+
)
724+
.unwrap();
725+
}
726+
}

0 commit comments

Comments
 (0)