From patchwork Fri Feb 24 07:25:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 61155 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp756379wrd; Thu, 23 Feb 2023 23:29:56 -0800 (PST) X-Google-Smtp-Source: AK7set/5N80cx+WFZxwlk5pCuAgPE7b0NW03lNEAjAzNDH23C4LbMrgCq3U61dqhl17UkVo0OoUT X-Received: by 2002:a17:907:3fa0:b0:8b1:2d7f:ed57 with SMTP id hr32-20020a1709073fa000b008b12d7fed57mr26826128ejc.42.1677223796296; Thu, 23 Feb 2023 23:29:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677223796; cv=none; d=google.com; s=arc-20160816; b=ARvM1FxwThYL2oUy9IY9qc2JikuwNlBL8b6R+VT8D9e//7pgMxaXJIDa/BxzmirwwP hSdJ/pojbay3NkgOQowId3h9d5BtpyhTgFOZz0XbvXPB3gPGCcnS1DjnqrvXR5XZA62X +5WpZtvYFewBUVeUgewlJIGAIEaumV5NkDF7j+6BRYHk1GzqJcKl3oY08ovpwIZ1UP4/ KGjrkqwvVaNUYf2hKhIVrPQZiFIv7GcHd3m91nJIfuospwN8UivY1dvvJRDVeB//TDdv XdIhBWuo01hofKSlZTzDSfIvNqV4eTxSc5bWRryRLCj9ffVvruryUGZZ0VanKz/zThhc M8/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=63p2gbjsOvxO45HePzifT4uBtNfXrYgoWVvMNk4r6oE=; b=NjIlJvX63P5AtlkMM/v4YfgoZEO+kt1XxluAp39WVpx0sw51ecCzFG9fDGDXu40E+G 3R+ajL7b+CXvayW9FvwYQdjFdl0x0i7KKyRVl38uZ1emUpgDwJ0hKkHZeTvJluSToZyO UQUwdV+xROnYdTeKwWh5Tr3h65r9YWm8U7GoLdlXday7LOYnaZiNdZr0EOmZKEcJHYme HzJcmy58ikflNzVLpItyssc8Gxx+kgRIy6gnGUvBFKndYj4qMtq34drp8BFF96I2bUXp etlOkmSa6Pmb1+n1WmG0ITuXhpEHIAt1cJ/3ONCoXWFZZd7QUiD5bqwRuuVoU+yv78mP gkrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=FlnTieWt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=asahilina.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id zz14-20020a170907350e00b008da033ef977si12162516ejb.731.2023.02.23.23.29.33; Thu, 23 Feb 2023 23:29:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=FlnTieWt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=asahilina.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229772AbjBXH0r (ORCPT + 99 others); Fri, 24 Feb 2023 02:26:47 -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 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= X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758696619533527473?= X-GMAIL-MSGID: =?utf-8?q?1758696619533527473?= 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 -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.