xref: /aosp_15_r20/external/kmod/CODING-STYLE (revision cc4ad7da8cefe208cb129ac2aa9a357c7c72deb2)
1*cc4ad7daSAndroid Build Coastguard WorkerEvery project has its coding style, and kmod is not an exception. This
2*cc4ad7daSAndroid Build Coastguard Workerdocument describes the preferred coding style for kmod code, in order to keep
3*cc4ad7daSAndroid Build Coastguard Workersome level of consistency among developers so that code can be easily
4*cc4ad7daSAndroid Build Coastguard Workerunderstood and maintained, and also to help your code survive under
5*cc4ad7daSAndroid Build Coastguard Workermaintainer's fastidious eyes so that you can get a passport for your patch
6*cc4ad7daSAndroid Build Coastguard WorkerASAP.
7*cc4ad7daSAndroid Build Coastguard Worker
8*cc4ad7daSAndroid Build Coastguard WorkerFirst of all, kmod coding style must follow every rule for Linux kernel
9*cc4ad7daSAndroid Build Coastguard Worker(http://www.kernel.org/doc/Documentation/CodingStyle). There also exists a tool
10*cc4ad7daSAndroid Build Coastguard Workernamed checkpatch.pl to help you check the compliance with it. Just type
11*cc4ad7daSAndroid Build Coastguard Worker"checkpatch.pl --no-tree patch_name" to check your patch. In theory, you need
12*cc4ad7daSAndroid Build Coastguard Workerto clean up all the warnings and errors except this one: "ERROR: Missing
13*cc4ad7daSAndroid Build Coastguard WorkerSigned-off-by: line(s)". kmod does not used Signed-Off lines, so including
14*cc4ad7daSAndroid Build Coastguard Workerthem is actually an error.  In certain circumstances one can ignore the 80
15*cc4ad7daSAndroid Build Coastguard Workercharacter per line limit.  This is generally only allowed if the alternative
16*cc4ad7daSAndroid Build Coastguard Workerwould make the code even less readable.
17*cc4ad7daSAndroid Build Coastguard Worker
18*cc4ad7daSAndroid Build Coastguard WorkerBesides the kernel coding style above, kmod coding style is heavily based on
19*cc4ad7daSAndroid Build Coastguard WorkeroFono's and BlueZ's. Below some basic rules:
20*cc4ad7daSAndroid Build Coastguard Worker
21*cc4ad7daSAndroid Build Coastguard Worker1) Wrap line at 80 char limit.
22*cc4ad7daSAndroid Build Coastguard Worker
23*cc4ad7daSAndroid Build Coastguard WorkerThere are a few exceptions:
24*cc4ad7daSAndroid Build Coastguard Worker	- Headers may or may not wrap
25*cc4ad7daSAndroid Build Coastguard Worker	- If it's a string that is hitting the limit, it's preferred not to break
26*cc4ad7daSAndroid Build Coastguard Worker	  in order to be able to grep for that string. E.g:
27*cc4ad7daSAndroid Build Coastguard Worker
28*cc4ad7daSAndroid Build Coastguard Worker		err = my_function(ctx, "this is a long string that will pass the 80chr limit");
29*cc4ad7daSAndroid Build Coastguard Worker
30*cc4ad7daSAndroid Build Coastguard Worker	- If code would become unreadable if line is wrapped
31*cc4ad7daSAndroid Build Coastguard Worker	- If there's only one argument to the function, don't put it alone in a
32*cc4ad7daSAndroid Build Coastguard Worker	  new line.
33*cc4ad7daSAndroid Build Coastguard Worker
34*cc4ad7daSAndroid Build Coastguard WorkerAlign the wrapped line either with tabs (BlueZ, oFono, etc) or tab + spaces
35*cc4ad7daSAndroid Build Coastguard Worker(kernel), at your discretion. Kernel's is preferred.
36*cc4ad7daSAndroid Build Coastguard Worker
37*cc4ad7daSAndroid Build Coastguard Worker2) It's better to return/exit early in a function than having a really long
38*cc4ad7daSAndroid Build Coastguard Worker   "if (...) { }". Example:
39*cc4ad7daSAndroid Build Coastguard Worker
40*cc4ad7daSAndroid Build Coastguard Worker   if (x) {	// worse	|	if (!x)			// better
41*cc4ad7daSAndroid Build Coastguard Worker	   ...			|		return b;
42*cc4ad7daSAndroid Build Coastguard Worker	   ...			|
43*cc4ad7daSAndroid Build Coastguard Worker	   ...			|	...
44*cc4ad7daSAndroid Build Coastguard Worker	   ...			|	...
45*cc4ad7daSAndroid Build Coastguard Worker	   ...			|	...
46*cc4ad7daSAndroid Build Coastguard Worker	   ...			|	...
47*cc4ad7daSAndroid Build Coastguard Worker	   ...			|	...
48*cc4ad7daSAndroid Build Coastguard Worker	   ...			|	...
49*cc4ad7daSAndroid Build Coastguard Worker   } else {			|	...
50*cc4ad7daSAndroid Build Coastguard Worker	   return b;		|	return a;
51*cc4ad7daSAndroid Build Coastguard Worker   }				|
52*cc4ad7daSAndroid Build Coastguard Worker				|
53*cc4ad7daSAndroid Build Coastguard Worker   return a;			|
54*cc4ad7daSAndroid Build Coastguard Worker
55*cc4ad7daSAndroid Build Coastguard Worker3) Don't initialize variable unnecessarily
56*cc4ad7daSAndroid Build Coastguard WorkerWhen declaring a variable, try not to initialize it unless necessary.
57*cc4ad7daSAndroid Build Coastguard Worker
58*cc4ad7daSAndroid Build Coastguard WorkerExample:
59*cc4ad7daSAndroid Build Coastguard Workerint i = 1;  // wrong
60*cc4ad7daSAndroid Build Coastguard Worker
61*cc4ad7daSAndroid Build Coastguard Workerfor (i = 0; i < 3; i++) {
62*cc4ad7daSAndroid Build Coastguard Worker}
63*cc4ad7daSAndroid Build Coastguard Worker
64*cc4ad7daSAndroid Build Coastguard Worker4) Let the includes in the following order, separated by a new line:
65*cc4ad7daSAndroid Build Coastguard Worker	< system headers >
66*cc4ad7daSAndroid Build Coastguard Worker	< shared/* >
67*cc4ad7daSAndroid Build Coastguard Worker	< libkmod >
68*cc4ad7daSAndroid Build Coastguard Worker	< tool >
69*cc4ad7daSAndroid Build Coastguard Worker	"local headers"
70