This document describes coding conventions and formatting styles we use in Graphene. All newly commited code must conform to them to pass a review.
Note: Old code is temporarily excluded from these rules until reformatted.
To make formatting easier we've added an integration with clang-format
(currently only for C
code). You must install appropriate package from your distribution to use it. For Ubuntu 18.04 you
can setup it this way:
sudo apt-get install clang-format
Usage: (assuming you're in the project's top directory)
make format
This make
target reformats all source files in-place, so we recommend you first commit them
(or add to git index with git add
-A
), reformat and then verify reformatting results using git diff
(or git diff --cached
if you
used git add
).
We use a style derived (and slightly modified) from Google C++ Styleguide.
See our .clang-format config for precise rules.
Brace placement:
void f() {
if (a && b) {
something();
}
}
if-else
formatting:
C
if (x == y) {
...
} else if (x > y) {
...
} else {
...
}
Asterisks (*
) should be placed on the left, with the type. Multiple pointer declarations in one
line are disallowed. Example:
int* pointer;
int* another_pointer;
int non_pointer_a, non_pointer_b, non_pointer_c;
C
int many_args(int something_looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
int also_looooooong,
int c);
...
many_args(some_looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong_calculations,
many_args(123,
also_looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
789),
many_args(1, 2, 3));
if
, else
, do
, for
, while
, switch
and union
should be followed by a space.
Includes should be grouped and then sorted lexicographically. Groups should be separated using a single empty line. Groups:
.h
header for .c
files.curl.h
).nofpts
is bad,
points_cnt
is ok).#define
.Naming:
NAMED_THIS_WAY
.named_this_way
.g_
(e.g. g_thread_list
).Types:
size_t
.uint64_t
.goto
may be used only for error handling.
Yoda conditions (e.g. if (42 == x)
) or any
other similar constructions are not allowed.
Prefer sizeof(instance)
to sizeof(type)
, it’s less error-prone.
TODO