From dc2a5c4075fe70793bc61a1c70f4e6d2ee327571 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Thu, 13 Feb 2025 11:16:31 -0700 Subject: [PATCH] Refactor item_name method to use ItemInfo struct --- bindgen-integration/build.rs | 15 +++++++++------ bindgen/callbacks.rs | 10 ++++++++-- bindgen/ir/item.rs | 14 +++++++++++++- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/bindgen-integration/build.rs b/bindgen-integration/build.rs index 0d8fa33c93..ef167775cf 100644 --- a/bindgen-integration/build.rs +++ b/bindgen-integration/build.rs @@ -1,7 +1,8 @@ extern crate bindgen; use bindgen::callbacks::{ - DeriveInfo, IntKind, MacroParsingBehavior, ParseCallbacks, Token, TokenKind, + DeriveInfo, IntKind, ItemInfo, MacroParsingBehavior, ParseCallbacks, Token, + TokenKind, }; use bindgen::{Builder, EnumVariation, Formatter}; use std::collections::HashSet; @@ -103,16 +104,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 f7f2f6cc9d..93005ce8e5 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -94,8 +94,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 } @@ -280,6 +280,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 @@ -289,8 +290,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