Skip to content

Commit f431aab

Browse files
committed
CODING_CONVENTIONS.md: Add preprocessor directive formatting
This explicitly spells out what informally has been the coding convention for some time on preprocessor directives, making it more transparent and easier to find. This is particularly useful as the code base has at least three different styles. Deducing what actually is the current policy would require a details look at how the style has changed over time, rather than being obvious from a quick `grep` over the code base.
1 parent 5d95895 commit f431aab

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

CODING_CONVENTIONS.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,89 @@
162162
* For complex statements it is always good to use more parentheses - or split up
163163
the statement and simplify it.
164164

165+
## Indentation of Preprocessor Directives
166+
167+
Add two spaces of indent *after* the `#` per level of indent. Increment the
168+
indent when entering conditional compilation using `#if`/`#ifdef`/`#ifndef`
169+
(except for the include guard, which does not add to the indent). Treat indent
170+
for C language statements and C preprocessor directives independently.
171+
172+
```
173+
/* BAD: */
174+
#if XOSC1
175+
#define XOSC XOSC1
176+
#define XOSC_NUM 1
177+
#elif XOSC2
178+
#define XOSC XSOC2
179+
#define XOSC_NUM 2
180+
#endif /* XOSC1/XOSC2 */
181+
```
182+
183+
```
184+
/* GOOD: */
185+
#if XOSC1
186+
# define XOSC XOSC1
187+
# define XOSC_NUM 1
188+
#elif XOSC2
189+
# define XOSC XSOC2
190+
# define XOSC_NUM 2
191+
#endif
192+
```
193+
194+
```
195+
/* BAD: */
196+
void init_foo(uint32_t param)
197+
{
198+
(void)param;
199+
#if HAS_FOO
200+
switch (param) {
201+
case CASE1:
202+
do_foo_init_for_case1;
203+
break;
204+
#if HAS_CASE_2
205+
case CASE2:
206+
do_foo_init_for_case2;
207+
break;
208+
#endif
209+
#endif
210+
}
211+
```
212+
213+
```
214+
/* GOOD: */
215+
void init_foo(uint32_t param)
216+
{
217+
(void)param;
218+
#if HAS_FOO
219+
switch (param) {
220+
case CASE1:
221+
do_foo_init_for_case1;
222+
break;
223+
# if HAS_CASE_2
224+
case CASE2:
225+
do_foo_init_for_case2;
226+
break;
227+
# endif
228+
#endif
229+
}
230+
```
231+
232+
### Reasoning
233+
234+
Adding the indent does improve readability a lot, more than adding comments.
235+
Hence, we prefer the indent to allow reviewers to quickly grasp the structure
236+
of the code.
237+
238+
Adding spaces before the `#` is not in compliance with the C standard (even
239+
though in practice compilers will be just fine with whitespace in front), but
240+
adding spaces afterwards is standard compliant. In either case, having the `#`
241+
at the beginning of the line makes it visually stand out from C statements,
242+
which eases reading the code.
243+
244+
Using an indent width of 2 makes preprocessor directives visually more
245+
distinctive from C code, which helps to quickly understand the structure
246+
of code.
247+
165248
## Includes
166249

167250
* The include of system headers (in <>-brackets) always precedes RIOT specific
@@ -171,7 +254,7 @@
171254
statement around includes of optional headers:
172255
```c
173256
#ifdef MODULE_ABC
174-
#include "abc.h"
257+
# include "abc.h"
175258
#endif
176259
```
177260

0 commit comments

Comments
 (0)