Skip to content

Extend generated_name_override callback to variables #2351

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

Merged
merged 9 commits into from
Nov 28, 2022
Merged

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
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// bindgen-parse-callbacks: remove-function-prefix-my_custom_prefix_

extern const int my_custom_prefix_var_const_name;

extern int my_custom_prefix_var_mut_name;

void my_custom_prefix_function_name(const int x);

28 changes: 17 additions & 11 deletions bindgen-tests/tests/parse_callbacks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
use bindgen::callbacks::*;

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

impl RemoveFunctionPrefixParseCallback {
impl RemovePrefixParseCallback {
pub fn new(prefix: &str) -> Self {
RemoveFunctionPrefixParseCallback {
remove_function_prefix: Some(prefix.to_string()),
RemovePrefixParseCallback {
remove_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) {
impl ParseCallbacks for RemovePrefixParseCallback {
fn generated_name_override(&self, item_info: ItemInfo) -> Option<String> {
if let Some(prefix) = &self.remove_prefix {
let (item_name, expected_prefix, expected_suffix) = match item_info
{
ItemInfo::Function { name } => (name, "function_", "_name"),
ItemInfo::Var { name } => (name, "var_", "_name"),
};
if let Some(name) = item_name.strip_prefix(prefix) {
assert!(name.starts_with(expected_prefix));
assert!(name.ends_with(expected_suffix));
return Some(name.to_string());
}
}
Expand Down Expand Up @@ -67,8 +74,7 @@ pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
.split("remove-function-prefix-")
.last()
.to_owned();
let lnopc =
RemoveFunctionPrefixParseCallback::new(prefix.unwrap());
let lnopc = RemovePrefixParseCallback::new(prefix.unwrap());
Box::new(lnopc)
} else {
panic!("Couldn't find name ParseCallbacks: {}", cb)
Expand Down
20 changes: 17 additions & 3 deletions bindgen/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ pub trait ParseCallbacks: fmt::Debug {
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> {
/// This function will run for every extern variable and function. The returned value determines
/// the name visible in the bindings.
fn generated_name_override(&self, _item_info: ItemInfo) -> Option<String> {
None
}

Expand Down Expand Up @@ -122,3 +122,17 @@ pub struct DeriveInfo<'a> {
/// The name of the type.
pub name: &'a str,
}

/// An enum providing information about the item being passed to `ParseCallbacks::generated_name_override`.
pub enum ItemInfo<'a> {
/// A Function
Function {
/// The name of the function
name: &'a str,
},
/// A Variable
Var {
/// The name of the variable
name: &'a str,
},
}
10 changes: 6 additions & 4 deletions bindgen/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::dot::DotAttributes;
use super::item::Item;
use super::traversal::{EdgeKind, Trace, Tracer};
use super::ty::TypeKind;
use crate::callbacks::ItemInfo;
use crate::clang::{self, Attribute};
use crate::parse::{ClangSubItemParser, ParseError, ParseResult};
use clang_sys::{self, CXCallingConv};
Expand Down Expand Up @@ -712,10 +713,11 @@ impl ClangSubItemParser for Function {
// but seems easy enough to handle it here.
name.push_str("_destructor");
}
if let Some(nm) = context
.options()
.last_callback(|callbacks| callbacks.generated_name_override(&name))
{
if let Some(nm) = context.options().last_callback(|callbacks| {
callbacks.generated_name_override(ItemInfo::Function {
name: name.as_str(),
})
}) {
name = nm;
}
assert!(!name.is_empty(), "Empty function name.");
Expand Down
16 changes: 14 additions & 2 deletions bindgen/ir/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::function::cursor_mangling;
use super::int::IntKind;
use super::item::Item;
use super::ty::{FloatKind, TypeKind};
use crate::callbacks::MacroParsingBehavior;
use crate::callbacks::{ItemInfo, MacroParsingBehavior};
use crate::clang;
use crate::clang::ClangToken;
use crate::parse::{ClangSubItemParser, ParseError, ParseResult};
Expand Down Expand Up @@ -272,7 +272,19 @@ impl ClangSubItemParser for Var {
))
}
CXCursor_VarDecl => {
let name = cursor.spelling();
let mut name = cursor.spelling();
if cursor.linkage() == CXLinkage_External {
if let Some(nm) = ctx.options().last_callback(|callbacks| {
callbacks.generated_name_override(ItemInfo::Var {
name: name.as_str(),
})
}) {
name = nm;
}
}
// No more changes to name
let name = name;

if name.is_empty() {
warn!("Empty constant name?");
return Err(ParseError::Continue);
Expand Down