xref: /aosp_15_r20/external/angle/doc/CodingStandard.md (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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*   {DO} avoid use of default arguments.
34*   {DONT} use C++ exceptions, they are disabled in the builds and not caught.
35*   {DO} use nullptr (instead of 0 or NULL) for pointers.
36*   {DO} use size\_t for loop iterators and size values.
37*   {DO} use uint8\_t pointers instead of void pointers to denote binary data.
38*   {DO} use C++11/14/17 according to the
39    [Chromium C++ features guide](https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++-features.md).
40
41### [Naming](https://google.github.io/styleguide/cppguide.html#Naming)
42
43#### File Names
44
45*   {DEV} Filenames should be all lowercase and can include underscores (`_`).
46    If the file is an implementation of a class, the filename may be capitalized
47    the same as the major class.
48*   {DEV} We use .cpp (instead of .cc), .h and .inl (inlined files) for C++
49    files and headers.
50
51#### Directory Names
52*   Directory names should be all lowercase, unless following an externally
53    imposed capitalization (eg include/EGL, or src/libGLESv2, etc)
54
55#### Variable Names
56
57Use the following guidelines, they do deviate somewhat from the [Google
58guidelines](https://google.github.io/styleguide/cppguide.html#Naming).
59
60* Class and type names: start with capital letter and use CamelCase.
61* {DEV} Class member variables: use an **`m`** prefix instead of trailing
62underscore and use CamelCase.
63* Global variables (if they must be used): use a **`g`** prefix.
64* {DEV} Variable names: start with lower case and use CamelCase (chosen for consistency)
65* {DEV} Function names: Member functions start with lower case and use CamelCase. Non-member and static member functions start with capital letter and
66use CamelCase (chosen for consistency)
67* {DO} Constants: start with a **`k`** and use CamelCase
68* Namespaces: short names. use all lower case
69* {DEV} Enum Names: use strongly typed class enums when possible. Use CamelCase for class enum members. See [official docs][EnumsOfficial].
70* Macros: all uppercase with underscores
71* Exceptions to naming: use common sense!
72
73[EnumsOfficial]: https://google.github.io/styleguide/cppguide.html#Enumerator_Names
74
75### [Comments](https://google.github.io/styleguide/cppguide.html#Comments)
76
77*   {DO} read and follow Google's recommendations.
78*   Each file **must** start with the following boilerplate notice:
79
80```
81//
82//  Copyright $YEAR The ANGLE Project Authors. All rights reserved.
83//  Use of this source code is governed by a BSD-style license that can be
84//  found in the LICENSE file.
85//
86```
87
88* $YEAR should be set to the current year at the time a file is created, and not changed thereafter.
89
90### [Formatting](https://google.github.io/styleguide/cppguide.html#Formatting)
91
92*   {DEV} Avoid excessively long lines. Please keep lines under 100 columns
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.
99*   switch statements: use the output of `git cl format`.
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
109Examples:
110
111```
112if (conditional)
113{
114    stuff();
115}
116else
117{
118    otherstuff()
119}
120```
121
122```
123switch (conditional)
124{
125  case foo:
126    dostuff();
127    break;
128  case bar:
129    otherstuff();
130    break;
131  default:
132    WTFBBQ();
133}
134```
135
136```
137class MyClass : public Foo
138{
139  public:
140    MyClass();
141    ~MyClass() {};
142  private:
143    DISALLOW_COPY_AND_ASSIGN(MyClass);
144};
145```
146
147```
148char *c;
149const string &str;
150```
151
152### [Exceptions to the Rules](https://google.github.io/styleguide/cppguide.html#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.
156
157### Generated Source Files
158
159Prefer storing generated sources as baked files in the repository. Avoid using
160GN actions to run Python scripts.
161
162**Definition:**
163
164Sometimes helper scripts can create compilable sources more easily from XML or
165JSON data sources than maintaining source files by hand. These scripts are often
166written in Python and output generated sources.
167
168**Decision**
169
170Storing generated sources in the repository makes integration easier for non-GN
171users. Python scripts can be expensive and slow to run at compile-time.
172Generated sources can be a pain point for messing up builds.
173
174It could be possible to solve the build clobbering problem. And we could replace
175Python with something faster. But to allow for easier integration with our tools
176and customers we should bake generated files into the repository.
177