2015-09-22 21:14:18 +03:00
|
|
|
# Coding Standard for the ANGLE Project
|
|
|
|
|
|
|
|
## Google Style Guide
|
|
|
|
|
2018-09-05 19:44:19 +03:00
|
|
|
We generally use the [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) as a basis for
|
2015-09-22 21:14:18 +03:00
|
|
|
our Coding Standard, however we will deviate from it in a few areas, as noted
|
|
|
|
below.
|
|
|
|
|
|
|
|
Items marked {DEV} indicate a deviation from the Google guidelines. Items marked
|
|
|
|
{DO} are reiterating points from the Google guidelines.
|
|
|
|
|
2016-07-15 17:59:53 +03:00
|
|
|
Before you upload code to Gerrit, use `git cl format` to auto-format your code.
|
|
|
|
This will catch most of the trivial formatting errors and save you time.
|
|
|
|
|
2017-11-27 08:15:54 +03:00
|
|
|
### [Header Files](https://google.github.io/styleguide/cppguide.html#Header_Files)
|
2015-09-22 21:14:18 +03:00
|
|
|
|
2018-03-07 21:48:04 +03:00
|
|
|
* We use **`.h`** for C++ headers.
|
2015-09-22 21:14:18 +03:00
|
|
|
* {DEV} #define guards should be of the form: `<PATH>_<FILE>_H_`. (Compiler
|
|
|
|
codebase is varied, including `<PROJECT>_` makes the names excessively
|
|
|
|
long).
|
|
|
|
|
2017-11-27 08:15:54 +03:00
|
|
|
### [Scoping](https://google.github.io/styleguide/cppguide.html#Scoping)
|
2015-09-22 21:14:18 +03:00
|
|
|
|
|
|
|
* {DO} avoid globally scoped variables, unless absolutely necessary.
|
|
|
|
|
2017-11-27 08:15:54 +03:00
|
|
|
### [Classes](https://google.github.io/styleguide/cppguide.html#Classes)
|
2015-09-22 21:14:18 +03:00
|
|
|
|
2016-07-15 17:59:53 +03:00
|
|
|
* {DEV} Inherit (privately) from angle::NonCopyable helper class (defined in
|
|
|
|
common/angleutils.h) to disable default copy and assignment operators.
|
2015-09-22 21:14:18 +03:00
|
|
|
|
2017-11-27 08:15:54 +03:00
|
|
|
### [Other C++ Features](https://google.github.io/styleguide/cppguide.html#Other_C++_Features)
|
2015-09-22 21:14:18 +03:00
|
|
|
|
|
|
|
* {DEV} all parameters passed by reference, except for STL containers (e.g.
|
|
|
|
std::vector, std::list), must be labeled `const`. For return parameters
|
|
|
|
other than STL containers, use a pointer.
|
2016-07-15 17:59:53 +03:00
|
|
|
* {DO} avoid use of default arguments.
|
2015-09-22 21:14:18 +03:00
|
|
|
* {DONT} use C++ exceptions, they are disabled in the builds and not caught.
|
|
|
|
* {DO} use nullptr (instead of 0 or NULL) for pointers.
|
|
|
|
* {DO} use size\_t for loop iterators and size values.
|
|
|
|
* {DO} use uint8\_t pointers instead of void pointers to denote binary data.
|
2018-03-07 21:48:04 +03:00
|
|
|
* {DO} use C++11/14 according to the [Chromium c++ 11/14 guide]
|
2015-09-22 21:14:18 +03:00
|
|
|
(http://chromium-cpp.appspot.com/).
|
|
|
|
|
2017-11-27 08:15:54 +03:00
|
|
|
### [Naming](https://google.github.io/styleguide/cppguide.html#Naming)
|
2015-09-22 21:14:18 +03:00
|
|
|
|
|
|
|
#### File Names
|
|
|
|
|
|
|
|
* {DEV} Filenames should be all lowercase and can include underscores (`_`).
|
|
|
|
If the file is an implementation of a class, the filename may be capitalized
|
|
|
|
the same as the major class.
|
|
|
|
* {DEV} We use .cpp (instead of .cc), .h and .inl (inlined files) for C++
|
2016-07-15 17:59:53 +03:00
|
|
|
files and headers.
|
|
|
|
|
|
|
|
#### Directory Names
|
2015-09-22 21:14:18 +03:00
|
|
|
* Directory names should be all lowercase, unless following an externally
|
|
|
|
imposed capitalization (eg include/EGL, or src/libGLESv2, etc)
|
|
|
|
|
|
|
|
#### Variable Names
|
|
|
|
|
2018-09-05 19:32:56 +03:00
|
|
|
Use the following guidelines, they do deviate somewhat from the [Google
|
|
|
|
guidelines](https://google.github.io/styleguide/cppguide.html#Naming).
|
2015-11-05 23:54:51 +03:00
|
|
|
|
2018-09-05 19:32:56 +03:00
|
|
|
* Class and type names: start with capital letter and use CamelCase.
|
|
|
|
* {DEV} Class member variables: use an **`m`** prefix instead of trailing
|
2015-11-05 22:27:38 +03:00
|
|
|
underscore and use CamelCase.
|
2018-09-05 19:32:56 +03:00
|
|
|
* Global variables (if they must be used): use a **`g`** prefix.
|
|
|
|
* {DEV} Variable names: start with lower case and use CamelCase (chosen for consistency)
|
|
|
|
* {DEV} Function names: Member functions start with lower case and use CamelCase. Non-member and static member functions start with capital letter and
|
2015-11-05 22:27:38 +03:00
|
|
|
use CamelCase (chosen for consistency)
|
2018-09-05 19:32:56 +03:00
|
|
|
* {DO} Constants: start with a **`k`** and use CamelCase
|
|
|
|
* Namespaces: short names. use all lower case
|
|
|
|
* Enum Names: use class enums when possible. The values should be uppercase with underscores or CamelCase.
|
|
|
|
* Macros: all uppercase with underscores
|
|
|
|
* Exceptions to naming: use common sense!
|
2015-09-22 21:14:18 +03:00
|
|
|
|
2017-11-27 08:15:54 +03:00
|
|
|
### [Comments](https://google.github.io/styleguide/cppguide.html#Comments)
|
2015-09-22 21:14:18 +03:00
|
|
|
|
|
|
|
* {DO} read and follow Google's recommendations.
|
2015-11-05 22:27:38 +03:00
|
|
|
* Each file **must** start with the following boilerplate notice:
|
2015-11-05 23:54:51 +03:00
|
|
|
|
2015-11-05 22:27:38 +03:00
|
|
|
```
|
|
|
|
//
|
2018-03-07 21:48:04 +03:00
|
|
|
// Copyright $YEAR The ANGLE Project Authors. All rights reserved.
|
2015-11-05 22:27:38 +03:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
//
|
|
|
|
```
|
2015-09-22 21:14:18 +03:00
|
|
|
|
2018-03-07 21:48:04 +03:00
|
|
|
* $YEAR should be set to the current year at the time a file is created, and not changed thereafter.
|
|
|
|
|
2017-11-27 08:15:54 +03:00
|
|
|
### [Formatting](https://google.github.io/styleguide/cppguide.html#Formatting)
|
2015-09-22 21:14:18 +03:00
|
|
|
|
2016-07-15 17:59:53 +03:00
|
|
|
* {DEV} Avoid excessively long lines. Please keep lines under 100 columns
|
2015-09-22 21:14:18 +03:00
|
|
|
long.
|
|
|
|
* Use unix-style newlines.
|
|
|
|
* {DO} use only spaces. No tab characters. Configure your editor to emit
|
|
|
|
spaces when you hit the TAB-key.
|
|
|
|
* {DEV} indent 4 spaces at a time.
|
|
|
|
* conditionals: place space outside the parenthesis. No spaces inside.
|
2016-07-15 17:59:53 +03:00
|
|
|
* switch statements: use the output of `git cl format`.
|
2015-09-22 21:14:18 +03:00
|
|
|
* class format(eg private, public, protected): indent by 2 spaces. Regular
|
|
|
|
4-space indent from the outer scope for declarations/definitions.
|
|
|
|
* pointers and references: **`*`** and **`&`** tight against the variable
|
|
|
|
* namespaces: are not indented.
|
|
|
|
* extern code blocks: are not indented.
|
|
|
|
* {DEV} braces should go on a separate line, except for functions defined in a
|
|
|
|
header file where the whole function declaration and definition fit on one
|
|
|
|
line.
|
|
|
|
|
2015-11-05 22:27:38 +03:00
|
|
|
Examples:
|
2015-11-05 23:54:51 +03:00
|
|
|
|
2015-11-05 22:27:38 +03:00
|
|
|
```
|
|
|
|
if (conditional)
|
|
|
|
{
|
|
|
|
stuff();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
otherstuff()
|
|
|
|
}
|
|
|
|
```
|
2015-11-05 23:54:51 +03:00
|
|
|
|
2015-11-05 22:27:38 +03:00
|
|
|
```
|
|
|
|
switch (conditional)
|
|
|
|
{
|
|
|
|
case foo:
|
|
|
|
dostuff();
|
|
|
|
break;
|
|
|
|
case bar:
|
|
|
|
otherstuff();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WTFBBQ();
|
|
|
|
}
|
|
|
|
```
|
2015-11-05 23:54:51 +03:00
|
|
|
|
2015-11-05 22:27:38 +03:00
|
|
|
```
|
|
|
|
class MyClass : public Foo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyClass();
|
|
|
|
~MyClass() {};
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(MyClass);
|
|
|
|
};
|
|
|
|
```
|
2015-11-05 23:54:51 +03:00
|
|
|
|
2015-11-05 22:27:38 +03:00
|
|
|
```
|
|
|
|
char *c;
|
|
|
|
const string &str;
|
|
|
|
```
|
2015-09-22 21:14:18 +03:00
|
|
|
|
2017-11-27 08:15:54 +03:00
|
|
|
### [Exceptions to the Rules](https://google.github.io/styleguide/cppguide.html#Exceptions_to_the_Rules)
|
2015-09-22 21:14:18 +03:00
|
|
|
|
|
|
|
* If modifying pre-existing code that does not match the standard, the altered
|
|
|
|
portions of the code should be changed to match the standard.
|