Message ID | 20231010125832.1002941-1-max.kellermann@ionos.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp176564vqb; Tue, 10 Oct 2023 05:59:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFoWwBTVu54fZwDGsA3+tiOk34GFpv7skYfuGwCfmcUr84KnKTNBShr7x3vszPIaLc4WXNH X-Received: by 2002:a17:902:bf49:b0:1c6:112f:5d02 with SMTP id u9-20020a170902bf4900b001c6112f5d02mr14077877pls.55.1696942745225; Tue, 10 Oct 2023 05:59:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696942745; cv=none; d=google.com; s=arc-20160816; b=G+vefU/koSLrMvUZdz9FSZ7UF0Vbbnn74znBhDb/0PA1SJjOaaUbt/9sHpr1cvD0TV tTte28miE0HIS64qxtviIEZ8CpTRn7efNAm/hyffcxEv0/tesBQ66lCfgz3MJr9OfX2q ku8eyB6gsPXvSC6NXNxIPp/fEtjPBN+U3euH47TutzNEFtpdywf4FXvskTTUH9kc0sL7 W10o9K7Dwk3xq5DBISh7IPp74NIIYyGr3QuGx7Ia96F18smGxq/K9LIClYN6gphV51iD dmSzqspFetIFaP0TGzGqznNC5BdmNd6o9c+EI8OM/lpBL/vRrCWgvwEUv5mnl28kxXR/ C33g== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=3SI4JY3DNx4EgaWZfWrgnEZk2q3gFyXvOUj+dz0SgUs=; fh=8j7DRFNQaTJb/GGblcfn9pjHA7vm9ryk/rOdKLzkBpg=; b=V1IN51M5VLAQAt4JNOK7JmkW/HfzP2wjRqbPzkxe6O98CW5YLw9nkZU4/RraFeEmkv cezt0YD6MgbmjM8XxDZ4Wr83iVsRS78F22pSekZl4xID0XSv9IrjpwhcCTrG/6zQwaqz q3KZnkRqxUwdPS6TCyedwdHW+HUGAMAyT15q5lckKLnwRB5IKUG4XRUheWReDA3b8S1P xGnWHQr9Ycv0Vx6FRcWAVyURORwSLHBAZBlev0T8jLk0N44MFib/nYQx1rROOxd9qOV3 3TjBVvuueuoApoMjHlDWOojN8FLlBxOHCsI2oGZzt6jEguaXgMn+XphC91DY/2CueooL 1QSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ionos.com header.s=google header.b=PQJICYGI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=ionos.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id l10-20020a170903244a00b001c58401354dsi7045093pls.565.2023.10.10.05.59.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 05:59:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@ionos.com header.s=google header.b=PQJICYGI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=ionos.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 2D048826ED32; Tue, 10 Oct 2023 05:59:03 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231978AbjJJM6k (ORCPT <rfc822;rua109.linux@gmail.com> + 20 others); Tue, 10 Oct 2023 08:58:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231400AbjJJM6i (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 10 Oct 2023 08:58:38 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DE80AC for <linux-kernel@vger.kernel.org>; Tue, 10 Oct 2023 05:58:37 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-406618d0992so54519175e9.0 for <linux-kernel@vger.kernel.org>; Tue, 10 Oct 2023 05:58:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ionos.com; s=google; t=1696942715; x=1697547515; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=3SI4JY3DNx4EgaWZfWrgnEZk2q3gFyXvOUj+dz0SgUs=; b=PQJICYGIq9Pd7O9fr3EXpYt4HFwpC+C9TYO8qiQPfK394B0yYdGc5jLl5iX0Y3HaUb RnIvd1bQ5BWMv45y8VIMUZUTKVIqLMSGwENfXOc56s5ozu5ch5H1rDcQntmqO+Ss0ubL +W7sBbItWaQaSF/GqyDhrEz6l0OGqr8j14Dskr9iYlRHD3wwwsvQH9g2qMILCCs/VGsJ TEPl7WQjXqBT1VVkuFQ9SPCRBzl8oasRvD28pxtvD2XpeHAWJi3TfXePaAkNkz9z+4rL g+u3HyODnttp4Y8gtU3buXY922qAVRf9obBjx/Rh8UhCa5j2GgERJdMwTMWvM/pSxgLk GYBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696942715; x=1697547515; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3SI4JY3DNx4EgaWZfWrgnEZk2q3gFyXvOUj+dz0SgUs=; b=g04S2ueCcRP+gWXqvxsXyget2r9pll+AEcMuHcQGJmfFNBt5ut+U75zWa1S8p+4qEn 0PWNwshCfptlyfMAcsTVoIDvCmkMBIPaIYuhZL9W/UWccolCzoumlSt0bI7UGUVVz91N svsXxprHOChOYRIE/EcE0tk6d8i6cyhF9UTEh46ZTZKaIz2qpldXaTGYZ1vqTKHNhER2 BBLlQz3KL8rjrevchw+xdWCVSVdmWpFtWiR5U1TwqfC6ZLuKIh57kgIwPSMB4uFcR0vz n6kiI4TX+Hlr59lAZon2IMs6r/jVK0CIa+ykgYaTUt01H98nX6LH3bqf5UTV2aQUHeFx rbNw== X-Gm-Message-State: AOJu0Yxnk6vQw8VS/UDTdHav7RoWoRm/TviNobkthiz6QeRt5eQPmeUe o8mAnxZ5e70Hm+KRv2vs4tToiSPh0bA9CDZNc/5rCQ== X-Received: by 2002:a05:6000:49:b0:32d:5cc0:2f0c with SMTP id k9-20020a056000004900b0032d5cc02f0cmr1091800wrx.40.1696942715517; Tue, 10 Oct 2023 05:58:35 -0700 (PDT) Received: from heron.intern.cm-ag (p200300dc6f49a600529a4cfffe3dd983.dip0.t-ipconnect.de. [2003:dc:6f49:a600:529a:4cff:fe3d:d983]) by smtp.gmail.com with ESMTPSA id z8-20020a05600c220800b003fee8793911sm14034296wml.44.2023.10.10.05.58.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Oct 2023 05:58:34 -0700 (PDT) From: Max Kellermann <max.kellermann@ionos.com> To: linux@roeck-us.net, joe@perches.com, gregkh@linuxfoundation.org, Jonathan Corbet <corbet@lwn.net> Cc: Max Kellermann <max.kellermann@ionos.com>, workflows@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] Documentation/process/coding-style.rst: space around const Date: Tue, 10 Oct 2023 14:58:31 +0200 Message-Id: <20231010125832.1002941-1-max.kellermann@ionos.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=2.7 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 10 Oct 2023 05:59:03 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779373436306386124 X-GMAIL-MSGID: 1779373436306386124 |
Series |
[v3] Documentation/process/coding-style.rst: space around const
|
|
Commit Message
Max Kellermann
Oct. 10, 2023, 12:58 p.m. UTC
There are currently no rules on the placement of "const", but a recent
code submission revealed that there is clearly a preference for spaces
around it.
checkpatch.pl has no check at all for this; though it does sometimes
complain, but only because it erroneously thinks that the "*" (on
local variables) is an unary dereference operator, not a pointer type.
Current coding style for const pointers-to-pointers:
"*const*": 2 occurrences
"* const*": 3
"*const *": 182
"* const *": 681
Just const pointers:
"*const": 2833 occurrences
"* const": 16615
Link: https://lore.kernel.org/r/264fa39d-aed6-4a54-a085-107997078f8d@roeck-us.net/
Link: https://lore.kernel.org/r/f511170fe61d7e7214a3a062661cf4103980dad6.camel@perches.com/
Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
---
V1 -> V2: removed "volatile" on gregkh's request.
V2 -> V3: moved patch changelog below the "---" line
---
Documentation/process/coding-style.rst | 11 +++++++++++
1 file changed, 11 insertions(+)
Comments
On Tue, Oct 10, 2023 at 02:58:31PM +0200, Max Kellermann wrote: > There are currently no rules on the placement of "const", but a recent > code submission revealed that there is clearly a preference for spaces > around it. > > checkpatch.pl has no check at all for this; though it does sometimes > complain, but only because it erroneously thinks that the "*" (on > local variables) is an unary dereference operator, not a pointer type. > > Current coding style for const pointers-to-pointers: > > "*const*": 2 occurrences > "* const*": 3 > "*const *": 182 > "* const *": 681 > > Just const pointers: > > "*const": 2833 occurrences > "* const": 16615 > > Link: https://lore.kernel.org/r/264fa39d-aed6-4a54-a085-107997078f8d@roeck-us.net/ > Link: https://lore.kernel.org/r/f511170fe61d7e7214a3a062661cf4103980dad6.camel@perches.com/ > Signed-off-by: Max Kellermann <max.kellermann@ionos.com> > --- > V1 -> V2: removed "volatile" on gregkh's request. > V2 -> V3: moved patch changelog below the "---" line > --- > Documentation/process/coding-style.rst | 11 +++++++++++ > 1 file changed, 11 insertions(+) > > diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst > index 6db37a46d305..71d62d81e506 100644 > --- a/Documentation/process/coding-style.rst > +++ b/Documentation/process/coding-style.rst > @@ -271,6 +271,17 @@ adjacent to the type name. Examples: > unsigned long long memparse(char *ptr, char **retptr); > char *match_strdup(substring_t *s); > > +Use space around the ``const`` keyword (except when adjacent to > +parentheses). Example: > + > +.. code-block:: c > + > + const void *a; > + void * const b; > + void ** const c; > + void * const * const d; > + int strcmp(const char *a, const char *b); > + > Use one space around (on each side of) most binary and ternary operators, > such as any of these:: > > -- > 2.39.2 > Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
On Tue, Oct 10, 2023 at 02:58:31PM +0200, Max Kellermann wrote: > There are currently no rules on the placement of "const", but a recent > code submission revealed that there is clearly a preference for spaces > around it. > > checkpatch.pl has no check at all for this; though it does sometimes > complain, but only because it erroneously thinks that the "*" (on > local variables) is an unary dereference operator, not a pointer type. > > Current coding style for const pointers-to-pointers: > > "*const*": 2 occurrences > "* const*": 3 > "*const *": 182 > "* const *": 681 > > Just const pointers: > > "*const": 2833 occurrences > "* const": 16615 > > Link: https://lore.kernel.org/r/264fa39d-aed6-4a54-a085-107997078f8d@roeck-us.net/ > Link: https://lore.kernel.org/r/f511170fe61d7e7214a3a062661cf4103980dad6.camel@perches.com/ > Signed-off-by: Max Kellermann <max.kellermann@ionos.com> Reviewed-by: Guenter Roeck <linux@roeck-us.net> > --- > V1 -> V2: removed "volatile" on gregkh's request. > V2 -> V3: moved patch changelog below the "---" line > --- > Documentation/process/coding-style.rst | 11 +++++++++++ > 1 file changed, 11 insertions(+) > > diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst > index 6db37a46d305..71d62d81e506 100644 > --- a/Documentation/process/coding-style.rst > +++ b/Documentation/process/coding-style.rst > @@ -271,6 +271,17 @@ adjacent to the type name. Examples: > unsigned long long memparse(char *ptr, char **retptr); > char *match_strdup(substring_t *s); > > +Use space around the ``const`` keyword (except when adjacent to > +parentheses). Example: > + > +.. code-block:: c > + > + const void *a; > + void * const b; > + void ** const c; > + void * const * const d; > + int strcmp(const char *a, const char *b); > + > Use one space around (on each side of) most binary and ternary operators, > such as any of these:: > > -- > 2.39.2 >
On Tue, 2023-10-10 at 14:58 +0200, Max Kellermann wrote: > There are currently no rules on the placement of "const", but a recent > code submission revealed that there is clearly a preference for spaces > around it. > > checkpatch.pl has no check at all for this; though it does sometimes > complain, but only because it erroneously thinks that the "*" (on > local variables) is an unary dereference operator, not a pointer type. > Maybe something like this for checkpatch: --- scripts/checkpatch.pl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 25fdb7fda1128..48d70d0ad9a2b 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -4726,6 +4726,16 @@ sub process { } } +# check for const* and *const uses that should have space around const + if ($line =~ /(?:const\*|\*const)/) { + if (WARN("CONST_POINTER", + "const pointers should have spaces around const\n" . $herecurr) && + $fix) { + $fixed[$fixlinenr] =~ s/\*const\b/* const/g; + $fixed[$fixlinenr] =~ s/\bconst\*/const */g; + } + } + # check for non-global char *foo[] = {"bar", ...} declarations. if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { WARN("STATIC_CONST_CHAR_ARRAY",
Max Kellermann wrote: > There are currently no rules on the placement of "const", but a recent > code submission revealed that there is clearly a preference for spaces > around it. > > checkpatch.pl has no check at all for this; though it does sometimes > complain, but only because it erroneously thinks that the "*" (on > local variables) is an unary dereference operator, not a pointer type. > > Current coding style for const pointers-to-pointers: > > "*const*": 2 occurrences > "* const*": 3 > "*const *": 182 > "* const *": 681 > > Just const pointers: > > "*const": 2833 occurrences > "* const": 16615 > > Link: https://lore.kernel.org/r/264fa39d-aed6-4a54-a085-107997078f8d@roeck-us.net/ > Link: https://lore.kernel.org/r/f511170fe61d7e7214a3a062661cf4103980dad6.camel@perches.com/ > Signed-off-by: Max Kellermann <max.kellermann@ionos.com> > --- > V1 -> V2: removed "volatile" on gregkh's request. > V2 -> V3: moved patch changelog below the "---" line > --- > Documentation/process/coding-style.rst | 11 +++++++++++ > 1 file changed, 11 insertions(+) > > diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst > index 6db37a46d305..71d62d81e506 100644 > --- a/Documentation/process/coding-style.rst > +++ b/Documentation/process/coding-style.rst > @@ -271,6 +271,17 @@ adjacent to the type name. Examples: > unsigned long long memparse(char *ptr, char **retptr); > char *match_strdup(substring_t *s); > > +Use space around the ``const`` keyword (except when adjacent to > +parentheses). Example: > + > +.. code-block:: c > + > + const void *a; > + void * const b; > + void ** const c; > + void * const * const d; > + int strcmp(const char *a, const char *b); > + > Use one space around (on each side of) most binary and ternary operators, > such as any of these:: I notice that clang-format reflows that example to: const void *a; void *const b; void **const c; void *const *const d; int strcmp(const char *a, const char *b); ...but someone more clang-format savvy than me would need to propose the changes to the kernel's .clang-format template to match the style suggestion.
On Wed, 11 Oct 2023 14:44:17 -0700, Dan Williams wrote: > > I notice that clang-format reflows that example to: > > const void *a; > void *const b; > void **const c; > void *const *const d; > int strcmp(const char *a, const char *b); > > ...but someone more clang-format savvy than me would need to propose the > changes to the kernel's .clang-format template to match the style > suggestion. I think we could use: diff --git a/.clang-format b/.clang-format index 0bbb1991defe..9eeb511c0814 100644 --- a/.clang-format +++ b/.clang-format @@ -671,6 +671,7 @@ SortIncludes: false SortUsingDeclarations: false SpaceAfterCStyleCast: false SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Both SpaceBeforeAssignmentOperators: true SpaceBeforeCtorInitializerColon: true SpaceBeforeInheritanceColon: true At least that makes it match the documentation example -- I got this: const void *a; void * const b; void ** const c; void * const * const d; int strcmp(const char *a, const char *b); But it is only supported in version >= 12, so we need to wait for the minimum LLVM version bump. (Thanks for the ping, Joe!) Cheers, Miguel
On Thu, 2023-10-12 at 13:50 +0200, Miguel Ojeda wrote: > On Wed, 11 Oct 2023 14:44:17 -0700, Dan Williams wrote: > > > > I notice that clang-format reflows that example to: > > > > const void *a; > > void *const b; > > void **const c; > > void *const *const d; > > int strcmp(const char *a, const char *b); > > > > ...but someone more clang-format savvy than me would need to propose the > > changes to the kernel's .clang-format template to match the style > > suggestion. > > I think we could use: > > diff --git a/.clang-format b/.clang-format > index 0bbb1991defe..9eeb511c0814 100644 > --- a/.clang-format > +++ b/.clang-format > @@ -671,6 +671,7 @@ SortIncludes: false > SortUsingDeclarations: false > SpaceAfterCStyleCast: false > SpaceAfterTemplateKeyword: true > +SpaceAroundPointerQualifiers: Both > SpaceBeforeAssignmentOperators: true > SpaceBeforeCtorInitializerColon: true > SpaceBeforeInheritanceColon: true > > At least that makes it match the documentation example -- I got this: > > const void *a; > void * const b; > void ** const c; > void * const * const d; > int strcmp(const char *a, const char *b); > > But it is only supported in version >= 12, so we need to wait for the > minimum LLVM version bump. Do older versions of clang-format ignore entries they don't understand?
On Thu, Oct 12, 2023 at 4:48 PM Joe Perches <joe@perches.com> wrote: > > Do older versions of clang-format ignore entries > they don't understand? Sadly, no, that is the reason we keep it at the minimum. However, I just took a look again at it, and I see that such support was added to LLVM 12, the `--Wno-error=unknown` flag in commit f64903fd8176 ("Add -Wno-error=unknown flag to clang-format."). So this means that the minimum is bumped to 12, we could in principle use newer options. I think the downsides are that users will need to pass the flag (potentially in e.g. their IDE or similar) and that formatting could be potentially chaotic depending on the options ignored. I guess particular subsystems could agree on which version to use. Cheers, Miguel
diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst index 6db37a46d305..71d62d81e506 100644 --- a/Documentation/process/coding-style.rst +++ b/Documentation/process/coding-style.rst @@ -271,6 +271,17 @@ adjacent to the type name. Examples: unsigned long long memparse(char *ptr, char **retptr); char *match_strdup(substring_t *s); +Use space around the ``const`` keyword (except when adjacent to +parentheses). Example: + +.. code-block:: c + + const void *a; + void * const b; + void ** const c; + void * const * const d; + int strcmp(const char *a, const char *b); + Use one space around (on each side of) most binary and ternary operators, such as any of these::