Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 1 | # Coding Standard for the ANGLE Project |
| 2 | |
| 3 | ## Google Style Guide |
| 4 | |
| 5 | We generally use the [Google C++ Style Guide] |
| 6 | (http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml) as a basis for |
| 7 | our Coding Standard, however we will deviate from it in a few areas, as noted |
| 8 | below. |
| 9 | |
| 10 | Items marked {DEV} indicate a deviation from the Google guidelines. Items marked |
| 11 | {DO} are reiterating points from the Google guidelines. |
| 12 | |
| 13 | ### [Header Files](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Header_Files) |
| 14 | |
| 15 | * We will use **`.h`** for C++ headers. |
| 16 | * {DEV} #define guards should be of the form: `<PATH>_<FILE>_H_`. (Compiler |
| 17 | codebase is varied, including `<PROJECT>_` makes the names excessively |
| 18 | long). |
| 19 | |
| 20 | ### [Scoping](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Scoping) |
| 21 | |
| 22 | * {DO} avoid globally scoped variables, unless absolutely necessary. |
| 23 | |
| 24 | ### [Classes](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Classes) |
| 25 | |
| 26 | * {DO} disable Copy and Assignment constructors using the |
| 27 | DISALLOW\_COPY\_AND\_ASSIGN macro (defined in common/angleutils.h) in the |
| 28 | **private** section of a class: ``` class Foo { public: Foo(int f); ~Foo(); |
| 29 | |
| 30 | private: DISALLOW_COPY_AND_ASSIGN(Foo); }; ``` |
| 31 | |
| 32 | ### [Other C++ Features](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Other_C++_Features) |
| 33 | |
| 34 | * {DEV} all parameters passed by reference, except for STL containers (e.g. |
| 35 | std::vector, std::list), must be labeled `const`. For return parameters |
| 36 | other than STL containers, use a pointer. |
| 37 | * {DO} avoid use of default arguments. exception: for functions emulating |
| 38 | variadic arguments, they are allowed. |
| 39 | * {DONT} use C++ exceptions, they are disabled in the builds and not caught. |
| 40 | * {DO} use nullptr (instead of 0 or NULL) for pointers. |
| 41 | * {DO} use size\_t for loop iterators and size values. |
| 42 | * {DO} use uint8\_t pointers instead of void pointers to denote binary data. |
| 43 | * {DO} use C++11 according to the [Chromium guide on C++11] |
| 44 | (http://chromium-cpp.appspot.com/). |
| 45 | * {DEV} we permit C++11 STL classes inside the D3D Renderers, since D3D is |
| 46 | only supported on MSVS. |
| 47 | |
| 48 | ### [Naming ](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Naming) |
| 49 | |
| 50 | #### File Names |
| 51 | |
| 52 | * {DEV} Filenames should be all lowercase and can include underscores (`_`). |
| 53 | If the file is an implementation of a class, the filename may be capitalized |
| 54 | the same as the major class. |
| 55 | * {DEV} We use .cpp (instead of .cc), .h and .inl (inlined files) for C++ |
| 56 | files and headers. #### Directory Names |
| 57 | * Directory names should be all lowercase, unless following an externally |
| 58 | imposed capitalization (eg include/EGL, or src/libGLESv2, etc) |
| 59 | |
| 60 | #### Variable Names |
| 61 | |
| 62 | Use the following guidelines, they do deviate somewhat from the Google |
Shannon Woods | 64b8860 | 2015-11-05 14:27:38 -0500 | [diff] [blame^] | 63 | guidelines. |
| 64 | * class and type names: start with capital letter and use CamelCase. |
Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 65 | * {DEV} class member variables: use an **`m`** prefix instead of trailing |
Shannon Woods | 64b8860 | 2015-11-05 14:27:38 -0500 | [diff] [blame^] | 66 | underscore and use CamelCase. |
| 67 | * global variables (if they must be used): use a **`g_`** prefix. |
| 68 | * {DEV} variable names: start with lower case and use CamelCase (chosen for consistency) |
| 69 | * {DEV} function names: Member functions start with lower case and use CamelCase. Non-member functions start with capital letter and |
| 70 | use CamelCase (chosen for consistency) |
| 71 | * Constants: start with a **`k`** and use CamelCase |
| 72 | * namespaces: use all lower case |
| 73 | * Enumerator Names - follow constants |
| 74 | * macros: all uppercase with underscores |
| 75 | * exceptions to naming: use common sense! |
Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 76 | |
| 77 | ### [Comments](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Comments) |
| 78 | |
| 79 | * {DO} read and follow Google's recommendations. |
Shannon Woods | 64b8860 | 2015-11-05 14:27:38 -0500 | [diff] [blame^] | 80 | * Each file **must** start with the following boilerplate notice: |
| 81 | ``` |
| 82 | // |
| 83 | // Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved. |
| 84 | // Use of this source code is governed by a BSD-style license that can be |
| 85 | // found in the LICENSE file. |
| 86 | // |
| 87 | ``` |
Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 88 | |
| 89 | ### [Formatting](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Formatting) |
| 90 | |
| 91 | * {DEV} Avoid excessively long lines. Please keep lines under 120 columns |
| 92 | long. |
| 93 | * Use unix-style newlines. |
| 94 | * {DO} use only spaces. No tab characters. Configure your editor to emit |
| 95 | spaces when you hit the TAB-key. |
| 96 | * {DEV} indent 4 spaces at a time. |
| 97 | * conditionals: place space outside the parenthesis. No spaces inside. |
| 98 | * switch statements: indent the case statements by 2 spaces. The body of the |
| 99 | case statements should be intended another 2 spaces so that they are a full |
| 100 | 4-space indent from the switch. |
| 101 | * class format(eg private, public, protected): indent by 2 spaces. Regular |
| 102 | 4-space indent from the outer scope for declarations/definitions. |
| 103 | * pointers and references: **`*`** and **`&`** tight against the variable |
| 104 | * namespaces: are not indented. |
| 105 | * extern code blocks: are not indented. |
| 106 | * {DEV} braces should go on a separate line, except for functions defined in a |
| 107 | header file where the whole function declaration and definition fit on one |
| 108 | line. |
| 109 | |
Shannon Woods | 64b8860 | 2015-11-05 14:27:38 -0500 | [diff] [blame^] | 110 | Examples: |
| 111 | ``` |
| 112 | if (conditional) |
| 113 | { |
| 114 | stuff(); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | otherstuff() |
| 119 | } |
| 120 | ``` |
| 121 | ``` |
| 122 | switch (conditional) |
| 123 | { |
| 124 | case foo: |
| 125 | dostuff(); |
| 126 | break; |
| 127 | case bar: |
| 128 | otherstuff(); |
| 129 | break; |
| 130 | default: |
| 131 | WTFBBQ(); |
| 132 | } |
| 133 | ``` |
| 134 | ``` |
| 135 | class MyClass : public Foo |
| 136 | { |
| 137 | public: |
| 138 | MyClass(); |
| 139 | ~MyClass() {}; |
| 140 | private: |
| 141 | DISALLOW_COPY_AND_ASSIGN(MyClass); |
| 142 | }; |
| 143 | ``` |
| 144 | ``` |
| 145 | char *c; |
| 146 | const string &str; |
| 147 | ``` |
Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 148 | |
| 149 | ### [Exceptions to the Rules](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Exceptions_to_the_Rules) |
| 150 | |
| 151 | * If modifying pre-existing code that does not match the standard, the altered |
| 152 | portions of the code should be changed to match the standard. |
| 153 | * For changes which fix bugs, we do not require that pre-existing style issues |
| 154 | be addressed in the change itself, but reviewers may request a follow-on CL |
| 155 | to address style inconsistencies. This exception does not apply to changes |
| 156 | which implement new features, perform refactoring, or introduce a style |
| 157 | issue or inconsistency themselves. |