lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <7cca1f5d-e214-481c-96a8-b841ad1d8fb9@t-8ch.de>
Date: Wed, 13 Aug 2025 22:59:37 +0200
From: Thomas Weißschuh <thomas@...ch.de>
To: Areej <areejhamid8560@...il.com>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org, 
	Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>, 
	Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>, 
	Björn Roy Baron <bjorn3_gh@...tonmail.com>, Benno Lossin <lossin@...nel.org>, 
	Andreas Hindborg <a.hindborg@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>, 
	Trevor Gross <tmgross@...ch.edu>, Danilo Krummrich <dakr@...nel.org>, 
	Greg Kroah-Hartman <gregkh@...uxfoundation.org>, Viresh Kumar <viresh.kumar@...aro.org>, 
	Tamir Duberstein <tamird@...il.com>, Xiangfei Ding <dingxiangfei2009@...il.com>
Subject: Re: [PATCH] rust: lib: add if_cfg! macro for conditional compilation

On 2025-08-14 01:38:26+0500, Areej wrote:
> Add a new if_cfg! macro to simplify conditional compilation using
> cfg attributes. This macro expands to paired #[cfg(cond)] and 
> #[cfg(not(cond))] blocks, allowing compile-time selection between 
> code branches in both expression and statement contexts.
> 
> Suggested-by: Benno Lossin <lossin@...nel.org>
> Link: https://github.com/Rust-for-Linux/linux/issues/1183
> Signed-off-by: Areej Hamid <areejhamid8560@...il.com>
> ---
>  rust/kernel/lib.rs | 37 +++++++++++++++++++++++++++++++++++++
>  1 file changed, 37 insertions(+)
> 
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index ed53169e795c..47e73949392d 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -294,6 +294,42 @@ macro_rules! asm {
>      };
>  }
>  
> +/// Conditionally compiles and executes code based on a `#[cfg]` condition.
> +///
> +/// Expands to `#[cfg(cond)] { ... }` and `#[cfg(not(cond))] { ... }`,
> +/// allowing conditional compilation in both expression and statement positions.
> +///
> +/// This macro is useful when both branches must be valid Rust code and the
> +/// selection between them is done at compile time via a config option.
> +/// # Examples
> +/// ```
> +/// # use kernel::if_cfg;
> +/// // Select a value depending on CONFIG_64BIT.
> +/// let x = if_cfg!(if CONFIG_64BIT {
> +///     64
> +/// } else {
> +///     32
> +/// });

Isn't this the same as cfg!()?

if cfg!(CONFIG_64BIT) {
	64
} else {
	32
}

https://doc.rust-lang.org/std/macro.cfg.html

> +///
> +/// // `x` will be 64 if CONFIG_64BIT is enabled, otherwise 32.
> +/// assert!(x == 64 || x == 32);
> +/// ```
> +#[macro_export]
> +macro_rules! if_cfg {
> +    (if $cond:tt { $($then:tt)* } else { $($else:tt)* }) => {{
> +        #[cfg($cond)]
> +        { $($then)* }
> +        #[cfg(not($cond))]
> +        { $($else)* }
> +    }};
> +    (if $cond:tt { $($then:tt)* }) => {{
> +        #[cfg($cond)]
> +        { $($then)* }
> +        #[cfg(not($cond))]
> +        { () }
> +    }};
> +}
> +
>  /// Gets the C string file name of a [`Location`].
>  ///
>  /// If `file_with_nul()` is not available, returns a string that warns about it.
> @@ -337,3 +373,4 @@ pub fn file_from_location<'a>(loc: &'a core::panic::Location<'a>) -> &'a core::f
>          c"<Location::file_with_nul() not supported>"
>      }
>  }
> +

Spurous whitespace change.

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ