Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1917793rwb; Fri, 11 Nov 2022 02:17:26 -0800 (PST) X-Google-Smtp-Source: AA0mqf7iiKg+HJWeivxRrAv3ekn8wkNGP4Y66uAAGkJpT8jtv1xiYYuYW8NGrUjGT7GxKVfgzpUB X-Received: by 2002:a17:907:9505:b0:7ac:9917:72ed with SMTP id ew5-20020a170907950500b007ac991772edmr1237996ejc.650.1668161846692; Fri, 11 Nov 2022 02:17:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668161846; cv=none; d=google.com; s=arc-20160816; b=FmBFpgpniuLYG42ot4EEjAJSOQH2EuIg1aXrJIszgykak+mr9SIbQ7mY2vYtv1mPGr I8fPb9arRxnGibFUvLlE3qPfNuXTw6rXNV1QfJzLMEBCrQNT7PnlWxT6qmVArYoeYh8h VDKX8UMzXx0TH2Cm3jTgrCF/euhl1A0dUgjdgQtaf5jI4aTxMYLPnTA17ip09T0pnoKu OhyREzuX9aLEEa7Rzw2BC/g67Q0ovCelpf6wShJK6pMlaELArzMIDCaiF0/g4NX5e7an 8HNBhLTTcqTrFxA5Tihm1bvBevNbY/OQxC0yS5vAY08oO3qxemML40RLikP86nxyOsMl auhg== 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:dkim-signature :from; bh=IfxMhTyWpAJMoziSkCSvw30s8AApUw/YyM4ljR9ufhs=; b=EjMf64ApPUqaXI6B5Fn7uHKgIdyUmIHuC5Jlu192O/zzFvl7SSJCcIm29Jbfu6SpMo uWS9lyyqJMAC/De8wZRTr2Nlkc6dgdgHleQpWlTF+RIKtNl0FSv50/PavcYHxOfCQQLM YXgc28p0kdgHeHJfQcOOFjs9QujNHeS1Sxb4ZAJRGvFbd+1iReaCrsW2/w1USt3mOtCe P8+XNmbiJkaQBwN3Xemh0kiHu2zvj/53cq/E0MvzzQ89N3GdqPjLFsegtJIwL08P3wC8 nYBesjiKb/jnIQzHlIyf3iF1qNd8qhcEqAYsOWtVjOSVasvE6HAZC0SohhRQa2AB4S+t 3/og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kloenk.dev header.s=mail header.b="pF/x7DY3"; 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=REJECT sp=REJECT dis=NONE) header.from=kloenk.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u9-20020a50eac9000000b00458b42ff246si1895900edp.236.2022.11.11.02.17.04; Fri, 11 Nov 2022 02:17:26 -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=@kloenk.dev header.s=mail header.b="pF/x7DY3"; 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=REJECT sp=REJECT dis=NONE) header.from=kloenk.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233630AbiKKJdj (ORCPT + 92 others); Fri, 11 Nov 2022 04:33:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233493AbiKKJdh (ORCPT ); Fri, 11 Nov 2022 04:33:37 -0500 X-Greylist: delayed 461 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Fri, 11 Nov 2022 01:33:32 PST Received: from gimli.kloenk.dev (gimli.kloenk.dev [IPv6:2a0f:4ac0:0:1::cb2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA8831174; Fri, 11 Nov 2022 01:33:30 -0800 (PST) From: Finn Behrens DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kloenk.dev; s=mail; t=1668158747; bh=IfxMhTyWpAJMoziSkCSvw30s8AApUw/YyM4ljR9ufhs=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=pF/x7DY3tm82372JfEhfnwx+eXGThYB2AaKt9v/iuoHU1uSA7repQr+McvUbHOYjr EfjHJhUyZV+X65t1ZVqhlsmx9dV9IBZcnhkWgkT8/VSr6YZ+SOVlGMw1ksLFb2rxbX 2nQNc17pLSu8Uep/KsPClt1Cxc/KxHmJC0e+oOGo= To: Miguel Ojeda Cc: Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: Re: [PATCH v1 05/28] rust: macros: add `concat_idents!` proc macro Date: Fri, 11 Nov 2022 10:25:45 +0100 Message-ID: In-Reply-To: <20221110164152.26136-6-ojeda@kernel.org> References: <20221110164152.26136-1-ojeda@kernel.org> <20221110164152.26136-6-ojeda@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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 On 10 Nov 2022, at 17:41, Miguel Ojeda wrote: > From: Bj=C3=B6rn Roy Baron > > This macro provides similar functionality to the unstable feature > `concat_idents` without having to rely on it. > > For instance: > > let x_1 =3D 42; > let x_2 =3D concat_idents!(x, _1); > assert!(x_1 =3D=3D 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=C3=B6rn Roy Baron > [Reworded, adapted for upstream and applied latest changes] > Signed-off-by: Miguel Ojeda Reviewed-by: Finn Behrens Regards, Finn > --- > 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.r= s > 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)) =3D it.next() { > + ident > + } else { > + panic!("Expected Ident") > + } > +} > + > +pub(crate) fn concat_idents(ts: TokenStream) -> TokenStream { > + let mut it =3D ts.into_iter(); > + let a =3D expect_ident(&mut it); > + assert_eq!(expect_punct(&mut it), ','); > + let b =3D expect_ident(&mut it); > + assert!(it.next().is_none(), "only two idents can be concatenated"= ); > + let res =3D 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 w= ith 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),+) =3D> { > +/// $(pub(crate) const $newname: u32 =3D kernel::macros::conca= t_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) > +} > -- = > 2.38.1