Skip to content

Commit 6828091

Browse files
committed
Allow constructor name override.
This uses an existing callback to allow overriding of constructor name. Part of google/autocxx#124, though only necessary if we also do google/autocxx#1456.
1 parent 20aa65a commit 6828091

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

bindgen-tests/tests/expectations/tests/rename_constructor.rs

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// bindgen-parse-callbacks: rename-constructor
2+
3+
class Foo {
4+
public:
5+
Foo();
6+
};

bindgen-tests/tests/parse_callbacks/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,28 @@ impl ParseCallbacks for WrapAsVariadicFn {
146146
}
147147
}
148148

149+
#[derive(Debug)]
150+
pub(super) struct RenameConstructor;
151+
152+
impl ParseCallbacks for RenameConstructor {
153+
fn generated_name_override(&self, item_info: ItemInfo) -> Option<String> {
154+
match item_info.kind {
155+
ItemKind::Function if item_info.name == "new" => {
156+
Some("create".into())
157+
}
158+
_ => None,
159+
}
160+
}
161+
}
162+
149163
pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
150164
match cb {
151165
"enum-variant-rename" => Box::new(EnumVariantRename),
152166
"blocklisted-type-implements-trait" => {
153167
Box::new(BlocklistedTypeImplementsTrait)
154168
}
155169
"wrap-as-variadic-fn" => Box::new(WrapAsVariadicFn),
170+
"rename-constructor" => Box::new(RenameConstructor),
156171
"type-visibility" => Box::new(TypeVisibility),
157172
call_back => {
158173
if let Some(prefix) =

bindgen/codegen/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use super::BindgenOptions;
2222

2323
use crate::callbacks::{
2424
AttributeInfo, DeriveInfo, DiscoveredItem, DiscoveredItemId, FieldInfo,
25-
TypeKind as DeriveTypeKind,
25+
ItemInfo, TypeKind as DeriveTypeKind,
2626
};
2727
use crate::codegen::error::Error;
2828
use crate::ir::analysis::{HasVtable, Sizedness};
@@ -3032,6 +3032,15 @@ impl Method {
30323032
_ => function.name().to_owned(),
30333033
};
30343034

3035+
if let Some(nm) = ctx.options().last_callback(|callbacks| {
3036+
callbacks.generated_name_override(ItemInfo {
3037+
name: name.as_str(),
3038+
kind: crate::callbacks::ItemKind::Function,
3039+
})
3040+
}) {
3041+
name = nm;
3042+
}
3043+
30353044
let TypeKind::Function(ref signature) =
30363045
*signature_item.expect_type().kind()
30373046
else {

0 commit comments

Comments
 (0)