Skip to content

Commit 2882af2

Browse files
twizmwazinemilio
authored andcommitted
Refactor item_name method to use ItemInfo struct
1 parent e2940cd commit 2882af2

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

bindgen-integration/build.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
extern crate bindgen;
22

33
use bindgen::callbacks::{
4-
DeriveInfo, IntKind, MacroParsingBehavior, ParseCallbacks, Token, TokenKind,
4+
DeriveInfo, IntKind, ItemInfo, MacroParsingBehavior, ParseCallbacks, Token,
5+
TokenKind,
56
};
67
use bindgen::{Builder, EnumVariation, Formatter};
78
use std::collections::HashSet;
@@ -103,16 +104,18 @@ impl ParseCallbacks for MacroCallback {
103104
}
104105
}
105106

106-
fn item_name(&self, original_item_name: &str) -> Option<String> {
107-
if original_item_name.starts_with("my_prefixed_") {
107+
fn item_name(&self, item_info: ItemInfo) -> Option<String> {
108+
if item_info.name.starts_with("my_prefixed_") {
108109
Some(
109-
original_item_name
110+
item_info
111+
.name
110112
.trim_start_matches("my_prefixed_")
111113
.to_string(),
112114
)
113-
} else if original_item_name.starts_with("MY_PREFIXED_") {
115+
} else if item_info.name.starts_with("MY_PREFIXED_") {
114116
Some(
115-
original_item_name
117+
item_info
118+
.name
116119
.trim_start_matches("MY_PREFIXED_")
117120
.to_string(),
118121
)

bindgen/callbacks.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ pub trait ParseCallbacks: fmt::Debug {
9494
None
9595
}
9696

97-
/// Allows to rename an item, replacing `_original_item_name`.
98-
fn item_name(&self, _original_item_name: &str) -> Option<String> {
97+
/// Allows to rename an item, replacing `_item_info.name`.
98+
fn item_name(&self, _item_info: ItemInfo) -> Option<String> {
9999
None
100100
}
101101

@@ -280,6 +280,7 @@ pub enum TypeKind {
280280
}
281281

282282
/// A struct providing information about the item being passed to [`ParseCallbacks::generated_name_override`].
283+
#[derive(Clone, Copy)]
283284
#[non_exhaustive]
284285
pub struct ItemInfo<'a> {
285286
/// The name of the item
@@ -289,8 +290,13 @@ pub struct ItemInfo<'a> {
289290
}
290291

291292
/// An enum indicating the kind of item for an `ItemInfo`.
293+
#[derive(Clone, Copy)]
292294
#[non_exhaustive]
293295
pub enum ItemKind {
296+
/// A module
297+
Module,
298+
/// A type
299+
Type,
294300
/// A Function
295301
Function,
296302
/// A Variable

bindgen/ir/item.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use super::module::Module;
1717
use super::template::{AsTemplateParam, TemplateParameters};
1818
use super::traversal::{EdgeKind, Trace, Tracer};
1919
use super::ty::{Type, TypeKind};
20+
use crate::callbacks::ItemInfo;
2021
use crate::clang;
2122
use crate::parse::{ClangSubItemParser, ParseError, ParseResult};
2223

@@ -922,8 +923,19 @@ impl Item {
922923
let name = names.join("_");
923924

924925
let name = if opt.user_mangled == UserMangled::Yes {
926+
let item_info = ItemInfo {
927+
name: &name,
928+
kind: match self.kind() {
929+
ItemKind::Module(..) => crate::callbacks::ItemKind::Module,
930+
ItemKind::Type(..) => crate::callbacks::ItemKind::Type,
931+
ItemKind::Function(..) => {
932+
crate::callbacks::ItemKind::Function
933+
}
934+
ItemKind::Var(..) => crate::callbacks::ItemKind::Var,
935+
},
936+
};
925937
ctx.options()
926-
.last_callback(|callbacks| callbacks.item_name(&name))
938+
.last_callback(|callbacks| callbacks.item_name(item_info))
927939
.unwrap_or(name)
928940
} else {
929941
name

0 commit comments

Comments
 (0)