From 27b6d79e61c063d503920ba3903c358c5cea13ab Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Thu, 13 Feb 2025 11:16:31 -0700 Subject: [PATCH 1/4] Refactor item_name method to use ItemInfo struct --- bindgen-integration/build.rs | 14 ++++++++------ bindgen/callbacks.rs | 6 ++++-- bindgen/ir/item.rs | 12 +++++++++++- 3 files changed, 23 insertions(+), 9 deletions(-) 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 8a21e98dea..a4dafc657a 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 } @@ -254,6 +254,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 @@ -263,6 +264,7 @@ pub struct ItemInfo<'a> { } /// An enum indicating the kind of item for an `ItemInfo`. +#[derive(Clone, Copy)] #[non_exhaustive] pub enum ItemKind { /// A Function diff --git a/bindgen/ir/item.rs b/bindgen/ir/item.rs index 8b0d24cd04..314d2341f5 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,17 @@ 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::Function(..) => { + crate::callbacks::ItemKind::Function + } + _ => 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 From bcd8398424554b03ad7c9507050abb3f5e4e67e7 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Tue, 18 Feb 2025 17:09:43 -0700 Subject: [PATCH 2/4] Add Module and Type variants to bindgen::callback::ItemKind --- bindgen/callbacks.rs | 4 ++++ bindgen/ir/item.rs | 9 +++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs index a4dafc657a..b22191d034 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -267,6 +267,10 @@ pub struct ItemInfo<'a> { #[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 314d2341f5..dc924476d1 100644 --- a/bindgen/ir/item.rs +++ b/bindgen/ir/item.rs @@ -926,10 +926,11 @@ impl Item { let item_info = ItemInfo { name: &name, kind: match self.kind() { - ItemKind::Function(..) => { - crate::callbacks::ItemKind::Function - } - _ => crate::callbacks::ItemKind::Var, + ItemKind::Module(..) => crate::callbacks::ItemKind::Module, + ItemKind::Type(..) => crate::callbacks::ItemKind::Type, + ItemKind::Function(..) => crate::callbacks::ItemKind::Function, + ItemKind::Var(..) => crate::callbacks::ItemKind::Var, + _ => panic!("Unexpected item kind"), }, }; ctx.options() From 9a57e8c56c6df14a941b09dc89856e0533abe03b Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Mon, 14 Apr 2025 15:38:45 -0700 Subject: [PATCH 3/4] Remove unecessary match arm panic --- bindgen/ir/item.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/bindgen/ir/item.rs b/bindgen/ir/item.rs index 7d89b77d92..11a53e97b1 100644 --- a/bindgen/ir/item.rs +++ b/bindgen/ir/item.rs @@ -930,7 +930,6 @@ impl Item { ItemKind::Type(..) => crate::callbacks::ItemKind::Type, ItemKind::Function(..) => crate::callbacks::ItemKind::Function, ItemKind::Var(..) => crate::callbacks::ItemKind::Var, - _ => panic!("Unexpected item kind"), }, }; ctx.options() From b2000d79652bf34f5e3c2882e81235506a13dc6f Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Mon, 14 Apr 2025 18:00:09 -0700 Subject: [PATCH 4/4] cargo +nightly fmt --- bindgen/ir/item.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bindgen/ir/item.rs b/bindgen/ir/item.rs index 11a53e97b1..d38879f390 100644 --- a/bindgen/ir/item.rs +++ b/bindgen/ir/item.rs @@ -928,7 +928,9 @@ impl Item { kind: match self.kind() { ItemKind::Module(..) => crate::callbacks::ItemKind::Module, ItemKind::Type(..) => crate::callbacks::ItemKind::Type, - ItemKind::Function(..) => crate::callbacks::ItemKind::Function, + ItemKind::Function(..) => { + crate::callbacks::ItemKind::Function + } ItemKind::Var(..) => crate::callbacks::ItemKind::Var, }, };