Skip to content

callbacks: Introduce MacroParsingBehavior to allow ignoring macros. #1243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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