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 | |
Jamie Madill | 5b13048 | 2016-07-15 10:59:53 -0400 | [diff] [blame] | 13 | Before you upload code to Gerrit, use `git cl format` to auto-format your code. |
| 14 | This will catch most of the trivial formatting errors and save you time. |
| 15 | |
Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 16 | ### [Header Files](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Header_Files) |
| 17 | |
| 18 | * We will use **`.h`** for C++ headers. |
| 19 | * {DEV} #define guards should be of the form: `<PATH>_<FILE>_H_`. (Compiler |
| 20 | codebase is varied, including `<PROJECT>_` makes the names excessively |
| 21 | long). |
| 22 | |
| 23 | ### [Scoping](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Scoping) |
| 24 | |
| 25 | * {DO} avoid globally scoped variables, unless absolutely necessary. |
| 26 | |
| 27 | ### [Classes](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Classes) |
| 28 | |
Jamie Madill | 5b13048 | 2016-07-15 10:59:53 -0400 | [diff] [blame] | 29 | * {DEV} Inherit (privately) from angle::NonCopyable helper class (defined in |
| 30 | common/angleutils.h) to disable default copy and assignment operators. |
Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 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. |
Jamie Madill | 5b13048 | 2016-07-15 10:59:53 -0400 | [diff] [blame] | 37 | * {DO} avoid use of default arguments. |
Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 38 | * {DONT} use C++ exceptions, they are disabled in the builds and not caught. |
| 39 | * {DO} use nullptr (instead of 0 or NULL) for pointers. |
| 40 | * {DO} use size\_t for loop iterators and size values. |
| 41 | * {DO} use uint8\_t pointers instead of void pointers to denote binary data. |
| 42 | * {DO} use C++11 according to the [Chromium guide on C++11] |
| 43 | (http://chromium-cpp.appspot.com/). |
Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 44 | |
| 45 | ### [Naming ](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Naming) |
| 46 | |
| 47 | #### File Names |
| 48 | |
| 49 | * {DEV} Filenames should be all lowercase and can include underscores (`_`). |
| 50 | If the file is an implementation of a class, the filename may be capitalized |
| 51 | the same as the major class. |
| 52 | * {DEV} We use .cpp (instead of .cc), .h and .inl (inlined files) for C++ |
Jamie Madill | 5b13048 | 2016-07-15 10:59:53 -0400 | [diff] [blame] | 53 | files and headers. |
| 54 | |
| 55 | #### Directory Names |
Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 56 | * Directory names should be all lowercase, unless following an externally |
| 57 | imposed capitalization (eg include/EGL, or src/libGLESv2, etc) |
| 58 | |
| 59 | #### Variable Names |
| 60 | |
| 61 | Use the following guidelines, they do deviate somewhat from the Google |
Jamie Madill | 5b13048 | 2016-07-15 10:59:53 -0400 | [diff] [blame] | 62 | guidelines. |
Shannon Woods | 21f76c2 | 2015-11-05 15:54:51 -0500 | [diff] [blame] | 63 | |
Shannon Woods | 64b8860 | 2015-11-05 14:27:38 -0500 | [diff] [blame] | 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 |
Jamie Madill | 5b13048 | 2016-07-15 10:59:53 -0400 | [diff] [blame] | 73 | * Enum Names - use class enums, and the values should be uppercase with underscores. |
Shannon Woods | 64b8860 | 2015-11-05 14:27:38 -0500 | [diff] [blame] | 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: |
Shannon Woods | 21f76c2 | 2015-11-05 15:54:51 -0500 | [diff] [blame] | 81 | |
Shannon Woods | 64b8860 | 2015-11-05 14:27:38 -0500 | [diff] [blame] | 82 | ``` |
| 83 | // |
Jamie Madill | 5b13048 | 2016-07-15 10:59:53 -0400 | [diff] [blame] | 84 | // Copyright (c) 2016 The ANGLE Project Authors. All rights reserved. |
Shannon Woods | 64b8860 | 2015-11-05 14:27:38 -0500 | [diff] [blame] | 85 | // Use of this source code is governed by a BSD-style license that can be |
| 86 | // found in the LICENSE file. |
| 87 | // |
| 88 | ``` |
Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 89 | |
| 90 | ### [Formatting](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Formatting) |
| 91 | |
Jamie Madill | 5b13048 | 2016-07-15 10:59:53 -0400 | [diff] [blame] | 92 | * {DEV} Avoid excessively long lines. Please keep lines under 100 columns |
Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 93 | long. |
| 94 | * Use unix-style newlines. |
| 95 | * {DO} use only spaces. No tab characters. Configure your editor to emit |
| 96 | spaces when you hit the TAB-key. |
| 97 | * {DEV} indent 4 spaces at a time. |
| 98 | * conditionals: place space outside the parenthesis. No spaces inside. |
Jamie Madill | 5b13048 | 2016-07-15 10:59:53 -0400 | [diff] [blame] | 99 | * switch statements: use the output of `git cl format`. |
Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 100 | * class format(eg private, public, protected): indent by 2 spaces. Regular |
| 101 | 4-space indent from the outer scope for declarations/definitions. |
| 102 | * pointers and references: **`*`** and **`&`** tight against the variable |
| 103 | * namespaces: are not indented. |
| 104 | * extern code blocks: are not indented. |
| 105 | * {DEV} braces should go on a separate line, except for functions defined in a |
| 106 | header file where the whole function declaration and definition fit on one |
| 107 | line. |
| 108 | |
Shannon Woods | 64b8860 | 2015-11-05 14:27:38 -0500 | [diff] [blame] | 109 | Examples: |
Shannon Woods | 21f76c2 | 2015-11-05 15:54:51 -0500 | [diff] [blame] | 110 | |
Shannon Woods | 64b8860 | 2015-11-05 14:27:38 -0500 | [diff] [blame] | 111 | ``` |
| 112 | if (conditional) |
| 113 | { |
| 114 | stuff(); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | otherstuff() |
| 119 | } |
| 120 | ``` |
Shannon Woods | 21f76c2 | 2015-11-05 15:54:51 -0500 | [diff] [blame] | 121 | |
Shannon Woods | 64b8860 | 2015-11-05 14:27:38 -0500 | [diff] [blame] | 122 | ``` |
| 123 | switch (conditional) |
| 124 | { |
| 125 | case foo: |
| 126 | dostuff(); |
| 127 | break; |
| 128 | case bar: |
| 129 | otherstuff(); |
| 130 | break; |
| 131 | default: |
| 132 | WTFBBQ(); |
| 133 | } |
| 134 | ``` |
Shannon Woods | 21f76c2 | 2015-11-05 15:54:51 -0500 | [diff] [blame] | 135 | |
Shannon Woods | 64b8860 | 2015-11-05 14:27:38 -0500 | [diff] [blame] | 136 | ``` |
| 137 | class MyClass : public Foo |
| 138 | { |
| 139 | public: |
| 140 | MyClass(); |
| 141 | ~MyClass() {}; |
| 142 | private: |
| 143 | DISALLOW_COPY_AND_ASSIGN(MyClass); |
| 144 | }; |
| 145 | ``` |
Shannon Woods | 21f76c2 | 2015-11-05 15:54:51 -0500 | [diff] [blame] | 146 | |
Shannon Woods | 64b8860 | 2015-11-05 14:27:38 -0500 | [diff] [blame] | 147 | ``` |
| 148 | char *c; |
| 149 | const string &str; |
| 150 | ``` |
Andrew Bonventre | 3c7fee3 | 2015-09-22 14:14:18 -0400 | [diff] [blame] | 151 | |
| 152 | ### [Exceptions to the Rules](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Exceptions_to_the_Rules) |
| 153 | |
| 154 | * If modifying pre-existing code that does not match the standard, the altered |
| 155 | portions of the code should be changed to match the standard. |