xref: /aosp_15_r20/external/angle/doc/CodingStandard.md (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker# Coding Standard for the ANGLE Project
2*8975f5c5SAndroid Build Coastguard Worker
3*8975f5c5SAndroid Build Coastguard Worker## Google Style Guide
4*8975f5c5SAndroid Build Coastguard Worker
5*8975f5c5SAndroid Build Coastguard WorkerWe generally use the [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) as a basis for
6*8975f5c5SAndroid Build Coastguard Workerour Coding Standard, however we will deviate from it in a few areas, as noted
7*8975f5c5SAndroid Build Coastguard Workerbelow.
8*8975f5c5SAndroid Build Coastguard Worker
9*8975f5c5SAndroid Build Coastguard WorkerItems marked {DEV} indicate a deviation from the Google guidelines. Items marked
10*8975f5c5SAndroid Build Coastguard Worker{DO} are reiterating points from the Google guidelines.
11*8975f5c5SAndroid Build Coastguard Worker
12*8975f5c5SAndroid Build Coastguard WorkerBefore you upload code to Gerrit, use `git cl format` to auto-format your code.
13*8975f5c5SAndroid Build Coastguard WorkerThis will catch most of the trivial formatting errors and save you time.
14*8975f5c5SAndroid Build Coastguard Worker
15*8975f5c5SAndroid Build Coastguard Worker### [Header Files](https://google.github.io/styleguide/cppguide.html#Header_Files)
16*8975f5c5SAndroid Build Coastguard Worker
17*8975f5c5SAndroid Build Coastguard Worker*   We use **`.h`** for C++ headers.
18*8975f5c5SAndroid Build Coastguard Worker*   {DEV} #define guards should be of the form: `<PATH>_<FILE>_H_`. (Compiler
19*8975f5c5SAndroid Build Coastguard Worker    codebase is varied, including `<PROJECT>_` makes the names excessively
20*8975f5c5SAndroid Build Coastguard Worker    long).
21*8975f5c5SAndroid Build Coastguard Worker
22*8975f5c5SAndroid Build Coastguard Worker### [Scoping](https://google.github.io/styleguide/cppguide.html#Scoping)
23*8975f5c5SAndroid Build Coastguard Worker
24*8975f5c5SAndroid Build Coastguard Worker*   {DO} avoid globally scoped variables, unless absolutely necessary.
25*8975f5c5SAndroid Build Coastguard Worker
26*8975f5c5SAndroid Build Coastguard Worker### [Classes](https://google.github.io/styleguide/cppguide.html#Classes)
27*8975f5c5SAndroid Build Coastguard Worker
28*8975f5c5SAndroid Build Coastguard Worker*   {DEV} Inherit (privately) from angle::NonCopyable helper class (defined in
29*8975f5c5SAndroid Build Coastguard Worker    common/angleutils.h) to disable default copy and assignment operators.
30*8975f5c5SAndroid Build Coastguard Worker
31*8975f5c5SAndroid Build Coastguard Worker### [Other C++ Features](https://google.github.io/styleguide/cppguide.html#Other_C++_Features)
32*8975f5c5SAndroid Build Coastguard Worker
33*8975f5c5SAndroid Build Coastguard Worker*   {DO} avoid use of default arguments.
34*8975f5c5SAndroid Build Coastguard Worker*   {DONT} use C++ exceptions, they are disabled in the builds and not caught.
35*8975f5c5SAndroid Build Coastguard Worker*   {DO} use nullptr (instead of 0 or NULL) for pointers.
36*8975f5c5SAndroid Build Coastguard Worker*   {DO} use size\_t for loop iterators and size values.
37*8975f5c5SAndroid Build Coastguard Worker*   {DO} use uint8\_t pointers instead of void pointers to denote binary data.
38*8975f5c5SAndroid Build Coastguard Worker*   {DO} use C++11/14/17 according to the
39*8975f5c5SAndroid Build Coastguard Worker    [Chromium C++ features guide](https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++-features.md).
40*8975f5c5SAndroid Build Coastguard Worker
41*8975f5c5SAndroid Build Coastguard Worker### [Naming](https://google.github.io/styleguide/cppguide.html#Naming)
42*8975f5c5SAndroid Build Coastguard Worker
43*8975f5c5SAndroid Build Coastguard Worker#### File Names
44*8975f5c5SAndroid Build Coastguard Worker
45*8975f5c5SAndroid Build Coastguard Worker*   {DEV} Filenames should be all lowercase and can include underscores (`_`).
46*8975f5c5SAndroid Build Coastguard Worker    If the file is an implementation of a class, the filename may be capitalized
47*8975f5c5SAndroid Build Coastguard Worker    the same as the major class.
48*8975f5c5SAndroid Build Coastguard Worker*   {DEV} We use .cpp (instead of .cc), .h and .inl (inlined files) for C++
49*8975f5c5SAndroid Build Coastguard Worker    files and headers.
50*8975f5c5SAndroid Build Coastguard Worker
51*8975f5c5SAndroid Build Coastguard Worker#### Directory Names
52*8975f5c5SAndroid Build Coastguard Worker*   Directory names should be all lowercase, unless following an externally
53*8975f5c5SAndroid Build Coastguard Worker    imposed capitalization (eg include/EGL, or src/libGLESv2, etc)
54*8975f5c5SAndroid Build Coastguard Worker
55*8975f5c5SAndroid Build Coastguard Worker#### Variable Names
56*8975f5c5SAndroid Build Coastguard Worker
57*8975f5c5SAndroid Build Coastguard WorkerUse the following guidelines, they do deviate somewhat from the [Google
58*8975f5c5SAndroid Build Coastguard Workerguidelines](https://google.github.io/styleguide/cppguide.html#Naming).
59*8975f5c5SAndroid Build Coastguard Worker
60*8975f5c5SAndroid Build Coastguard Worker* Class and type names: start with capital letter and use CamelCase.
61*8975f5c5SAndroid Build Coastguard Worker* {DEV} Class member variables: use an **`m`** prefix instead of trailing
62*8975f5c5SAndroid Build Coastguard Workerunderscore and use CamelCase.
63*8975f5c5SAndroid Build Coastguard Worker* Global variables (if they must be used): use a **`g`** prefix.
64*8975f5c5SAndroid Build Coastguard Worker* {DEV} Variable names: start with lower case and use CamelCase (chosen for consistency)
65*8975f5c5SAndroid Build Coastguard Worker* {DEV} Function names: Member functions start with lower case and use CamelCase. Non-member and static member functions start with capital letter and
66*8975f5c5SAndroid Build Coastguard Workeruse CamelCase (chosen for consistency)
67*8975f5c5SAndroid Build Coastguard Worker* {DO} Constants: start with a **`k`** and use CamelCase
68*8975f5c5SAndroid Build Coastguard Worker* Namespaces: short names. use all lower case
69*8975f5c5SAndroid Build Coastguard Worker* {DEV} Enum Names: use strongly typed class enums when possible. Use CamelCase for class enum members. See [official docs][EnumsOfficial].
70*8975f5c5SAndroid Build Coastguard Worker* Macros: all uppercase with underscores
71*8975f5c5SAndroid Build Coastguard Worker* Exceptions to naming: use common sense!
72*8975f5c5SAndroid Build Coastguard Worker
73*8975f5c5SAndroid Build Coastguard Worker[EnumsOfficial]: https://google.github.io/styleguide/cppguide.html#Enumerator_Names
74*8975f5c5SAndroid Build Coastguard Worker
75*8975f5c5SAndroid Build Coastguard Worker### [Comments](https://google.github.io/styleguide/cppguide.html#Comments)
76*8975f5c5SAndroid Build Coastguard Worker
77*8975f5c5SAndroid Build Coastguard Worker*   {DO} read and follow Google's recommendations.
78*8975f5c5SAndroid Build Coastguard Worker*   Each file **must** start with the following boilerplate notice:
79*8975f5c5SAndroid Build Coastguard Worker
80*8975f5c5SAndroid Build Coastguard Worker```
81*8975f5c5SAndroid Build Coastguard Worker//
82*8975f5c5SAndroid Build Coastguard Worker//  Copyright $YEAR The ANGLE Project Authors. All rights reserved.
83*8975f5c5SAndroid Build Coastguard Worker//  Use of this source code is governed by a BSD-style license that can be
84*8975f5c5SAndroid Build Coastguard Worker//  found in the LICENSE file.
85*8975f5c5SAndroid Build Coastguard Worker//
86*8975f5c5SAndroid Build Coastguard Worker```
87*8975f5c5SAndroid Build Coastguard Worker
88*8975f5c5SAndroid Build Coastguard Worker* $YEAR should be set to the current year at the time a file is created, and not changed thereafter.
89*8975f5c5SAndroid Build Coastguard Worker
90*8975f5c5SAndroid Build Coastguard Worker### [Formatting](https://google.github.io/styleguide/cppguide.html#Formatting)
91*8975f5c5SAndroid Build Coastguard Worker
92*8975f5c5SAndroid Build Coastguard Worker*   {DEV} Avoid excessively long lines. Please keep lines under 100 columns
93*8975f5c5SAndroid Build Coastguard Worker    long.
94*8975f5c5SAndroid Build Coastguard Worker*   Use unix-style newlines.
95*8975f5c5SAndroid Build Coastguard Worker*   {DO} use only spaces. No tab characters. Configure your editor to emit
96*8975f5c5SAndroid Build Coastguard Worker    spaces when you hit the TAB-key.
97*8975f5c5SAndroid Build Coastguard Worker*   {DEV} indent 4 spaces at a time.
98*8975f5c5SAndroid Build Coastguard Worker*   conditionals: place space outside the parenthesis. No spaces inside.
99*8975f5c5SAndroid Build Coastguard Worker*   switch statements: use the output of `git cl format`.
100*8975f5c5SAndroid Build Coastguard Worker*   class format(eg private, public, protected): indent by 2 spaces. Regular
101*8975f5c5SAndroid Build Coastguard Worker    4-space indent from the outer scope for declarations/definitions.
102*8975f5c5SAndroid Build Coastguard Worker*   pointers and references: **`*`** and **`&`** tight against the variable
103*8975f5c5SAndroid Build Coastguard Worker*   namespaces: are not indented.
104*8975f5c5SAndroid Build Coastguard Worker*   extern code blocks: are not indented.
105*8975f5c5SAndroid Build Coastguard Worker*   {DEV} braces should go on a separate line, except for functions defined in a
106*8975f5c5SAndroid Build Coastguard Worker    header file where the whole function declaration and definition fit on one
107*8975f5c5SAndroid Build Coastguard Worker    line.
108*8975f5c5SAndroid Build Coastguard Worker
109*8975f5c5SAndroid Build Coastguard WorkerExamples:
110*8975f5c5SAndroid Build Coastguard Worker
111*8975f5c5SAndroid Build Coastguard Worker```
112*8975f5c5SAndroid Build Coastguard Workerif (conditional)
113*8975f5c5SAndroid Build Coastguard Worker{
114*8975f5c5SAndroid Build Coastguard Worker    stuff();
115*8975f5c5SAndroid Build Coastguard Worker}
116*8975f5c5SAndroid Build Coastguard Workerelse
117*8975f5c5SAndroid Build Coastguard Worker{
118*8975f5c5SAndroid Build Coastguard Worker    otherstuff()
119*8975f5c5SAndroid Build Coastguard Worker}
120*8975f5c5SAndroid Build Coastguard Worker```
121*8975f5c5SAndroid Build Coastguard Worker
122*8975f5c5SAndroid Build Coastguard Worker```
123*8975f5c5SAndroid Build Coastguard Workerswitch (conditional)
124*8975f5c5SAndroid Build Coastguard Worker{
125*8975f5c5SAndroid Build Coastguard Worker  case foo:
126*8975f5c5SAndroid Build Coastguard Worker    dostuff();
127*8975f5c5SAndroid Build Coastguard Worker    break;
128*8975f5c5SAndroid Build Coastguard Worker  case bar:
129*8975f5c5SAndroid Build Coastguard Worker    otherstuff();
130*8975f5c5SAndroid Build Coastguard Worker    break;
131*8975f5c5SAndroid Build Coastguard Worker  default:
132*8975f5c5SAndroid Build Coastguard Worker    WTFBBQ();
133*8975f5c5SAndroid Build Coastguard Worker}
134*8975f5c5SAndroid Build Coastguard Worker```
135*8975f5c5SAndroid Build Coastguard Worker
136*8975f5c5SAndroid Build Coastguard Worker```
137*8975f5c5SAndroid Build Coastguard Workerclass MyClass : public Foo
138*8975f5c5SAndroid Build Coastguard Worker{
139*8975f5c5SAndroid Build Coastguard Worker  public:
140*8975f5c5SAndroid Build Coastguard Worker    MyClass();
141*8975f5c5SAndroid Build Coastguard Worker    ~MyClass() {};
142*8975f5c5SAndroid Build Coastguard Worker  private:
143*8975f5c5SAndroid Build Coastguard Worker    DISALLOW_COPY_AND_ASSIGN(MyClass);
144*8975f5c5SAndroid Build Coastguard Worker};
145*8975f5c5SAndroid Build Coastguard Worker```
146*8975f5c5SAndroid Build Coastguard Worker
147*8975f5c5SAndroid Build Coastguard Worker```
148*8975f5c5SAndroid Build Coastguard Workerchar *c;
149*8975f5c5SAndroid Build Coastguard Workerconst string &str;
150*8975f5c5SAndroid Build Coastguard Worker```
151*8975f5c5SAndroid Build Coastguard Worker
152*8975f5c5SAndroid Build Coastguard Worker### [Exceptions to the Rules](https://google.github.io/styleguide/cppguide.html#Exceptions_to_the_Rules)
153*8975f5c5SAndroid Build Coastguard Worker
154*8975f5c5SAndroid Build Coastguard Worker*   If modifying pre-existing code that does not match the standard, the altered
155*8975f5c5SAndroid Build Coastguard Worker    portions of the code should be changed to match the standard.
156*8975f5c5SAndroid Build Coastguard Worker
157*8975f5c5SAndroid Build Coastguard Worker### Generated Source Files
158*8975f5c5SAndroid Build Coastguard Worker
159*8975f5c5SAndroid Build Coastguard WorkerPrefer storing generated sources as baked files in the repository. Avoid using
160*8975f5c5SAndroid Build Coastguard WorkerGN actions to run Python scripts.
161*8975f5c5SAndroid Build Coastguard Worker
162*8975f5c5SAndroid Build Coastguard Worker**Definition:**
163*8975f5c5SAndroid Build Coastguard Worker
164*8975f5c5SAndroid Build Coastguard WorkerSometimes helper scripts can create compilable sources more easily from XML or
165*8975f5c5SAndroid Build Coastguard WorkerJSON data sources than maintaining source files by hand. These scripts are often
166*8975f5c5SAndroid Build Coastguard Workerwritten in Python and output generated sources.
167*8975f5c5SAndroid Build Coastguard Worker
168*8975f5c5SAndroid Build Coastguard Worker**Decision**
169*8975f5c5SAndroid Build Coastguard Worker
170*8975f5c5SAndroid Build Coastguard WorkerStoring generated sources in the repository makes integration easier for non-GN
171*8975f5c5SAndroid Build Coastguard Workerusers. Python scripts can be expensive and slow to run at compile-time.
172*8975f5c5SAndroid Build Coastguard WorkerGenerated sources can be a pain point for messing up builds.
173*8975f5c5SAndroid Build Coastguard Worker
174*8975f5c5SAndroid Build Coastguard WorkerIt could be possible to solve the build clobbering problem. And we could replace
175*8975f5c5SAndroid Build Coastguard WorkerPython with something faster. But to allow for easier integration with our tools
176*8975f5c5SAndroid Build Coastguard Workerand customers we should bake generated files into the repository.
177