diff mbox series

[v2] rust: macros: enable use of hyphens in module names

Message ID 20250122133952.501055-1-anisse@astier.eu
State Superseded
Headers show
Series [v2] rust: macros: enable use of hyphens in module names | expand

Commit Message

Anisse Astier Jan. 22, 2025, 1:39 p.m. UTC
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>
---
Hello,

Change since v1:
 - rebase on branch rfl/staging/dev

Sorry for sending a v2 so quickly, but v1 was based on the wrong branch :-/

Kind regards,

Anisse
---
 rust/macros/module.rs | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

Comments

Viresh Kumar Jan. 30, 2025, 4:58 a.m. UTC | #1
On 22-01-25, 14:39, Anisse Astier wrote:
> +    /* Rust does not allow hyphens in identifiers, use underscore instead */
> +    let name_identifier = info.name.replace("-", "_");

With CLIPPY=1 W=1, this gives:

warning: single-character string constant used as pattern
   --> /mnt/ssd/all/work/repos/kernel/linux/rust/macros/module.rs:186:45
    |
186 |     let name_identifier = info.name.replace("-", "_");
    |                                             ^^^ help: consider using a `char`: `'-'`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern
    = note: `-W clippy::single-char-pattern` implied by `-W clippy::all`
    = help: to override `-W clippy::all` add `#[allow(clippy::single_char_pattern)]`

warning: 1 warning emitted

This fixes it:

diff --git a/rust/macros/module.rs b/rust/macros/module.rs
index 1eff30d2ca6a..2e740bbdb598 100644
--- a/rust/macros/module.rs
+++ b/rust/macros/module.rs
@@ -183,7 +183,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
     let info = ModuleInfo::parse(&mut it);

     /* Rust does not allow hyphens in identifiers, use underscore instead */
-    let name_identifier = info.name.replace("-", "_");
+    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);


Will include it in my V8 now, unless you have any objections to it.
Thanks.
Anisse Astier Jan. 30, 2025, 6:08 a.m. UTC | #2
Jeu 30 janv 2025, à 05:58, Viresh Kumar a écrit :
> On 22-01-25, 14:39, Anisse Astier wrote:
>> +    /* Rust does not allow hyphens in identifiers, use underscore instead */
>> +    let name_identifier = info.name.replace("-", "_");
>
> With CLIPPY=1 W=1, this gives:
>
> warning: single-character string constant used as pattern
>    --> /mnt/ssd/all/work/repos/kernel/linux/rust/macros/module.rs:186:45
>     |
> 186 |     let name_identifier = info.name.replace("-", "_");
>     |                                             ^^^ help: consider 
> using a `char`: `'-'`
>     |
>     = help: for further information visit 
> https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern
>     = note: `-W clippy::single-char-pattern` implied by `-W clippy::all`
>     = help: to override `-W clippy::all` add 
> `#[allow(clippy::single_char_pattern)]`
>
> warning: 1 warning emitted
>
> This fixes it:
>
> diff --git a/rust/macros/module.rs b/rust/macros/module.rs
> index 1eff30d2ca6a..2e740bbdb598 100644
> --- a/rust/macros/module.rs
> +++ b/rust/macros/module.rs
> @@ -183,7 +183,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
>      let info = ModuleInfo::parse(&mut it);
>
>      /* Rust does not allow hyphens in identifiers, use underscore instead */
> -    let name_identifier = info.name.replace("-", "_");
> +    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);
>
>
> Will include it in my V8 now, unless you have any objections to it.

No objections and nice catch!

Regards,

Anisse
Alice Ryhl Jan. 30, 2025, 11:51 a.m. UTC | #3
On Wed, Jan 22, 2025 at 2:39 PM Anisse Astier <anisse@astier.eu> wrote:
>
> 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>
diff mbox series

Patch

diff --git a/rust/macros/module.rs b/rust/macros/module.rs
index cdf94f4982df..1eff30d2ca6a 100644
--- a/rust/macros/module.rs
+++ b/rust/macros/module.rs
@@ -182,7 +182,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);
     }
@@ -298,14 +300,14 @@  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
                         \"#
                     );
@@ -313,7 +315,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.
@@ -323,12 +325,12 @@  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`
+                        // - furthermore it is only called after `__{name_identifier}_init` has returned `0`
                         //   (which delegates to `__init`).
                         unsafe {{ __exit() }}
                     }}
@@ -369,6 +371,7 @@  unsafe fn __exit() {{
         ",
         type_ = info.type_,
         name = info.name,
+        name_identifier = name_identifier,
         modinfo = modinfo.buffer,
         initcall_section = ".initcall6.init"
     )