diff --git a/bindgen-integration/build.rs b/bindgen-integration/build.rs index 583f46ea93..554ae40e3c 100644 --- a/bindgen-integration/build.rs +++ b/bindgen-integration/build.rs @@ -1,7 +1,7 @@ extern crate bindgen; use bindgen::callbacks::{ - DeriveInfo, IntKind, MacroParsingBehavior, ParseCallbacks, + DeriveInfo, IntKind, ItemInfo, MacroParsingBehavior, ParseCallbacks, }; use bindgen::{Builder, EnumVariation, Formatter}; use std::collections::HashSet; @@ -103,16 +103,18 @@ impl ParseCallbacks for MacroCallback { } } - fn item_name(&self, original_item_name: &str) -> Option { - if original_item_name.starts_with("my_prefixed_") { + fn item_name(&self, item_info: ItemInfo) -> Option { + if item_info.name.starts_with("my_prefixed_") { Some( - original_item_name + item_info + .name .trim_start_matches("my_prefixed_") .to_string(), ) - } else if original_item_name.starts_with("MY_PREFIXED_") { + } else if item_info.name.starts_with("MY_PREFIXED_") { Some( - original_item_name + item_info + .name .trim_start_matches("MY_PREFIXED_") .to_string(), ) diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs index cd7bb9773d..ba1575180b 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -89,8 +89,8 @@ pub trait ParseCallbacks: fmt::Debug { None } - /// Allows to rename an item, replacing `_original_item_name`. - fn item_name(&self, _original_item_name: &str) -> Option { + /// Allows to rename an item, replacing `_item_info.name`. + fn item_name(&self, _item_info: ItemInfo) -> Option { None } @@ -275,6 +275,7 @@ pub enum TypeKind { } /// A struct providing information about the item being passed to [`ParseCallbacks::generated_name_override`]. +#[derive(Clone, Copy)] #[non_exhaustive] pub struct ItemInfo<'a> { /// The name of the item @@ -284,8 +285,13 @@ pub struct ItemInfo<'a> { } /// An enum indicating the kind of item for an `ItemInfo`. +#[derive(Clone, Copy)] #[non_exhaustive] pub enum ItemKind { + /// A module + Module, + /// A type + Type, /// A Function Function, /// A Variable diff --git a/bindgen/ir/item.rs b/bindgen/ir/item.rs index 25c3c258aa..d38879f390 100644 --- a/bindgen/ir/item.rs +++ b/bindgen/ir/item.rs @@ -17,6 +17,7 @@ use super::module::Module; use super::template::{AsTemplateParam, TemplateParameters}; use super::traversal::{EdgeKind, Trace, Tracer}; use super::ty::{Type, TypeKind}; +use crate::callbacks::ItemInfo; use crate::clang; use crate::parse::{ClangSubItemParser, ParseError, ParseResult}; @@ -922,8 +923,19 @@ impl Item { let name = names.join("_"); let name = if opt.user_mangled == UserMangled::Yes { + let item_info = ItemInfo { + name: &name, + kind: match self.kind() { + ItemKind::Module(..) => crate::callbacks::ItemKind::Module, + ItemKind::Type(..) => crate::callbacks::ItemKind::Type, + ItemKind::Function(..) => { + crate::callbacks::ItemKind::Function + } + ItemKind::Var(..) => crate::callbacks::ItemKind::Var, + }, + }; ctx.options() - .last_callback(|callbacks| callbacks.item_name(&name)) + .last_callback(|callbacks| callbacks.item_name(item_info)) .unwrap_or(name) } else { name