Proposal to add rule for nested CPP Directives in RTEMS coding conventions

Vijay Kumar Banerjee vijay at rtems.org
Fri Apr 3 20:11:40 UTC 2020


On Sat, Apr 4, 2020 at 1:22 AM Vaibhav Gupta <vaibhav.varodek at gmail.com>
wrote:

> CPP directives like:
> 1) #if
> 2) #ifdef
> 3) #ifndef
> 4) #elif
> 5) #else
> 6) #endif
>
> do form code blocks. Since, they don't make use of brackets,
> as a part of C language rule, their nested and repeated use
> can make the code very hard to read. Can even result it
> spaghetti code.
>
> This can be understood by this patch:
> https://lists.rtems.org/pipermail/devel/2020-April/058964.html
>
> The proposed idea is:
> 1) Nested CPP directives should follow same indentation rules
>     as of nested conditional statements like 'if' and loops.
>
> 2) To mark which closing CPP directive (#endif) pairs with which
> opening CPP directive (#if or #ifdef or #ifndef), make use of comments.
> ex:
> #ifdef <some-macro> /* To test/verify/enable xyz */
>   .....
>   ....
> #endif /* To test/verify/enable xyz */
>
> There's a rule like this in the FreeBSD and we follow it in RTEMS-libbsd.
It is nicely explained in the FreeBSD style guide so I'm pasting an excerpt
below:

"
    The comment for #endif should match the expression used in the corre-
     sponding #if or #ifdef.  The comment for #else and #elif should match
the
     inverse of the expression(s) used in the preceding #if and/or #elif
     statements.  In the comments, the subexpression "defined(FOO)" is
abbre-
     viated as "FOO".  For the purposes of comments, "#ifndef FOO" is
treated
     as "#if !defined(FOO)".

     #ifdef KTRACE
     #include <sys/ktrace.h>
     #endif

     #ifdef COMPAT_43
     /* A large region here, or other conditional code. */
     #else /* !COMPAT_43 */
     /* Or here. */
     #endif /* COMPAT_43 */

     #ifndef COMPAT_43
     /* Yet another large region here, or other conditional code. */
     #else /* COMPAT_43 */
     /* Or here. */
     #endif /* !COMPAT_43 */

"

It would be nice to have a rule like this for RTEMS codes as well.


--Vaibhav Gupta
> _______________________________________________
> devel mailing list
> devel at rtems.org
> http://lists.rtems.org/mailman/listinfo/devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20200404/6dd7bd49/attachment-0001.html>


More information about the devel mailing list