Skip to content

Generated name override #2214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
6 changes: 6 additions & 0 deletions src/callbacks.rs
Original file line number Diff line number Diff line change
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 link_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 src/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,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.link_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
36 changes: 36 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
use bindgen::callbacks::ParseCallbacks;
use bindgen::{
builder, AliasVariation, Builder, CodegenConfig, EnumVariation,
MacroTypeVariation, RustTarget, DEFAULT_ANON_FIELDS_PREFIX,
RUST_TARGET_STRINGS,
};
use clap::{App, Arg};
use std::fmt::Debug;
use std::fs::File;
use std::io::{self, stderr, Error, ErrorKind, Write};
use std::path::PathBuf;
use std::str::FromStr;

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

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

impl ParseCallbacks for LinkNameOverrideParseCallback {
fn link_name_override(&self, function_name: &str) -> Option<String> {
if let Some(prefix) = &self.remove_function_prefix {
if function_name.starts_with(prefix) {
return Some(function_name[prefix.len()..].to_string());
}
}
None
}
}

/// Construct a new [`Builder`](./struct.Builder.html) from command line flags.
pub fn builder_from_flags<I>(
args: I,
Expand Down Expand Up @@ -545,6 +571,11 @@ where
Arg::new("vtable-generation")
.long("vtable-generation")
.help("Enables generation of vtable functions."),
Arg::new("remove-function-prefix")
.long("remove-function-prefix")
.multiple_occurrences(true)
.takes_value(true)
.help("Remove prefix when generating Rust function name.")
]) // .args()
.get_matches_from(args);

Expand Down Expand Up @@ -1015,6 +1046,11 @@ where
builder = builder.vtable_generation(true);
}

if let Some(prefix) = matches.value_of("remove-function-prefix") {
let lnopc = LinkNameOverrideParseCallback::new(prefix);
builder = builder.parse_callbacks(Box::new(lnopc));
}

let verbose = matches.is_present("verbose");

Ok((builder, output, verbose))
Expand Down
11 changes: 11 additions & 0 deletions tests/expectations/tests/issue-1375-prefixed-functions.rs

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

5 changes: 5 additions & 0 deletions tests/headers/issue-1375-prefixed-functions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// bindgen-flags: --remove-function-prefix my_custom_prefix_
// bindgen-parse-callbacks: remove-function-prefix-my_custom_prefix_

void my_custom_prefix_function_name(const int x);

14 changes: 13 additions & 1 deletion tests/parse_callbacks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::options::LinkNameOverrideParseCallback;
use bindgen::callbacks::*;

#[derive(Debug)]
Expand Down Expand Up @@ -37,6 +38,17 @@ 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 = LinkNameOverrideParseCallback::new(prefix.unwrap());
Box::new(lnopc)
} else {
panic!("Couldn't find name ParseCallbacks: {}", cb)
}
}
}
}