CodingStyle.txt
author Philipp Weyer <pw@igh-essen.com>
Fri, 18 Aug 2017 12:30:16 +0200
changeset 2679 33b922ec1871
parent 2127 0814802f5928
permissions -rw-r--r--
Fixed lib include paths
2126
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
$Id$
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
vim: spelllang=en tw=78
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
This is a short introduction to the coding style that shall be used. The below
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
rules are applicable for all C source files, except the Ethernet drivers, for
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
which the Linux Kernel coding style shall be used to minimize the
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
differences).
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
2127
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    10
1) Line length
2126
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
2127
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    12
- Lines shall not exceed 78 characters.
2126
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
2127
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    14
2) Whitespace
2126
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
2127
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    16
- Indentation shall be done using 4 space characters
2126
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
2127
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    18
- No whitespace shall be left at the end of a line.
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    19
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    20
- After commas, colons and semicolons, a single space shall be
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    21
  placed (if not followed by a line break).
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    22
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    23
- Binary operators (=, ==, ~=, |, ||, etc.) shall be enclosed by 2 spaces
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    24
  (except . and ->).
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    25
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    26
3) Placing braces
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    27
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    28
- Braces shall be placed in the following way (K&R style):
2126
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
    if (...) {
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
        ...
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
    } else if (...) {
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
        ...
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
    } else {
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
        ...
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
    }
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
    int function(...)
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
    {
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
        ...
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
    }
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
2127
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    43
4) Defines and Macros
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    44
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    45
- Defines and macros shall be named in CAPITAL letters. If a macro contains
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    46
  multiple statements, they should be enclosed by a 'do {} while (0)' loop.
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    47
  Macro parameters shall also be capital letters and shall be enclosed py
0814802f5928 Again coding style.
Florian Pose <fp@igh-essen.com>
parents: 2126
diff changeset
    48
  parantheses if necessary.
2126
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
    #define MACRO(A, B) \
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
        do { \
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
            if ((A) == 1) { \
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
                statement(B); \
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
        } while (0)
0383b71b88e4 Added coding style document.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55