Skip to content

Add a callback to rename field names #2524

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bindgen/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ pub trait ParseCallbacks: fmt::Debug {
fn process_comment(&self, _comment: &str) -> Option<String> {
None
}

/// Allows renaming the name of a field, replacing `_name`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you specify what parent_name is? Assuming it's the struct type, something like type_name may be better, since usually parent/child refer to nested types or modules (rather than fields).

fn process_field_name(
&self,
_parent_name: &str,
_name: &str,
) -> Option<String> {
None
}
}

/// Relevant information about a type to which new derive attributes will be added using
Expand Down
19 changes: 17 additions & 2 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,7 @@ trait FieldCodegen<'a> {
fields: &mut F,
methods: &mut M,
extra: Self::Extra,
parent_name: &str,
) where
F: Extend<proc_macro2::TokenStream>,
M: Extend<proc_macro2::TokenStream>;
Expand All @@ -1315,6 +1316,7 @@ impl<'a> FieldCodegen<'a> for Field {
fields: &mut F,
methods: &mut M,
_: (),
parent_name: &str,
) where
F: Extend<proc_macro2::TokenStream>,
M: Extend<proc_macro2::TokenStream>,
Expand All @@ -1331,6 +1333,7 @@ impl<'a> FieldCodegen<'a> for Field {
fields,
methods,
(),
parent_name,
);
}
Field::Bitfields(ref unit) => {
Expand All @@ -1344,6 +1347,7 @@ impl<'a> FieldCodegen<'a> for Field {
fields,
methods,
(),
parent_name,
);
}
}
Expand Down Expand Up @@ -1393,6 +1397,7 @@ impl<'a> FieldCodegen<'a> for FieldData {
fields: &mut F,
methods: &mut M,
_: (),
parent_name: &str,
) where
F: Extend<proc_macro2::TokenStream>,
M: Extend<proc_macro2::TokenStream>,
Expand Down Expand Up @@ -1438,9 +1443,15 @@ impl<'a> FieldCodegen<'a> for FieldData {

let field_name = self
.name()
.map(|name| ctx.rust_mangle(name).into_owned())
.map(|name| {
let name = ctx.rust_mangle(name);
ctx.options()
.process_field_name(&parent_name, &name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clippy says

this expression creates a reference which is immediately dereferenced by the compiler

can you remove the &? (not sure which one or both)

.map(|name| Cow::Owned(name))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.map(|name| Cow::Owned(name))
.map(Cow::Owned)

.unwrap_or(name.to_owned())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.unwrap_or(name.to_owned())
.unwrap_or(name.into_owned())

Per clippy

this to_owned call clones the Cow<', str> itself and does not cause the Cow<', str> contents to become owned

})
.expect("Each field should have a name in codegen!");
let field_ident = ctx.rust_ident_raw(field_name.as_str());
let field_ident = ctx.rust_ident_raw(&field_name);

if let Some(padding_field) =
struct_layout.saw_field(&field_name, field_ty, self.offset())
Expand Down Expand Up @@ -1641,6 +1652,7 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit {
fields: &mut F,
methods: &mut M,
_: (),
parent_name: &str,
) where
F: Extend<proc_macro2::TokenStream>,
M: Extend<proc_macro2::TokenStream>,
Expand Down Expand Up @@ -1720,6 +1732,7 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit {
fields,
methods,
(&unit_field_name, &mut bitfield_representable_as_int),
parent_name,
);

// Generating a constructor requires the bitfield to be representable as an integer.
Expand Down Expand Up @@ -1800,6 +1813,7 @@ impl<'a> FieldCodegen<'a> for Bitfield {
_fields: &mut F,
methods: &mut M,
(unit_field_name, bitfield_representable_as_int): (&'a str, &mut bool),
_parent_name: &str,
) where
F: Extend<proc_macro2::TokenStream>,
M: Extend<proc_macro2::TokenStream>,
Expand Down Expand Up @@ -2002,6 +2016,7 @@ impl CodeGenerator for CompInfo {
&mut fields,
&mut methods,
(),
&canonical_name,
);
}
// Check whether an explicit padding field is needed
Expand Down
10 changes: 10 additions & 0 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,16 @@ impl BindgenOptions {
.and_then(|cb| cb.process_comment(&comment))
.unwrap_or(comment)
}

fn process_field_name(
&self,
parent_name: &str,
name: &str,
) -> Option<String> {
self.parse_callbacks
.last()
.and_then(|cb| cb.process_field_name(parent_name, name))
}
}

fn deprecated_target_diagnostic(target: RustTarget, _options: &BindgenOptions) {
Expand Down