<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Apr 4, 2020 at 1:22 AM Vaibhav Gupta <<a href="mailto:vaibhav.varodek@gmail.com">vaibhav.varodek@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">CPP directives like:<br>
1) #if<br>
2) #ifdef<br>
3) #ifndef<br>
4) #elif<br>
5) #else<br>
6) #endif<br>
<br>
do form code blocks. Since, they don't make use of brackets,<br>
as a part of C language rule, their nested and repeated use<br>
can make the code very hard to read. Can even result it<br>
spaghetti code.<br>
<br>
This can be understood by this patch:<br>
<a href="https://lists.rtems.org/pipermail/devel/2020-April/058964.html" rel="noreferrer" target="_blank">https://lists.rtems.org/pipermail/devel/2020-April/058964.html</a><br>
<br>
The proposed idea is:<br>
1) Nested CPP directives should follow same indentation rules<br>
as of nested conditional statements like 'if' and loops.<br>
<br>
2) To mark which closing CPP directive (#endif) pairs with which<br>
opening CPP directive (#if or #ifdef or #ifndef), make use of comments.<br>
ex:<br>
#ifdef <some-macro> /* To test/verify/enable xyz */<br>
.....<br>
....<br>
#endif /* To test/verify/enable xyz */<br>
<br></blockquote><div>There's a rule like this in the FreeBSD and we follow it in RTEMS-libbsd.</div><div>It is nicely explained in the FreeBSD style guide so I'm pasting an excerpt</div><div>below:</div><div><br></div><div>"</div><div> The comment for #endif should match the expression used in the corre-</div><div> sponding #if or #ifdef. The comment for #else and #elif should match the<br> inverse of the expression(s) used in the preceding #if and/or #elif<br> statements. In the comments, the subexpression "defined(FOO)" is abbre-<br> viated as "FOO". For the purposes of comments, "#ifndef FOO" is treated<br> as "#if !defined(FOO)".<br><br> #ifdef KTRACE<br> #include <sys/ktrace.h><br> #endif<br><br> #ifdef COMPAT_43<br> /* A large region here, or other conditional code. */<br> #else /* !COMPAT_43 */<br> /* Or here. */<br> #endif /* COMPAT_43 */<br><br> #ifndef COMPAT_43<br> /* Yet another large region here, or other conditional code. */<br> #else /* COMPAT_43 */<br> /* Or here. */<br> #endif /* !COMPAT_43 */<br></div><div><pre style="font-size:1.1em;color:rgb(0,0,0)">"
</pre><pre style="font-size:1.1em;color:rgb(0,0,0)">It would be nice to have a rule like this for RTEMS codes as well.</pre><pre style="font-size:1.1em;color:rgb(0,0,0)"><br></pre></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
--Vaibhav Gupta<br>
_______________________________________________<br>
devel mailing list<br>
<a href="mailto:devel@rtems.org" target="_blank">devel@rtems.org</a><br>
<a href="http://lists.rtems.org/mailman/listinfo/devel" rel="noreferrer" target="_blank">http://lists.rtems.org/mailman/listinfo/devel</a><br>
</blockquote></div></div>