rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: introduce sfile macro for easier code tracing
@ 2025-05-29 18:45 Timur Tabi
  2025-05-29 20:14 ` Benno Lossin
                   ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: Timur Tabi @ 2025-05-29 18:45 UTC (permalink / raw)
  To: Miguel Ojeda, Danilo Krummrich, rust-for-linux, Alice Ryhl

Introduce the sfile (short file) macro that returns the stem of
the current source file filename.

Rust provides a file!() macro that is similar to C's __FILE__ predefined
macro.  Unlike __FILE__, however, file!() returns a full path, which is
klunky when used for debug traces such as

	pr_info!("{}:{}\n", file!(), line!());

sfile!() can be used in situations instead, to provide a more compact
print.  For example, if file!() returns "rust/kernel/print.rs", sfile!()
returns just "print".

The macro goes avoids str::rfind() because currently, that function is not
const.  The compiler emits a call to memrchr, even when called on string
literals.  Instead, the macro implements its own versions of rfind(),
allowing the compiler to generate the slice at compile time.

Unfortunately, Rust does not consider the .. operator to be const either,
so sfind!() cannot be assigned to consts.  For example, the following will
not compile:

	const SFILE: &'static str = sfile!();

Signed-off-by: Timur Tabi <ttabi@nvidia.com>
---
 rust/kernel/print.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs
index cf4714242e14..3c7b0c74bfb9 100644
--- a/rust/kernel/print.rs
+++ b/rust/kernel/print.rs
@@ -414,3 +414,51 @@ macro_rules! pr_cont (
         $crate::print_macro!($crate::print::format_strings::CONT, true, $($arg)*)
     )
 );
+
+/// Returns just the base filename of the current file.
+/// file!() returns the full path of the current file, which is often too long.
+/// Use this macro to trace your code:
+/// pr_err!("{}:{}\n", sfile!(), line!());
+/// Note: Avoiding rfind() allows this macro to be evaluated at compile time
+/// in most situations, such as the above pr_err!() example.  However,
+/// because .. is apparently a non-const operator, the following will not work:
+///     const SFILE: &'static str = sfile!();
+#[macro_export]
+macro_rules! sfile {
+    () => {{
+        const FILE: &str = file!();
+
+        /// Return the index of the last occurrence of @needle in @haystack,
+        /// or zero if not found.  We can't use rfind() because it's not const (yet).
+        const fn find_last_or_zero(haystack: &str, needle: char) -> usize {
+            let bytes = haystack.as_bytes();
+            let mut i = haystack.len();
+            while i > 0 {
+                i -= 1;
+                if bytes[i] == needle as u8 {
+                    return i;
+                }
+            }
+            0
+        }
+
+        /// Return the index of the last occurrence of @needle in @haystack,
+        /// or the length of the string if not found.
+        const fn find_last_or_len(haystack: &str, needle: char) -> usize {
+            let len = haystack.len();
+            let bytes = haystack.as_bytes();
+            let mut i = len;
+            while i > 0 {
+                i -= 1;
+                if bytes[i] == needle as u8 {
+                    return i;
+                }
+            }
+            len
+        }
+
+        let start = find_last_or_zero(FILE, '/') + 1;
+        let len = find_last_or_len(&FILE[start..], '.');
+        &FILE[start..start+len]
+    }};
+}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2025-06-06 15:50 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-29 18:45 [PATCH] rust: introduce sfile macro for easier code tracing Timur Tabi
2025-05-29 20:14 ` Benno Lossin
2025-06-03 17:15   ` Timur Tabi
2025-06-03 21:54     ` Benno Lossin
2025-05-29 20:21 ` Miguel Ojeda
2025-06-03 18:15   ` Timur Tabi
2025-06-03 21:58     ` Benno Lossin
2025-06-03 22:05       ` Timur Tabi
2025-06-03 22:15         ` Miguel Ojeda
2025-06-04 23:12           ` Timur Tabi
2025-06-05  3:20             ` Miguel Ojeda
2025-06-03 22:15     ` Miguel Ojeda
2025-06-03 23:29       ` Timur Tabi
2025-06-04 10:28         ` Miguel Ojeda
2025-06-04 15:16           ` Benno Lossin
2025-06-04 15:41             ` Miguel Ojeda
2025-06-05  6:05             ` Greg KH
2025-06-04 20:38           ` Timur Tabi
2025-06-05  6:07             ` Greg KH
2025-06-05 15:02               ` Timur Tabi
2025-06-05 15:21                 ` gregkh
2025-06-05 15:38                   ` Miguel Ojeda
2025-06-05 16:42                     ` gregkh
2025-06-05 17:39                       ` Miguel Ojeda
2025-06-06 15:50                 ` Miguel Ojeda
2025-05-30  3:47 ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).