summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2018-01-31 11:26:31 -0600
committerGitHub <noreply@github.com>2018-01-31 11:26:31 -0600
commit92b86c5ca3b5aa14e23a19578898e7f43168a2fc (patch)
tree4486129d711d4324a983bc1e0071d4b6b381c71a
parentce7e69bfbc5a6e47eb1d9ee4f0b43a5afeef0f31 (diff)
parentb33d329a764f9a254e5ccee025f9cf3f31322c97 (diff)
Auto merge of #1243 - emilio:parse-callbacks, r=fitzgen
callbacks: Introduce MacroParsingBehavior to allow ignoring macros. This is symmetric, yet less powerful, than enum_variant_behavior. Fixes #687.
-rw-r--r--bindgen-integration/build.rs12
-rw-r--r--bindgen-integration/cpp/Test.h6
-rw-r--r--src/callbacks.rs22
-rw-r--r--src/ir/var.rs11
4 files changed, 43 insertions, 8 deletions
diff --git a/bindgen-integration/build.rs b/bindgen-integration/build.rs
index a9970135..ed5b9281 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 323a9216..e09c9ee7 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 30bd3faa..469314c4 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 db105da1..c71a9314 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);