Skip to content

Commit a4e3b76

Browse files
anissevireshk
authored andcommitted
rust: macros: enable use of hyphens in module names
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 identifier cannot contain hyphens, so replace them with underscores. Signed-off-by: Anisse Astier <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> [ Viresh: Replace "-" with '-', minor commit log fix, rename variable and fix line length checkpatch warnings ] Reviewed-by: Benno Lossin <[email protected]> Signed-off-by: Viresh Kumar <[email protected]>
1 parent d01d702 commit a4e3b76

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

rust/macros/module.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
185185

186186
let info = ModuleInfo::parse(&mut it);
187187

188-
let mut modinfo = ModInfoBuilder::new(info.name.as_ref());
188+
// Rust does not allow hyphens in identifiers, use underscore instead.
189+
let ident = info.name.replace('-', "_");
190+
let mut modinfo = ModInfoBuilder::new(ident.as_ref());
189191
if let Some(author) = info.author {
190192
modinfo.emit("author", &author);
191193
}
@@ -310,22 +312,23 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
310312
#[doc(hidden)]
311313
#[link_section = \"{initcall_section}\"]
312314
#[used]
313-
pub static __{name}_initcall: extern \"C\" fn() -> kernel::ffi::c_int = __{name}_init;
315+
pub static __{ident}_initcall: extern \"C\" fn() ->
316+
kernel::ffi::c_int = __{ident}_init;
314317
315318
#[cfg(not(MODULE))]
316319
#[cfg(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)]
317320
core::arch::global_asm!(
318321
r#\".section \"{initcall_section}\", \"a\"
319-
__{name}_initcall:
320-
.long __{name}_init - .
322+
__{ident}_initcall:
323+
.long __{ident}_init - .
321324
.previous
322325
\"#
323326
);
324327
325328
#[cfg(not(MODULE))]
326329
#[doc(hidden)]
327330
#[no_mangle]
328-
pub extern \"C\" fn __{name}_init() -> kernel::ffi::c_int {{
331+
pub extern \"C\" fn __{ident}_init() -> kernel::ffi::c_int {{
329332
// SAFETY: This function is inaccessible to the outside due to the double
330333
// module wrapping it. It is called exactly once by the C side via its
331334
// placement above in the initcall section.
@@ -335,13 +338,13 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
335338
#[cfg(not(MODULE))]
336339
#[doc(hidden)]
337340
#[no_mangle]
338-
pub extern \"C\" fn __{name}_exit() {{
341+
pub extern \"C\" fn __{ident}_exit() {{
339342
// SAFETY:
340343
// - This function is inaccessible to the outside due to the double
341344
// module wrapping it. It is called exactly once by the C side via its
342345
// unique name,
343-
// - furthermore it is only called after `__{name}_init` has returned `0`
344-
// (which delegates to `__init`).
346+
// - furthermore it is only called after `__{ident}_init` has
347+
// returned `0` (which delegates to `__init`).
345348
unsafe {{ __exit() }}
346349
}}
347350
@@ -381,6 +384,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
381384
",
382385
type_ = info.type_,
383386
name = info.name,
387+
ident = ident,
384388
modinfo = modinfo.buffer,
385389
initcall_section = ".initcall6.init"
386390
)

0 commit comments

Comments
 (0)