Skip to content

Commit 63bf643

Browse files
authored
Merge pull request #2228 from justsmth/generated_name_override
Generated name override
2 parents 576fd8d + c1d8cfb commit 63bf643

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

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

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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+

bindgen-tests/tests/parse_callbacks/mod.rs

+37-1
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 let Some(name) = function_name.strip_prefix(prefix) {
20+
return Some(name.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
}

bindgen/callbacks.rs

+6
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> {

bindgen/ir/function.rs

+6
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,12 @@ impl ClangSubItemParser for Function {
664664
// but seems easy enough to handle it here.
665665
name.push_str("_destructor");
666666
}
667+
if let Some(callbacks) = context.parse_callbacks() {
668+
if let Some(nm) = callbacks.generated_name_override(&name) {
669+
name = nm;
670+
}
671+
}
672+
assert!(!name.is_empty(), "Empty function name.");
667673

668674
let mangled_name = cursor_mangling(context, &cursor);
669675
let comment = cursor.raw_comment();

0 commit comments

Comments
 (0)