From 8817ba6d397143c5983c4f1be8f52028aed84f02 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Sat, 13 May 2023 20:47:19 +1200 Subject: [PATCH 1/3] Add ParseCallbacks::process_field_name --- bindgen/callbacks.rs | 9 +++++++++ bindgen/codegen/mod.rs | 19 +++++++++++++++++-- bindgen/lib.rs | 10 ++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs index 582446687c..21ac9af691 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -135,6 +135,15 @@ pub trait ParseCallbacks: fmt::Debug { fn process_comment(&self, _comment: &str) -> Option { None } + + /// . + fn process_field_name( + &self, + _parent_name: &str, + _name: &str, + ) -> Option { + None + } } /// Relevant information about a type to which new derive attributes will be added using diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index 47f23193cb..2626ebbe5f 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -1296,6 +1296,7 @@ trait FieldCodegen<'a> { fields: &mut F, methods: &mut M, extra: Self::Extra, + parent_name: &str, ) where F: Extend, M: Extend; @@ -1315,6 +1316,7 @@ impl<'a> FieldCodegen<'a> for Field { fields: &mut F, methods: &mut M, _: (), + parent_name: &str, ) where F: Extend, M: Extend, @@ -1331,6 +1333,7 @@ impl<'a> FieldCodegen<'a> for Field { fields, methods, (), + parent_name, ); } Field::Bitfields(ref unit) => { @@ -1344,6 +1347,7 @@ impl<'a> FieldCodegen<'a> for Field { fields, methods, (), + parent_name, ); } } @@ -1393,6 +1397,7 @@ impl<'a> FieldCodegen<'a> for FieldData { fields: &mut F, methods: &mut M, _: (), + parent_name: &str, ) where F: Extend, M: Extend, @@ -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) + .map(|name| Cow::Owned(name)) + .unwrap_or(name.to_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()) @@ -1641,6 +1652,7 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit { fields: &mut F, methods: &mut M, _: (), + parent_name: &str, ) where F: Extend, M: Extend, @@ -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. @@ -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, M: Extend, @@ -2002,6 +2016,7 @@ impl CodeGenerator for CompInfo { &mut fields, &mut methods, (), + &canonical_name, ); } // Check whether an explicit padding field is needed diff --git a/bindgen/lib.rs b/bindgen/lib.rs index e4b71b0496..459f5ec541 100644 --- a/bindgen/lib.rs +++ b/bindgen/lib.rs @@ -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 { + self.parse_callbacks + .last() + .and_then(|cb| cb.process_field_name(parent_name, name)) + } } fn deprecated_target_diagnostic(target: RustTarget, _options: &BindgenOptions) { From f988b27d4c055a69236dde2eef50a9a2ecafd55d Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Sat, 13 May 2023 20:50:39 +1200 Subject: [PATCH 2/3] Add doc comment --- bindgen/callbacks.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs index 21ac9af691..36e03e5ae8 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -136,7 +136,7 @@ pub trait ParseCallbacks: fmt::Debug { None } - /// . + /// Allows renaming the name of a field, replacing `_name`. fn process_field_name( &self, _parent_name: &str, From cdf53b2956ba6eaf213c52af48a2ccc24bb55708 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Sat, 13 May 2023 21:10:40 +1200 Subject: [PATCH 3/3] unused parent_name -> _parent_name --- bindgen/codegen/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index 2626ebbe5f..7c5fb5e516 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -1813,7 +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, + _parent_name: &str, ) where F: Extend, M: Extend,