Skip to content

Commit

Permalink
Merge pull request #2228 from justsmth/generated_name_override
Browse files Browse the repository at this point in the history
Generated name override
  • Loading branch information
pvdrz committed Oct 6, 2022
2 parents 576fd8d + c1d8cfb commit 63bf643
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.

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

4 changes: 4 additions & 0 deletions bindgen-tests/tests/headers/issue-1375-prefixed-functions.h
@@ -0,0 +1,4 @@
// bindgen-parse-callbacks: remove-function-prefix-my_custom_prefix_

void my_custom_prefix_function_name(const int x);

38 changes: 37 additions & 1 deletion bindgen-tests/tests/parse_callbacks/mod.rs
@@ -1,5 +1,29 @@
use bindgen::callbacks::*;

#[derive(Debug)]
pub struct RemoveFunctionPrefixParseCallback {
pub remove_function_prefix: Option<String>,
}

impl RemoveFunctionPrefixParseCallback {
pub fn new(prefix: &str) -> Self {
RemoveFunctionPrefixParseCallback {
remove_function_prefix: Some(prefix.to_string()),
}
}
}

impl ParseCallbacks for RemoveFunctionPrefixParseCallback {
fn generated_name_override(&self, function_name: &str) -> Option<String> {
if let Some(prefix) = &self.remove_function_prefix {
if let Some(name) = function_name.strip_prefix(prefix) {
return Some(name.to_string());
}
}
None
}
}

#[derive(Debug)]
struct EnumVariantRename;

Expand Down Expand Up @@ -37,6 +61,18 @@ pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
"blocklisted-type-implements-trait" => {
Box::new(BlocklistedTypeImplementsTrait)
}
_ => panic!("Couldn't find name ParseCallbacks: {}", cb),
call_back => {
if call_back.starts_with("remove-function-prefix-") {
let prefix = call_back
.split("remove-function-prefix-")
.last()
.to_owned();
let lnopc =
RemoveFunctionPrefixParseCallback::new(prefix.unwrap());
Box::new(lnopc)
} else {
panic!("Couldn't find name ParseCallbacks: {}", cb)
}
}
}
}
6 changes: 6 additions & 0 deletions bindgen/callbacks.rs
Expand Up @@ -31,6 +31,12 @@ pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
MacroParsingBehavior::Default
}

/// This function will run for every function. The returned value determines the name visible
/// in the bindings.
fn generated_name_override(&self, _function_name: &str) -> Option<String> {
None
}

/// The integer kind an integer macro should have, given a name and the
/// value of that macro, or `None` if you want the default to be chosen.
fn int_macro(&self, _name: &str, _value: i64) -> Option<IntKind> {
Expand Down
6 changes: 6 additions & 0 deletions bindgen/ir/function.rs
Expand Up @@ -664,6 +664,12 @@ impl ClangSubItemParser for Function {
// but seems easy enough to handle it here.
name.push_str("_destructor");
}
if let Some(callbacks) = context.parse_callbacks() {
if let Some(nm) = callbacks.generated_name_override(&name) {
name = nm;
}
}
assert!(!name.is_empty(), "Empty function name.");

let mangled_name = cursor_mangling(context, &cursor);
let comment = cursor.raw_comment();
Expand Down

0 comments on commit 63bf643

Please sign in to comment.