Skip to content
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

Refactor item_name method to use ItemInfo struct #3129

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 8 additions & 6 deletions bindgen-integration/build.rs
Original file line number Diff line number Diff line change
@@ -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<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(),
)
10 changes: 8 additions & 2 deletions bindgen/callbacks.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
/// Allows to rename an item, replacing `_item_info.name`.
fn item_name(&self, _item_info: ItemInfo) -> Option<String> {
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,8 +264,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
13 changes: 12 additions & 1 deletion bindgen/ir/item.rs
Original file line number Diff line number Diff line change
@@ -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,18 @@ 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,
_ => panic!("Unexpected item kind"),
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this correct? There are no tests for this but reporting everything that isn't a function a Var seems totally wrong.

Copy link
Author

Choose a reason for hiding this comment

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

Good catch, it isn't really correct. It looks like the ir ItemKind type has 4 variants, while the callback ItemKind type has only 2. Modules and Types are the variants missing, I'll add those to the callback enum so that they can be correctly handled.

};
ctx.options()
.last_callback(|callbacks| callbacks.item_name(&name))
.last_callback(|callbacks| callbacks.item_name(item_info))
.unwrap_or(name)
} else {
name
Loading
Oops, something went wrong.