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);

23 changes: 18 additions & 5 deletions bindgen-tests/tests/parse_callbacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,34 @@ use bindgen::callbacks::*;

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

impl RemoveFunctionPrefixParseCallback {
pub fn new(prefix: &str) -> Self {
RemoveFunctionPrefixParseCallback {
remove_function_prefix: Some(prefix.to_string()),
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) {
fn generated_name_override(
&self,
item_name: &str,
item_kind: CallbackItemKind,
) -> Option<String> {
if let Some(prefix) = &self.remove_prefix {
if let Some(name) = item_name.strip_prefix(prefix) {
match item_kind {
CallbackItemKind::Function => {
assert!(name.starts_with("function_"));
}
CallbackItemKind::Var => {
assert!(name.starts_with("var_"));
}
}
assert!(name.ends_with("_name"));
return Some(name.to_string());
}
}
Expand Down
14 changes: 13 additions & 1 deletion bindgen/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ pub use crate::ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue};
pub use crate::ir::int::IntKind;
use std::fmt;

/// An enum to identify the kind of item being passed to `ParseCallbacks::generated_name_override`.
pub enum CallbackItemKind {
/// A Function
Function,
/// A Variable
Var,
}

/// An enum to allow ignoring parsing of macros.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MacroParsingBehavior {
Expand All @@ -32,7 +40,11 @@ pub trait ParseCallbacks: fmt::Debug {

/// 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> {
fn generated_name_override(
&self,
_item_name: &str,
_item_kind: CallbackItemKind,
) -> Option<String> {
None
}

Expand Down
8 changes: 4 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::CallbackItemKind;
use crate::clang::{self, Attribute};
use crate::parse::{
ClangItemParser, ClangSubItemParser, ParseError, ParseResult,
Expand Down Expand Up @@ -714,10 +715,9 @@ 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(&name, CallbackItemKind::Function)
}) {
name = nm;
}
assert!(!name.is_empty(), "Empty function name.");
Expand Down
17 changes: 15 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::{CallbackItemKind, MacroParsingBehavior};
use crate::clang;
use crate::clang::ClangToken;
use crate::parse::{
Expand Down Expand Up @@ -274,7 +274,20 @@ 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(
&name,
CallbackItemKind::Var,
)
}) {
name = nm;
}
}
// No more changes to name
let name = name;

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