Skip to content

Commit 15a18fa

Browse files
author
bors-servo
authored
Auto merge of #591 - ambaxter:macro_exp, r=emilio
How would you suggest testing this?
2 parents 74f6599 + 7908561 commit 15a18fa

File tree

7 files changed

+43
-14
lines changed

7 files changed

+43
-14
lines changed

bindgen-integration/build.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
extern crate bindgen;
22
extern crate gcc;
33

4+
use std::collections::HashSet;
45
use std::env;
56
use std::path::PathBuf;
7+
use std::sync::{Arc, RwLock};
68
use bindgen::Builder;
9+
use bindgen::callbacks::ParseCallbacks;
10+
11+
#[derive(Debug)]
12+
struct MacroCallback {
13+
macros: Arc<RwLock<HashSet<String>>>,
14+
}
15+
16+
impl ParseCallbacks for MacroCallback {
17+
fn parsed_macro(&self, _name: &str) {
18+
self.macros.write().unwrap().insert(String::from(_name));
19+
}
20+
}
721

822
fn main() {
923
gcc::Config::new()
1024
.cpp(true)
1125
.file("cpp/Test.cc")
1226
.compile("libtest.a");
1327

28+
let macros = Arc::new(RwLock::new(HashSet::new()));
29+
1430
let bindings = Builder::default()
1531
.no_unstable_rust()
1632
.enable_cxx_namespaces()
@@ -19,9 +35,11 @@ fn main() {
1935
.clang_arg("-x")
2036
.clang_arg("c++")
2137
.clang_arg("-std=c++11")
38+
.parse_callbacks(Box::new(MacroCallback {macros: macros.clone()}))
2239
.generate()
2340
.expect("Unable to generate bindings");
2441

42+
assert!(macros.read().unwrap().contains("TESTMACRO"));
2543

2644
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
2745
bindings

bindgen-integration/cpp/Test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#define TESTMACRO
4+
35
class Test {
46
int m_int;
57
double m_double;

src/chooser.rs renamed to src/callbacks.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ use std::panic::UnwindSafe;
77

88
/// A trait to allow configuring different kinds of types in different
99
/// situations.
10-
pub trait TypeChooser: fmt::Debug + UnwindSafe {
10+
pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
11+
12+
/// This function will be run on every macro that is identified
13+
fn parsed_macro(&self, _name: &str) {}
14+
1115
/// The integer kind an integer macro should have, given a name and the
1216
/// value of that macro, or `None` if you want the default to be chosen.
1317
fn int_macro(&self, _name: &str, _value: i64) -> Option<IntKind> {

src/ir/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use super::traversal::{self, Edge, ItemTraversal};
1111
use super::ty::{FloatKind, TemplateDeclaration, Type, TypeKind};
1212
use BindgenOptions;
1313
use cexpr;
14-
use chooser::TypeChooser;
14+
use callbacks::ParseCallbacks;
1515
use clang::{self, Cursor};
1616
use clang_sys;
1717
use parse::ClangItemParser;
@@ -233,9 +233,9 @@ impl<'ctx> BindgenContext<'ctx> {
233233
.expect("should have been parsing a type, if we finished parsing a type")
234234
}
235235

236-
/// Get the user-provided type chooser by reference, if any.
237-
pub fn type_chooser(&self) -> Option<&TypeChooser> {
238-
self.options().type_chooser.as_ref().map(|t| &**t)
236+
/// Get the user-provided callbacks by reference, if any.
237+
pub fn parse_callbacks(&self) -> Option<&ParseCallbacks> {
238+
self.options().parse_callbacks.as_ref().map(|t| &**t)
239239
}
240240

241241
/// Define a new item.

src/ir/enum_ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl Enum {
9191
};
9292
if let Some(val) = value {
9393
let name = cursor.spelling();
94-
let custom_behavior = ctx.type_chooser()
94+
let custom_behavior = ctx.parse_callbacks()
9595
.and_then(|t| {
9696
t.enum_variant_behavior(type_name, &name, val)
9797
})

src/ir/var.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ impl ClangSubItemParser for Var {
116116
use cexpr::literal::CChar;
117117
match cursor.kind() {
118118
CXCursor_MacroDefinition => {
119+
120+
if let Some(visitor) = ctx.parse_callbacks() {
121+
visitor.parsed_macro(&cursor.spelling());
122+
}
123+
119124
let value = parse_macro(ctx, &cursor, ctx.translation_unit());
120125

121126
let (id, value) = match value {
@@ -170,7 +175,7 @@ impl ClangSubItemParser for Var {
170175
(TypeKind::Pointer(char_ty), VarType::String(val))
171176
}
172177
EvalResult::Int(Wrapping(value)) => {
173-
let kind = ctx.type_chooser()
178+
let kind = ctx.parse_callbacks()
174179
.and_then(|c| c.int_macro(&name, value))
175180
.unwrap_or_else(|| if value < 0 {
176181
if value < i32::min_value() as i64 {

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ mod parse;
6363
mod regex_set;
6464
mod uses;
6565

66-
pub mod chooser;
66+
pub mod callbacks;
6767

6868
#[cfg(rustfmt)]
6969
mod codegen;
@@ -445,10 +445,10 @@ impl Builder {
445445
self
446446
}
447447

448-
/// Allows configuring types in different situations, see the `TypeChooser`
448+
/// Allows configuring types in different situations, see the `ParseCallbacks`
449449
/// documentation.
450-
pub fn type_chooser(mut self, cb: Box<chooser::TypeChooser>) -> Self {
451-
self.options.type_chooser = Some(cb);
450+
pub fn parse_callbacks(mut self, cb: Box<callbacks::ParseCallbacks>) -> Self {
451+
self.options.parse_callbacks = Some(cb);
452452
self
453453
}
454454

@@ -567,9 +567,9 @@ pub struct BindgenOptions {
567567
/// of all types defined therein. See the `uses` module for more.
568568
pub dummy_uses: Option<String>,
569569

570-
/// A user-provided type chooser to allow customizing different kinds of
570+
/// A user-provided visitor to allow customizing different kinds of
571571
/// situations.
572-
pub type_chooser: Option<Box<chooser::TypeChooser>>,
572+
pub parse_callbacks: Option<Box<callbacks::ParseCallbacks>>,
573573

574574
/// Which kind of items should we generate? By default, we'll generate all
575575
/// of them.
@@ -650,7 +650,7 @@ impl Default for BindgenOptions {
650650
clang_args: vec![],
651651
input_header: None,
652652
dummy_uses: None,
653-
type_chooser: None,
653+
parse_callbacks: None,
654654
codegen_config: CodegenConfig::all(),
655655
conservative_inline_namespaces: false,
656656
generate_comments: true,

0 commit comments

Comments
 (0)