diff options
-rw-r--r-- | bindgen-integration/build.rs | 25 | ||||
-rw-r--r-- | bindgen-integration/cpp/Test.h | 4 | ||||
-rw-r--r-- | src/callbacks.rs | 5 | ||||
-rw-r--r-- | src/ir/var.rs | 3 |
4 files changed, 35 insertions, 2 deletions
diff --git a/bindgen-integration/build.rs b/bindgen-integration/build.rs index 1a86add1..cadec267 100644 --- a/bindgen-integration/build.rs +++ b/bindgen-integration/build.rs @@ -4,13 +4,14 @@ extern crate gcc; use std::collections::HashSet; use std::env; use std::path::PathBuf; -use std::sync::{Arc, RwLock}; +use std::sync::{Arc, RwLock, Mutex}; use bindgen::Builder; use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks}; #[derive(Debug)] struct MacroCallback { macros: Arc<RwLock<HashSet<String>>>, + seen_hellos: Mutex<u32>, } impl ParseCallbacks for MacroCallback { @@ -33,6 +34,26 @@ impl ParseCallbacks for MacroCallback { None } } + + fn str_macro(&self, name: &str, value: &[u8]) { + match &name { + &"TESTMACRO_STRING_EXPANDED" | + &"TESTMACRO_STRING" | + &"TESTMACRO_INTEGER" => { + // The integer test macro is, actually, not expected to show up here at all -- but + // should produce an error if it does. + assert_eq!(value, b"Hello Preprocessor!", "str_macro handle received unexpected value"); + *self.seen_hellos.lock().unwrap() += 1; + }, + _ => {} + } + } +} + +impl Drop for MacroCallback { + fn drop(&mut self) { + assert_eq!(*self.seen_hellos.lock().unwrap(), 2, "str_macro handle was not called once for all relevant macros") + } } fn main() { @@ -51,7 +72,7 @@ fn main() { .module_raw_line("root::testing", "pub type Bar = i32;") .header("cpp/Test.h") .clang_args(&["-x", "c++", "-std=c++11"]) - .parse_callbacks(Box::new(MacroCallback {macros: macros.clone()})) + .parse_callbacks(Box::new(MacroCallback {macros: macros.clone(), seen_hellos: Mutex::new(0)})) .generate() .expect("Unable to generate bindings"); diff --git a/bindgen-integration/cpp/Test.h b/bindgen-integration/cpp/Test.h index fe9bf0d4..03a55b88 100644 --- a/bindgen-integration/cpp/Test.h +++ b/bindgen-integration/cpp/Test.h @@ -2,6 +2,10 @@ #define TESTMACRO +#define TESTMACRO_INTEGER 42 +#define TESTMACRO_STRING "Hello Preprocessor!" +#define TESTMACRO_STRING_EXPANDED TESTMACRO_STRING + #include <cwchar> enum { diff --git a/src/callbacks.rs b/src/callbacks.rs index 26f77338..81fe0c0e 100644 --- a/src/callbacks.rs +++ b/src/callbacks.rs @@ -35,6 +35,11 @@ pub trait ParseCallbacks: fmt::Debug + UnwindSafe { None } + /// This will be run on every string macro. The callback can not influence the further + /// treatment of the macro, but may use the value to generate additional code or configuration. + fn str_macro(&self, _name: &str, _value: &[u8]) { + } + /// This function should return whether, given an enum variant /// name, and value, this enum variant will forcibly be a constant. fn enum_variant_behavior( diff --git a/src/ir/var.rs b/src/ir/var.rs index e29985ef..14f133fd 100644 --- a/src/ir/var.rs +++ b/src/ir/var.rs @@ -199,6 +199,9 @@ impl ClangSubItemParser for Var { true, ctx, ); + if let Some(callbacks) = ctx.parse_callbacks() { + callbacks.str_macro(&name, &val); + } (TypeKind::Pointer(char_ty), VarType::String(val)) } EvalResult::Int(Wrapping(value)) => { |