diff --git a/bindgen-integration/build.rs b/bindgen-integration/build.rs index a9970135cd..ed5b928150 100644 --- a/bindgen-integration/build.rs +++ b/bindgen-integration/build.rs @@ -6,7 +6,7 @@ use std::env; use std::path::PathBuf; use std::sync::{Arc, RwLock}; use bindgen::Builder; -use bindgen::callbacks::ParseCallbacks; +use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks}; #[derive(Debug)] struct MacroCallback { @@ -14,8 +14,14 @@ struct MacroCallback { } impl ParseCallbacks for MacroCallback { - fn parsed_macro(&self, _name: &str) { - self.macros.write().unwrap().insert(String::from(_name)); + fn will_parse_macro(&self, name: &str) -> MacroParsingBehavior { + self.macros.write().unwrap().insert(name.into()); + + if name == "MY_ANNOYING_MACRO" { + return MacroParsingBehavior::Ignore + } + + MacroParsingBehavior::Default } } diff --git a/bindgen-integration/cpp/Test.h b/bindgen-integration/cpp/Test.h index 4ab8373e6b..d128a0e05e 100644 --- a/bindgen-integration/cpp/Test.h +++ b/bindgen-integration/cpp/Test.h @@ -2,6 +2,12 @@ #define TESTMACRO +enum { + MY_ANNOYING_MACRO = +#define MY_ANNOYING_MACRO 1 + MY_ANNOYING_MACRO, +}; + class Test { int m_int; double m_double; diff --git a/src/callbacks.rs b/src/callbacks.rs index 30bd3faad5..469314c4ce 100644 --- a/src/callbacks.rs +++ b/src/callbacks.rs @@ -5,11 +5,29 @@ pub use ir::int::IntKind; use std::fmt; use std::panic::UnwindSafe; +/// An enum to allow ignoring parsing of macros. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum MacroParsingBehavior { + /// Ignore the macro, generating no code for it, or anything that depends on + /// it. + Ignore, + /// The default behavior bindgen would have otherwise. + Default, +} + +impl Default for MacroParsingBehavior { + fn default() -> Self { + MacroParsingBehavior::Default + } +} + /// A trait to allow configuring different kinds of types in different /// situations. pub trait ParseCallbacks: fmt::Debug + UnwindSafe { - /// This function will be run on every macro that is identified - fn parsed_macro(&self, _name: &str) {} + /// This function will be run on every macro that is identified. + fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior { + MacroParsingBehavior::Default + } /// The integer kind an integer macro should have, given a name and the /// value of that macro, or `None` if you want the default to be chosen. diff --git a/src/ir/var.rs b/src/ir/var.rs index db105da1ae..c71a931419 100644 --- a/src/ir/var.rs +++ b/src/ir/var.rs @@ -1,5 +1,6 @@ //! Intermediate representation of variables. +use callbacks::MacroParsingBehavior; use super::context::{BindgenContext, TypeId}; use super::dot::DotAttributes; use super::function::cursor_mangling; @@ -122,9 +123,13 @@ impl ClangSubItemParser for Var { use cexpr::literal::CChar; match cursor.kind() { CXCursor_MacroDefinition => { - - if let Some(visitor) = ctx.parse_callbacks() { - visitor.parsed_macro(&cursor.spelling()); + if let Some(callbacks) = ctx.parse_callbacks() { + match callbacks.will_parse_macro(&cursor.spelling()) { + MacroParsingBehavior::Ignore => { + return Err(ParseError::Continue); + } + MacroParsingBehavior::Default => {} + } } let value = parse_macro(ctx, &cursor);