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: 7bit 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 --- 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 -use proc_macro::{token_stream, Punct, TokenTree}; +use proc_macro::{token_stream, Group, Punct, TokenTree}; pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option { if let Some(TokenTree::Ident(ident)) = it.next() { @@ -56,6 +56,14 @@ pub(crate) fn expect_string_ascii(it: &mut token_stream::IntoIter) -> String { string } +pub(crate) fn expect_group(it: &mut token_stream::IntoIter) -> Group { + if let TokenTree::Group(group) = it.next().expect("Reached end of token 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 use crate::helpers::*; -use proc_macro::{token_stream, Literal, TokenStream, TokenTree}; +use proc_macro::{token_stream, Delimiter, Literal, TokenStream, TokenTree}; use std::fmt::Write; +fn expect_string_array(it: &mut token_stream::IntoIter) -> Vec { + let group = expect_group(it); + assert_eq!(group.delimiter(), Delimiter::Bracket); + let mut values = Vec::new(); + let mut it = group.stream().into_iter(); + + while let Some(val) = try_string(&mut it) { + assert!(val.is_ascii(), "Expected ASCII string"); + values.push(val); + match it.next() { + Some(TokenTree::Punct(punct)) => assert_eq!(punct.as_char(), ','), + None => break, + _ => 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>, } impl ModuleInfo { @@ -112,7 +130,7 @@ impl ModuleInfo { "author" => info.author = Some(expect_string(it)), "description" => info.description = Some(expect_string(it)), "license" => info.license = expect_string_ascii(it), - "alias" => info.alias = Some(expect_string_ascii(it)), + "alias" => info.alias = Some(expect_string_array(it)), _ => 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) = info.alias { - modinfo.emit("alias", &alias); + if let Some(aliases) = info.alias { + for alias in aliases { + modinfo.emit("alias", &alias); + } } // Built-in modules also export the `file` modinfo string. -- 2.35.1