xref: /aosp_15_r20/external/tpm2-tss/doc/coding_standard_c.md (revision 758e9fba6fc9adbf15340f70c73baee7b168b1c9)
1*758e9fbaSOystein Eftevaag# Coding Standard
2*758e9fbaSOystein EftevaagThe coding standard followed in this code base is similar in most ways to the
3*758e9fbaSOystein Eftevaagstyle followed by the Linux kernel. Naturally there are exceptions. Most
4*758e9fbaSOystein Eftevaagnotably is the indentation (more on this later). Above all else we ask
5*758e9fbaSOystein Eftevaagthat when modifying the code that you follow the most important rule:
6*758e9fbaSOystein Eftevaagcheck the surrounding code and imitate its style [1].
7*758e9fbaSOystein Eftevaag
8*758e9fbaSOystein Eftevaag## C Standard and Compiler Stuff
9*758e9fbaSOystein EftevaagCode should hold as close to the C99 standard as possible with the exception
10*758e9fbaSOystein Eftevaagthat GCC specific extensions are generally accepted. The code must compile
11*758e9fbaSOystein Eftevaagwithout warnings for the primary target compiler when all warnings are
12*758e9fbaSOystein Eftevaagenabled. If a warning is unavoidable, the offending line must be documented
13*758e9fbaSOystein Eftevaagwith an explanation of why said code can not be modified to appease the
14*758e9fbaSOystein Eftevaagcompiler [2].
15*758e9fbaSOystein Eftevaag
16*758e9fbaSOystein EftevaagIt is worth noting however that this code base has yet to run into a compiler
17*758e9fbaSOystein Eftevaagwarning that wasn't the authors fault. It is likely that if you do find
18*758e9fbaSOystein Eftevaagyourself disabling `-Werror` you're probably doing something wrong.
19*758e9fbaSOystein Eftevaag
20*758e9fbaSOystein Eftevaag## Comments
21*758e9fbaSOystein EftevaagThere are two acceptable commenting styles: block, line.  Block comments
22*758e9fbaSOystein Eftevaagare used to document a largeish block of code, typically a function. Sometimes
23*758e9fbaSOystein Eftevaagblock comments are useful with a function to document a loop or a particularly
24*758e9fbaSOystein Eftevaagtricky part of an algorithm. Line comments are typically a single sentence on a
25*758e9fbaSOystein Eftevaagsingle line. They provide some brief explanation of a line or two of code.
26*758e9fbaSOystein Eftevaag
27*758e9fbaSOystein EftevaagNOTE: Comments are most useful to convey the purpose, parameters and results
28*758e9fbaSOystein Eftevaagfrom functions and objects in a system. Excessive use of comments within a
29*758e9fbaSOystein Eftevaagfunction is indicative of two things: over documentation and code that needs
30*758e9fbaSOystein Eftevaagto be refactored. To combat the first case let's say that it's safe to assume
31*758e9fbaSOystein Eftevaagthat your code will be read by competent C programmers. The second case can be
32*758e9fbaSOystein Eftevaagtricky and there's no rule of thumb. Follow your instincts and keep in mind
33*758e9fbaSOystein Eftevaagthat excessive line comments are a sort of "code smell" that may mean your
34*758e9fbaSOystein Eftevaagcode would benefit from some restructuring.
35*758e9fbaSOystein Eftevaag
36*758e9fbaSOystein Eftevaag### Examples
37*758e9fbaSOystein Eftevaag```c
38*758e9fbaSOystein Eftevaag/*
39*758e9fbaSOystein Eftevaag * This block comment could apply to some function and describe its inner
40*758e9fbaSOystein Eftevaag * workings.  Notice these sentences have traditional capitalization and
41*758e9fbaSOystein Eftevaag * punctuation... that's because it has to be read in a way completely
42*758e9fbaSOystein Eftevaag * unlike the Post-It style content of next-line and line comments.
43*758e9fbaSOystein Eftevaag */
44*758e9fbaSOystein Eftevaag```
45*758e9fbaSOystein Eftevaag```c
46*758e9fbaSOystein Eftevaag// This is *not* an acceptable block comment.
47*758e9fbaSOystein Eftevaag// Don't do this.
48*758e9fbaSOystein Eftevaag// Please.
49*758e9fbaSOystein Eftevaag```
50*758e9fbaSOystein Eftevaag```c
51*758e9fbaSOystein Eftevaag/* This is a line comment */
52*758e9fbaSOystein Eftevaag```
53*758e9fbaSOystein Eftevaag
54*758e9fbaSOystein Eftevaag## Whitespace
55*758e9fbaSOystein EftevaagAll indentation must be spaces, not tabs. Lines are indented in multiples of
56*758e9fbaSOystein Eftevaag4 spaces. Each line of code and documentation will end with a non-whitespace
57*758e9fbaSOystein Eftevaagcharacter. There must *not* be any whitespace between the last line of code
58*758e9fbaSOystein Eftevaagor documentation in a file and the end of the file.
59*758e9fbaSOystein Eftevaag
60*758e9fbaSOystein Eftevaag## Naming Variables, Functions and Other Stuff
61*758e9fbaSOystein EftevaagNames should clearly convey the purpose of whatever is being named. While the
62*758e9fbaSOystein Eftevaagdata type of a variable conveys some information, this alone is insufficient.
63*758e9fbaSOystein EftevaagMultiple word names are descriptive and most easily read if words are
64*758e9fbaSOystein Eftevaagseparated with the underscore character, "_".
65*758e9fbaSOystein Eftevaag
66*758e9fbaSOystein EftevaagVariable and function names must be lowercase. Words in each name must be
67*758e9fbaSOystein Eftevaagseparated by an underscore character: "_". Macros and constants (anything
68*758e9fbaSOystein Eftevaagdeclared with \#define) must be in all-caps, again with words separated by
69*758e9fbaSOystein Eftevaagunderscores.
70*758e9fbaSOystein Eftevaag
71*758e9fbaSOystein EftevaagObjects created using the GObject system follow the GObject naming convention
72*758e9fbaSOystein Eftevaagwith individual words in object names as upper case characters.
73*758e9fbaSOystein Eftevaag
74*758e9fbaSOystein Eftevaag### Exceptions
75*758e9fbaSOystein EftevaagExceptions to these rules are made for compliance with the TCG
76*758e9fbaSOystein Eftevaagspecifications. All function names, parameters, and other data types must
77*758e9fbaSOystein Eftevaagbe implemented faithfully to the specification and so may violate the naming
78*758e9fbaSOystein Eftevaagconventions defined here.
79*758e9fbaSOystein Eftevaag
80*758e9fbaSOystein Eftevaag### Examples
81*758e9fbaSOystein Eftevaag```c
82*758e9fbaSOystein Eftevaagunsigned int table_index = find_index(jacket_table, “color”, COLOR_RED);
83*758e9fbaSOystein Eftevaag```
84*758e9fbaSOystein Eftevaag```c
85*758e9fbaSOystein Eftevaagint last_item = 0;
86*758e9fbaSOystein Eftevaagwhile (!last_item) {
87*758e9fbaSOystein Eftevaag     /* ... */
88*758e9fbaSOystein Eftevaag}
89*758e9fbaSOystein Eftevaag```
90*758e9fbaSOystein Eftevaag```c
91*758e9fbaSOystein Eftevaagint char_found = is_alpha (c);
92*758e9fbaSOystein Eftevaag```
93*758e9fbaSOystein Eftevaag
94*758e9fbaSOystein EftevaagSingle letter variable names should be avoided.  Exceptions are:
95*758e9fbaSOystein Eftevaag* "i", "j", and "k" are loop counters or temporary array indexes
96*758e9fbaSOystein Eftevaag* "m" and "n" are row and column indexes for multidimensional arrays
97*758e9fbaSOystein Eftevaag* "c" and "s" are temporary/parameter characters or strings
98*758e9fbaSOystein Eftevaag* "r", "g", "b", and "a" are red, green, blue, and alpha levels, but only when
99*758e9fbaSOystein Eftevaag* they are used together
100*758e9fbaSOystein Eftevaag* "x", "y", and "z" are coordinate values
101*758e9fbaSOystein Eftevaag
102*758e9fbaSOystein EftevaagAbbreviated words in variable names should be avoided.  Exceptions are:
103*758e9fbaSOystein Eftevaag* "char" = character
104*758e9fbaSOystein Eftevaag* "col" = column.  Typically there is also "row" so it is not confused with color
105*758e9fbaSOystein Eftevaag* "cnt" = count
106*758e9fbaSOystein Eftevaag* "pos" = position
107*758e9fbaSOystein Eftevaag* "rem" = remainder
108*758e9fbaSOystein Eftevaag* "ctx" = context
109*758e9fbaSOystein Eftevaag
110*758e9fbaSOystein EftevaagFunction names should follow the naming conventions of variables and clearly
111*758e9fbaSOystein Eftevaagdescribe not only what the function does, but also the nature of what it
112*758e9fbaSOystein Eftevaagreturns (if anything). Functions that return boolean integers should be named
113*758e9fbaSOystein Eftevaagto reflect the true condition even if they are created to detect false
114*758e9fbaSOystein Eftevaagconditions. Functions should never be hidden in conditional statements, with
115*758e9fbaSOystein Eftevaagthe exception of loops where it makes the code more simple.
116*758e9fbaSOystein Eftevaag```c
117*758e9fbaSOystein Eftevaagbool is_number_prime = is_prime(i);
118*758e9fbaSOystein Eftevaag```
119*758e9fbaSOystein Eftevaag```c
120*758e9fbaSOystein Eftevaagif (is_number_prime) {
121*758e9fbaSOystein Eftevaag    /* ... */
122*758e9fbaSOystein Eftevaag}
123*758e9fbaSOystein Eftevaag```
124*758e9fbaSOystein Eftevaag```c
125*758e9fbaSOystein Eftevaagwhile (!labeled_correctly(sample[i])) {
126*758e9fbaSOystein Eftevaag    /* ... */
127*758e9fbaSOystein Eftevaag}
128*758e9fbaSOystein Eftevaag```
129*758e9fbaSOystein Eftevaag
130*758e9fbaSOystein EftevaagA function that is exported for use in multiple source files should be
131*758e9fbaSOystein Eftevaagprefixed with the source file (or object module) name where it is defined.
132*758e9fbaSOystein EftevaagFor example, the file list.c may contain the implementation of a dynamic
133*758e9fbaSOystein Eftevaaglist ADT including an exported method for creating a list instance and an
134*758e9fbaSOystein Eftevaaginternal (static) method for overflow checking. The first function might be
135*758e9fbaSOystein Eftevaagnamed "list_create", and the second, "is_overflowed".
136*758e9fbaSOystein Eftevaag
137*758e9fbaSOystein EftevaagThe use of the static keyword when defining functions is a useful way to scope
138*758e9fbaSOystein Eftevaagthe visibility of the function to the same translation unit. A negative side
139*758e9fbaSOystein Eftevaageffect of this is preventing the testing of the function through the unit
140*758e9fbaSOystein Eftevaagtesting harness. Generally we accept exposing symbols to get better test
141*758e9fbaSOystein Eftevaagcoverage.
142*758e9fbaSOystein Eftevaag
143*758e9fbaSOystein Eftevaag## Files
144*758e9fbaSOystein EftevaagTypically every header file has the same base filename as an associated source
145*758e9fbaSOystein Eftevaagfile. Exceptions to this rule are generally limited to modules that will
146*758e9fbaSOystein Eftevaagexpose a separate set symbols to external consumers. In this case the internal
147*758e9fbaSOystein Eftevaagversion of the header should be suffixed with '-priv'.
148*758e9fbaSOystein Eftevaag
149*758e9fbaSOystein EftevaagFiles names are formatted in the same way as described above with the
150*758e9fbaSOystein Eftevaagexception of hyphens "-" separating words.
151*758e9fbaSOystein Eftevaag
152*758e9fbaSOystein EftevaagThe body of each header file must be surrounded by an include guard (aka
153*758e9fbaSOystein Eftevaag"header guard"). These guards shall be given the same name as the file in
154*758e9fbaSOystein Eftevaagwhich they reside. Their names shall be all caps, with words separated by
155*758e9fbaSOystein Eftevaagthe underscore character "_".
156*758e9fbaSOystein Eftevaag
157*758e9fbaSOystein EftevaagHeader files should never define functions or variables.
158*758e9fbaSOystein Eftevaag
159*758e9fbaSOystein EftevaagHeader files should only \#include what is necessary to allow a file that
160*758e9fbaSOystein Eftevaagincludes it to compile.  Associated source files will always \#include the
161*758e9fbaSOystein Eftevaagheader of the same name, but should \#include files whose resources are used
162*758e9fbaSOystein Eftevaagwithin the source even if they are already included in that header. This
163*758e9fbaSOystein Eftevaagprovides a complete context for readers of the source file... i.e., they
164*758e9fbaSOystein Eftevaagdon't have to search through headers to determine where a resource came from.
165*758e9fbaSOystein Eftevaag
166*758e9fbaSOystein EftevaagFiles included by all source files must conform to the following format and
167*758e9fbaSOystein Eftevaagorder. Each entry in the list below defines a contiguous block of `include`
168*758e9fbaSOystein Eftevaagdirectives separated by a blank line:
169*758e9fbaSOystein Eftevaag* System headers - These are headers provided by the core c libraries
170*758e9fbaSOystein Eftevaag(typically from libc).
171*758e9fbaSOystein Eftevaag* External dependencies - These are headers installed on the platform defining
172*758e9fbaSOystein Eftevaaginterfaces to external libraries.
173*758e9fbaSOystein Eftevaag* Standard TSS2 headers - These are headers that define the public TSS2 types
174*758e9fbaSOystein Eftevaagand interfaces. They are all located under $(srcdir)/include/* and will be
175*758e9fbaSOystein Eftevaaginstalled as part of the `install` build target. These *must* be included
176*758e9fbaSOystein Eftevaagusing the quoted include variant (using `"` instead of the angle brackets).
177*758e9fbaSOystein Eftevaag* Internal headers - These are headers defining the interfaces to code modules
178*758e9fbaSOystein Eftevaagthat are internal to the project.
179*758e9fbaSOystein Eftevaag
180*758e9fbaSOystein EftevaagHeaders in each block must listed in alphabetical order.
181*758e9fbaSOystein Eftevaag
182*758e9fbaSOystein Eftevaag### Example
183*758e9fbaSOystein Eftevaagheader: `example-module.h`
184*758e9fbaSOystein Eftevaag```
185*758e9fbaSOystein Eftevaag/*
186*758e9fbaSOystein Eftevaag * BSD license block
187*758e9fbaSOystein Eftevaag */
188*758e9fbaSOystein Eftevaag#ifndef EXAMPLE_MODULE_H
189*758e9fbaSOystein Eftevaag#define EXAMPLE_MODULE_H
190*758e9fbaSOystein Eftevaag
191*758e9fbaSOystein Eftevaag#include <stdint.h>
192*758e9fbaSOystein Eftevaag#include <sys/types.h>
193*758e9fbaSOystein Eftevaag
194*758e9fbaSOystein Eftevaag#include "tss2/tss2_tpm2_types.h"
195*758e9fbaSOystein Eftevaag
196*758e9fbaSOystein Eftevaag#include "internal-module.h"
197*758e9fbaSOystein Eftevaag
198*758e9fbaSOystein Eftevaag/*
199*758e9fbaSOystein Eftevaag * preprocess or directives and declarations using stuff from included headers
200*758e9fbaSOystein Eftevaag */
201*758e9fbaSOystein Eftevaag
202*758e9fbaSOystein Eftevaag#endif /* EXAMPLE_MODULE_H */
203*758e9fbaSOystein Eftevaag```
204*758e9fbaSOystein Eftevaag
205*758e9fbaSOystein Eftevaagimplementation: `example-module.c`
206*758e9fbaSOystein Eftevaag```
207*758e9fbaSOystein Eftevaag/*
208*758e9fbaSOystein Eftevaag * BSD license block
209*758e9fbaSOystein Eftevaag */
210*758e9fbaSOystein Eftevaag#include <inttypes.h>
211*758e9fbaSOystein Eftevaag#include <stdint.h>
212*758e9fbaSOystein Eftevaag
213*758e9fbaSOystein Eftevaag#include <foo/bar.h>
214*758e9fbaSOystein Eftevaag
215*758e9fbaSOystein Eftevaag#include "tss2/tss2_tcti.h"
216*758e9fbaSOystein Eftevaag#include "tss2/tss2_tpm2_types.h"
217*758e9fbaSOystein Eftevaag
218*758e9fbaSOystein Eftevaag#include "example-module.h"
219*758e9fbaSOystein Eftevaag#include "internal-module.h"
220*758e9fbaSOystein Eftevaag
221*758e9fbaSOystein Eftevaag/*
222*758e9fbaSOystein Eftevaag * Implementation / code using headers listed above.
223*758e9fbaSOystein Eftevaag */
224*758e9fbaSOystein Eftevaag```
225*758e9fbaSOystein Eftevaag
226*758e9fbaSOystein Eftevaag## Types
227*758e9fbaSOystein EftevaagTypes shall be selected for their use case. Variables should only be of a
228*758e9fbaSOystein Eftevaagsigned type if something should ever be negative. A common, incorrect use, is
229*758e9fbaSOystein Eftevaagto declare loop counters as int instead of unsigned, or to use an int to hold
230*758e9fbaSOystein Eftevaagthe size of some object.
231*758e9fbaSOystein Eftevaag
232*758e9fbaSOystein Eftevaag## Formatting
233*758e9fbaSOystein EftevaagAlways use space characters, 4 spaces per level of indentation.
234*758e9fbaSOystein Eftevaag
235*758e9fbaSOystein EftevaagConditional statements (such as if, else, while, for, switch, etc) must place
236*758e9fbaSOystein Eftevaagthe opening brace on the same line after the end of the control flow statement.
237*758e9fbaSOystein EftevaagThe closing brace should be placed at the same column position as the beginning
238*758e9fbaSOystein Eftevaagof the associated control flow statement on a line by itself.
239*758e9fbaSOystein Eftevaag
240*758e9fbaSOystein EftevaagFunction definitions specify the return type on a line, followed by the
241*758e9fbaSOystein Eftevaagfunction name followed by the first parameter. Each additional parameter is
242*758e9fbaSOystein Eftevaaglisted on a separate line aligned with the line above. The opening brace
243*758e9fbaSOystein Eftevaagdefning the functions scope must be on the following line at column position 0.
244*758e9fbaSOystein Eftevaag
245*758e9fbaSOystein EftevaagA space must separate a control flow statement or function and the opening
246*758e9fbaSOystein Eftevaagparenthesis.
247*758e9fbaSOystein Eftevaag
248*758e9fbaSOystein EftevaagLine length should not exceed 80 characters and should be split on the nearest
249*758e9fbaSOystein Eftevaagwhitespace or delimiter character. When splitting lines with
250*758e9fbaSOystein Eftevaag
251*758e9fbaSOystein Eftevaag### Example
252*758e9fbaSOystein Eftevaag```c
253*758e9fbaSOystein Eftevaagif (some_int > 0) {
254*758e9fbaSOystein Eftevaag    statement1;
255*758e9fbaSOystein Eftevaag    statement2;
256*758e9fbaSOystein Eftevaag}
257*758e9fbaSOystein Eftevaag```
258*758e9fbaSOystein Eftevaag```c
259*758e9fbaSOystein Eftevaagvoid
260*758e9fbaSOystein Eftevaagsome_function (short_t       arg_1,
261*758e9fbaSOystein Eftevaag               longer_name_t arg_2)
262*758e9fbaSOystein Eftevaag{
263*758e9fbaSOystein Eftevaag    statement1;
264*758e9fbaSOystein Eftevaag    statement2;
265*758e9fbaSOystein Eftevaag}
266*758e9fbaSOystein Eftevaag```
267*758e9fbaSOystein Eftevaag```c
268*758e9fbaSOystein Eftevaagsome_long_variable_name =
269*758e9fbaSOystein Eftevaag    some_long_function_name (lots_of_parameters_1, lots_of_parameters_2);
270*758e9fbaSOystein Eftevaag```
271*758e9fbaSOystein Eftevaag```c
272*758e9fbaSOystein Eftevaagsome_long_variable_name = some_long_function_name (lots_of_parameters_1,
273*758e9fbaSOystein Eftevaag                                                   lots_of_parameters_2,
274*758e9fbaSOystein Eftevaag                                                   lots_of_parameters_3);
275*758e9fbaSOystein Eftevaag```
276*758e9fbaSOystein EftevaagThese formatting conditions are contrary to Kernighan and Ritchie's "one true brace style" [3].
277*758e9fbaSOystein Eftevaag
278*758e9fbaSOystein Eftevaag## References
279*758e9fbaSOystein Eftevaag1. GNOME C Coding Style : https://developer.gnome.org/programming-guidelines/stable/c-coding-style.html.en
280*758e9fbaSOystein Eftevaag2. Alan Bridger, Mick Brooks, and Jim Pisano, C Coding Standards, 2001, http://www.alma.nrao.edu/development/computing/docs/joint/0009/2001-02-28.pdf
281*758e9fbaSOystein Eftevaag3. Brian Kernighan, Dennis Ritchie, The C Programing Language, 1988
282