Skip to content

Commit 6525e65

Browse files
twizmwazinemilio
authored andcommitted
Refactor item_name method to use ItemInfo struct
1 parent 2613129 commit 6525e65

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

bindgen-integration/build.rs

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

33
use bindgen::callbacks::{
4-
DeriveInfo, IntKind, MacroParsingBehavior, ParseCallbacks, Token, TokenKind,
4+
DeriveInfo, IntKind, ItemInfo, MacroParsingBehavior, ParseCallbacks, Token, TokenKind,
55
};
66
use bindgen::{Builder, EnumVariation, Formatter};
77
use std::collections::HashSet;
@@ -103,16 +103,18 @@ impl ParseCallbacks for MacroCallback {
103103
}
104104
}
105105

106-
fn item_name(&self, original_item_name: &str) -> Option<String> {
107-
if original_item_name.starts_with("my_prefixed_") {
106+
fn item_name(&self, item_info: ItemInfo) -> Option<String> {
107+
if item_info.name.starts_with("my_prefixed_") {
108108
Some(
109-
original_item_name
109+
item_info
110+
.name
110111
.trim_start_matches("my_prefixed_")
111112
.to_string(),
112113
)
113-
} else if original_item_name.starts_with("MY_PREFIXED_") {
114+
} else if item_info.name.starts_with("MY_PREFIXED_") {
114115
Some(
115-
original_item_name
116+
item_info
117+
.name
116118
.trim_start_matches("MY_PREFIXED_")
117119
.to_string(),
118120
)

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)