summaryrefslogtreecommitdiffstats
path: root/cpp-style-and-conventions.md
blob: 51c2bfeaf1ed546783f13848227dbd3e58cd7ee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# C++ Coding Style and Conventions

## General Philosophy

Being an extensive and complicated language, there are often differences of
opinions on "good" and "bad" C++ code.  Bjarne Stroustrup has said "Within C++
is a smaller, simpler, safer language struggling to get out."  We are striving
to write in this variant of C++ and are therefore following the "C++ Core
Guidelines" that Bjarne and Herb Sutter introduced at CppCon 2015.

Beyond a set of rules that help codify "good" and "bad" C++, we have general
principles that help us align the software we develop with the constraints
within the problem domain being solved by OpenBMC.  These are:
 1. Code should be clear and concise.
 2. Code should be written with modern practices.
 3. Code should be performant.

### Code should be clear and concise

> Brevity is the soul of wit.

It is important that code be optimized for the reviewer and maintainer and not
for the writer.  Solutions should avoid tricks that detract from the clarity
of reviewing and understanding it.

Modern practices allow C++ to be an expressive, but concise, language.  We tend
to favor solutions which succinctly represent the problem in as few lines as
possible.

When there is a conflict between clarity and conciseness, clarity should win
out.

### Code should be written with modern practices

We strive to keep our code conforming to and utilizing of the latest in C++
standards.  Today, that means all C++ code should be compiled using C++14
compiler settings.  As the C++17 standard is finalized and compilers support
it, we will move to it as well.

We also strive to keep the codebase up-to-date with the latest recommended
practices by the language designers.  This is reflected by the choice in
following the C++ Core Guidelines.

[[Not currently implemented]] We finally desire to have computers do our
thinking for us wherever possible.  This means having Continuous Integration
tests on each repository so that regressions are quickly identified prior to
merge. It also means having as much of this document enforced by tools as
possible by, for example, clang-format and clang-tidy.

For those coming to the project from pre-C++11 environments we strongly
recommend the book "Effective Modern C++" as a way to get up to speed on the
differences between C++98/03 and C++11/14/17.

### Code should be performant.

OpenBMC targets embedded processors that typically have 32-64MB of flash and
similar processing power of a typical smart-watch available in 2016.  This
means that there are times where we must limit library selection and/or coding
techniques to compensate for this constraint.  Due to the current technology,
performance evaluation is done in order of { code size, cpu utilization, and
memory size }.

From a macro-optimization perspective, we expect all solutions to have an
appropriate algorithmic complexity for the problem at hand.  Therefore, an
`O(n^3)` algorithm may be rejected even though it has good clarity when an
`O(n*lg(n))` solution exists.

## Global Guidelines and Practices

Please follow the guidelines established by the C++ Core Guidelines (CCG).

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md

[[ Last reviewed revision is 53bc78f ]]

Exceptions:

### Guideline Support Library (GSL)

We do not currently utilize the Guideline Support Library provided by the CCG.
Any recommendation within the CCG of GSL conventions may be ignored at this
time.

### Style recommendations

The following are not followed:
 * NL.10 Avoid CamelCase
 * NL.17 Use K&R-derived layout

## Library and Feature Specifics

Additional recommendations within the OpenBMC project on specific language
features or libraries.

### Exceptions

We do use exceptions as a basis for error handling within OpenBMC.

### Boost

Use of boost is allowed, under the following circumstances:
1. Use is done as a header-only library.  This allows unused functions and
   methods to be removed by the compiler at link time, and avoids adding large
amounts of overhead and flash usage.
2. Implementers should include the lowest level header required to solve the
   problem at hand.  This allows uses to be found and moved forward when new
standards are available, as well as reduce compile times, and decrease the
possibility of accidental use.  (ie, #include <boost/container/flat_map.hpp>
not #include <boost/container.hpp>)
3. The module used should not have an equivalent in the std namespace that meets
   the same requirements for implementation or clarity:  For example,
std::chrono should be preferred over boost::chrono.  std::array over
boost::array.
4. Use does not conflict with any of the core tenants of this coding standard
   (clarity, modern practices, or performance).

### iostream

The iostream conventions of using 'operator<<' contribute to an increased code
size over printf-style operations, due to individual function calls for each
appended value.  We therefore do not use iostreams, or iostream-style APIs,
for logging.

There are cases when using an iostream utility (such as sstream) can result in
clearer and similar-sized code.  iostream may be used in those situations.

## Coding Style

Indentation, naming practices, etc.

### General

* Line length should be limited to 80 characters.
* Indentation should be done with 4 space characters.

### Clang Formatting

Individual OpenBMC repositories can use [clang-format](https://clang.llvm.org/docs/ClangFormat.html)
if desired. The OpenBMC CI infrastructure will automatically verify the
code formatting on code check-in if a .clang_format file is found
within the root directory of the repository. This allows for automatic
validation of code formatting upon check-in.

If a custom configuration is desired, such as using different clang formatting
for C and C++ files, a format-code.sh script can be created, which can for
example use different .clang* files as input depending on the file type.
The format-code.sh script will be executed as part of CI if found in the root
directory of the repository, and will check that there are no files that were
modified after running it (same check as running clang).

OpenBMC requires a clang-format of version 6.0 or greater. An example of
how to run clang-format against all code in your repo can be found by
referencing the [tool](https://github.com/openbmc/openbmc-build-scripts/blob/master/scripts/format-code.sh) used by CI.

[Example .clang-format](https://www.github.com/openbmc/docs/blob/master/style/cpp/.clang-format)

### Bracket style

* Utilize 'Allman' style brackets.  Brackets are on their own line at the same
  indentation level as the statement that creates the scope.

```
if (condition)
{
    ...
}
```

```
void foo()
{
    ...
}
```

* Even one line conditional and loop statements should have brackets.

```
/// Wrong.
if (condition)
    do_something;

/// Correct
if (condition)
{
    do_something;
}
```

### Indentation

* Content within a namespace should be at the same indentation level as the
  namespace itself.

```
namespace foo
{

content

}
```

* Content within a class / struct should be indented.

```
class Foo
{
    public:
        Foo();
}
```

* Content within a function / conditional / loop should be indented.

```
void foo()
{
    while (1)
    {
        if (bar())
        {
            ...
        }
    }
}
```

* Switch / case statements should be indented.

```
switch (foo)
{
    case bar:
    {
        bar();
        break;
    }

    case baz:
    {
        baz();
        break;
    }
}
```

* Labels should be indented so they appear at 1 level less than the current
  indentation, rather than flush to the left.  (This is not to say that goto
  and labels are preferred or should be regularly used, but simply when they
  are used, this is how they are to be used.)

```
void foo()
{
    if (bar)
    {
        do
        {
            if (baz)
            {
                goto exit;
            }

        } while(1);

    exit:
        cleanup();
    }
}
```

### Naming Conventions.

* We generally abstain from any prefix or suffix on names.
* Acronyms should be same-case throughout and follow the requirements as
  in their appropriate section.

```
/// Correct.
SomeBMCType someBMCVariable = bmcFunction();

/// Wrong: type and variable are mixed-case, function isn't lowerCamelCase.
SomeBmcType someBmcVariable = BMCFunction();
```

### Header Ordering

Header inclusion order for a header file:
```
local headers (e.g. "daemon_sys.hpp")
c-libraries
cpp-libraries (including openbmc libraries)
```

Header inclusion order for a source file:
```
source.hpp (if applicable)
local headers
c-libraries
cpp-libraries
```

All in alphabetically sorted order.

#### Files

* C++ headers should end in ".hpp".  C headers should end in ".h".
* C++ files should be named with lower_snake_case.

#### Types

* Prefer 'using' over 'typedef' for type aliases.
* Structs, classes, enums, and typed template parameters should all be in
  UpperCamelCase.
* Prefer namespace scoping rather than long names with prefixes.
* A single-word type alias within a struct / class may be lowercase to match
  STL conventions (`using type = T`) while a multi-word type alias should be
  UpperCamelCase (`using ArrayOfT = std::array<T, N>`).
* Exception: A library API may use lower_snake_case to match conventions of the
  STL or an underlying C library it is abstracting.  Application APIs should
  all be UpperCamelCase.
* Exception: A for-convenience template type alias of a template class may end
  in `_t` to match the conventions of the STL.

```
template <typename T>
class Foo
{
    using type = std::decay_t<T>;
};

template <typename T> using foo_t = Foo<T>::type;
```

#### Variables

* Variables should all be lowerCamelCase, including class members, with no
  underscores.

#### Functions

* Functions should all be lowerCamelCase.
* Exception: A library API may use lower_snake-case to match conventions of
  the STL or an underlying C library it is abstracting.  Application APIs
  should all be lowerCamelCase.

#### Constants

* Constants and enums should be named like variables in lowerCamelCase.

#### Namespaces

* Namespaces should be lower_snake_case.
* Top-level namespace should be named based on the containing repository.
* Favor a namespace called 'details' or 'internal' to indicate the equivalent
  of a "private" namespace in a header file and anonymous namespaces in a C++
  file.

### Header Guards

Prefer '#pragma once' header guard over '#ifndef'-style.

### Additional Whitespace

* Follow NL.18: Use C++-style declarator layout.

```
foo(T& bar, const S* baz); /// Correct.
foo(T &bar, const S *baz); /// Incorrect.
```

* Follow NL.15: Use spaces sparingly.

* Insert whitespace after a conditional and before parens.

```
if (...)
while (...)
for (...)
```

* Insert whitespace around binary operators for readability.

```
foo((a-1)/b,c-2); /// Incorrect.
foo((a - 1) / b, c - 2); /// Correct.
```

* Do not insert whitespace around unary operators.
```
a = * b;  /// Incorrect.
a = & b;  /// Incorrect.
a = b -> c;  /// Incorrect.
if (! a)  /// Incorrect.
```

* Do not insert whitespace inside parens or between a function call and
  parameters.

```
foo(x, y); /// Correct.
foo ( x , y ); /// Incorrect.

do (...)
{
} while(0); /// 'while' here is structured like a function call.
```

* Prefer line-breaks after operators to show continuation.
```
if (this1 == that1 &&
    this2 == that2) /// Correct.

if (this1 == that1
    && this2 == that2) /// Incorrect.
```

* Long lines should have continuation start at the same level as the parens or
  all all items inside the parens should be at a 2-level indent.

```
reallyLongFunctionCall(foo,
                       bar,
                       baz); // Correct.

reallyLongFunctionCall(
        foo,
        bar,
        baz); // Also correct.

reallyLongFunctionCall(
        foo, bar, baz); // Similarly correct.

reallyLongFunctionCall(foo,
        bar,
        baz); // Incorrect.
```

### Misc Guidelines.

* Always use `size_t` or `ssize_t` for things that are sizes, counts, etc.
  You need a strong rationale for using a sized type (ex. `uint8_t`) when a
  size_t will do.

* Use `uint8_t`, `int16_t`, `uint32_t`, `int64_t`, etc. for types where size
  is important due to hardware interaction.  Do not use them, without good
  reason, when hardware interaction is not involved; prefer size_t or int
  instead.


OpenPOWER on IntegriCloud