blob: cc43b26be9d440f0f5db24225c2bb36e1aa1ce46 [file] [log] [blame] [view]
Andrew Bonventre3c7fee32015-09-22 14:14:18 -04001# Coding Standard for the ANGLE Project
2
3## Google Style Guide
4
5We generally use the [Google C++ Style Guide]
6(http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml) as a basis for
7our Coding Standard, however we will deviate from it in a few areas, as noted
8below.
9
10Items marked {DEV} indicate a deviation from the Google guidelines. Items marked
11{DO} are reiterating points from the Google guidelines.
12
Jamie Madill5b130482016-07-15 10:59:53 -040013Before you upload code to Gerrit, use `git cl format` to auto-format your code.
14This will catch most of the trivial formatting errors and save you time.
15
Andrew Bonventre3c7fee32015-09-22 14:14:18 -040016### [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 Madill5b130482016-07-15 10:59:53 -040029* {DEV} Inherit (privately) from angle::NonCopyable helper class (defined in
30 common/angleutils.h) to disable default copy and assignment operators.
Andrew Bonventre3c7fee32015-09-22 14:14:18 -040031
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 Madill5b130482016-07-15 10:59:53 -040037* {DO} avoid use of default arguments.
Andrew Bonventre3c7fee32015-09-22 14:14:18 -040038* {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 Bonventre3c7fee32015-09-22 14:14:18 -040044
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 Madill5b130482016-07-15 10:59:53 -040053 files and headers.
54
55#### Directory Names
Andrew Bonventre3c7fee32015-09-22 14:14:18 -040056* 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
61Use the following guidelines, they do deviate somewhat from the Google
Jamie Madill5b130482016-07-15 10:59:53 -040062guidelines.
Shannon Woods21f76c22015-11-05 15:54:51 -050063
Shannon Woods64b88602015-11-05 14:27:38 -050064* class and type names: start with capital letter and use CamelCase.
Andrew Bonventre3c7fee32015-09-22 14:14:18 -040065* {DEV} class member variables: use an **`m`** prefix instead of trailing
Shannon Woods64b88602015-11-05 14:27:38 -050066underscore 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
70use CamelCase (chosen for consistency)
71* Constants: start with a **`k`** and use CamelCase
72* namespaces: use all lower case
Jamie Madill5b130482016-07-15 10:59:53 -040073* Enum Names - use class enums, and the values should be uppercase with underscores.
Shannon Woods64b88602015-11-05 14:27:38 -050074* macros: all uppercase with underscores
75* exceptions to naming: use common sense!
Andrew Bonventre3c7fee32015-09-22 14:14:18 -040076
77### [Comments](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Comments)
78
79* {DO} read and follow Google's recommendations.
Shannon Woods64b88602015-11-05 14:27:38 -050080* Each file **must** start with the following boilerplate notice:
Shannon Woods21f76c22015-11-05 15:54:51 -050081
Shannon Woods64b88602015-11-05 14:27:38 -050082```
83//
Jamie Madill5b130482016-07-15 10:59:53 -040084// Copyright (c) 2016 The ANGLE Project Authors. All rights reserved.
Shannon Woods64b88602015-11-05 14:27:38 -050085// Use of this source code is governed by a BSD-style license that can be
86// found in the LICENSE file.
87//
88```
Andrew Bonventre3c7fee32015-09-22 14:14:18 -040089
90### [Formatting](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Formatting)
91
Jamie Madill5b130482016-07-15 10:59:53 -040092* {DEV} Avoid excessively long lines. Please keep lines under 100 columns
Andrew Bonventre3c7fee32015-09-22 14:14:18 -040093 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 Madill5b130482016-07-15 10:59:53 -040099* switch statements: use the output of `git cl format`.
Andrew Bonventre3c7fee32015-09-22 14:14:18 -0400100* 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 Woods64b88602015-11-05 14:27:38 -0500109Examples:
Shannon Woods21f76c22015-11-05 15:54:51 -0500110
Shannon Woods64b88602015-11-05 14:27:38 -0500111```
112if (conditional)
113{
114 stuff();
115}
116else
117{
118 otherstuff()
119}
120```
Shannon Woods21f76c22015-11-05 15:54:51 -0500121
Shannon Woods64b88602015-11-05 14:27:38 -0500122```
123switch (conditional)
124{
125 case foo:
126 dostuff();
127 break;
128 case bar:
129 otherstuff();
130 break;
131 default:
132 WTFBBQ();
133}
134```
Shannon Woods21f76c22015-11-05 15:54:51 -0500135
Shannon Woods64b88602015-11-05 14:27:38 -0500136```
137class MyClass : public Foo
138{
139 public:
140 MyClass();
141 ~MyClass() {};
142 private:
143 DISALLOW_COPY_AND_ASSIGN(MyClass);
144};
145```
Shannon Woods21f76c22015-11-05 15:54:51 -0500146
Shannon Woods64b88602015-11-05 14:27:38 -0500147```
148char *c;
149const string &str;
150```
Andrew Bonventre3c7fee32015-09-22 14:14:18 -0400151
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.