From: Kees Cook <keescook@chromium.org>
To: Jonathan Corbet <corbet@lwn.net>
Cc: Kees Cook <keescook@chromium.org>,
Linus Torvalds <torvalds@linux-foundation.org>,
Joe Perches <joe@perches.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
linux-hardening@vger.kernel.org
Subject: [PATCH] docs: Explain the desired position of function attributes
Date: Fri, 24 Sep 2021 13:23:02 -0700 [thread overview]
Message-ID: <20210924202302.2335542-1-keescook@chromium.org> (raw)
While discussing how to format the addition of various function
attributes, some "unwritten rules" of ordering surfaced[1]. Capture a
synthesized version of Linus's, Joe's, and Rasmus's recommendations on
this subject for future reference.
[1] https://lore.kernel.org/mm-commits/CAHk-=wiOCLRny5aifWNhr621kYrJwhfURsa0vFPeUEm8mF0ufg@mail.gmail.com
Signed-off-by: Kees Cook <keescook@chromium.org>
---
Documentation/process/coding-style.rst | 27 ++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst
index 42969ab37b34..3559c34a9281 100644
--- a/Documentation/process/coding-style.rst
+++ b/Documentation/process/coding-style.rst
@@ -487,6 +487,33 @@ because it is a simple way to add valuable information for the reader.
Do not use the ``extern`` keyword with function prototypes as this makes
lines longer and isn't strictly necessary.
+When writing a function prototype, please keep the `order of elements regular
+<https://lore.kernel.org/mm-commits/CAHk-=wiOCLRny5aifWNhr621kYrJwhfURsa0vFPeUEm8mF0ufg@mail.gmail.com>`_. For example::
+
+ __must_check __printf(4, 5) __malloc __init
+ static __always_inline void *action(enum magic value, size_t size,
+ u8 count, char *fmt, ...)
+ {
+ ...
+ }
+
+The preferred order of elements for a function prototype is:
+
+- attributes on the preceding lines
+
+ - return type attributes (here, ``__must_check``)
+ - function parameter attributes (here, ``__printf(4,5)``)
+ - function behavior attributes (here, ``__malloc``)
+ - storage class attributes (here, ``__init``)
+
+- main function prototype on the next lines
+
+ - storage class (here, ``static __always_inline`` -- even though
+ ``__always_inline`` is technically an attribute, it is treated like
+ ``inline``)
+ - return type (here, ``void *``)
+ - function name (here, ``action``)
+ - function parameters (as described earlier: each with type and name)
7) Centralized exiting of functions
-----------------------------------
--
2.30.2
next reply other threads:[~2021-09-24 20:23 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-24 20:23 Kees Cook [this message]
2021-09-24 20:27 ` [PATCH] docs: Explain the desired position of function attributes Randy Dunlap
2021-09-24 21:46 ` Kees Cook
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210924202302.2335542-1-keescook@chromium.org \
--to=keescook@chromium.org \
--cc=corbet@lwn.net \
--cc=joe@perches.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).