mirror of
https://github.com/openharmony/third_party_rust_bindgen.git
synced 2026-07-19 19:53:31 -04:00
callbacks: Introduce MacroParsingBehavior to allow ignoring macros.
This is symmetric, yet less powerful, than enum_variant_behavior. Fixes #687.
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
+20
-2
@@ -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.
|
||||
|
||||
+8
-3
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user