Skip to content

Commit 39889fa

Browse files
authored
Merge pull request #21703 from crasbe/pr/coding_conv
CODING_CONVENTION: Enable Syntax Highlighting and add more examples
2 parents 703feab + 54027c0 commit 39889fa

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

CODING_CONVENTIONS.md

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ of recognised exceptions where we can (or even must) rely on extensions include:
8585
* Type definitions (using `typedef`) always end on "_t".
8686
* If a typedef is used for a struct, it has to be specified at the struct
8787
definition (i.e., not as a separate line). E.g.:
88-
```
88+
```c
8989
typedef struct {
9090
uint8_t a;
9191
uint8_t b;
9292
} foobar_t;
9393
```
9494
* Use of a separate line typedef for structs is allowed for forward
9595
declarations, e.g.,
96-
```
96+
```c
9797
typedef struct mystruct mystruct_t;
9898
[...]
9999
struct mystruct {
@@ -149,15 +149,15 @@ of recognised exceptions where we can (or even must) rely on extensions include:
149149

150150
* Names of all public functions and variables must start with the name of the
151151
corresponding library, e.g.:
152-
```
152+
```c
153153
thread_getpid(void);
154154
hwtimer_init_comp(uint32_t fcpu);
155155
int transceiver_pid;
156156
```
157157
* Private functions and variables do NOT have this library prefix.
158158
* Do NOT use CamelCase. Function, variable and file names as well as enums,
159159
structs or typedefs are written in lowercase with underscores.
160-
```
160+
```c
161161
/* instead of: */
162162
void CamelCaseNamedFunction(int camelCaseNamedVar);
163163
@@ -178,7 +178,7 @@ of recognised exceptions where we can (or even must) rely on extensions include:
178178
`do`-statement, it goes into the same line.
179179
* Use curly braces even for one-line blocks. This improves debugging and later
180180
additions.
181-
```
181+
```c
182182
/* instead of: */
183183
if (debug) println("DEBUG");
184184
else println("DEBUG ELSE");
@@ -194,6 +194,20 @@ of recognised exceptions where we can (or even must) rely on extensions include:
194194
* Commas are always followed by a space.
195195
* For complex statements it is always good to use more parentheses - or split up
196196
the statement and simplify it.
197+
* Switch cases are an exception, the `case` statement is supposed to be on
198+
the same indentation level as the `switch`:
199+
```c
200+
switch(foo) {
201+
case BAR:
202+
printf("Hello");
203+
break;
204+
case PUB:
205+
printf("World");
206+
break;
207+
default:
208+
break;
209+
}
210+
```
197211

198212
## Indentation of Preprocessor Directives
199213

@@ -202,7 +216,7 @@ indent when entering conditional compilation using `#if`/`#ifdef`/`#ifndef`
202216
(except for the include guard, which does not add to the indent). Treat indent
203217
for C language statements and C preprocessor directives independently.
204218

205-
```
219+
```c
206220
/* BAD: */
207221
#if XOSC1
208222
#define XOSC XOSC1
@@ -213,7 +227,7 @@ for C language statements and C preprocessor directives independently.
213227
#endif /* XOSC1/XOSC2 */
214228
```
215229

216-
```
230+
```c
217231
/* GOOD: */
218232
#if XOSC1
219233
# define XOSC XOSC1
@@ -224,7 +238,7 @@ for C language statements and C preprocessor directives independently.
224238
#endif
225239
```
226240

227-
```
241+
```c
228242
/* BAD: */
229243
void init_foo(uint32_t param)
230244
{
@@ -243,7 +257,7 @@ void init_foo(uint32_t param)
243257
}
244258
```
245259
246-
```
260+
```c
247261
/* GOOD: */
248262
void init_foo(uint32_t param)
249263
{
@@ -349,7 +363,7 @@ Note: these rules will be enforced by the CI.
349363
* C Header files should be always wrapped for C++ compatibility to prevent
350364
issues with name mangling, i.e. mark all the containing functions and
351365
definitions as `extern "C"`
352-
``` C
366+
```c
353367
#ifdef __cplusplus
354368
extern "C" {
355369
#endif
@@ -368,22 +382,22 @@ extern "C" {
368382

369383
* Absolute values must be specified as macros or enums, not as literals, i.e.
370384
instead of
371-
```
385+
```c
372386
int timeout = 7 * 1000000;
373387
```
374388
write
375-
```
389+
```c
376390
int timeout = TIMEOUT_INTERVAL * USEC_PER_SEC;
377391
```
378392
## Comments
379393
* All comments should be written as C-style comments.
380394

381395
E.g:
382-
```
396+
```c
383397
/* This is a C-style comment */
384398
```
385399
Wrong:
386-
```
400+
```c
387401
// C++ comment here
388402
```
389403

@@ -398,7 +412,7 @@ Wrong:
398412

399413
An exemplary doxygen documentation in a header file can look like this.
400414

401-
```
415+
```c
402416
/*
403417
* SPDX-FileCopyrightText: 2014 Peter Schmerzl <[email protected]>
404418
* SPDX-License-Identifier: LGPL-2.1-only
@@ -426,6 +440,19 @@ An exemplary doxygen documentation in a header file can look like this.
426440
* @return 1 if setting the state was successful, 0 otherwise.
427441
*/
428442
int set_foobar(int state, int *old_state);
443+
444+
/**
445+
* @brief Document multiple return values.
446+
*
447+
* You can use the `@return` command to specify the general kind of return
448+
* value and the `@retval` commands to specify distinct values that will be
449+
* returned by your function.
450+
*
451+
* @return Length of FOO on success or an error code on failure.
452+
* @retval -EIO on Input Output Errors
453+
* @retval -ENOMEM on small microcontrollers with little RAM
454+
*/
455+
int get_foolength(void);
429456
```
430457
431458
### SPDX
@@ -440,7 +467,7 @@ notices tend to vary depending on the author, making it difficult to parse
440467
automatically and reliably.
441468
442469
Old Style - License Information:
443-
```
470+
```c
444471
/*
445472
* Copyright (C) 2013, 2014 INRIA
446473
* 2015 Freie Universität Berlin
@@ -453,7 +480,7 @@ Old Style - License Information:
453480
```
454481

455482
New Style - SPDX Format:
456-
```
483+
```c
457484
/*
458485
* SPDX-FileCopyrightText: 2013-2014 INRIA
459486
* SPDX-FileCopyrightText: 2015 Freie Universität Berlin
@@ -515,7 +542,7 @@ not a string literal`.
515542
possibility to suppress this warning/error. You MUST do so by adding a
516543
comment, including a rationale why it is a false positive and why the code
517544
can't be fixed otherwise, in the following format:
518-
```
545+
```c
519546
/* cppcheck-suppress <category of error/warning>
520547
* (reason: cppcheck is being really silly. this is certainly not a
521548
* null-pointer dereference */

0 commit comments

Comments
 (0)