• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Coding style
2
3- Indentation in tabs, 8 characters wide, spaces after the tabs where
4  vertical alignment is required (see below)
5
6**Note: this file uses spaces due to markdown rendering issues for tabs.
7  Code must be implemented using tabs.**
8
9- Max line width 80ch, do not break up printed strings though
10
11- Break up long lines at logical groupings, one line for each logical group
12
13```c
14int a = somelongname() +
15        someotherlongname();
16
17if (a < 0 &&
18    (b > 20 & d < 10) &&
19    d != 0.0)
20
21
22somelongfunctioncall(arg1,
23                     arg2,
24                     arg3);
25```
26
27- Function declarations: return type on separate line, {} on separate line,
28  arguments broken up as above.
29
30```c
31static inline int
32foobar(int a, int b)
33{
34
35}
36
37void
38somenamethatiswaytoolong(int a,
39                         int b,
40                         int c)
41{
42}
43```
44
45- `/* comments only */`, no `// comments`
46
47- `variable_name`, not `VariableName` or `variableName`. same for functions.
48
49- no typedefs of structs, enums, unions
50
51- if it generates a compiler warning, it needs to be fixed
52- if it generates a static checker warning, it needs to be fixed or
53  commented
54
55- declare variables at the top, try to keep them as local as possible.
56  Exception: if the same variable is re-used in multiple blocks, declare it
57  at the top.
58  Exception: basic loop variables, e.g. for (int i = 0; ...)
59
60```c
61int a;
62int c;
63
64if (foo) {
65        int b;
66
67        c = get_value();
68        usevalue(c);
69}
70
71if (bar) {
72        c = get_value();
73        useit(c);
74}
75```
76
77- do not mix function invocations and variable definitions.
78
79  wrong:
80
81```c
82{
83        int a = foo();
84        int b = 7;
85}
86```
87
88  right:
89```c
90{
91        int a;
92        int b = 7;
93
94        a = foo();
95}
96```
97
98  There are exceptions here, e.g. `tp_libinput_context()`,
99  `litest_current_device()`
100
101- if/else: { on the same line, no curly braces if both blocks are a single
102  statement. If either if or else block are multiple statements, both must
103  have curly braces.
104
105```c
106if (foo) {
107        blah();
108        bar();
109} else {
110        a = 10;
111}
112```
113
114- public functions MUST be doxygen-commented, use doxygen's `@foo` rather than
115  `\foo` notation
116
117- `#include "config.h"` comes first, followed by system headers, followed by
118  external library headers, followed by internal headers.
119  sort alphabetically where it makes sense (specifically system headers)
120
121```c
122#include "config.h"
123
124#include <stdio.h>
125#include <string.h>
126
127#include <libevdev/libevdev.h>
128
129#include "libinput-private.h"
130```
131
132- goto jumps only to the end of the function, and only for good reasons
133  (usually cleanup). goto never jumps backwards
134
135- Use stdbool.h's bool for booleans within the library (instead of `int`).
136  Exception: the public API uses int, not bool.
137
138# Git commit message requirements
139
140Our CI will check the commit messages for a few requirements. Below is the
141list of what we expect from a git commit.
142
143## Commit message content
144
145A [good commit message](http://who-t.blogspot.com/2009/12/on-commit-messages.html) needs to
146answer three questions:
147
148- Why is it necessary? It may fix a bug, it may add a feature, it may
149  improve performance, reliabilty, stability, or just be a change for the
150  sake of correctness.
151- How does it address the issue? For short obvious patches this part can be
152  omitted, but it should be a high level description of what the approach
153  was.
154- What effects does the patch have? (In addition to the obvious ones, this
155  may include benchmarks, side effects, etc.)
156
157These three questions establish the context for the actual code changes, put
158reviewers and others into the frame of mind to look at the diff and check if
159the approach chosen was correct. A good commit message also helps
160maintainers to decide if a given patch is suitable for stable branches or
161inclusion in a distribution.
162
163## Developer Certificate of Origin
164
165Your commit **must** be signed off with a line:
166```
167Signed-off-by: <your name> <your email address>
168```
169By signing off, you indicate the [developer certificate of origin](https://developercertificate.org/).
170
171> By making a contribution to this project, I certify that:
172>
173> (a) The contribution was created in whole or in part by me and I
174>     have the right to submit it under the open source license
175>     indicated in the file; or
176>
177> (b) The contribution is based upon previous work that, to the best
178>     of my knowledge, is covered under an appropriate open source
179>     license and I have the right under that license to submit that
180>     work with modifications, whether created in whole or in part
181>     by me, under the same open source license (unless I am
182>     permitted to submit under a different license), as indicated
183>     in the file; or
184>
185> (c) The contribution was provided directly to me by some other
186>     person who certified (a), (b) or (c) and I have not modified
187>     it.
188>
189> (d) I understand and agree that this project and the contribution
190>     are public and that a record of the contribution (including all
191>     personal information I submit with it, including my sign-off) is
192>     maintained indefinitely and may be redistributed consistent with
193>     this project or the open source license(s) involved.
194
195## Commit message format
196
197The canonical git commit message format is:
198
199```
200one line as the subject line with a high-level note
201
202full explanation of the patch follows after an empty line. This explanation
203can be multiple paragraphs and is largely free-form. Markdown is not
204supported.
205
206You can include extra data where required like:
207- benchmark one says 10s
208- benchmark two says 12s
209
210Signed-off-by: <your name> <your email>
211```
212
213The subject line is the first thing everyone sees about this commit, so make
214sure it's on point.
215
216## Commit message technical requirements
217
218- The commit message should use present tense (not past tense). Do write
219  "change foo to bar", not "changed foo to bar".
220- The text width of the commit should be 78 chars or less, especially the
221  subject line.
222- The author and signed-off-by must be your real name and email address. We
223  do not accept the default `@users.noreply` gitlab addresses.
224  ```
225  git config --global user.name Your Name
226  git config --global user.email your@email
227  ```
228