From: Martin Kelly <martin@martingkelly.com>
To: Josh Triplett <josh@joshtriplett.org>,
Jonathan Corbet <corbet@lwn.net>,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] CodingStyle: Add a chapter on conditional compilation
Date: Wed, 29 Oct 2014 20:33:53 -0700 [thread overview]
Message-ID: <5451B1A1.2030901@martingkelly.com> (raw)
In-Reply-To: <430ce084b015d22a597ef7e4f6436dadaea7b23d.1414606455.git.josh@joshtriplett.org>
On 10/29/2014 11:15 AM, Josh Triplett wrote:
> Document several common practices and conventions regarding conditional
> compilation, most notably the preference for ifdefs in headers rather
> than .c files.
>
> Signed-off-by: Josh Triplett <josh@joshtriplett.org>
> ---
>
> I found myself explaining a few of these unwritten rules in patch
> feedback, so I figured I'd document them.
>
> Documentation/CodingStyle | 43 +++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 43 insertions(+)
>
> diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle
> index 3171822..9f28b14 100644
> --- a/Documentation/CodingStyle
> +++ b/Documentation/CodingStyle
> @@ -845,6 +845,49 @@ next instruction in the assembly output:
> : /* outputs */ : /* inputs */ : /* clobbers */);
>
>
> + Chapter 20: Conditional Compilation
> +
> +Wherever possible, don't use preprocessor conditionals (#if, #ifdef) in .c
> +files; doing so makes code harder to read and logic harder to follow. Instead,
> +use such conditionals in a header file defining functions for use in those .c
> +files, providing no-op stub versions in the #else case, and then call those
> +functions unconditionally from .c files. The compiler will avoid generating
> +any code for the stub calls, producing identical results, but the logic will
> +remain easy to follow.
> +
> +Prefer to compile out entire functions, rather than portions of functions or
> +portions of expressions. Rather than putting an ifdef in an expression, factor
> +out part or all of the expression into a separate helper function and apply the
> +conditional to that function.
> +
> +If you have a function or variable which may potentially go unused in a
> +particular configuration, and the compiler would warn about its definition
> +going unused, mark the definition as __maybe_unused rather than wrapping it in
> +a preprocessor conditional. (However, if a function or variable *always* goes
> +unused, delete it.)
> +
> +Within code, where possible, use the IS_ENABLED macro to convert a Kconfig
> +symbol into a C boolean expression, and use it in a normal C conditional:
> +
> + if (IS_ENABLED(CONFIG_SOMETHING)) {
> + ...
> + }
> +
> +The compiler will constant-fold the conditional away, and include or exclude
> +the block of code just as with an #ifdef, so this will not add any runtime
> +overhead. However, this approach still allows the C compiler to see the code
> +inside the block, and check it for correctness (syntax, types, symbol
> +references, etc). Thus, you still have to use an #ifdef if the code inside the
> +block references symbols that will not exist if the condition is not met.
> +
> +At the end of any non-trivial #if or #ifdef block (more than a few lines),
> +place a comment after the #endif on the same line, noting the conditional
> +expression used. For instance:
> +
> +#ifdef CONFIG_SOMETHING
> +...
> +#endif /* CONFIG_SOMETHING */
> +
>
> Appendix I: References
>
>
I like that you're formalizing this in the official document; thanks!
next prev parent reply other threads:[~2014-10-30 3:33 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-29 18:15 [PATCH] CodingStyle: Add a chapter on conditional compilation Josh Triplett
2014-10-29 19:12 ` Geert Uytterhoeven
2014-10-29 22:20 ` Josh Triplett
2014-10-30 0:35 ` Randy Dunlap
2014-10-30 2:24 ` Josh Triplett
2014-10-30 3:33 ` Martin Kelly [this message]
2014-11-03 16:46 ` Jonathan Corbet
2014-11-03 17:47 ` Joe Perches
2014-11-03 18:05 ` Josh Triplett
2014-11-03 18:39 ` Joe Perches
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=5451B1A1.2030901@martingkelly.com \
--to=martin@martingkelly.com \
--cc=corbet@lwn.net \
--cc=josh@joshtriplett.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.