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 | 777e130 | 2020-06-02 21:44:40 | [diff] [blame] | 24 | Google style |
| 25 | [targets C++17](https://google.github.io/styleguide/cppguide.html#C++_Version). |
Joe Mason | 8ae58eb3 | 2021-11-24 14:10:38 | [diff] [blame] | 26 | Chromium targets C++14; C++17 support is not expected before 2022. (See the |
| 27 | [tracking bug](https://crbug.com/752720) for more details.) Additionally, some |
| 28 | features of supported C++ versions remain forbidden. The status of Chromium's |
| 29 | C++ support is covered in more detail in [Modern C++ use in Chromium](c++11.md). |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 30 | |
| 31 | ## Naming |
| 32 | |
| 33 | * "Chromium" is the name of the project, not the product, and should never |
| 34 | appear in code, variable names, API names etc. Use "Chrome" instead. |
| 35 | |
Dominic Farolino | f3d0307 | 2021-07-20 15:55:54 | [diff] [blame] | 36 | ## Tests and Test-only Code |
Tommy C. Li | 1a13764a | 2018-09-17 21:18:34 | [diff] [blame] | 37 | |
| 38 | * Functions used only for testing should be restricted to test-only usages |
Peter Kasting | 777e130 | 2020-06-02 21:44:40 | [diff] [blame] | 39 | with the testing suffixes supported by |
John Palmer | be05130 | 2021-05-19 11:48:35 | [diff] [blame] | 40 | [PRESUMBIT.py](https://chromium.googlesource.com/chromium/src/+/main/PRESUBMIT.py). |
Caitlin Fischer | 210cfab | 2020-05-07 20:04:30 | [diff] [blame] | 41 | `ForTesting` is the conventional suffix although similar patterns, such as |
| 42 | `ForTest`, are also accepted. These suffixes are checked at presubmit time |
| 43 | to ensure the functions are called only by test files. |
Roland Bock | 1f21a73 | 2021-05-20 16:59:53 | [diff] [blame] | 44 | * Classes used only for testing should be in a GN build target that is |
| 45 | marked `testonly=true`. Tests can depend on such targets, but production |
| 46 | code can not. |
Dominic Farolino | 200a14ec | 2021-07-20 16:42:40 | [diff] [blame] | 47 | * While test files generally appear alongside the production code they test, |
| 48 | support code for `testonly` targets should be placed in a `test/` subdirectory. |
| 49 | For example, see `//mojo/core/core_unittest.cc` and |
| 50 | `//mojo/core/test/mojo_test_base.cc`. For test classes used across multiple |
| 51 | directories, it might make sense to move them into a nested `test` namespace for |
| 52 | clarity. |
Dominic Farolino | f3d0307 | 2021-07-20 15:55:54 | [diff] [blame] | 53 | * Despite the Google C++ style guide |
| 54 | [deprecating](https://google.github.io/styleguide/cppguide.html#File_Names) |
| 55 | the `_unittest.cc` suffix for unit test files, in Chromium we still use this |
| 56 | suffix to distinguish unit tests from browser tests, which are written in |
| 57 | files with the `_browsertest.cc` suffix. |
Tommy C. Li | 1a13764a | 2018-09-17 21:18:34 | [diff] [blame] | 58 | |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 59 | ## Code formatting |
| 60 | |
| 61 | * Put `*` and `&` by the type rather than the variable name. |
danakj | cd463f1 | 2021-02-12 19:53:13 | [diff] [blame] | 62 | * In class declarations, group function overrides together within each access |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 63 | control section, with one labeled group per parent class. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 64 | * Prefer `(foo == 0)` to `(0 == foo)`. |
| 65 | |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 66 | ## Unnamed namespaces |
| 67 | |
| 68 | Items local to a .cc file should be wrapped in an unnamed namespace. While some |
| 69 | such items are already file-scope by default in C++, not all are; also, shared |
| 70 | objects on Linux builds export all symbols, so unnamed namespaces (which |
| 71 | restrict these symbols to the compilation unit) improve function call cost and |
| 72 | reduce the size of entry point tables. |
| 73 | |
| 74 | ## Exporting symbols |
| 75 | |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 76 | Symbols can be exported (made visible outside of a shared library/DLL) by |
| 77 | annotating with a `<COMPONENT>_EXPORT` macro name (where `<COMPONENT>` is the |
| 78 | name of the component being built, e.g. BASE, NET, CONTENT, etc.). Class |
| 79 | annotations should precede the class name: |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 80 | ```c++ |
| 81 | class FOO_EXPORT Foo { |
| 82 | void Bar(); |
| 83 | void Baz(); |
| 84 | // ... |
| 85 | }; |
| 86 | ``` |
| 87 | |
| 88 | Function annotations should precede the return type: |
| 89 | ```c++ |
| 90 | class FooSingleton { |
| 91 | FOO_EXPORT Foo& GetFoo(); |
Jeremy Roman | 31220288 | 2019-02-19 21:43:50 | [diff] [blame] | 92 | FOO_EXPORT Foo& SetFooForTesting(Foo* foo); |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 93 | void SetFoo(Foo* foo); // Not exported. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 94 | }; |
| 95 | ``` |
| 96 | |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 97 | ## Multiple inheritance |
| 98 | |
| 99 | Multiple inheritance and virtual inheritance are permitted in Chromium code, |
| 100 | but discouraged (beyond the "interface" style of inheritance allowed by the |
| 101 | Google style guide, for which we do not require classes to have the "Interface" |
| 102 | suffix). Consider whether composition could solve the problem instead. |
| 103 | |
| 104 | ## Inline functions |
| 105 | |
| 106 | Simple accessors should generally be the only inline functions. These should be |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 107 | named using `snake_case()`. Virtual functions should never be declared this way. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 108 | |
| 109 | ## Logging |
| 110 | |
| 111 | Remove most logging calls before checking in. Unless you're adding temporary |
| 112 | logging to track down a specific bug, and you have a plan for how to collect |
| 113 | the logged data from user machines, you should generally not add logging |
| 114 | statements. |
| 115 | |
| 116 | For the rare case when logging needs to stay in the codebase for a while, |
| 117 | prefer `DVLOG(1)` to other logging methods. This avoids bloating the release |
| 118 | executable and in debug can be selectively enabled at runtime by command-line |
| 119 | arguments: |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 120 | * `--v=n` sets the global log level to n (default 0). All log statements with |
| 121 | a log level less than or equal to the global level will be printed. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 122 | * `--vmodule=mod=n[,mod=n,...]` overrides the global log level for the module |
| 123 | mod. Supplying the string foo for mod will affect all files named foo.cc, |
| 124 | while supplying a wildcard like `*bar/baz*` will affect all files with |
| 125 | `bar/baz` in their full pathnames. |
| 126 | |
| 127 | ## Platform-specific code |
| 128 | |
| 129 | To `#ifdef` code for specific platforms, use the macros defined in |
| 130 | `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] | 131 | set by specific compilers or build environments (e.g. `WIN32`). |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 132 | |
| 133 | Place platform-specific #includes in their own section below the "normal" |
| 134 | `#includes`. Repeat the standard `#include` order within this section: |
| 135 | |
| 136 | ```c++ |
| 137 | #include "foo/foo.h" |
| 138 | |
| 139 | #include <stdint.h> |
| 140 | #include <algorithm> |
| 141 | |
| 142 | #include "base/strings/utf_string_conversions.h" |
| 143 | #include "chrome/common/render_messages.h" |
| 144 | |
| 145 | #if defined(OS_WIN) |
| 146 | #include <windows.h> |
Robert Liao | c7c9a1c | 2017-10-18 01:41:31 | [diff] [blame] | 147 | #include "base/win/com_init_util.h" |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 148 | #elif defined(OS_POSIX) |
| 149 | #include "base/posix/global_descriptors.h" |
| 150 | #endif |
| 151 | ``` |
| 152 | |
| 153 | ## Types |
| 154 | |
| 155 | * Use `size_t` for object and allocation sizes, object counts, array and |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 156 | pointer offsets, vector indices, and so on. This prevents casts when |
| 157 | dealing with STL APIs, and if followed consistently across the codebase, |
| 158 | minimizes casts elsewhere. |
| 159 | * Occasionally classes may have a good reason to use a type other than |
| 160 | `size_t` for one of these concepts, e.g. as a storage space optimization. In |
| 161 | these cases, continue to use `size_t` in public-facing function |
| 162 | declarations, and continue to use unsigned types internally (e.g. |
| 163 | `uint32_t`). |
Dana Fried | 74a1889 | 2018-09-13 19:10:01 | [diff] [blame] | 164 | * Follow [Google C++ casting |
| 165 | conventions](https://google.github.io/styleguide/cppguide.html#Casting) |
| 166 | to convert arithmetic types when you know the conversion is safe. Use |
Chris Palmer | 8218b857 | 2019-02-26 00:19:16 | [diff] [blame] | 167 | `checked_cast<T>` (from `base/numerics/safe_conversions.h`) when you need to |
| 168 | `CHECK` that the source value is in range for the destination type. Use |
| 169 | `saturated_cast<T>` if you instead wish to clamp out-of-range values. |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 170 | `CheckedNumeric` is an ergonomic way to perform safe arithmetic and casting |
| 171 | in many cases. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 172 | * When passing values across network or process boundaries, use |
| 173 | explicitly-sized types for safety, since the sending and receiving ends may |
Chris Palmer | 8218b857 | 2019-02-26 00:19:16 | [diff] [blame] | 174 | not have been compiled with the same sizes for things like `int` and |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 175 | `size_t`. However, to the greatest degree possible, avoid letting these |
| 176 | sized types bleed through the APIs of the layers in question. |
Peter Kasting | 9e210a52 | 2021-03-20 15:45:42 | [diff] [blame] | 177 | * The Google Style Guide [bans |
| 178 | UTF-16](https://google.github.io/styleguide/cppguide.html#Non-ASCII_Characters). |
| 179 | For various reasons, Chromium uses UTF-16 extensively. Use `std::u16string` |
| 180 | and `char16_t*` for 16-bit strings, `u"..."` to declare UTF-16 literals, and |
| 181 | either the actual characters or the `\uXXXX` or `\UXXXXXXXX` escapes for |
| 182 | Unicode characters. Avoid `\xXX...`-style escapes, which can cause subtle |
| 183 | problems if someone attempts to change the type of string that holds the |
| 184 | literal. In code used only on Windows, it may be necessary to use |
| 185 | `std::wstring` and `wchar_t*`; these are legal, but note that they are |
| 186 | distinct types and are often not 16-bit on other platforms. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 187 | |
| 188 | ## Object ownership and calling conventions |
| 189 | |
| 190 | When functions need to take raw or smart pointers as parameters, use the |
| 191 | following conventions. Here we refer to the parameter type as `T` and name as |
| 192 | `t`. |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 193 | * If the function does not modify `t`'s ownership, declare the param as `T*`. |
| 194 | The caller is expected to ensure `t` stays alive as long as necessary, |
| 195 | generally through the duration of the call. Exception: In rare cases (e.g. |
| 196 | using lambdas with STL algorithms over containers of `unique_ptr<>`s), you |
| 197 | may be forced to declare the param as `const std::unique_ptr<T>&`. Do this |
| 198 | only when required. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 199 | * If the function takes ownership of a non-refcounted object, declare the |
| 200 | param as `std::unique_ptr<T>`. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 201 | * If the function (at least sometimes) takes a ref on a refcounted object, |
| 202 | declare the param as `scoped_refptr<T>`. The caller can decide |
| 203 | whether it wishes to transfer ownership (by calling `std::move(t)` when |
| 204 | passing `t`) or retain its ref (by simply passing t directly). |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 205 | * In short, functions should never take ownership of parameters passed as raw |
| 206 | pointers, and there should rarely be a need to pass smart pointers by const |
| 207 | ref. |
| 208 | |
Gabriel Charette | b62806f4a | 2019-01-29 21:08:41 | [diff] [blame] | 209 | Conventions for return values are similar with an important distinction: |
| 210 | * Return raw pointers if-and-only-if the caller does not take ownership. |
| 211 | * Return `std::unique_ptr<T>` or `scoped_refptr<T>` by value when the impl is |
| 212 | handing off ownership. |
| 213 | * **Distinction**: Return `const scoped_refptr<T>&` when the impl retains |
| 214 | ownership so the caller isn't required to take a ref: this avoids bumping |
| 215 | the reference count if the caller doesn't need ownership and also |
| 216 | [helps binary size](https://crrev.com/c/1435627)). |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 217 | |
| 218 | A great deal of Chromium code predates the above rules. In particular, some |
| 219 | functions take ownership of params passed as `T*`, or take `const |
| 220 | scoped_refptr<T>&` instead of `T*`, or return `T*` instead of |
| 221 | `scoped_refptr<T>` (to avoid refcount churn pre-C++11). Try to clean up such |
| 222 | code when you find it, or at least not make such usage any more widespread. |
| 223 | |
Lukasz Anforowicz | 269e41d | 2021-11-30 18:32:01 | [diff] [blame^] | 224 | ## Non-owning pointers in class fields |
| 225 | |
| 226 | Use `raw_ptr<T>` for class and struct fields in place of a raw C++ pointer `T*` |
| 227 | whenever possible, except in paths that include `/renderer/` or |
| 228 | `blink/public/web/`. `raw_ptr<T>` is a non-owning smart pointer that has |
| 229 | improved memory-safety over raw pointers, and can prevent exploitation of a |
| 230 | significant percentage of Use-after-Free bugs. |
| 231 | |
| 232 | Using `raw_ptr<T>` may not be possible in rare cases for |
| 233 | [performance reasons](../../base/memory/raw_ptr.md#Performance). |
| 234 | Additionally, `raw_ptr<T>` doesn’t support some C++ scenarios (e.g. `constexpr`, |
| 235 | ObjC pointers). Tooling will help to encourage use of `raw_ptr<T>`. See |
| 236 | [raw_ptr.md](../../base/memory/raw_ptr.md#When-to-use-raw_ptr_T) |
| 237 | for how to add exclusions. |
| 238 | |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 239 | ## Forward declarations vs. #includes |
| 240 | |
| 241 | Unlike the Google style guide, Chromium style prefers forward declarations to |
| 242 | `#includes` where possible. This can reduce compile times and result in fewer |
| 243 | files needing recompilation when a header changes. |
| 244 | |
| 245 | You can and should use forward declarations for most types passed or returned |
| 246 | by value, reference, or pointer, or types stored as pointer members or in most |
| 247 | STL containers. However, if it would otherwise make sense to use a type as a |
| 248 | member by-value, don't convert it to a pointer just to be able to |
| 249 | forward-declare the type. |
| 250 | |
| 251 | ## File headers |
| 252 | |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 253 | All files in Chromium start with a common license header. That header should |
| 254 | look like this: |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 255 | |
| 256 | ```c++ |
| 257 | // Copyright $YEAR The Chromium Authors. All rights reserved. |
| 258 | // Use of this source code is governed by a BSD-style license that can be |
| 259 | // found in the LICENSE file. |
| 260 | ``` |
| 261 | |
| 262 | Some important notes about this header: |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 263 | * There is no `(c)` after `Copyright`. |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 264 | * `$YEAR` should be set to the current year at the time a file is created, and |
| 265 | not changed thereafter. |
| 266 | * For files specific to Chromium OS, replace the word Chromium with the phrase |
| 267 | Chromium OS. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 268 | * If the style changes, don't bother to update existing files to comply with |
| 269 | the new style. For the same reason, don't just blindly copy an existing |
| 270 | file's header when creating a new file, since the existing file may use an |
| 271 | outdated style. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 272 | * The Chromium project hosts mirrors of some upstream open-source projects. |
| 273 | When contributing to these portions of the repository, retain the existing |
| 274 | file headers. |
| 275 | |
| 276 | Use standard `#include` guards in all header files (see the Google style guide |
| 277 | sections on these for the naming convention). Do not use `#pragma once`; |
| 278 | historically it was not supported on all platforms, and it does not seem to |
| 279 | outperform #include guards even on platforms which do support it. |
| 280 | |
| 281 | ## CHECK(), DCHECK(), and NOTREACHED() |
| 282 | |
| 283 | The `CHECK()` macro will cause an immediate crash if its condition is not met. |
| 284 | `DCHECK()` is like `CHECK()` but is only compiled in when `DCHECK_IS_ON` is true |
| 285 | (debug builds and some bot configurations, but not end-user builds). |
| 286 | `NOTREACHED()` is equivalent to `DCHECK(false)`. Here are some rules for using |
| 287 | these: |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 288 | * Use `DCHECK()` or `NOTREACHED()` as assertions, e.g. to document pre- and |
| 289 | post-conditions. A `DCHECK()` means "this condition must always be true", |
| 290 | not "this condition is normally true, but perhaps not in exceptional |
| 291 | cases." Things like disk corruption or strange network errors are examples |
| 292 | of exceptional circumstances that nevertheless should not result in |
| 293 | `DCHECK()` failure. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 294 | * A consequence of this is that you should not handle DCHECK() failures, even |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 295 | if failure would result in a crash. Attempting to handle a `DCHECK()` |
| 296 | failure is a statement that the `DCHECK()` can fail, which contradicts the |
| 297 | point of writing the `DCHECK()`. In particular, do not write code like the |
| 298 | following: |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 299 | ```c++ |
| 300 | DCHECK(foo); |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 301 | if (!foo) // Eliminate this code. |
| 302 | ... |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 303 | |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 304 | if (!bar) { // Replace this whole conditional with "DCHECK(bar);". |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 305 | NOTREACHED(); |
Peter Kasting | 5e04bd3 | 2019-05-21 21:44:10 | [diff] [blame] | 306 | return; |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 307 | } |
| 308 | ``` |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 309 | * Use `CHECK()` if the consequence of a failed assertion would be a security |
| 310 | vulnerability, where crashing the browser is preferable. Because this takes |
| 311 | down the whole browser, sometimes there are better options than `CHECK()`. |
| 312 | For example, if a renderer sends the browser process a malformed IPC, an |
| 313 | attacker may control the renderer, but we can simply kill the offending |
| 314 | renderer instead of crashing the whole browser. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 315 | * You can temporarily use `CHECK()` instead of `DCHECK()` when trying to |
| 316 | force crashes in release builds to sniff out which of your assertions is |
| 317 | failing. Don't leave these in the codebase forever; remove them or change |
| 318 | them back once you've solved the problem. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 319 | * Don't use these macros in tests, as they crash the test binary and leave |
| 320 | bots in a bad state. Use the `ASSERT_xx()` and `EXPECT_xx()` family of |
| 321 | macros, which report failures gracefully and can continue running other |
| 322 | tests. |
danakj | cd463f1 | 2021-02-12 19:53:13 | [diff] [blame] | 323 | * Dereferencing a null pointer in C++ is generally UB (undefined behavior) as |
| 324 | the compiler is free to assume a dereference means the pointer is not null |
| 325 | and may apply optimizations based on that. As such, there is sometimes a |
| 326 | strong opinion to `CHECK()` pointers before dereference. Chromium builds |
| 327 | with the `no-delete-null-pointer-checks` Clang/GCC flag which prevents such |
| 328 | optimizations, meaning the side effect of a null dereference would just be |
| 329 | the use of 0x0 which will lead to a crash on all the platforms Chromium |
| 330 | supports. As such we do not use `CHECK()` to guard pointer deferences. A |
| 331 | `DCHECK()` can be used to document that a pointer is never null, and doing |
| 332 | so as early as possible can help with debugging, though our styleguide now |
| 333 | recommends using a reference instead of a pointer when it cannot be null. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 334 | |
| 335 | ## Miscellany |
| 336 | |
| 337 | * Use UTF-8 file encodings and LF line endings. |
brettw | f0e606a5 | 2016-07-06 21:17:20 | [diff] [blame] | 338 | * Unit tests and performance tests should be placed in the same directory as |
| 339 | the functionality they're testing. |
Xiaohan Wang | cc517f08 | 2019-01-16 01:58:14 | [diff] [blame] | 340 | * The [C++ Dos and Don'ts](c++-dos-and-donts.md) page has more helpful |
| 341 | information. |