brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 1 | # Chromium C++ style guide |
| 2 | |
Peter Kasting | 777e130 | 2020-06-02 21:44:40 | [diff] [blame] | 3 | _For other languages, please see the |
John Palmer | be05130 | 2021-05-19 11:48:35 | [diff] [blame] | 4 | [Chromium style guides](https://chromium.googlesource.com/chromium/src/+/main/styleguide/styleguide.md)._ |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 5 | |
| 6 | Chromium follows the [Google C++ Style |
| 7 | Guide](https://google.github.io/styleguide/cppguide.html) unless an exception |
| 8 | is listed below. |
| 9 | |
| 10 | A checkout should give you |
John Palmer | be05130 | 2021-05-19 11:48:35 | [diff] [blame] | 11 | [clang-format](https://chromium.googlesource.com/chromium/src/+/main/docs/clang_format.md) |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 12 | to automatically format C++ code. By policy, Clang's formatting of code should |
| 13 | always be accepted in code reviews. |
| 14 | |
brettw | 196290a | 2016-07-07 03:52:16 | [diff] [blame] | 15 | You can propose changes to this style guide by sending an email to |
| 16 | `[email protected]`. Ideally, the list will arrive at some consensus and you can |
| 17 | request review for a change to this file. If there's no consensus, |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 18 | `src/styleguide/c++/OWNERS` get to decide. |
| 19 | |
Tom McKee | bfea2678 | 2020-02-18 20:57:47 | [diff] [blame] | 20 | Blink code in `third_party/blink` uses [Blink style](blink-c++.md). |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 21 | |
Jeremy Roman | 1bebbea | 2019-06-20 19:17:14 | [diff] [blame] | 22 | ## Modern C++ features |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 23 | |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 24 | Google and Chromium style |
Jan Wilken Dörrie | 276003c | 2023-11-27 16:59:57 | [diff] [blame] | 25 | [targets C++20](https://google.github.io/styleguide/cppguide.html#C++_Version). |
Peter Kasting | 1865f277 | 2021-12-23 21:23:58 | [diff] [blame] | 26 | Additionally, some features of supported C++ versions remain forbidden. The |
| 27 | status of Chromium's C++ support is covered in more detail in |
Avi Drissman | 0aafa9e | 2022-01-18 21:41:01 | [diff] [blame] | 28 | [Modern C++ use in Chromium](c++-features.md). |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 29 | |
| 30 | ## Naming |
| 31 | |
| 32 | * "Chromium" is the name of the project, not the product, and should never |
| 33 | appear in code, variable names, API names etc. Use "Chrome" instead. |
| 34 | |
Dominic Farolino | f3d0307 | 2021-07-20 15:55:54 | [diff] [blame] | 35 | ## Tests and Test-only Code |
Tommy C. Li | 1a13764a | 2018-09-17 21:18:34 | [diff] [blame] | 36 | |
| 37 | * Functions used only for testing should be restricted to test-only usages |
Peter Kasting | 777e130 | 2020-06-02 21:44:40 | [diff] [blame] | 38 | with the testing suffixes supported by |
Joseph Koshy | 0b38a32 | 2022-07-13 15:01:40 | [diff] [blame] | 39 | [PRESUBMIT.py](https://chromium.googlesource.com/chromium/src/+/main/PRESUBMIT.py). |
Caitlin Fischer | 210cfab | 2020-05-07 20:04:30 | [diff] [blame] | 40 | `ForTesting` is the conventional suffix although similar patterns, such as |
| 41 | `ForTest`, are also accepted. These suffixes are checked at presubmit time |
Sam Fortiner | 3b827bd | 2023-11-27 21:42:01 | [diff] [blame^] | 42 | to ensure the functions are called only by test files. In the rare case of |
| 43 | adding a test-only code path to an area where a testing suffix is not |
| 44 | possible, CHECK_IS_TEST() may be appropriate. |
Roland Bock | 1f21a73 | 2021-05-20 16:59:53 | [diff] [blame] | 45 | * Classes used only for testing should be in a GN build target that is |
| 46 | marked `testonly=true`. Tests can depend on such targets, but production |
| 47 | code can not. |
Dominic Farolino | 200a14ec | 2021-07-20 16:42:40 | [diff] [blame] | 48 | * While test files generally appear alongside the production code they test, |
| 49 | support code for `testonly` targets should be placed in a `test/` subdirectory. |
| 50 | For example, see `//mojo/core/core_unittest.cc` and |
| 51 | `//mojo/core/test/mojo_test_base.cc`. For test classes used across multiple |
| 52 | directories, it might make sense to move them into a nested `test` namespace for |
| 53 | clarity. |
Dominic Farolino | f3d0307 | 2021-07-20 15:55:54 | [diff] [blame] | 54 | * Despite the Google C++ style guide |
| 55 | [deprecating](https://google.github.io/styleguide/cppguide.html#File_Names) |
| 56 | the `_unittest.cc` suffix for unit test files, in Chromium we still use this |
| 57 | suffix to distinguish unit tests from browser tests, which are written in |
| 58 | files with the `_browsertest.cc` suffix. |
Tommy C. Li | 1a13764a | 2018-09-17 21:18:34 | [diff] [blame] | 59 | |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 60 | ## Code formatting |
| 61 | |
| 62 | * Put `*` and `&` by the type rather than the variable name. |
danakj | cd463f1 | 2021-02-12 19:53:13 | [diff] [blame] | 63 | * In class declarations, group function overrides together within each access |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 64 | control section, with one labeled group per parent class. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 65 | * Prefer `(foo == 0)` to `(0 == foo)`. |
| 66 | |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 67 | ## Unnamed namespaces |
| 68 | |
| 69 | Items local to a .cc file should be wrapped in an unnamed namespace. While some |
| 70 | such items are already file-scope by default in C++, not all are; also, shared |
| 71 | objects on Linux builds export all symbols, so unnamed namespaces (which |
| 72 | restrict these symbols to the compilation unit) improve function call cost and |
| 73 | reduce the size of entry point tables. |
| 74 | |
| 75 | ## Exporting symbols |
| 76 | |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 77 | Symbols can be exported (made visible outside of a shared library/DLL) by |
| 78 | annotating with a `<COMPONENT>_EXPORT` macro name (where `<COMPONENT>` is the |
| 79 | name of the component being built, e.g. BASE, NET, CONTENT, etc.). Class |
| 80 | annotations should precede the class name: |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 81 | ```c++ |
| 82 | class FOO_EXPORT Foo { |
| 83 | void Bar(); |
| 84 | void Baz(); |
| 85 | // ... |
| 86 | }; |
| 87 | ``` |
| 88 | |
| 89 | Function annotations should precede the return type: |
| 90 | ```c++ |
| 91 | class FooSingleton { |
| 92 | FOO_EXPORT Foo& GetFoo(); |
Jeremy Roman | 31220288 | 2019-02-19 21:43:50 | [diff] [blame] | 93 | FOO_EXPORT Foo& SetFooForTesting(Foo* foo); |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 94 | void SetFoo(Foo* foo); // Not exported. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 95 | }; |
| 96 | ``` |
| 97 | |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 98 | ## Multiple inheritance |
| 99 | |
| 100 | Multiple inheritance and virtual inheritance are permitted in Chromium code, |
| 101 | but discouraged (beyond the "interface" style of inheritance allowed by the |
| 102 | Google style guide, for which we do not require classes to have the "Interface" |
| 103 | suffix). Consider whether composition could solve the problem instead. |
| 104 | |
| 105 | ## Inline functions |
| 106 | |
| 107 | Simple accessors should generally be the only inline functions. These should be |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 108 | named using `snake_case()`. Virtual functions should never be declared this way. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 109 | |
| 110 | ## Logging |
| 111 | |
| 112 | Remove most logging calls before checking in. Unless you're adding temporary |
| 113 | logging to track down a specific bug, and you have a plan for how to collect |
| 114 | the logged data from user machines, you should generally not add logging |
| 115 | statements. |
| 116 | |
| 117 | For the rare case when logging needs to stay in the codebase for a while, |
| 118 | prefer `DVLOG(1)` to other logging methods. This avoids bloating the release |
| 119 | executable and in debug can be selectively enabled at runtime by command-line |
| 120 | arguments: |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 121 | * `--v=n` sets the global log level to n (default 0). All log statements with |
| 122 | a log level less than or equal to the global level will be printed. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 123 | * `--vmodule=mod=n[,mod=n,...]` overrides the global log level for the module |
| 124 | mod. Supplying the string foo for mod will affect all files named foo.cc, |
| 125 | while supplying a wildcard like `*bar/baz*` will affect all files with |
| 126 | `bar/baz` in their full pathnames. |
| 127 | |
| 128 | ## Platform-specific code |
| 129 | |
| 130 | To `#ifdef` code for specific platforms, use the macros defined in |
| 131 | `build/build_config.h` and in the Chromium build config files, not other macros |
Tommy C. Li | 1a13764a | 2018-09-17 21:18:34 | [diff] [blame] | 132 | set by specific compilers or build environments (e.g. `WIN32`). |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 133 | |
| 134 | Place platform-specific #includes in their own section below the "normal" |
| 135 | `#includes`. Repeat the standard `#include` order within this section: |
| 136 | |
| 137 | ```c++ |
| 138 | #include "foo/foo.h" |
| 139 | |
| 140 | #include <stdint.h> |
| 141 | #include <algorithm> |
| 142 | |
| 143 | #include "base/strings/utf_string_conversions.h" |
Xiaohan Wang | 0727d88 | 2022-01-21 03:03:43 | [diff] [blame] | 144 | #include "build/build_config.h" |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 145 | #include "chrome/common/render_messages.h" |
| 146 | |
Xiaohan Wang | 0727d88 | 2022-01-21 03:03:43 | [diff] [blame] | 147 | #if BUILDFLAG(IS_WIN) |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 148 | #include <windows.h> |
Robert Liao | c7c9a1c | 2017-10-18 01:41:31 | [diff] [blame] | 149 | #include "base/win/com_init_util.h" |
Xiaohan Wang | 0727d88 | 2022-01-21 03:03:43 | [diff] [blame] | 150 | #elif BUILDFLAG(IS_POSIX) |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 151 | #include "base/posix/global_descriptors.h" |
| 152 | #endif |
| 153 | ``` |
| 154 | |
| 155 | ## Types |
| 156 | |
Dominick Ng | 71a699a | 2022-01-26 23:46:57 | [diff] [blame] | 157 | * Refer to the [Mojo style |
| 158 | guide](https://chromium.googlesource.com/chromium/src/+/main/docs/security/mojo.md) |
| 159 | when working with types that will be passed across network or process |
| 160 | boundaries. For example, explicitly-sized integral types must be used for |
| 161 | safety, since the sending and receiving ends may not have been compiled |
| 162 | with the same sizes for things like `int` and `size_t`. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 163 | * Use `size_t` for object and allocation sizes, object counts, array and |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 164 | pointer offsets, vector indices, and so on. This prevents casts when |
| 165 | dealing with STL APIs, and if followed consistently across the codebase, |
| 166 | minimizes casts elsewhere. |
| 167 | * Occasionally classes may have a good reason to use a type other than |
| 168 | `size_t` for one of these concepts, e.g. as a storage space optimization. In |
| 169 | these cases, continue to use `size_t` in public-facing function |
| 170 | declarations, and continue to use unsigned types internally (e.g. |
| 171 | `uint32_t`). |
Dominick Ng | 71a699a | 2022-01-26 23:46:57 | [diff] [blame] | 172 | * Follow the [integer semantics |
| 173 | guide](https://chromium.googlesource.com/chromium/src/+/main/docs/security/integer-semantics.md) |
| 174 | for all arithmetic conversions and calculations used in memory management |
| 175 | or passed across network or process boundaries. In other circumstances, |
| 176 | follow [Google C++ casting |
Dana Fried | 74a1889 | 2018-09-13 19:10:01 | [diff] [blame] | 177 | conventions](https://google.github.io/styleguide/cppguide.html#Casting) |
| 178 | to convert arithmetic types when you know the conversion is safe. Use |
Chris Palmer | 8218b857 | 2019-02-26 00:19:16 | [diff] [blame] | 179 | `checked_cast<T>` (from `base/numerics/safe_conversions.h`) when you need to |
| 180 | `CHECK` that the source value is in range for the destination type. Use |
| 181 | `saturated_cast<T>` if you instead wish to clamp out-of-range values. |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 182 | `CheckedNumeric` is an ergonomic way to perform safe arithmetic and casting |
| 183 | in many cases. |
Peter Kasting | 9e210a52 | 2021-03-20 15:45:42 | [diff] [blame] | 184 | * The Google Style Guide [bans |
| 185 | UTF-16](https://google.github.io/styleguide/cppguide.html#Non-ASCII_Characters). |
| 186 | For various reasons, Chromium uses UTF-16 extensively. Use `std::u16string` |
| 187 | and `char16_t*` for 16-bit strings, `u"..."` to declare UTF-16 literals, and |
| 188 | either the actual characters or the `\uXXXX` or `\UXXXXXXXX` escapes for |
| 189 | Unicode characters. Avoid `\xXX...`-style escapes, which can cause subtle |
| 190 | problems if someone attempts to change the type of string that holds the |
| 191 | literal. In code used only on Windows, it may be necessary to use |
| 192 | `std::wstring` and `wchar_t*`; these are legal, but note that they are |
| 193 | distinct types and are often not 16-bit on other platforms. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 194 | |
| 195 | ## Object ownership and calling conventions |
| 196 | |
| 197 | When functions need to take raw or smart pointers as parameters, use the |
| 198 | following conventions. Here we refer to the parameter type as `T` and name as |
| 199 | `t`. |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 200 | * If the function does not modify `t`'s ownership, declare the param as `T*`. |
| 201 | The caller is expected to ensure `t` stays alive as long as necessary, |
| 202 | generally through the duration of the call. Exception: In rare cases (e.g. |
| 203 | using lambdas with STL algorithms over containers of `unique_ptr<>`s), you |
| 204 | may be forced to declare the param as `const std::unique_ptr<T>&`. Do this |
| 205 | only when required. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 206 | * If the function takes ownership of a non-refcounted object, declare the |
| 207 | param as `std::unique_ptr<T>`. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 208 | * If the function (at least sometimes) takes a ref on a refcounted object, |
| 209 | declare the param as `scoped_refptr<T>`. The caller can decide |
| 210 | whether it wishes to transfer ownership (by calling `std::move(t)` when |
| 211 | passing `t`) or retain its ref (by simply passing t directly). |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 212 | * In short, functions should never take ownership of parameters passed as raw |
| 213 | pointers, and there should rarely be a need to pass smart pointers by const |
| 214 | ref. |
| 215 | |
Gabriel Charette | b62806f4a | 2019-01-29 21:08:41 | [diff] [blame] | 216 | Conventions for return values are similar with an important distinction: |
| 217 | * Return raw pointers if-and-only-if the caller does not take ownership. |
| 218 | * Return `std::unique_ptr<T>` or `scoped_refptr<T>` by value when the impl is |
| 219 | handing off ownership. |
| 220 | * **Distinction**: Return `const scoped_refptr<T>&` when the impl retains |
| 221 | ownership so the caller isn't required to take a ref: this avoids bumping |
| 222 | the reference count if the caller doesn't need ownership and also |
| 223 | [helps binary size](https://crrev.com/c/1435627)). |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 224 | |
| 225 | A great deal of Chromium code predates the above rules. In particular, some |
| 226 | functions take ownership of params passed as `T*`, or take `const |
| 227 | scoped_refptr<T>&` instead of `T*`, or return `T*` instead of |
| 228 | `scoped_refptr<T>` (to avoid refcount churn pre-C++11). Try to clean up such |
| 229 | code when you find it, or at least not make such usage any more widespread. |
| 230 | |
Lukasz Anforowicz | 269e41d | 2021-11-30 18:32:01 | [diff] [blame] | 231 | ## Non-owning pointers in class fields |
| 232 | |
danakj | 2c78205 | 2022-06-30 02:13:48 | [diff] [blame] | 233 | Use `const raw_ref<T>` or `raw_ptr<T>` for class and struct fields in place of a |
| 234 | raw C++ reference `T&` or pointer `T*` whenever possible, except in paths that include |
Orr Bernstein | 41ce8f0 | 2022-12-20 17:09:39 | [diff] [blame] | 235 | `/renderer/` or `blink/public/web/`. These are non-owning smart pointers that |
danakj | 2c78205 | 2022-06-30 02:13:48 | [diff] [blame] | 236 | have improved memory-safety over raw pointers and references, and can prevent |
| 237 | exploitation of a significant percentage of Use-after-Free bugs. |
Lukasz Anforowicz | 269e41d | 2021-11-30 18:32:01 | [diff] [blame] | 238 | |
danakj | 2c78205 | 2022-06-30 02:13:48 | [diff] [blame] | 239 | Prefer `const raw_ref<T>` whenever the held pointer will never be null, and it's |
| 240 | ok to drop the `const` if the internal reference can be reassigned to point to a |
| 241 | different `T`. Use `raw_ptr<T>` in order to express that the pointer _can_ be |
| 242 | null. Only `raw_ptr<T>` can be default-constructed, since `raw_ref<T>` disallows |
| 243 | nullness. |
| 244 | |
| 245 | Using `raw_ref<T>` or `raw_ptr<T>` may not be possible in rare cases for |
| 246 | [performance reasons](../../base/memory/raw_ptr.md#Performance). Additionally, |
| 247 | `raw_ptr<T>` doesn’t support some C++ scenarios (e.g. `constexpr`, ObjC |
| 248 | pointers). Tooling will help to encourage use of these types in the future. See |
| 249 | [raw_ptr.md](../../base/memory/raw_ptr.md#When-to-use-raw_ptr_T) for how to add |
| 250 | exclusions. |
Lukasz Anforowicz | 269e41d | 2021-11-30 18:32:01 | [diff] [blame] | 251 | |
Peter Kasting | 618227f | 2023-03-02 02:36:42 | [diff] [blame] | 252 | ## thread_local variables |
| 253 | |
| 254 | Much code in Chrome needs to be "sequence-aware" rather than "thread-aware". If |
| 255 | you need a sequence-local variable, see |
| 256 | [`base::SequenceLocalStorageSlot`](../../base/threading/sequence_local_storage_slot.h). |
| 257 | |
| 258 | If you truly need a thread-local variable, then you can use a `thread_local`, as |
| 259 | long as it complies with the following requirements: |
Peter Kasting | c242b10 | 2023-07-27 20:39:12 | [diff] [blame] | 260 | * Its type must satisfy `std::is_trivially_destructible_v<T>`, due to past |
| 261 | problems with "spooky action at a distance" during destruction. Note that |
| 262 | `raw_ptr<T>` is not a trivially-destructible type and may not be contained |
| 263 | in `thread_locals`. |
Peter Kasting | 618227f | 2023-03-02 02:36:42 | [diff] [blame] | 264 | * It must not be exported (e.g. via `COMPONENT_EXPORT`), since this may result |
| 265 | in codegen bugs on Mac; and at least on Windows, this probably won't compile |
| 266 | in the component build anyway. As a workaround, create an exported getter |
| 267 | function that creates a `thread_local` internally and returns a ref to it. |
| 268 | * If it lives at class/namespace scope, it must be marked `ABSL_CONST_INIT`, |
| 269 | as specified in |
| 270 | [the Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html#thread_local). |
| 271 | * It must not be constructed inside OOM handlers or any other code that cannot |
| 272 | allocate memory, since on POSIX, construction may alloc. |
Peter Kasting | 4f39823 | 2023-03-16 01:34:24 | [diff] [blame] | 273 | |
Peter Kasting | 618227f | 2023-03-02 02:36:42 | [diff] [blame] | 274 | If you can't comply with these requirements, consider |
| 275 | [`base::ThreadLocalOwnedPointer`](../../base/threading/thread_local.h) or |
| 276 | another nearby low-level utility. |
| 277 | |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 278 | ## Forward declarations vs. #includes |
| 279 | |
| 280 | Unlike the Google style guide, Chromium style prefers forward declarations to |
| 281 | `#includes` where possible. This can reduce compile times and result in fewer |
| 282 | files needing recompilation when a header changes. |
| 283 | |
| 284 | You can and should use forward declarations for most types passed or returned |
| 285 | by value, reference, or pointer, or types stored as pointer members or in most |
| 286 | STL containers. However, if it would otherwise make sense to use a type as a |
| 287 | member by-value, don't convert it to a pointer just to be able to |
| 288 | forward-declare the type. |
| 289 | |
| 290 | ## File headers |
| 291 | |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 292 | All files in Chromium start with a common license header. That header should |
| 293 | look like this: |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 294 | |
| 295 | ```c++ |
Avi Drissman | 7b017a99 | 2022-09-07 15:50:38 | [diff] [blame] | 296 | // Copyright $YEAR The Chromium Authors |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 297 | // Use of this source code is governed by a BSD-style license that can be |
| 298 | // found in the LICENSE file. |
| 299 | ``` |
| 300 | |
| 301 | Some important notes about this header: |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 302 | * `$YEAR` should be set to the current year at the time a file is created, and |
| 303 | not changed thereafter. |
Tommy Chiang | 174366f | 2022-07-27 16:40:53 | [diff] [blame] | 304 | * For files specific to ChromiumOS, replace the word Chromium with the phrase |
| 305 | ChromiumOS. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 306 | * The Chromium project hosts mirrors of some upstream open-source projects. |
| 307 | When contributing to these portions of the repository, retain the existing |
| 308 | file headers. |
| 309 | |
| 310 | Use standard `#include` guards in all header files (see the Google style guide |
| 311 | sections on these for the naming convention). Do not use `#pragma once`; |
| 312 | historically it was not supported on all platforms, and it does not seem to |
| 313 | outperform #include guards even on platforms which do support it. |
| 314 | |
Peter Boström | 835c7705 | 2023-03-03 21:33:23 | [diff] [blame] | 315 | ## CHECK(), DCHECK(), NOTREACHED_NORETURN() and NOTREACHED() |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 316 | |
Peter Boström | 835c7705 | 2023-03-03 21:33:23 | [diff] [blame] | 317 | Use the `CHECK()` family of macros to both document and verify invariants. |
| 318 | * Exception: If the invariant is known to be too expensive to verify in |
| 319 | production, you may fall back to `DCHECK()`. Do not do this unless |
| 320 | necessary. |
| 321 | * Historically, Chromium code used `DCHECK()` in most cases, so a great deal |
Vincent Scheib | d4a8acc | 2023-03-16 23:00:07 | [diff] [blame] | 322 | of existing code uses `DCHECK()` instead of `CHECK()`. You are encouraged |
| 323 | to migrate to `CHECK()` or add a comment explaining why DCHECK is |
| 324 | appropriate given the current guidance. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 325 | |
Peter Boström | 835c7705 | 2023-03-03 21:33:23 | [diff] [blame] | 326 | Use `NOTREACHED_NORETURN()` to indicate a piece of code is unreachable. Control |
| 327 | flow does not leave this call, so there should be no executable statements after |
| 328 | it (even return statements from non-void functions). The compiler will issue |
| 329 | dead-code warnings. |
| 330 | * Prefer to unconditionally `CHECK()` instead of conditionally hitting a |
| 331 | `NOTREACHED[_NORETURN]()`, where feasible. |
| 332 | * Historically, Chromium code used `NOTREACHED()` for this purpose. This is |
| 333 | not annotated as `[[noreturn]]`. You are welcome (and encouraged) to migrate |
| 334 | to `NOTREACHED_NORETURN()`, just expect to need to make some tweaks to |
| 335 | surrounding code. |
| 336 | |
| 337 | Use `base::ImmediateCrash()` in the rare case where it's necessary to terminate |
| 338 | the current process for reasons outside its control, that are not violations of |
| 339 | our invariants. |
| 340 | |
| 341 | Use `base::debug::DumpWithoutCrashing()` to generate a crash report but keep |
| 342 | running in the case where you are investigating some failure but know that it's |
| 343 | safe to continue execution. |
| 344 | |
| 345 | Use `DLOG(FATAL)` (does nothing in production) or `LOG(DFATAL)` (logs an error |
| 346 | and continues running in production) if you need to log an error in tests from |
| 347 | production code. From test code, use `ADD_FAILURE()` directly. Do not use these |
| 348 | for invariant failures. Those should use `CHECK()` or `NOTREACHED_NORETURN()` as |
| 349 | noted above. |
| 350 | |
| 351 | For more details, see [checks.md](checks.md). |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 352 | |
Roland Bock | d662a2d | 2022-07-12 20:55:27 | [diff] [blame] | 353 | ## Test-only code paths in production code |
| 354 | |
| 355 | Try to avoid test-only code paths in production code. Such code paths make |
| 356 | production code behave differently in tests. This makes both tests and |
| 357 | production code hard to reason about. Consider dependency injection, fake |
| 358 | classes, etc to avoid such code paths. |
| 359 | |
| 360 | However, if a test-only path in production code cannot be avoided, instrument |
| 361 | that code path with `CHECK_IS_TEST();` to assert that the code is only run in |
| 362 | tests. |
| 363 | |
| 364 | ```c++ |
| 365 | // `profile_manager` may not be available in tests. |
| 366 | if (!profile_manager) { |
| 367 | CHECK_IS_TEST(); |
| 368 | return std::string(); |
| 369 | } |
| 370 | ``` |
| 371 | |
| 372 | `CHECK_IS_TEST();` will crash outside of tests. This asserts that the test-only |
| 373 | code path is not accidentally or maliciously taken in production. |
| 374 | |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 375 | ## Miscellany |
| 376 | |
| 377 | * Use UTF-8 file encodings and LF line endings. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 378 | * Unit tests and performance tests should be placed in the same directory as |
| 379 | the functionality they're testing. |
Xiaohan Wang | cc517f08 | 2019-01-16 01:58:14 | [diff] [blame] | 380 | * The [C++ Dos and Don'ts](c++-dos-and-donts.md) page has more helpful |
| 381 | information. |