Skip to content

Commit 92b86c5

Browse files
author
bors-servo
authored
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.
2 parents ce7e69b + b33d329 commit 92b86c5

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

bindgen-integration/build.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,22 @@ use std::env;
66
use std::path::PathBuf;
77
use std::sync::{Arc, RwLock};
88
use bindgen::Builder;
9-
use bindgen::callbacks::ParseCallbacks;
9+
use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks};
1010

1111
#[derive(Debug)]
1212
struct MacroCallback {
1313
macros: Arc<RwLock<HashSet<String>>>,
1414
}
1515

1616
impl ParseCallbacks for MacroCallback {
17-
fn parsed_macro(&self, _name: &str) {
18-
self.macros.write().unwrap().insert(String::from(_name));
17+
fn will_parse_macro(&self, name: &str) -> MacroParsingBehavior {
18+
self.macros.write().unwrap().insert(name.into());
19+
20+
if name == "MY_ANNOYING_MACRO" {
21+
return MacroParsingBehavior::Ignore
22+
}
23+
24+
MacroParsingBehavior::Default
1925
}
2026
}
2127

bindgen-integration/cpp/Test.h

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
#define TESTMACRO
44

5+
enum {
6+
MY_ANNOYING_MACRO =
7+
#define MY_ANNOYING_MACRO 1
8+
MY_ANNOYING_MACRO,
9+
};
10+
511
class Test {
612
int m_int;
713
double m_double;

src/callbacks.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,29 @@ pub use ir::int::IntKind;
55
use std::fmt;
66
use std::panic::UnwindSafe;
77

8+
/// An enum to allow ignoring parsing of macros.
9+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
10+
pub enum MacroParsingBehavior {
11+
/// Ignore the macro, generating no code for it, or anything that depends on
12+
/// it.
13+
Ignore,
14+
/// The default behavior bindgen would have otherwise.
15+
Default,
16+
}
17+
18+
impl Default for MacroParsingBehavior {
19+
fn default() -> Self {
20+
MacroParsingBehavior::Default
21+
}
22+
}
23+
824
/// A trait to allow configuring different kinds of types in different
925
/// situations.
1026
pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
11-
/// This function will be run on every macro that is identified
12-
fn parsed_macro(&self, _name: &str) {}
27+
/// This function will be run on every macro that is identified.
28+
fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior {
29+
MacroParsingBehavior::Default
30+
}
1331

1432
/// The integer kind an integer macro should have, given a name and the
1533
/// value of that macro, or `None` if you want the default to be chosen.

src/ir/var.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Intermediate representation of variables.
22
3+
use callbacks::MacroParsingBehavior;
34
use super::context::{BindgenContext, TypeId};
45
use super::dot::DotAttributes;
56
use super::function::cursor_mangling;
@@ -122,9 +123,13 @@ impl ClangSubItemParser for Var {
122123
use cexpr::literal::CChar;
123124
match cursor.kind() {
124125
CXCursor_MacroDefinition => {
125-
126-
if let Some(visitor) = ctx.parse_callbacks() {
127-
visitor.parsed_macro(&cursor.spelling());
126+
if let Some(callbacks) = ctx.parse_callbacks() {
127+
match callbacks.will_parse_macro(&cursor.spelling()) {
128+
MacroParsingBehavior::Ignore => {
129+
return Err(ParseError::Continue);
130+
}
131+
MacroParsingBehavior::Default => {}
132+
}
128133
}
129134

130135
let value = parse_macro(ctx, &cursor);

0 commit comments

Comments
 (0)