Skip to content

Commit

Permalink
callbacks: Introduce MacroParsingBehavior to allow ignoring macros.
Browse files Browse the repository at this point in the history
This is symmetric, yet less powerful, than enum_variant_behavior.

Fixes rust-lang#687.
  • Loading branch information
emilio committed Jan 30, 2018
1 parent 34b9216 commit b33d329
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
12 changes: 9 additions & 3 deletions bindgen-integration/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ 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 {
macros: Arc<RwLock<HashSet<String>>>,
}

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
}
}

Expand Down
6 changes: 6 additions & 0 deletions bindgen-integration/cpp/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 20 additions & 2 deletions src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 8 additions & 3 deletions src/ir/var.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b33d329

Please sign in to comment.