From patchwork Thu Mar 23 12:35:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 74066 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp2900204wrt; Thu, 23 Mar 2023 06:08:25 -0700 (PDT) X-Google-Smtp-Source: AK7set9N50aSZVI++GbdjqGW3HZ45e6j6h6R0ktOECo30n8EsN7+Mt0RBZbSQGIKFY/QKx/sT8Ep X-Received: by 2002:a05:6a20:7b15:b0:d9:167c:f612 with SMTP id s21-20020a056a207b1500b000d9167cf612mr3110516pzh.19.1679576904942; Thu, 23 Mar 2023 06:08:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679576904; cv=none; d=google.com; s=arc-20160816; b=C4sqarMsdCoZiw54uynGYS55q7WRd2/qDu19gXNaboUaGlvxDKIboKkOnHLdyLPOIw GSXlQxJTcHCLJb5vGX0X2lmiEJFVTSv7ENRoOA63vqTkh3EFdA/LIcDnWA7ZYPUEzLzB l/x+osYAMxrfWFYSsZCsoX2KvGRYwTpDkQdFfTn0U7UuNsjHTtB260v3bhj8WkLWfsf3 pHsgvodJ3kKrrGkOiUQ/bOCinM4xN8ucMYf1zZHmsLMV6Jh2l8UqJfC5HxqBHzXvi3ot //dhd5w7ZGRTRA7F2gCKl2LQXfbDUx7eugRkZ1PLXG1FHybmtuhtG86cLA31KK95AyRb NKfw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f8-20020a056a0022c800b006222f66d440si12493888pfj.37.2023.03.23.06.08.11; Thu, 23 Mar 2023 06:08: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 S231741AbjCWMf4 (ORCPT + 99 others); Thu, 23 Mar 2023 08:35:56 -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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761164032587153858?= X-GMAIL-MSGID: =?utf-8?q?1761164032587153858?= 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(-) --- base-commit: fe15c26ee26efa11741a7b632e9f23b01aca4cc6 change-id: 20230224-rust-macros-633dbf870ae1 Thank you, ~~ Lina 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.