• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Coding Standard for the ANGLE Project
2
3## Google Style Guide
4
5We generally use the [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) as a basis for
6our Coding Standard, however we will deviate from it in a few areas, as noted
7below.
8
9Items marked {DEV} indicate a deviation from the Google guidelines. Items marked
10{DO} are reiterating points from the Google guidelines.
11
12Before you upload code to Gerrit, use `git cl format` to auto-format your code.
13This will catch most of the trivial formatting errors and save you time.
14
15### [Header Files](https://google.github.io/styleguide/cppguide.html#Header_Files)
16
17*   We use **`.h`** for C++ headers.
18*   {DEV} #define guards should be of the form: `<PATH>_<FILE>_H_`. (Compiler
19    codebase is varied, including `<PROJECT>_` makes the names excessively
20    long).
21
22### [Scoping](https://google.github.io/styleguide/cppguide.html#Scoping)
23
24*   {DO} avoid globally scoped variables, unless absolutely necessary.
25
26### [Classes](https://google.github.io/styleguide/cppguide.html#Classes)
27
28*   {DEV} Inherit (privately) from angle::NonCopyable helper class (defined in
29    common/angleutils.h) to disable default copy and assignment operators.
30
31### [Other C++ Features](https://google.github.io/styleguide/cppguide.html#Other_C++_Features)
32
33*   {DEV} all parameters passed by reference, except for STL containers (e.g.
34    std::vector, std::list), must be labeled `const`. For return parameters
35    other than STL containers, use a pointer.
36*   {DO} avoid use of default arguments.
37*   {DONT} use C++ exceptions, they are disabled in the builds and not caught.
38*   {DO} use nullptr (instead of 0 or NULL) for pointers.
39*   {DO} use size\_t for loop iterators and size values.
40*   {DO} use uint8\_t pointers instead of void pointers to denote binary data.
41*   {DO} use C++11/14 according to the [Chromium c++ 11/14 guide]
42    (http://chromium-cpp.appspot.com/).
43
44### [Naming](https://google.github.io/styleguide/cppguide.html#Naming)
45
46#### File Names
47
48*   {DEV} Filenames should be all lowercase and can include underscores (`_`).
49    If the file is an implementation of a class, the filename may be capitalized
50    the same as the major class.
51*   {DEV} We use .cpp (instead of .cc), .h and .inl (inlined files) for C++
52    files and headers.
53
54#### Directory Names
55*   Directory names should be all lowercase, unless following an externally
56    imposed capitalization (eg include/EGL, or src/libGLESv2, etc)
57
58#### Variable Names
59
60Use the following guidelines, they do deviate somewhat from the [Google
61guidelines](https://google.github.io/styleguide/cppguide.html#Naming).
62
63* Class and type names: start with capital letter and use CamelCase.
64* {DEV} Class member variables: use an **`m`** prefix instead of trailing
65underscore and use CamelCase.
66* Global variables (if they must be used): use a **`g`** prefix.
67* {DEV} Variable names: start with lower case and use CamelCase (chosen for consistency)
68* {DEV} Function names: Member functions start with lower case and use CamelCase. Non-member and static member functions start with capital letter and
69use CamelCase (chosen for consistency)
70* {DO} Constants: start with a **`k`** and use CamelCase
71* Namespaces: short names. use all lower case
72* Enum Names: use class enums when possible. The values should be uppercase with underscores or CamelCase.
73* Macros: all uppercase with underscores
74* Exceptions to naming: use common sense!
75
76### [Comments](https://google.github.io/styleguide/cppguide.html#Comments)
77
78*   {DO} read and follow Google's recommendations.
79*   Each file **must** start with the following boilerplate notice:
80
81```
82//
83//  Copyright $YEAR 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```
88
89* $YEAR should be set to the current year at the time a file is created, and not changed thereafter.
90
91### [Formatting](https://google.github.io/styleguide/cppguide.html#Formatting)
92
93*   {DEV} Avoid excessively long lines. Please keep lines under 100 columns
94    long.
95*   Use unix-style newlines.
96*   {DO} use only spaces. No tab characters. Configure your editor to emit
97    spaces when you hit the TAB-key.
98*   {DEV} indent 4 spaces at a time.
99*   conditionals: place space outside the parenthesis. No spaces inside.
100*   switch statements: use the output of `git cl format`.
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
110Examples:
111
112```
113if (conditional)
114{
115    stuff();
116}
117else
118{
119    otherstuff()
120}
121```
122
123```
124switch (conditional)
125{
126  case foo:
127    dostuff();
128    break;
129  case bar:
130    otherstuff();
131    break;
132  default:
133    WTFBBQ();
134}
135```
136
137```
138class MyClass : public Foo
139{
140  public:
141    MyClass();
142    ~MyClass() {};
143  private:
144    DISALLOW_COPY_AND_ASSIGN(MyClass);
145};
146```
147
148```
149char *c;
150const string &str;
151```
152
153### [Exceptions to the Rules](https://google.github.io/styleguide/cppguide.html#Exceptions_to_the_Rules)
154
155*   If modifying pre-existing code that does not match the standard, the altered
156    portions of the code should be changed to match the standard.
157
158### Generated Source Files
159
160Prefer storing generated sources as baked files in the repository. Avoid using
161GN actions to run Python scripts.
162
163**Definition:**
164
165Sometimes helper scripts can create compilable sources more easily from XML or
166JSON data sources than maintaining source files by hand. These scripts are often
167written in Python and output generated sources.
168
169**Decision**
170
171Storing generated sources in the repository makes integration easier for non-GN
172users. Python scripts can be expensive and slow to run at compile-time.
173Generated sources can be a pain point for messing up builds.
174
175It could be possible to solve the build clobbering problem. And we could replace
176Python with something faster. But to allow for easier integration with our tools
177and customers we should bake generated files into the repository.
178