Skip to content

Commit 3c8d6e9

Browse files
committed
Generated name override
1 parent a1a0043 commit 3c8d6e9

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

src/callbacks.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
3131
MacroParsingBehavior::Default
3232
}
3333

34+
/// This function will run for every function. The returned value determines the name visible
35+
/// in the bindings.
36+
fn generated_name_override(&self, _function_name: &str) -> Option<String> {
37+
None
38+
}
39+
3440
/// The integer kind an integer macro should have, given a name and the
3541
/// value of that macro, or `None` if you want the default to be chosen.
3642
fn int_macro(&self, _name: &str, _value: i64) -> Option<IntKind> {

src/ir/function.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,12 @@ impl ClangSubItemParser for Function {
635635
// but seems easy enough to handle it here.
636636
name.push_str("_destructor");
637637
}
638+
if let Some(callbacks) = context.parse_callbacks() {
639+
if let Some(nm) = callbacks.generated_name_override(&name) {
640+
name = nm;
641+
}
642+
}
643+
assert!(!name.is_empty(), "Empty function name.");
638644

639645
let mangled_name = cursor_mangling(context, &cursor);
640646
let comment = cursor.raw_comment();

tests/expectations/tests/issue-1375-prefixed-functions.rs

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// bindgen-parse-callbacks: remove-function-prefix-my_custom_prefix_
2+
3+
void my_custom_prefix_function_name(const int x);
4+

tests/parse_callbacks/mod.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
use bindgen::callbacks::*;
22

3+
#[derive(Debug)]
4+
pub struct RemoveFunctionPrefixParseCallback {
5+
pub remove_function_prefix: Option<String>,
6+
}
7+
8+
impl RemoveFunctionPrefixParseCallback {
9+
pub fn new(prefix: &str) -> Self {
10+
RemoveFunctionPrefixParseCallback {
11+
remove_function_prefix: Some(prefix.to_string()),
12+
}
13+
}
14+
}
15+
16+
impl ParseCallbacks for RemoveFunctionPrefixParseCallback {
17+
fn generated_name_override(&self, function_name: &str) -> Option<String> {
18+
if let Some(prefix) = &self.remove_function_prefix {
19+
if function_name.starts_with(prefix) {
20+
return Some(function_name[prefix.len()..].to_string());
21+
}
22+
}
23+
None
24+
}
25+
}
26+
327
#[derive(Debug)]
428
struct EnumVariantRename;
529

@@ -37,6 +61,18 @@ pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
3761
"blocklisted-type-implements-trait" => {
3862
Box::new(BlocklistedTypeImplementsTrait)
3963
}
40-
_ => panic!("Couldn't find name ParseCallbacks: {}", cb),
64+
call_back => {
65+
if call_back.starts_with("remove-function-prefix-") {
66+
let prefix = call_back
67+
.split("remove-function-prefix-")
68+
.last()
69+
.to_owned();
70+
let lnopc =
71+
RemoveFunctionPrefixParseCallback::new(prefix.unwrap());
72+
Box::new(lnopc)
73+
} else {
74+
panic!("Couldn't find name ParseCallbacks: {}", cb)
75+
}
76+
}
4177
}
4278
}

0 commit comments

Comments
 (0)