[PATCH V12 06/15] rust: macros: enable use of hyphens in module names

Viresh Kumar posted 15 patches 6 months, 4 weeks ago
[PATCH V12 06/15] rust: macros: enable use of hyphens in module names
Posted by Viresh Kumar 6 months, 4 weeks ago
From: Anisse Astier <anisse@astier.eu>

Some modules might need naming that contains hyphens "-" to match the
auto-probing by name in the platform devices that comes from the device
tree.

But rust identifiers cannot contain hyphens, so replace the module name
by an underscore anywhere we'd use it as an identifier.

Signed-off-by: Anisse Astier <anisse@astier.eu>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
[Viresh: Replace "-" with '-', and fix line length checkpatch warnings]
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 rust/macros/module.rs | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/rust/macros/module.rs b/rust/macros/module.rs
index a9418fbc9b44..27cc72d474f0 100644
--- a/rust/macros/module.rs
+++ b/rust/macros/module.rs
@@ -185,7 +185,9 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
 
     let info = ModuleInfo::parse(&mut it);
 
-    let mut modinfo = ModInfoBuilder::new(info.name.as_ref());
+    /* Rust does not allow hyphens in identifiers, use underscore instead */
+    let name_identifier = info.name.replace('-', "_");
+    let mut modinfo = ModInfoBuilder::new(name_identifier.as_ref());
     if let Some(author) = info.author {
         modinfo.emit("author", &author);
     }
@@ -310,14 +312,15 @@ mod __module_init {{
                     #[doc(hidden)]
                     #[link_section = \"{initcall_section}\"]
                     #[used]
-                    pub static __{name}_initcall: extern \"C\" fn() -> kernel::ffi::c_int = __{name}_init;
+                    pub static __{name_identifier}_initcall: extern \"C\" fn() ->
+                        kernel::ffi::c_int = __{name_identifier}_init;
 
                     #[cfg(not(MODULE))]
                     #[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
                     core::arch::global_asm!(
                         r#\".section \"{initcall_section}\", \"a\"
-                        __{name}_initcall:
-                            .long   __{name}_init - .
+                        __{name_identifier}_initcall:
+                            .long   __{name_identifier}_init - .
                             .previous
                         \"#
                     );
@@ -325,7 +328,7 @@ mod __module_init {{
                     #[cfg(not(MODULE))]
                     #[doc(hidden)]
                     #[no_mangle]
-                    pub extern \"C\" fn __{name}_init() -> kernel::ffi::c_int {{
+                    pub extern \"C\" fn __{name_identifier}_init() -> kernel::ffi::c_int {{
                         // SAFETY: This function is inaccessible to the outside due to the double
                         // module wrapping it. It is called exactly once by the C side via its
                         // placement above in the initcall section.
@@ -335,13 +338,13 @@ mod __module_init {{
                     #[cfg(not(MODULE))]
                     #[doc(hidden)]
                     #[no_mangle]
-                    pub extern \"C\" fn __{name}_exit() {{
+                    pub extern \"C\" fn __{name_identifier}_exit() {{
                         // SAFETY:
                         // - This function is inaccessible to the outside due to the double
                         //   module wrapping it. It is called exactly once by the C side via its
                         //   unique name,
-                        // - furthermore it is only called after `__{name}_init` has returned `0`
-                        //   (which delegates to `__init`).
+                        // - furthermore it is only called after `__{name_identifier}_init` has
+                        //   returned `0` (which delegates to `__init`).
                         unsafe {{ __exit() }}
                     }}
 
@@ -381,6 +384,7 @@ unsafe fn __exit() {{
         ",
         type_ = info.type_,
         name = info.name,
+        name_identifier = name_identifier,
         modinfo = modinfo.buffer,
         initcall_section = ".initcall6.init"
     )
-- 
2.31.1.272.g89b43f80a514
Re: [PATCH V12 06/15] rust: macros: enable use of hyphens in module names
Posted by Miguel Ojeda 6 months, 4 weeks ago
On Mon, May 19, 2025 at 9:08 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> +    /* Rust does not allow hyphens in identifiers, use underscore instead */

(In case you see this before you apply)

Nit: `//` for comments, also please end with a period.

Cheers,
Miguel
Re: [PATCH V12 06/15] rust: macros: enable use of hyphens in module names
Posted by Viresh Kumar 6 months, 4 weeks ago
On 19-05-25, 16:15, Miguel Ojeda wrote:
> On Mon, May 19, 2025 at 9:08 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
> >
> > +    /* Rust does not allow hyphens in identifiers, use underscore instead */
> 
> (In case you see this before you apply)
> 
> Nit: `//` for comments, also please end with a period.

Done.

-- 
viresh
Re: [PATCH V12 06/15] rust: macros: enable use of hyphens in module names
Posted by Anisse Astier 6 months, 4 weeks ago

Mar 20 mai 2025, à 06:33, Viresh Kumar a écrit :
> On 19-05-25, 16:15, Miguel Ojeda wrote:
>> On Mon, May 19, 2025 at 9:08 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>> >
>> > +    /* Rust does not allow hyphens in identifiers, use underscore instead */
>> 
>> (In case you see this before you apply)
>> 
>> Nit: `//` for comments, also please end with a period.
>
> Done.

Thank you Viresh for iterating on this and picking up review comments. Do not hesitate to add your Co-developed-by.

Kind regards,

Anisse
Re: [PATCH V12 06/15] rust: macros: enable use of hyphens in module names
Posted by Viresh Kumar 6 months, 4 weeks ago
On 20-05-25, 08:13, Anisse Astier wrote:
> Thank you Viresh for iterating on this and picking up review
> comments. Do not hesitate to add your Co-developed-by.

I haven't added Co-developed-by, but I did mention the changes I have
made in the cover-letter. This is all your work and thanks a lot for
this patch. I was struggling with this for quite some time and was
desperately looking for help :)

-- 
viresh
Re: [PATCH V12 06/15] rust: macros: enable use of hyphens in module names
Posted by Benno Lossin 6 months, 4 weeks ago
On Mon May 19, 2025 at 9:07 AM CEST, Viresh Kumar wrote:
> From: Anisse Astier <anisse@astier.eu>
>
> Some modules might need naming that contains hyphens "-" to match the
> auto-probing by name in the platform devices that comes from the device
> tree.
>
> But rust identifiers cannot contain hyphens, so replace the module name
> by an underscore anywhere we'd use it as an identifier.

I think this is supposed to read "But Rust identifier cannot contain
hyphens, so replace them with underscores.".

> Signed-off-by: Anisse Astier <anisse@astier.eu>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> [Viresh: Replace "-" with '-', and fix line length checkpatch warnings]
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> ---
>  rust/macros/module.rs | 20 ++++++++++++--------
>  1 file changed, 12 insertions(+), 8 deletions(-)

One nit below, with or without:

Reviewed-by: Benno Lossin <lossin@kernel.org>

> diff --git a/rust/macros/module.rs b/rust/macros/module.rs
> index a9418fbc9b44..27cc72d474f0 100644
> --- a/rust/macros/module.rs
> +++ b/rust/macros/module.rs
> @@ -185,7 +185,9 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
>  
>      let info = ModuleInfo::parse(&mut it);
>  
> -    let mut modinfo = ModInfoBuilder::new(info.name.as_ref());
> +    /* Rust does not allow hyphens in identifiers, use underscore instead */
> +    let name_identifier = info.name.replace('-', "_");

I think we could just name this variable `ident`.

---
Cheers,
Benno

> +    let mut modinfo = ModInfoBuilder::new(name_identifier.as_ref());
>      if let Some(author) = info.author {
>          modinfo.emit("author", &author);
>      }
Re: [PATCH V12 06/15] rust: macros: enable use of hyphens in module names
Posted by Viresh Kumar 6 months, 4 weeks ago
On 19-05-25, 10:00, Benno Lossin wrote:
> I think this is supposed to read "But Rust identifier cannot contain
> hyphens, so replace them with underscores.".
 
> I think we could just name this variable `ident`.

Fixed both. Thanks.

-- 
viresh