2019-12-05 08:16:09 +10:00
|
|
|
# Coding style
|
2018-06-14 08:39:28 +10:00
|
|
|
|
2015-06-02 09:53:00 +10:00
|
|
|
- Indentation in tabs, 8 characters wide, spaces after the tabs where
|
|
|
|
|
vertical alignment is required (see below)
|
|
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
**Note: this file uses spaces due to markdown rendering issues for tabs.
|
|
|
|
|
Code must be implemented using tabs.**
|
|
|
|
|
|
2015-06-02 09:53:00 +10:00
|
|
|
- Max line width 80ch, do not break up printed strings though
|
|
|
|
|
|
|
|
|
|
- Break up long lines at logical groupings, one line for each logical group
|
|
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
```c
|
|
|
|
|
int a = somelongname() +
|
|
|
|
|
someotherlongname();
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
if (a < 0 &&
|
|
|
|
|
(b > 20 & d < 10) &&
|
|
|
|
|
d != 0.0)
|
2015-06-02 09:53:00 +10:00
|
|
|
|
|
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
somelongfunctioncall(arg1,
|
|
|
|
|
arg2,
|
|
|
|
|
arg3);
|
|
|
|
|
```
|
2015-06-02 09:53:00 +10:00
|
|
|
|
|
|
|
|
- Function declarations: return type on separate line, {} on separate line,
|
|
|
|
|
arguments broken up as above.
|
|
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
```c
|
|
|
|
|
static inline int
|
|
|
|
|
foobar(int a, int b)
|
|
|
|
|
{
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
}
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
void
|
|
|
|
|
somenamethatiswaytoolong(int a,
|
|
|
|
|
int b,
|
|
|
|
|
int c)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
```
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
- `/* comments only */`, no `// comments`
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
- `variable_name`, not `VariableName` or `variableName`. same for functions.
|
2015-06-02 09:53:00 +10:00
|
|
|
|
|
|
|
|
- no typedefs of structs, enums, unions
|
|
|
|
|
|
|
|
|
|
- if it generates a compiler warning, it needs to be fixed
|
|
|
|
|
- if it generates a static checker warning, it needs to be fixed or
|
|
|
|
|
commented
|
|
|
|
|
|
|
|
|
|
- declare variables at the top, try to keep them as local as possible.
|
|
|
|
|
Exception: if the same variable is re-used in multiple blocks, declare it
|
|
|
|
|
at the top.
|
2018-03-23 10:17:26 +10:00
|
|
|
Exception: basic loop variables, e.g. for (int i = 0; ...)
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
```c
|
|
|
|
|
int a;
|
|
|
|
|
int c;
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
if (foo) {
|
|
|
|
|
int b;
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
c = get_value();
|
|
|
|
|
usevalue(c);
|
|
|
|
|
}
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
if (bar) {
|
|
|
|
|
c = get_value();
|
|
|
|
|
useit(c);
|
|
|
|
|
}
|
|
|
|
|
```
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2015-07-08 08:38:11 +10:00
|
|
|
- do not mix function invocations and variable definitions.
|
|
|
|
|
|
|
|
|
|
wrong:
|
|
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
```c
|
|
|
|
|
{
|
|
|
|
|
int a = foo();
|
|
|
|
|
int b = 7;
|
|
|
|
|
}
|
|
|
|
|
```
|
2015-07-08 08:38:11 +10:00
|
|
|
|
|
|
|
|
right:
|
2018-06-14 08:39:28 +10:00
|
|
|
```c
|
|
|
|
|
{
|
|
|
|
|
int a;
|
|
|
|
|
int b = 7;
|
2015-07-08 08:38:11 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
a = foo();
|
|
|
|
|
}
|
|
|
|
|
```
|
2015-07-08 08:38:11 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
There are exceptions here, e.g. `tp_libinput_context()`,
|
|
|
|
|
`litest_current_device()`
|
2015-07-08 08:38:11 +10:00
|
|
|
|
2015-06-23 07:45:36 +10:00
|
|
|
- if/else: { on the same line, no curly braces if both blocks are a single
|
|
|
|
|
statement. If either if or else block are multiple statements, both must
|
|
|
|
|
have curly braces.
|
|
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
```c
|
|
|
|
|
if (foo) {
|
|
|
|
|
blah();
|
|
|
|
|
bar();
|
|
|
|
|
} else {
|
|
|
|
|
a = 10;
|
|
|
|
|
}
|
|
|
|
|
```
|
2015-06-23 07:45:36 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
- public functions MUST be doxygen-commented, use doxygen's `@foo` rather than
|
|
|
|
|
`\foo` notation
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
- `#include "config.h"` comes first, followed by system headers, followed by
|
2015-06-02 09:53:00 +10:00
|
|
|
external library headers, followed by internal headers.
|
|
|
|
|
sort alphabetically where it makes sense (specifically system headers)
|
|
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
```c
|
|
|
|
|
#include "config.h"
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
#include <libevdev/libevdev.h>
|
2015-06-02 09:53:00 +10:00
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
#include "libinput-private.h"
|
|
|
|
|
```
|
2015-07-08 08:38:11 +10:00
|
|
|
|
|
|
|
|
- goto jumps only to the end of the function, and only for good reasons
|
|
|
|
|
(usually cleanup). goto never jumps backwards
|
|
|
|
|
|
2018-06-14 08:39:28 +10:00
|
|
|
- Use stdbool.h's bool for booleans within the library (instead of `int`).
|
2015-07-08 08:38:11 +10:00
|
|
|
Exception: the public API uses int, not bool.
|
2019-12-05 08:16:09 +10:00
|
|
|
|
|
|
|
|
# Git commit message requirements
|
|
|
|
|
|
|
|
|
|
Our CI will check the commit messages for a few requirements. Below is the
|
|
|
|
|
list of what we expect from a git commit.
|
|
|
|
|
|
|
|
|
|
## Commit message content
|
|
|
|
|
|
|
|
|
|
A [good commit message](http://who-t.blogspot.com/2009/12/on-commit-messages.html) needs to
|
|
|
|
|
answer three questions:
|
|
|
|
|
|
|
|
|
|
- Why is it necessary? It may fix a bug, it may add a feature, it may
|
|
|
|
|
improve performance, reliabilty, stability, or just be a change for the
|
|
|
|
|
sake of correctness.
|
|
|
|
|
- How does it address the issue? For short obvious patches this part can be
|
|
|
|
|
omitted, but it should be a high level description of what the approach
|
|
|
|
|
was.
|
|
|
|
|
- What effects does the patch have? (In addition to the obvious ones, this
|
|
|
|
|
may include benchmarks, side effects, etc.)
|
|
|
|
|
|
|
|
|
|
These three questions establish the context for the actual code changes, put
|
|
|
|
|
reviewers and others into the frame of mind to look at the diff and check if
|
|
|
|
|
the approach chosen was correct. A good commit message also helps
|
|
|
|
|
maintainers to decide if a given patch is suitable for stable branches or
|
|
|
|
|
inclusion in a distribution.
|
|
|
|
|
|
|
|
|
|
## Developer Certificate of Origin
|
|
|
|
|
|
|
|
|
|
Your commit **must** be signed off with a line:
|
|
|
|
|
```
|
|
|
|
|
Signed-off-by: <your name> <your email address>
|
|
|
|
|
```
|
|
|
|
|
By signing off, you indicate the [developer certificate of origin](https://developercertificate.org/).
|
|
|
|
|
|
|
|
|
|
> By making a contribution to this project, I certify that:
|
|
|
|
|
>
|
|
|
|
|
> (a) The contribution was created in whole or in part by me and I
|
|
|
|
|
> have the right to submit it under the open source license
|
|
|
|
|
> indicated in the file; or
|
|
|
|
|
>
|
|
|
|
|
> (b) The contribution is based upon previous work that, to the best
|
|
|
|
|
> of my knowledge, is covered under an appropriate open source
|
|
|
|
|
> license and I have the right under that license to submit that
|
|
|
|
|
> work with modifications, whether created in whole or in part
|
|
|
|
|
> by me, under the same open source license (unless I am
|
|
|
|
|
> permitted to submit under a different license), as indicated
|
|
|
|
|
> in the file; or
|
|
|
|
|
>
|
|
|
|
|
> (c) The contribution was provided directly to me by some other
|
|
|
|
|
> person who certified (a), (b) or (c) and I have not modified
|
|
|
|
|
> it.
|
|
|
|
|
>
|
|
|
|
|
> (d) I understand and agree that this project and the contribution
|
|
|
|
|
> are public and that a record of the contribution (including all
|
|
|
|
|
> personal information I submit with it, including my sign-off) is
|
|
|
|
|
> maintained indefinitely and may be redistributed consistent with
|
|
|
|
|
> this project or the open source license(s) involved.
|
|
|
|
|
|
|
|
|
|
## Commit message format
|
|
|
|
|
|
|
|
|
|
The canonical git commit message format is:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
one line as the subject line with a high-level note
|
|
|
|
|
|
|
|
|
|
full explanation of the patch follows after an empty line. This explanation
|
|
|
|
|
can be multiple paragraphs and is largely free-form. Markdown is not
|
|
|
|
|
supported.
|
|
|
|
|
|
|
|
|
|
You can include extra data where required like:
|
|
|
|
|
- benchmark one says 10s
|
|
|
|
|
- benchmark two says 12s
|
|
|
|
|
|
|
|
|
|
Signed-off-by: <your name> <your email>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The subject line is the first thing everyone sees about this commit, so make
|
|
|
|
|
sure it's on point.
|
|
|
|
|
|
|
|
|
|
## Commit message technical requirements
|
|
|
|
|
|
|
|
|
|
- The commit message should use present tense (not past tense). Do write
|
|
|
|
|
"change foo to bar", not "changed foo to bar".
|
|
|
|
|
- The text width of the commit should be 78 chars or less, especially the
|
|
|
|
|
subject line.
|
|
|
|
|
- The author and signed-off-by must be your real name and email address. We
|
|
|
|
|
do not accept the default `@users.noreply` gitlab addresses.
|
|
|
|
|
```
|
|
|
|
|
git config --global user.name Your Name
|
|
|
|
|
git config --global user.email your@email
|
|
|
|
|
```
|