From patchwork Thu Nov 10 16:41:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 18205 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp251328wru; Thu, 10 Nov 2022 08:43:41 -0800 (PST) X-Google-Smtp-Source: AMsMyM4eu/7EqW8SYsMtPxatIQnCKwJOb53ULiStlD52HHzt+xW2jGq0FfYh81FgsEHd8/ZLBYwp X-Received: by 2002:a50:fa96:0:b0:463:56ff:4d3a with SMTP id w22-20020a50fa96000000b0046356ff4d3amr55952485edr.345.1668098620931; Thu, 10 Nov 2022 08:43:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668098620; cv=none; d=google.com; s=arc-20160816; b=ilQ/v/P+1jtLbs9U7zC9++vTr+UkM103LUr8drlzJX5Oi/uXInEGJCbOdnk29TIZNk Y7FnZj55JqtfQbJcLlLHxdjY6aGaipTg9f2QFKaaFo5qFLs+mVtS5hEmR0T5W0IIgO3j ow6WofnBf2/8sYU49lJsocF7LrQ3+twen4j8otkqxX7+XhFkBl7heqOjL8qBgRy9dN23 jI6+o2Xb5b+0LbcSxS3VsbQOkZ+0S73lbGWr/IgVrguMP3I9F3BW8dX/VSGsvOU9H4kW 5GPc+OozGGDa5amMYGhFbApEwDhYlsagNi5fgx4a/hHjStVPzAaTX45PG3StuLKcUxBm Hj6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ZNPuuMc10L4dUu70U9sOvLzAYihy0C6hKT8Vm+e30y0=; b=YcF+5IbtuTTNMbIZauNTqKaDY5lYv9LS9B6RLoEJ+rtFBexGtL1VbL1dvHxor5RLwD +dGV6iks/CB3MQx8Z58zx9o2TsQKmkPPTqJcK7a8lVN72qwn1V1UEcUxWBSQm/WMV8KO XCos5A9+4GY/N6mmQVBbAf1NMVrogfhRagfLSRUJtUxsXAzya/Q8sHzdrhzBZhsWo64A 3HuYL238rE2F8qW8WFy6e+UFMPbpCPpJ178+nSQfvjw7D8S6GPWaiiSKTD7IArC8o5L+ BgUCbeLXAZYCw3jVk5BDzHyiOJUDqp1bPTF4eKPceiCdBw9q6c4y+MJudGGcJgMJnaSB oLaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BSw+HTvT; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s17-20020a056402521100b004593671898esi19063edd.611.2022.11.10.08.43.16; Thu, 10 Nov 2022 08:43:40 -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=@kernel.org header.s=k20201202 header.b=BSw+HTvT; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232127AbiKJQmp (ORCPT + 99 others); Thu, 10 Nov 2022 11:42:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232067AbiKJQmh (ORCPT ); Thu, 10 Nov 2022 11:42:37 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F1B743873; Thu, 10 Nov 2022 08:42:34 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id AF77D61BCB; Thu, 10 Nov 2022 16:42:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0E308C433C1; Thu, 10 Nov 2022 16:42:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668098553; bh=AMkgQPuKRPM8nukZjM2ZwIbmA4VXBv+Y/FxTSShnnN4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BSw+HTvTR1YTwG2L2/IDN14ICE8/eRnpXQ+wUaxgoRuAo4ky79Uzh1d9UoUkMGawo hCR1elrv3JDcen+1T/eG56Yx3t5rjjMaVFoZILt42d7jiU1umM5Nkqs540U2+FdcMp sIuRSVi2M5ahdmCmEX2qfXJhbrixMri2seWgjThtQyu+nZuMq6LfOotGIlJ6jvQzxQ O64fBYUlUrGO/35w9YvADIUoIc3Nz4S5UnH7UuphAzbcLq+AgqtLzcgSE9e3mWflyE mRf7b/MTzNouujFnULKfb2jG7VKPCv071P0Q4BgHT7DluNjmJYuRWp139XP8JGKdi6 o6O3MXqnfEmCw== From: Miguel Ojeda To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v1 05/28] rust: macros: add `concat_idents!` proc macro Date: Thu, 10 Nov 2022 17:41:17 +0100 Message-Id: <20221110164152.26136-6-ojeda@kernel.org> In-Reply-To: <20221110164152.26136-1-ojeda@kernel.org> References: <20221110164152.26136-1-ojeda@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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?1749128179042619607?= X-GMAIL-MSGID: =?utf-8?q?1749128179042619607?= From: Björn Roy Baron This macro provides similar functionality to the unstable feature `concat_idents` without having to rely on it. For instance: let x_1 = 42; let x_2 = concat_idents!(x, _1); assert!(x_1 == x_2); It has different behavior with respect to macro hygiene. Unlike the unstable `concat_idents!` macro, it allows, for example, referring to local variables by taking the span of the second macro as span for the output identifier. Signed-off-by: Björn Roy Baron [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda Reviewed-by: Finn Behrens --- rust/macros/concat_idents.rs | 23 +++++++++++++++++++ rust/macros/lib.rs | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 rust/macros/concat_idents.rs diff --git a/rust/macros/concat_idents.rs b/rust/macros/concat_idents.rs new file mode 100644 index 000000000000..3b5a9dd70e8a --- /dev/null +++ b/rust/macros/concat_idents.rs @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 + +use proc_macro::{token_stream, Ident, TokenStream, TokenTree}; + +use crate::helpers::expect_punct; + +fn expect_ident(it: &mut token_stream::IntoIter) -> Ident { + if let Some(TokenTree::Ident(ident)) = it.next() { + ident + } else { + panic!("Expected Ident") + } +} + +pub(crate) fn concat_idents(ts: TokenStream) -> TokenStream { + let mut it = ts.into_iter(); + let a = expect_ident(&mut it); + assert_eq!(expect_punct(&mut it), ','); + let b = expect_ident(&mut it); + assert!(it.next().is_none(), "only two idents can be concatenated"); + let res = Ident::new(&(a.to_string() + &b.to_string()), b.span()); + TokenStream::from_iter([TokenTree::Ident(res)]) +} diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs index 91764bfb1f89..15555e7ff487 100644 --- a/rust/macros/lib.rs +++ b/rust/macros/lib.rs @@ -2,6 +2,7 @@ //! Crate for all kernel procedural macros. +mod concat_idents; mod helpers; mod module; @@ -70,3 +71,46 @@ use proc_macro::TokenStream; pub fn module(ts: TokenStream) -> TokenStream { module::module(ts) } + +/// Concatenate two identifiers. +/// +/// This is useful in macros that need to declare or reference items with names +/// starting with a fixed prefix and ending in a user specified name. The resulting +/// identifier has the span of the second argument. +/// +/// # Examples +/// +/// ```ignore +/// use kernel::macro::concat_idents; +/// +/// macro_rules! pub_no_prefix { +/// ($prefix:ident, $($newname:ident),+) => { +/// $(pub(crate) const $newname: u32 = kernel::macros::concat_idents!($prefix, $newname);)+ +/// }; +/// } +/// +/// pub_no_prefix!( +/// binder_driver_return_protocol_, +/// BR_OK, +/// BR_ERROR, +/// BR_TRANSACTION, +/// BR_REPLY, +/// BR_DEAD_REPLY, +/// BR_TRANSACTION_COMPLETE, +/// BR_INCREFS, +/// BR_ACQUIRE, +/// BR_RELEASE, +/// BR_DECREFS, +/// BR_NOOP, +/// BR_SPAWN_LOOPER, +/// BR_DEAD_BINDER, +/// BR_CLEAR_DEATH_NOTIFICATION_DONE, +/// BR_FAILED_REPLY +/// ); +/// +/// assert_eq!(BR_OK, binder_driver_return_protocol_BR_OK); +/// ``` +#[proc_macro] +pub fn concat_idents(ts: TokenStream) -> TokenStream { + concat_idents::concat_idents(ts) +}