Skip to content

Refactor item_name method to use ItemInfo struct #3221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions bindgen-integration/build.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -103,16 +104,18 @@ impl ParseCallbacks for MacroCallback {
}
}

fn item_name(&self, original_item_name: &str) -> Option<String> {
if original_item_name.starts_with("my_prefixed_") {
fn item_name(&self, item_info: ItemInfo) -> Option<String> {
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(),
)
Expand Down
10 changes: 8 additions & 2 deletions bindgen/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
/// Allows to rename an item, replacing `_item_info.name`.
fn item_name(&self, _item_info: ItemInfo) -> Option<String> {
None
}

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Comment on lines +293 to 295
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have had the common Debug, PartialEq, Eq, Hash derives as well, just like #3055?

/// A module
Module,
/// A type
Type,
/// A Function
Function,
/// A Variable
Expand Down
14 changes: 13 additions & 1 deletion bindgen/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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
Expand Down