Message ID | 20240108160746.177421-3-shamrocklee@posteo.net |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-19830-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:37c1:b0:101:2151:f287 with SMTP id y1csp1120866dyq; Mon, 8 Jan 2024 08:17:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IFOl8vrosjnLjaMApUcsRyVWXeR9OgABHebr+yJ1yCNBfQRawlsE7mvso+mD0iVgZPhuWB9 X-Received: by 2002:a17:906:3848:b0:a23:6075:52a2 with SMTP id w8-20020a170906384800b00a23607552a2mr1674533ejc.6.1704730656943; Mon, 08 Jan 2024 08:17:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704730656; cv=none; d=google.com; s=arc-20160816; b=zNRQCCsbGlA7CPS5PgL9GeToKJl2jg34GZ/F3XD98V8Ck1GZ5nDTNYkDIYqqNDkFku fR3LHUfkB8fsn+R95Khq9/iiMRWhfp7mh2xbgL62VE6L69QV39pmpVDNroh0ZfCnW37X dDy+oX14HTrfYIEoCPH/62/Gn+3tEQBzzwtBmDz+Ae3L7FQXFfA7OY+1pEsEk9yTEXH2 pDzBYmHdvXV5JUpL2sUkxDRsMw14HjC6M6p3H6/Ro0TEMAbacxWRJ7eSjGczD1qs0xWm 93dOEChPA02ZwviBuF15h9IwyqvOtJ1eLIxqgsy1tauwkSb8SUUWi6f2w6cIegU3+7Pa sOjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=dZlY3RW6BcZHp8d9+132Kbyt1q2aPeG62rCc3NCL/Ow=; fh=cUwWqstLDc7ccmCnrKeoozbphs53ssrLk+suBfNEIKQ=; b=IsXfkOvndRBypgRMMlkUz+pEEdZjRA+fo5x5Dlv5EzxVzBISNnndHTOZXPJu+TwTyU ZtUWqhqtDLW0dEoC+XRSjEnb5uxVg7OWOV+GGSB4V9PtK8EBWWQOZERRMXP1ZQc4kWE4 rY7OraP3nDcxWyI6RJvQbGMPExjPZEZtmTGtpRPhCb0osu4vm85BEZHI79aoC6Mbhui4 JlHmaML7xh7s2UtlMKfo1uPyK21ILRsCVj/c6QEJDV3IcKD6B+fjk0ZvXL6tQwcv7X9S W1JAWfiZVf2QeYhZHs0Kk0FaiPGh89O9YjNE011r5s2qrE8Ty9wj4fXOu+2pTOlsDQ1S 7IZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=HzQVztjN; spf=pass (google.com: domain of linux-kernel+bounces-19830-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-19830-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a2-20020a1709065f8200b00a28716e1a2asi28112eju.611.2024.01.08.08.17.36 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 08:17:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-19830-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=HzQVztjN; spf=pass (google.com: domain of linux-kernel+bounces-19830-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-19830-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 8616B1F22458 for <ouuuleilei@gmail.com>; Mon, 8 Jan 2024 16:17:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BC5DA537E2; Mon, 8 Jan 2024 16:16:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=posteo.net header.i=@posteo.net header.b="HzQVztjN" X-Original-To: linux-kernel@vger.kernel.org Received: from mout01.posteo.de (mout01.posteo.de [185.67.36.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 76E5B5380D for <linux-kernel@vger.kernel.org>; Mon, 8 Jan 2024 16:16:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=posteo.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=posteo.net Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id DBCD8240029 for <linux-kernel@vger.kernel.org>; Mon, 8 Jan 2024 17:16:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1704730590; bh=VhNI/VyHttpB6Dc+nOmQ0eI7dT/saNfHep5H24iJdO8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version: Content-Transfer-Encoding:From; b=HzQVztjNExNwWDrBuHzEvHtkFFb6vD0OXysV/RE2YSQAQeRZRM4R2KPi1eqBI1IE0 BSv2xCrBjDcgcfMHIjpUhv7F7AXvuX8XRuiUkCupg5ER15Ex18/IoSvHTPiCypErA9 870DCFFhsdxf6Ku/I5Bdn4tyn4/jWmgW0j+pNBTZ19BpBQFy9thMKIBdX+Xy1LGjKn ktiSoGhu0cJbFOuiyqcWbCl5Iv5BRsQl12Bl6+IAmoxmpSpOAEKunG3bM33lUuyF6b w7VL9S2/9IFb4670o/kT67shDy+0J9nn2ZRyR2cEJqPySYA83y8pVs3zf3EgzSV+PD zV6Uyu0OElw7A== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4T7zhV4n4Wz9rxN; Mon, 8 Jan 2024 17:16:26 +0100 (CET) From: Yueh-Shun Li <shamrocklee@posteo.net> To: Jonathan Corbet <corbet@lwn.net> Cc: Yueh-Shun Li <shamrocklee@posteo.net>, Hu Haowen <src.res.211@gmail.com>, Alex Shi <alexs@kernel.org>, Yanteng Si <siyanteng@loongson.cn>, Randy Dunlap <rdunlap@infradead.org>, workflows@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/4] coding-style: show how reusing macros prevents naming collisions Date: Mon, 8 Jan 2024 16:03:23 +0000 Message-ID: <20240108160746.177421-3-shamrocklee@posteo.net> In-Reply-To: <20240108160746.177421-1-shamrocklee@posteo.net> References: <107b6b5e-ca14-4b2b-ba2e-38ecd74c0ad3@infradead.org> <20240108160746.177421-1-shamrocklee@posteo.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787539653211679118 X-GMAIL-MSGID: 1787539653211679118 |
Series |
coding-style: recommend reusing macros from split headers instead of kernel.h
|
|
Commit Message
Yueh-Shun Li
Jan. 8, 2024, 4:03 p.m. UTC
In section "18) Don't re-invent the kernel macros" in "Linux kernel
coding style":
Show how reusing macros from shared headers prevents naming collisions
using "stringify", the one of the most widely reinvented macro, as an
example.
This patch aims to provide a stronger reason to reuse shared macros,
by showing the risk of improvised macro variants.
Signed-off-by: Yueh-Shun Li <shamrocklee@posteo.net>
---
Documentation/process/coding-style.rst | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
Comments
Yueh-Shun Li <shamrocklee@posteo.net> writes: > In section "18) Don't re-invent the kernel macros" in "Linux kernel > coding style": > > Show how reusing macros from shared headers prevents naming collisions > using "stringify", the one of the most widely reinvented macro, as an > example. > > This patch aims to provide a stronger reason to reuse shared macros, > by showing the risk of improvised macro variants. > > Signed-off-by: Yueh-Shun Li <shamrocklee@posteo.net> > --- > Documentation/process/coding-style.rst | 22 ++++++++++++++++++++++ > 1 file changed, 22 insertions(+) > > diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst > index 2504cb00a961..1e79aba4b346 100644 > --- a/Documentation/process/coding-style.rst > +++ b/Documentation/process/coding-style.rst > @@ -1070,6 +1070,28 @@ Similarly, if you need to calculate the size of some structure member, use > There are also ``min()`` and ``max()`` macros in ``include/linux/minmax.h`` > that do strict type checking if you need them. > > +Using existing macros provided by the shared headers also prevents naming > +collisions. For example, if one developer define in ``foo.h`` > + > +.. code-block:: c > + > + #define __stringify(x) __stringify_1(x) > + #define __stringify_1(x) #x > + > +and another define in ``bar.h`` > + > +.. code-block:: c > + > + #define stringify(x) __stringify(x) > + #define __stringify(x) #x > + > +When both headers are ``#include``-d into the same file, the facilities provided > +by ``foo.h`` might be broken by ``bar.h``. > + > +If both ``foo.h`` and ``bar.h`` use the macro ``__stringify()`` provided by > +``include/linux/stringify.h``, they wouldn't have stepped onto each other's > +toes. > + So everything we add to our documentation has a cost in terms of reader attention. We ask people to read through a lot of material now, and should only increase that ask for good reason. With that context, I have to wonder whether we really need to tell our readers, who are supposed to be capable developers, that reuse can help to avoid name collisions? Thanks, jon
Dear Mr. Corbet, Thank you very much for your feed back. On 2024-01-09 00:28, Jonathan Corbet wrote: > Yueh-Shun Li <shamrocklee@posteo.net> writes: > >> In section "18) Don't re-invent the kernel macros" in "Linux kernel >> coding style": >> >> Show how reusing macros from shared headers prevents naming collisions >> using "stringify", the one of the most widely reinvented macro, as an >> example. >> >> This patch aims to provide a stronger reason to reuse shared macros, >> by showing the risk of improvised macro variants. >> >> Signed-off-by: Yueh-Shun Li <shamrocklee@posteo.net> >> --- >> Documentation/process/coding-style.rst | 22 ++++++++++++++++++++++ >> 1 file changed, 22 insertions(+) >> >> diff --git a/Documentation/process/coding-style.rst >> b/Documentation/process/coding-style.rst >> index 2504cb00a961..1e79aba4b346 100644 >> --- a/Documentation/process/coding-style.rst >> +++ b/Documentation/process/coding-style.rst >> @@ -1070,6 +1070,28 @@ Similarly, if you need to calculate the size of >> some structure member, use >> There are also ``min()`` and ``max()`` macros in >> ``include/linux/minmax.h`` >> that do strict type checking if you need them. >> >> +Using existing macros provided by the shared headers also prevents >> naming >> +collisions. For example, if one developer define in ``foo.h`` >> + >> +.. code-block:: c >> + >> + #define __stringify(x) __stringify_1(x) >> + #define __stringify_1(x) #x >> + >> +and another define in ``bar.h`` >> + >> +.. code-block:: c >> + >> + #define stringify(x) __stringify(x) >> + #define __stringify(x) #x >> + >> +When both headers are ``#include``-d into the same file, the >> facilities provided >> +by ``foo.h`` might be broken by ``bar.h``. >> + >> +If both ``foo.h`` and ``bar.h`` use the macro ``__stringify()`` >> provided by >> +``include/linux/stringify.h``, they wouldn't have stepped onto each >> other's >> +toes. >> + > > So everything we add to our documentation has a cost in terms of reader > attention. We ask people to read through a lot of material now, and > should only increase that ask for good reason. > > With that context, I have to wonder whether we really need to tell our > readers, who are supposed to be capable developers, that reuse can help > to avoid name collisions? > The motivation comes from existing inconsistency of the "__stringify()" macro definition between e.g. "samples/bpf/tracex5.bpf.c" and other files. I agree that increasing the length of the documentation without substantial benefits would not be helpful for the readers, and doubling the length of a section is too much for its purpose. Should I shorten it into one sentence, like ``` On the other hand, locally-defined variants, such as ``#define __stringify(x) #x``, could lead to naming collisions that break otherwise functioning facilities. ``` or just omit it in the next version of patches? > Thanks, > > jon Thank you for your time and guidance. Shamrock
Yueh-Shun Li <shamrocklee@posteo.net> writes: >> So everything we add to our documentation has a cost in terms of reader >> attention. We ask people to read through a lot of material now, and >> should only increase that ask for good reason. >> >> With that context, I have to wonder whether we really need to tell our >> readers, who are supposed to be capable developers, that reuse can help >> to avoid name collisions? >> > > The motivation comes from existing inconsistency of the "__stringify()" > macro > definition between e.g. "samples/bpf/tracex5.bpf.c" and other files. > > I agree that increasing the length of the documentation without > substantial benefits would not be helpful for the readers, and > doubling the length of a section is too much for its purpose. > > Should I shorten it into one sentence, like > > ``` > On the other hand, locally-defined variants, such as ``#define > __stringify(x) #x``, > could lead to naming collisions that break otherwise functioning > facilities. > ``` > > or just omit it in the next version of patches? My own feeling (others may well disagree) is that this isn't worth mentioning in the coding-style document. What you *could* do is to fix the redefinitions (if that hasn't happened yet) and make sure that the macros in question are covered in our kernel documentation. Thanks, jon
diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst index 2504cb00a961..1e79aba4b346 100644 --- a/Documentation/process/coding-style.rst +++ b/Documentation/process/coding-style.rst @@ -1070,6 +1070,28 @@ Similarly, if you need to calculate the size of some structure member, use There are also ``min()`` and ``max()`` macros in ``include/linux/minmax.h`` that do strict type checking if you need them. +Using existing macros provided by the shared headers also prevents naming +collisions. For example, if one developer define in ``foo.h`` + +.. code-block:: c + + #define __stringify(x) __stringify_1(x) + #define __stringify_1(x) #x + +and another define in ``bar.h`` + +.. code-block:: c + + #define stringify(x) __stringify(x) + #define __stringify(x) #x + +When both headers are ``#include``-d into the same file, the facilities provided +by ``foo.h`` might be broken by ``bar.h``. + +If both ``foo.h`` and ``bar.h`` use the macro ``__stringify()`` provided by +``include/linux/stringify.h``, they wouldn't have stepped onto each other's +toes. + Feel free to search across and peruse the header files to see what else is already defined that you shouldn't reproduce in your code.