Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp7521503rwl; Thu, 23 Mar 2023 05:37:24 -0700 (PDT) X-Google-Smtp-Source: AK7set+5x8cE3cWelq90i3vALRtomBOht7yQPdIdqOCn5+JMB3LlVCm2yheu0CaqEfCv1xONepgm X-Received: by 2002:a17:906:4f82:b0:931:8ad4:a586 with SMTP id o2-20020a1709064f8200b009318ad4a586mr10809935eju.30.1679575044547; Thu, 23 Mar 2023 05:37:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679575044; cv=none; d=google.com; s=arc-20160816; b=M5HnmlScAR0IVdI35C76zTd+4+Lh5WMddne8IFMoIXxNhmV3P6wmclhzZPswpyUzRD oxdTkMOBgNtX6ZnMSFa73WOlRQ++3xnH8jW/cijJ8xRwSvEGyKMxshNHXDF6F/qQ90Ed ifginVXCSBgkxW2diqCP3WYrguuWxgeFCns/seXez3fj6JDmWrrvyc8luswfYhx/vv/T 4Pt+nKlyBAzibI1BB+25uygUb9Ux1R1fV/9vRjXECPmdw8Eu4qqpeRcr/OkEsJb1DTn1 WC1FZ3fPyWTCFhZD8oTGwacWPak4Nk98Cn+P95FdSHE7F4tGwFIjpXtmh2dC6nzG5qNk cpQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:message-id:content-transfer-encoding :mime-version:subject:date:from:dkim-signature; bh=PaqGgNulIrjNwmRKHhElHzZGh1B5tC0uZiurOP/mQLU=; b=L/OJQQ1lQlOWb4IkWliqKExwAOdzkbfrqLxBUnYFdWofj8Ks13RyFdEkX/4cWckLiY xxdyTu2sX2u7SiIUVfzctq93vC7IUTQDKlPXjfTtjARkzVgHgqDXgEW+GtW6yVPTKGW2 QfkOd7ftCn8y4P5U4MAb2RsdgI4i0j4SpPZKkRa9daRNGjKXIgL7q+0EVXfpsK+2flxQ W/2rRWHwzO7r4fGfsrYqc7Skjnskv6Q6WGukPb2GNpWlIeNLWr+atUEpMdrqvqTqXlXq 7hTpkfOnxWiAkhjfuw/rgW1PUrjcakAgdfdA625Ib4UiD6C/jXg4KpQyLWzTfk11MAMR /wwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=VXelSGS2; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qt1-20020a170906ece100b008e97f723e53si15783581ejb.782.2023.03.23.05.36.28; Thu, 23 Mar 2023 05:37:24 -0700 (PDT) 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=VXelSGS2; 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 S231722AbjCWMfz (ORCPT + 99 others); Thu, 23 Mar 2023 08:35:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231627AbjCWMfy (ORCPT ); Thu, 23 Mar 2023 08:35:54 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 480E91C7EF; Thu, 23 Mar 2023 05:35:46 -0700 (PDT) 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 495EE420CF; Thu, 23 Mar 2023 12:35:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1679574944; bh=k+/jNhkIqz21sSLfPIEvNUhnHjFRyyWDKLpN6Jw1wxU=; h=From:Date:Subject:To:Cc; b=VXelSGS2IcIt3B+VHlZr9BxU5mUptTGoqcA6yRdv4+ynP69yhP4tnhBZPsOtpS2cf H+0XWFHrrU0U3mV8fxE5x6NspLo2zVit53oGLM5ij/8plajqQaOXab3PbQxP3E2cl4 NyzweZJVCTLbFURO58z+9uy84e/RdbtASBQ8fS5u2zClkF6FN28wjEaMDxn7b+GUZe hHTI0vDQzsGivABMCad2aDpl5S0+5rhEAaRM0TmxrZrSJfA6begDehIed19FcfKdSZ GakS9CwZSy7ZDpZpghMwB/mks9+LzW2GQW2xMb/1HOIm2c8d4aPH6yUT/n5c3ew5NI LCNZ8QV5w09Ag== From: Asahi Lina Date: Thu, 23 Mar 2023 21:35:10 +0900 Subject: [PATCH v2] 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-v2-1-7396e8b7018d@asahilina.net> X-B4-Tracking: v=1; b=H4sIAH1HHGQC/22Nyw6CMBBFf4V0bbUPgsrK/zAspjDQSbSQDhAN4 d9tWbs8N/fkbIIxErKoi01EXIlpDAnMqRCthzCgpC6xMMpYZUwp48KzfEMbR5aVtZ3rb1cFqEU yHDBKFyG0PjvDtFyOe95NPkwRe/ocuWeT2BPPY/we9VXn9X9o1VJJZ+89YFmhVuYBDJ5eFOAcc BbNvu8/O05zf8sAAAA= 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, Finn Behrens , Sumera Priyadarsini , Vincenzo Palazzo , Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1679574940; l=4848; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=k+/jNhkIqz21sSLfPIEvNUhnHjFRyyWDKLpN6Jw1wxU=; b=lgA5l4q9e7uUQnB7Ylry8BsisMHTO8M6xNJaAdgue98S+UwcbB6JqodsZB4SVNwB6A3JhYROn 1AoPjlEVO6FA6p3zP6kBTa5PiqljotC48ADXoTMQ5hmrHHcP8XX5LDY X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 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 Reviewed-by: Vincenzo Palazzo Signed-off-by: Asahi Lina --- Note: This used to be part of the series "rust: Miscellaneous macro improvements", but we agreed to drop the concat_idents!() change for now (and therefore the prerequisite it carried), so that leaves just the module macro change. --- 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 cf7ad950dc1e..b2bdd4d8c958 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, TokenTree}; +use proc_macro::{token_stream, Group, 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 a7e363c2b044..5e7ab83908a0 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. --- base-commit: fe15c26ee26efa11741a7b632e9f23b01aca4cc6 change-id: 20230224-rust-macros-633dbf870ae1 Thank you, ~~ Lina