Skip to content
Draft
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
23 changes: 23 additions & 0 deletions bindgen-tests/tests/expectations/tests/rename_constructor.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions bindgen-tests/tests/headers/rename_constructor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// bindgen-parse-callbacks: rename-constructor

class Foo {
public:
Foo();
};
15 changes: 15 additions & 0 deletions bindgen-tests/tests/parse_callbacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,28 @@ impl ParseCallbacks for WrapAsVariadicFn {
}
}

#[derive(Debug)]
pub(super) struct RenameConstructor;

impl ParseCallbacks for RenameConstructor {
fn generated_name_override(&self, item_info: ItemInfo) -> Option<String> {
match item_info.kind {
ItemKind::Function if item_info.name == "new" => {
Some("create".into())
}
_ => None,
}
}
}

pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
match cb {
"enum-variant-rename" => Box::new(EnumVariantRename),
"blocklisted-type-implements-trait" => {
Box::new(BlocklistedTypeImplementsTrait)
}
"wrap-as-variadic-fn" => Box::new(WrapAsVariadicFn),
"rename-constructor" => Box::new(RenameConstructor),
"type-visibility" => Box::new(TypeVisibility),
call_back => {
if let Some(prefix) =
Expand Down
11 changes: 10 additions & 1 deletion bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::BindgenOptions;

use crate::callbacks::{
AttributeInfo, DeriveInfo, DiscoveredItem, DiscoveredItemId, FieldInfo,
TypeKind as DeriveTypeKind,
ItemInfo, TypeKind as DeriveTypeKind,
};
use crate::codegen::error::Error;
use crate::ir::analysis::{HasVtable, Sizedness};
Expand Down Expand Up @@ -3032,6 +3032,15 @@ impl Method {
_ => function.name().to_owned(),
};

if let Some(nm) = ctx.options().last_callback(|callbacks| {
callbacks.generated_name_override(ItemInfo {
name: name.as_str(),
kind: crate::callbacks::ItemKind::Function,
})
}) {
name = nm;
}

let TypeKind::Function(ref signature) =
*signature_item.expect_type().kind()
else {
Expand Down
Loading