From nobody Tue Sep 9 12:05:16 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F07DC64ED8 for ; Fri, 24 Feb 2023 07:26:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229746AbjBXH0i (ORCPT ); Fri, 24 Feb 2023 02:26:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229506AbjBXH0f (ORCPT ); Fri, 24 Feb 2023 02:26:35 -0500 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 722301EBDF; Thu, 23 Feb 2023 23:26:34 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: linasend@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id 6B55D424B9; Fri, 24 Feb 2023 07:26:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1677223593; bh=znEXimllrWJvfY/ohmIs0JciCK61gw0J+CRFrn2gudU=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=KJoHBpU9113mO+mdOiaJNn+0HklOkWluH51fJFbKl8NYFDEBkIICjJyoRhOLobouU QzH3np0L6dmJO+aRIahBLIEY3Iby8a9Hzx1zmLHjELLA9UPfIIAYhX+HS+Sty9fe12 EjEwDHNE2btQIyFRnNHPbOb89uShMQmcL8qVqLsTSJMr0JV9CRux2yS2EBram+jfRw AiFlXtJs3LjNKhuzNRk2X1Qbx6ddH4ZJ/wM1cRlUV5eRq1H9GHSnXEwQaiTU/9hVCL KQozTR6J4ZTU7R9qa3fDLZVf7JR28dXlz7XpAAG9//vGTtCJwiaQX1W8sbrDKdlaVG 7OIvQV1aXpFQg== From: Asahi Lina Date: Fri, 24 Feb 2023 16:25:55 +0900 Subject: [PATCH 1/3] rust: macros: Make expect_punct() return the Punct directly MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230224-rust-macros-v1-1-b39fae46e102@asahilina.net> References: <20230224-rust-macros-v1-0-b39fae46e102@asahilina.net> In-Reply-To: <20230224-rust-macros-v1-0-b39fae46e102@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1677223586; l=2668; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=znEXimllrWJvfY/ohmIs0JciCK61gw0J+CRFrn2gudU=; b=H/oQs1/Pt+6iQus1S0oDMkRIMRYO/sCD2ik5qRDSJzQ3aWOjUiTjg2D+qmrDeb1VTO+yDB6ZI XGJ6yp2aYR9Aik6WMEy5SEeA1CISZ8nNhb01BFVy76fEoq2IxnOFK4l X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This makes it mirror the way expect_ident() works, and means we can more easily push the result back into the token stream. Signed-off-by: Asahi Lina Reviewed-by: Finn Behrens Reviewed-by: Gary Guo Reviewed-by: Vincenzo Palazzo --- rust/macros/concat_idents.rs | 2 +- rust/macros/helpers.rs | 6 +++--- rust/macros/module.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rust/macros/concat_idents.rs b/rust/macros/concat_idents.rs index 7e4b450f3a50..6d955d65016e 100644 --- a/rust/macros/concat_idents.rs +++ b/rust/macros/concat_idents.rs @@ -15,7 +15,7 @@ fn expect_ident(it: &mut token_stream::IntoIter) -> Ident= { pub(crate) fn concat_idents(ts: TokenStream) -> TokenStream { let mut it =3D ts.into_iter(); let a =3D expect_ident(&mut it); - assert_eq!(expect_punct(&mut it), ','); + assert_eq!(expect_punct(&mut it).as_char(), ','); let b =3D expect_ident(&mut it); assert!(it.next().is_none(), "only two idents can be concatenated"); let res =3D Ident::new(&format!("{a}{b}"), b.span()); diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs index cf7ad950dc1e..65706ecc007e 100644 --- a/rust/macros/helpers.rs +++ b/rust/macros/helpers.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 =20 -use proc_macro::{token_stream, TokenTree}; +use proc_macro::{token_stream, Punct, TokenTree}; =20 pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option= { if let Some(TokenTree::Ident(ident)) =3D it.next() { @@ -38,9 +38,9 @@ pub(crate) fn expect_ident(it: &mut token_stream::IntoIte= r) -> String { try_ident(it).expect("Expected Ident") } =20 -pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> char { +pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> Punct { if let TokenTree::Punct(punct) =3D it.next().expect("Reached end of to= ken stream for Punct") { - punct.as_char() + punct } else { panic!("Expected Punct"); } diff --git a/rust/macros/module.rs b/rust/macros/module.rs index a7e363c2b044..07503b242d2d 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -104,7 +104,7 @@ impl ModuleInfo { ); } =20 - assert_eq!(expect_punct(it), ':'); + assert_eq!(expect_punct(it).as_char(), ':'); =20 match key.as_str() { "type" =3D> info.type_ =3D expect_ident(it), @@ -119,7 +119,7 @@ impl ModuleInfo { ), } =20 - assert_eq!(expect_punct(it), ','); + assert_eq!(expect_punct(it).as_char(), ','); =20 seen_keys.push(key); } --=20 2.35.1 From nobody Tue Sep 9 12:05:16 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96222C64ED8 for ; Fri, 24 Feb 2023 07:26:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229763AbjBXH0l (ORCPT ); Fri, 24 Feb 2023 02:26:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229739AbjBXH0i (ORCPT ); Fri, 24 Feb 2023 02:26:38 -0500 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84E725E878; Thu, 23 Feb 2023 23:26:37 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: linasend@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id 89D5942627; Fri, 24 Feb 2023 07:26:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1677223596; bh=i3HnPKPfmfqBLbRSmTJVD/vTe4oYMr0tLkjCkOowWxo=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=mHLIUVdMDHCfaQJTwTd4ksLkgryWXo4VLHMXJtg6uviwjS5EWxgNNWlrSg1ErQ326 DUT0sJbw9/KAETC9dAI+xVTcWc821z+l/0Itw4jPQ3EAsow+BvlpxX4MxmUVcSQ0hC Jbd7ERKwAlAsPZ4rat8nCUYk7vz9jsIcoBLbDaqux76T96/KICeEiRN09LcpJchb80 atp4F4FtXCN1yeQSEKB4n2bC5tzNweKbQ2BMq6uGQaqQgqHLKtJhqp1x0HPqSAS5ST mJvGrztDEwGUmPrtEXITf/7GoixVky08rA+te6CgFLgU3f3rvcWDAo1oSQkEzVDqoU yyF38IDwV0NxQ== From: Asahi Lina Date: Fri, 24 Feb 2023 16:25:56 +0900 Subject: [PATCH 2/3] rust: macros: concat_idents: Allow :: paths in the first argument MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230224-rust-macros-v1-2-b39fae46e102@asahilina.net> References: <20230224-rust-macros-v1-0-b39fae46e102@asahilina.net> In-Reply-To: <20230224-rust-macros-v1-0-b39fae46e102@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1677223586; l=1727; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=i3HnPKPfmfqBLbRSmTJVD/vTe4oYMr0tLkjCkOowWxo=; b=DPiJmYbiFviJCZNTLudvSrFUdKkvqk1hC9plJ20sOU8vPA9fwNu18df1XX8QF2ceaXXwpg+Y7 XS7veM7W473AkoL6twukc3WNjQq33aFILRpHVtIayz6H224BE84By1p X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This makes things like concat_idents!(bindings::foo, bar) work. Otherwise, there is no way to concatenate two idents and then use the result as part of a type path. Signed-off-by: Asahi Lina Reviewed-by: Vincenzo Palazzo --- rust/macros/concat_idents.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/rust/macros/concat_idents.rs b/rust/macros/concat_idents.rs index 6d955d65016e..d6614b900aa2 100644 --- a/rust/macros/concat_idents.rs +++ b/rust/macros/concat_idents.rs @@ -14,10 +14,28 @@ fn expect_ident(it: &mut token_stream::IntoIter) -> Ide= nt { =20 pub(crate) fn concat_idents(ts: TokenStream) -> TokenStream { let mut it =3D ts.into_iter(); - let a =3D expect_ident(&mut it); - assert_eq!(expect_punct(&mut it).as_char(), ','); + let mut out =3D TokenStream::new(); + let a =3D loop { + let ident =3D expect_ident(&mut it); + let punct =3D expect_punct(&mut it); + match punct.as_char() { + ',' =3D> break ident, + ':' =3D> { + let punct2 =3D expect_punct(&mut it); + assert_eq!(punct2.as_char(), ':'); + out.extend([ + TokenTree::Ident(ident), + TokenTree::Punct(punct), + TokenTree::Punct(punct2), + ]); + } + _ =3D> panic!("Expected , or ::"), + } + }; + let b =3D expect_ident(&mut it); assert!(it.next().is_none(), "only two idents can be concatenated"); let res =3D Ident::new(&format!("{a}{b}"), b.span()); - TokenStream::from_iter([TokenTree::Ident(res)]) + out.extend([TokenTree::Ident(res)]); + out } --=20 2.35.1 From nobody Tue Sep 9 12:05:16 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 388CAC61DA3 for ; Fri, 24 Feb 2023 07:26:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229777AbjBXH0s (ORCPT ); Fri, 24 Feb 2023 02:26:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229741AbjBXH0o (ORCPT ); Fri, 24 Feb 2023 02:26:44 -0500 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2ED0660D7D; Thu, 23 Feb 2023 23:26:41 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: linasend@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id A781942646; Fri, 24 Feb 2023 07:26:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1677223599; bh=LygvIXyiu/pWUo+M29D8IFw41H4slbdizLqU4lX4T2Y=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=FlnTieWtFwDB2Egb6fecWNzL1KasB+mBiHi/xqG3N+bstoon07OXnAe/PKlSvsDCp mn1rEI3PFmvkhf/IBBrKl2ravZ5Mk5SOMaq4a6QCdeHrQ4bj9WnbiQvWFzSg0zkpha EOTB+B7Oq2/Ktv9b6NWctXZS/dYXyqCRM9L9o39a78eJq+uM5uTpjBuW+WDhcnQBwX lU1Xtr8oIDeb7kdyAbcfasMBbm+rQFBdsJxqe/3UengcVe7MTd8IWz4xOTnCu9kZRq xIHykA4PxZV+2p4Bct85fqB/ziKrXq0s984Jwozmnp1fNVXNhGTYH3HSOdXvJXz9m9 5Gh5nBesGyfbA== From: Asahi Lina Date: Fri, 24 Feb 2023 16:25:57 +0900 Subject: [PATCH 3/3] rust: macros: Allow specifying multiple module aliases MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230224-rust-macros-v1-3-b39fae46e102@asahilina.net> References: <20230224-rust-macros-v1-0-b39fae46e102@asahilina.net> In-Reply-To: <20230224-rust-macros-v1-0-b39fae46e102@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina , Finn Behrens , Sumera Priyadarsini X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1677223586; l=4444; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=LygvIXyiu/pWUo+M29D8IFw41H4slbdizLqU4lX4T2Y=; b=Syyde+8G3IdOb1l5Hx23kaFhCkFCkne/rv2VEwIshnd/DIxBGVT1ObbDvME/Ed7EOoro55iXZ 2G92j/w/oASCzSebIE7DMXCXBRMBq46IiJPFgf1GYMxQSIh4hkT8fK7 X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Modules can (and usually do) have multiple alias tags, in order to specify multiple possible device matches for autoloading. Allow this by changing the alias ModuleInfo field to an Option>. Note: For normal device IDs this is autogenerated by modpost (which is not properly integrated with Rust support yet), so it is useful to be able to manually add device match aliases for now, and should still be useful in the future for corner cases that modpost does not handle. This pulls in the expect_group() helper from the rfl/rust branch (with credit to authors). Co-developed-by: Miguel Ojeda Signed-off-by: Miguel Ojeda Co-developed-by: Finn Behrens Signed-off-by: Finn Behrens Co-developed-by: Sumera Priyadarsini Signed-off-by: Sumera Priyadarsini Signed-off-by: Asahi Lina Reviewed-by: Vincenzo Palazzo --- rust/macros/helpers.rs | 10 +++++++++- rust/macros/module.rs | 30 +++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs index 65706ecc007e..15bf0c892421 100644 --- a/rust/macros/helpers.rs +++ b/rust/macros/helpers.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 =20 -use proc_macro::{token_stream, Punct, TokenTree}; +use proc_macro::{token_stream, Group, Punct, TokenTree}; =20 pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option= { if let Some(TokenTree::Ident(ident)) =3D it.next() { @@ -56,6 +56,14 @@ pub(crate) fn expect_string_ascii(it: &mut token_stream:= :IntoIter) -> String { string } =20 +pub(crate) fn expect_group(it: &mut token_stream::IntoIter) -> Group { + if let TokenTree::Group(group) =3D it.next().expect("Reached end of to= ken stream for Group") { + group + } else { + panic!("Expected Group"); + } +} + pub(crate) fn expect_end(it: &mut token_stream::IntoIter) { if it.next().is_some() { panic!("Expected end"); diff --git a/rust/macros/module.rs b/rust/macros/module.rs index 07503b242d2d..92cb35c235e1 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -1,9 +1,27 @@ // SPDX-License-Identifier: GPL-2.0 =20 use crate::helpers::*; -use proc_macro::{token_stream, Literal, TokenStream, TokenTree}; +use proc_macro::{token_stream, Delimiter, Literal, TokenStream, TokenTree}; use std::fmt::Write; =20 +fn expect_string_array(it: &mut token_stream::IntoIter) -> Vec { + let group =3D expect_group(it); + assert_eq!(group.delimiter(), Delimiter::Bracket); + let mut values =3D Vec::new(); + let mut it =3D group.stream().into_iter(); + + while let Some(val) =3D try_string(&mut it) { + assert!(val.is_ascii(), "Expected ASCII string"); + values.push(val); + match it.next() { + Some(TokenTree::Punct(punct)) =3D> assert_eq!(punct.as_char(),= ','), + None =3D> break, + _ =3D> panic!("Expected ',' or end of array"), + } + } + values +} + struct ModInfoBuilder<'a> { module: &'a str, counter: usize, @@ -78,7 +96,7 @@ struct ModuleInfo { name: String, author: Option, description: Option, - alias: Option, + alias: Option>, } =20 impl ModuleInfo { @@ -112,7 +130,7 @@ impl ModuleInfo { "author" =3D> info.author =3D Some(expect_string(it)), "description" =3D> info.description =3D Some(expect_string= (it)), "license" =3D> info.license =3D expect_string_ascii(it), - "alias" =3D> info.alias =3D Some(expect_string_ascii(it)), + "alias" =3D> info.alias =3D Some(expect_string_array(it)), _ =3D> panic!( "Unknown key \"{}\". Valid keys are: {:?}.", key, EXPECTED_KEYS @@ -163,8 +181,10 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream { modinfo.emit("description", &description); } modinfo.emit("license", &info.license); - if let Some(alias) =3D info.alias { - modinfo.emit("alias", &alias); + if let Some(aliases) =3D info.alias { + for alias in aliases { + modinfo.emit("alias", &alias); + } } =20 // Built-in modules also export the `file` modinfo string. --=20 2.35.1