blob: 7790502a5cb5d9ef83fd2b9139535b4fc4715fc9 [file] [log] [blame]
Keir Mierle2c1e56b2019-11-15 16:32:11 -08001.. _chapter-style:
2
3.. default-domain:: cpp
4
5.. highlight:: sh
6
7===========================
8Style Guide and Conventions
9===========================
10
Armando Montanezf2bbb752020-03-03 09:50:37 -080011.. tip::
12 Pigweed runs ``pw format`` as part of ``pw presubmit`` to perform some code
13 formatting checks. To speed up the review process, consider adding ``pw
14 presubmit`` as a git push hook using the following command:
15 ``pw presubmit --install``
16
Keir Mierle2c1e56b2019-11-15 16:32:11 -080017---------
18C++ style
19---------
20
21The Pigweed C++ style guide is closely based on Google's external C++ Style
22Guide, which is found on the web at
23https://google.github.io/styleguide/cppguide.html. The Google C++ Style Guide
24applies to Pigweed except as described in this document.
25
26The Pigweed style guide only applies to Pigweed itself. It does not apply to
Alexei Frolov44d54732020-01-10 14:45:43 -080027projects that use Pigweed or to the third-party code included with Pigweed.
Keir Mierle2c1e56b2019-11-15 16:32:11 -080028Non-Pigweed code is free to use features restricted by Pigweed, such as dynamic
29memory allocation and the entirety of the C++ Standard Library.
30
31Recommendations in the :doc:`embedded_cpp_guide` are considered part of the
32Pigweed style guide, but are separated out since it covers more general
33embedded development beyond just C++ style.
34
35Automatic formatting
36====================
37Pigweed uses `clang-format <https://clang.llvm.org/docs/ClangFormat.html>`_ to
38automatically format Pigweed source code. A ``.clang-format`` configuration is
39provided with the Pigweed repository.
40
41Automatic formatting is essential to facilitate large-scale, automated changes
42in Pigweed. Therefore, all code in Pigweed is expected to be formatted with
43``clang-format`` prior to submission. Existing code may be reformatted at any
44time.
45
46If ``clang-format`` formats code in an undesirable or incorrect way, it can be
47disabled for the affected lines by adding ``// clang-format off``.
48``clang-format`` must then be re-enabled with a ``// clang-format on`` comment.
49
50.. code-block:: cpp
51
52 // clang-format off
Alexei Frolov44d54732020-01-10 14:45:43 -080053 constexpr int kMyMatrix[] = {
Keir Mierle2c1e56b2019-11-15 16:32:11 -080054 100, 23, 0,
55 0, 542, 38,
56 1, 2, 201,
57 };
58 // clang-format on
59
60C Standard Library
61==================
62In C++ headers, always use the C++ versions of C Standard Library headers (e.g.
63``<cstdlib>`` instead of ``<stdlib.h>``). If the header is used by both C and
64C++ code, only the C header should be used.
65
66In C++ code, it is preferred to use C functions from the ``std`` namespace. For
67example, use ``std::memcpy`` instead of ``memcpy``. The C++ standard does not
68require the global namespace versions of the functions to be provided. Using
69``std::`` is more consistent with the C++ Standard Library and makes it easier
70to distinguish Pigweed functions from library functions.
71
72Within core Pigweed, do not use C standard library functions that allocate
73memory, such as ``std::malloc``. There are exceptions to this for when dynamic
74allocation is enabled for a system; Pigweed modules are allowed to add extra
75functionality when a heap is present; but this must be optional.
76
77C++ Standard Library
78====================
79Much of the C++ Standard Library is not a good fit for embedded software. Many
80of the classes and functions were not designed with the RAM, flash, and
81performance constraints of a microcontroller in mind. For example, simply
82adding the line ``#include <iostream>`` can increase the binary size by 150 KB!
Alexei Frolov44d54732020-01-10 14:45:43 -080083This is larger than many microcontrollers' entire internal storage.
Keir Mierle2c1e56b2019-11-15 16:32:11 -080084
85However, with appropriate caution, a limited set of standard C++ libraries can
86be used to great effect. Developers can leverage familiar, well-tested
87abstractions instead of writing their own. C++ library algorithms and classes
88can give equivalent or better performance than hand-written C code.
89
90A limited subset of the C++ Standard Library is permitted in Pigweed. To keep
91Pigweed small, flexible, and portable, functions that allocate dynamic memory
92must be avoided. Care must be exercised when using multiple instantiations of a
93template function, which can lead to code bloat.
94
95The following C++ Standard Library headers are always permitted:
96
97 * ``<array>``
98 * ``<complex>``
99 * ``<initializer_list>``
100 * ``<iterator>``
101 * ``<limits>``
102 * ``<optional>``
103 * ``<random>``
104 * ``<ratio>``
105 * ``<span>``
106 * ``<string_view>``
107 * ``<tuple>``
108 * ``<type_traits>``
109 * ``<utility>``
110 * ``<variant>``
111 * C Standard Library headers (``<c*>``)
112
113With caution, parts of the following headers can be used:
114
115 * ``<algorithm>`` -- be wary of potential memory allocation
116 * ``<atomic>`` -- not all MCUs natively support atomic operations
117 * ``<bitset>`` -- conversions to or from strings are disallowed
118 * ``<functional>`` -- do **not** use ``std::function``
119 * ``<new>`` -- for placement new
120 * ``<numeric>`` -- be wary of code size with multiple template instantiations
121
122Never use any of these headers:
123
124 * Dynamic containers (``<list>``, ``<map>``, ``<set>``, ``<vector>``, etc.)
125 * Streams (``<iostream>``, ``<ostream>``, ``<fstream>``, etc.)
126 * ``<exception>``
127 * ``<future>``, ``<mutex>``, ``<thread>``
128 * ``<memory>``
129 * ``<regex>``
130 * ``<scoped_allocator>``
131 * ``<sstream>``
132 * ``<stdexcept>``
133 * ``<string>``
134 * ``<valarray>``
135
136Headers not listed here should be carefully evaluated before they are used.
137
138These restrictions do not apply to third party code or to projects that use
139Pigweed.
140
141Combining C and C++
142===================
143Prefer to write C++ code over C code, using ``extern "C"`` for symbols that must
144have C linkage. ``extern "C"`` functions should be defined within C++
145namespaces to simplify referring to other code.
146
147C++ functions with no parameters do not include ``void`` in the parameter list.
148C functions with no parameters must include ``void``.
149
150.. code-block:: cpp
151
152 namespace pw {
153
154 bool ThisIsACppFunction() { return true; }
155
156 extern "C" int pw_ThisIsACFunction(void) { return -1; }
157
158 extern "C" {
159
160 int pw_ThisIsAlsoACFunction(void) {
161 return ThisIsACppFunction() ? 100 : 0;
162 }
163
164 } // extern "C"
165
166 } // namespace pw
167
168Comments
169========
170Prefer C++-style (``//``) comments over C-style commments (``/* */``). C-style
171comments should only be used for inline comments.
172
173.. code-block:: cpp
174
175 // Use C++-style comments, except where C-style comments are necessary.
176 // This returns a random number using an algorithm I found on the internet.
177 #define RANDOM_NUMBER() [] { \
178 return 4; /* chosen by fair dice roll */ \
179 }()
180
181Indent code in comments with two additional spaces, making a total of three
182spaces after the ``//``. All code blocks must begin and end with an empty
Alexei Frolov44d54732020-01-10 14:45:43 -0800183comment line, even if the blank comment line is the last line in the block.
Keir Mierle2c1e56b2019-11-15 16:32:11 -0800184
185.. code-block:: cpp
186
187 // Here is an example of code in comments.
188 //
189 // int indentation_spaces = 2;
190 // int total_spaces = 3;
191 //
192 // engine_1.thrust = RANDOM_NUMBER() * indentation_spaces + total_spaces;
193 //
194 bool SomeFunction();
195
196Control statements
197==================
198All loops and conditional statements must use braces.
199
200The syntax ``while (true)`` if preferred over ``for (;;)`` for infinite loops.
201
202Include guards
203==============
204The first non-comment line of every header file must be ``#pragma once``. Do
205not use traditional macro include guards. The ``#pragma once`` should come
206directly after the Pigweed copyright block, with no blank line, followed by a
207blank, like this:
208
209.. code-block:: cpp
210
Keir Mierle086ef1c2020-03-19 02:03:51 -0700211 // Copyright 2020 The Pigweed Authors
Keir Mierle2c1e56b2019-11-15 16:32:11 -0800212 //
213 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
Wyatt Hepler1a960942019-11-26 14:13:38 -0800214 // use this file except in compliance with the License. You may obtain a copy of
215 // the License at
Keir Mierle2c1e56b2019-11-15 16:32:11 -0800216 //
217 // https://www.apache.org/licenses/LICENSE-2.0
218 //
219 // Unless required by applicable law or agreed to in writing, software
220 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
221 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Wyatt Hepler1a960942019-11-26 14:13:38 -0800222 // License for the specific language governing permissions and limitations under
223 // the License.
Keir Mierle2c1e56b2019-11-15 16:32:11 -0800224 #pragma once
225
226 // Header file-level comment goes here...
227
228Memory allocation
229=================
230Dynamic memory allocation can be problematic. Heap allocations and deallocations
231occupy valuable CPU cycles. Memory usage becomes nondeterministic, which can
232result in a system crashing without a clear culprit.
233
234To keep Pigweed portable, core Pigweed code is not permitted to dynamically
235(heap) allocate memory, such as with ``malloc`` or ``new``. All memory should be
236allocated with automatic (stack) or static (global) storage duration. Pigweed
237must not use C++ libraries that use dynamic allocation.
238
239Projects that use Pigweed are free to use dynamic allocation, provided they
240have selected a target that enables the heap.
241
242Naming
243======
244Entities shall be named according to the `Google style guide
245<https://google.github.io/styleguide/cppguide.html>`_, with the following
246additional requirements.
247
248**C++ code**
249 * All Pigweed C++ code must be in the ``pw`` namespace. Namespaces for
250 modules should be nested under ``pw``. For example,
251 ``pw::string::Format()``.
252 * Whenever possible, private code should be in a source (.cc) file and placed
253 in anonymous namespace nested under ``pw``.
254 * If private code must be exposed in a header file, it must be in a namespace
255 nested under ``pw``. The namespace may be named for its subsystem or use a
256 name that designates it as private, such as ``internal``.
Keir Mierle003044a2020-04-14 10:56:20 -0700257 * Template arguments for non-type names (e.g. ``template <int foo_bar>``)
258 should follow the variable naming convention, which means snake case (e.g.
259 ``foo_bar``). This matches the Google C++ style, however the wording in the
260 official style guide isn't explicit and could be interpreted to use
261 ``kFooBar`` style naming. Wide practice establishes that the naming
262 convention is ``snake_case``, and so that is the style we use in Pigweed.
263
264 **Note:** At time of writing much of Pigweed incorrectly follows the
265 ``kCamelCase`` naming for non-type template arguments. This is a bug that
266 will be fixed eventually.
Keir Mierle2c1e56b2019-11-15 16:32:11 -0800267
268**C code**
269 * Public names used by C code must be prefixed with ``pw_``.
270 * If private code must be exposed in a header, private names used by C code
271 must be prefixed with ``_pw_``.
272 * Avoid writing C source (.c) files in Pigweed. Prefer to write C++ code with
273 C linkage using ``extern "C"``. Within C source, private C functions and
274 variables must be named with the ``_pw_`` prefix and should be declared
275 ``static`` whenever possible; for example, ``_pw__MyPrivateFunction``.
276 * The C prefix rules apply to
277
278 * C functions (``int pw_FunctionName(void);``),
279 * variables used by C code (``int pw_variable_name;``),
280 * constant variables used by C code (``int pw_kConstantName;``), and
281 * structs used by C code (``typedef struct {} pw_StructName;``).
282
283 The prefix does not apply to struct members, which use normal Google style.
284
285**Preprocessor macros**
286 * Public Pigweed macros must be prefixed with ``PW_``.
287 * Private Pigweed macros must be prefixed with ``_PW_``.
288
289**Example**
290
291.. code-block:: cpp
292
293 namespace pw {
294 namespace nested_namespace {
295
296 // C++ names (types, variables, functions) must be in the pw namespace.
297 // They are named according to the Google style guide.
298 constexpr int kGlobalConstant = 123;
299
300 // Prefer using functions over extern global variables.
301 extern int global_variable;
302
303 class Class {};
304
305 void Function();
306
307 extern "C" {
308
309 // Public Pigweed code used from C must be prefixed with pw_.
310 extern const int pw_kGlobalConstant;
311
312 extern int pw_global_variable;
313
314 void pw_Function(void);
315
316 typedef struct {
317 int member_variable;
318 } pw_Struct;
319
320 // Private Pigweed code used from C must be prefixed with _pw_.
321 extern const int _pw_kPrivateGlobalConstant;
322
323 extern int _pw_private_global_variable;
324
325 void _pw_PrivateFunction(void);
326
327 typedef struct {
328 int member_variable;
329 } _pw_PrivateStruct;
330
331 } // extern "C"
332
333 // Public macros must be prefixed with PW_.
334 #define PW_PUBLIC_MACRO(arg) arg
335
336 // Private macros must be prefixed with _PW_.
337 #define _PW_PRIVATE_MACRO(arg) arg
338
339 } // namespace nested_namespace
340 } // namespace pw
341
342Namespace scope formatting
343==========================
344All non-indented blocks (namespaces, ``extern "C"`` blocks, and preprocessor
345conditionals) must have a comment on their closing line with the
346contents of the starting line.
347
348All nested namespaces should be declared together with no blank lines between
349them.
350
351.. code-block:: cpp
352
353 #include "some/header.h"
354
355 namespace pw::nested {
356 namespace {
357
358 constexpr int kAnonConstantGoesHere = 0;
359
360 } // namespace
361
362 namespace other {
363
364 const char* SomeClass::yes = "no";
365
366 bool ThisIsAFunction() {
367 #if PW_CONFIG_IS_SET
368 return true;
369 #else
370 return false;
371 #endif // PW_CONFIG_IS_SET
372 }
373
374 extern "C" {
375
376 const int pw_kSomeConstant = 10;
377 int pw_some_global_variable = 600;
378
379 void pw_CFunction() { ... }
380
381 } // extern "C"
382
383 } // namespace
384 } // namespace pw::nested
385
386Pointers and references
387=======================
388For pointer and reference types, place the asterisk or ampersand next to the
389type.
390
391.. code-block:: cpp
392
393 int* const number = &that_thing;
394 constexpr const char* kString = "theory!"
395
396 bool FindTheOneRing(const Region& where_to_look) { ... }
397
398Prefer storing references over storing pointers. Pointers are required when the
399pointer can change its target or may be ``nullptr``. Otherwise, a reference or
400const reference should be used. In accordance with the Google C++ style guide,
401only const references are permitted as function arguments; pointers must be used
402in place of mutable references when passed as function arguments.
403
404Preprocessor macros
405===================
406Macros should only be used when they significantly improve upon the C++ code
407they replace. Macros should make code more readable, robust, and safe, or
408provide features not possible with standard C++, such as stringification, line
409number capturing, or conditional compilation. When possible, use C++ constructs
410like constexpr variables in place of macros. Never use macros as constants,
411except when a string literal is needed or the value must be used by C code.
412
413When macros are needed, the macros should be accompanied with extensive tests
414to ensure the macros are hard to use wrong.
415
416Stand-alone statement macros
417----------------------------
418Macros that are standalone statements must require the caller to terminate the
Alexei Frolov44d54732020-01-10 14:45:43 -0800419macro invocation with a semicolon. For example, the following does *not* conform
Keir Mierle2c1e56b2019-11-15 16:32:11 -0800420to Pigweed's macro style:
421
422.. code-block:: cpp
423
424 // BAD! Definition has built-in semicolon.
425 #define PW_LOG_IF_BAD(mj) \
426 CallSomeFunction(mj);
427
428 // BAD! Compiles without error; semicolon is missing.
Alexei Frolov44d54732020-01-10 14:45:43 -0800429 PW_LOG_IF_BAD("foo")
Keir Mierle2c1e56b2019-11-15 16:32:11 -0800430
431Here's how to do this instead:
432
433.. code-block:: cpp
434
435 // GOOD; requires semicolon to compile.
436 #define PW_LOG_IF_BAD(mj) \
437 CallSomeFunction(mj)
438
439 // GOOD; fails to compile due to lacking semicolon.
Alexei Frolov44d54732020-01-10 14:45:43 -0800440 PW_LOG_IF_BAD("foo")
Keir Mierle2c1e56b2019-11-15 16:32:11 -0800441
442For macros in function scope that do not already require a semicolon, the
443contents can be placed in a ``do { ... } while (0)`` loop.
444
445.. code-block:: cpp
446
447 #define PW_LOG_IF_BAD(mj) \
448 do { \
449 if (mj.Bad()) { \
450 Log(#mj " is bad") \
451 } \
452 } while (0)
453
454Standalone macros at global scope that do not already require a semicolon can
455add a ``static_assert`` or throwaway struct declaration statement as their
456last line.
457
458.. code-block:: cpp
459
460 #define PW_NEAT_THING(thing) \
461 bool IsNeat_##thing() { return true; } \
462 static_assert(true, "Macros must be terminated with a semicolon")
463
464Private macros in public headers
465--------------------------------
466Private macros in public headers must be prefixed with ``_PW_``, even if they
467are undefined after use; this prevents collisions with downstream users. For
468example:
469
470.. code-block:: cpp
471
472 #define _PW_MY_SPECIAL_MACRO(op) ...
473 ...
474 // Code that uses _PW_MY_SPECIAL_MACRO()
475 ...
476 #undef _PW_MY_SPECIAL_MACRO
477
478Macros in private implementation files (.cc)
479--------------------------------------------
480Macros within .cc files that should only used within one file should be
481undefined after their last use; for example:
482
483.. code-block:: cpp
484
485 #define DEFINE_OPERATOR(op) \
486 T operator ## op(T x, T y) { return x op y; } \
487 static_assert(true, "Macros must be terminated with a semicolon") \
488
489 DEFINE_OPERATOR(+);
490 DEFINE_OPERATOR(-);
491 DEFINE_OPERATOR(/);
492 DEFINE_OPERATOR(*);
493
494 #undef DEFINE_OPERATOR
495
496Preprocessor conditional statements
497===================================
498When using macros for conditional compilation, prefer to use ``#if`` over
499``#ifdef``. This checks the value of the macro rather than whether it exists.
500
501 * ``#if`` handles undefined macros equivalently to ``#ifdef``. Undefined
502 macros expand to 0 in preprocessor conditional statements.
503 * ``#if`` evaluates false for macros defined as 0, while ``#ifdef`` evaluates
504 true.
505 * Macros defined using compiler flags have a default value of 1 in GCC and
506 Clang, so they work equivalently for ``#if`` and ``#ifdef``.
507 * Macros defined to an empty statement cause compile-time errors in ``#if``
508 statements, which avoids ambiguity about how the macro should be used.
509
510All ``#endif`` statements should be commented with the expression from their
511corresponding ``#if``. Do not indent within preprocessor conditional statements.
512
513.. code-block:: cpp
514
515 #if USE_64_BIT_WORD
516 using Word = uint64_t;
517 #else
518 using Word = uint32_t;
519 #endif // USE_64_BIT_WORD
520
521Unsigned integers
522=================
523Unsigned integers are permitted in Pigweed. Aim for consistency with existing
524code and the C++ Standard Library. Be very careful mixing signed and unsigned
525integers.
526
527------------
528Python style
529------------
530Pigweed uses the standard Python style: PEP8, which is available on the web at
531https://www.python.org/dev/peps/pep-0008/. All Pigweed Python code should pass
532``yapf`` when configured for PEP8 style.
533
Wyatt Heplerab4eb7a2020-01-08 18:04:31 -0800534Python 3
535========
536Pigweed uses Python 3. Some modules may offer limited support for Python 2, but
537Python 3.6 or newer is required for most Pigweed code.