xref: /aosp_15_r20/external/arm-trusted-firmware/docs/process/coding-style.rst (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong ParkCoding Style
2*54fd6939SJiyong Park============
3*54fd6939SJiyong Park
4*54fd6939SJiyong ParkThe following sections outline the |TF-A| coding style for *C* code. The style
5*54fd6939SJiyong Parkis based on the `Linux kernel coding style`_, with a few modifications.
6*54fd6939SJiyong Park
7*54fd6939SJiyong ParkThe style should not be considered *set in stone*. Feel free to provide feedback
8*54fd6939SJiyong Parkand suggestions.
9*54fd6939SJiyong Park
10*54fd6939SJiyong Park.. note::
11*54fd6939SJiyong Park   You will almost certainly find code in the |TF-A| repository that does not
12*54fd6939SJiyong Park   follow the style. The intent is for all code to do so eventually.
13*54fd6939SJiyong Park
14*54fd6939SJiyong ParkFile Encoding
15*54fd6939SJiyong Park-------------
16*54fd6939SJiyong Park
17*54fd6939SJiyong ParkThe source code must use the **UTF-8** character encoding. Comments and
18*54fd6939SJiyong Parkdocumentation may use non-ASCII characters when required (e.g. Greek letters
19*54fd6939SJiyong Parkused for units) but code itself is still limited to ASCII characters.
20*54fd6939SJiyong Park
21*54fd6939SJiyong ParkNewlines must be in **Unix** style, which means that only the Line Feed (``LF``)
22*54fd6939SJiyong Parkcharacter is used to break a line and reset to the first column.
23*54fd6939SJiyong Park
24*54fd6939SJiyong ParkLanguage
25*54fd6939SJiyong Park--------
26*54fd6939SJiyong Park
27*54fd6939SJiyong ParkThe primary language for comments and naming must be International English. In
28*54fd6939SJiyong Parkcases where there is a conflict between the American English and British English
29*54fd6939SJiyong Parkspellings of a word, the American English spelling is used.
30*54fd6939SJiyong Park
31*54fd6939SJiyong ParkExceptions are made when referring directly to something that does not use
32*54fd6939SJiyong Parkinternational style, such as the name of a company. In these cases the existing
33*54fd6939SJiyong Parkname should be used as-is.
34*54fd6939SJiyong Park
35*54fd6939SJiyong ParkC Language Standard
36*54fd6939SJiyong Park-------------------
37*54fd6939SJiyong Park
38*54fd6939SJiyong ParkThe C language mode used for TF-A is *GNU99*. This is the "GNU dialect of ISO
39*54fd6939SJiyong ParkC99", which implies the *ISO C99* standard with GNU extensions.
40*54fd6939SJiyong Park
41*54fd6939SJiyong ParkBoth GCC and Clang compiler toolchains have support for *GNU99* mode, though
42*54fd6939SJiyong ParkClang does lack support for a small number of GNU extensions. These
43*54fd6939SJiyong Parkmissing extensions are rarely used, however, and should not pose a problem.
44*54fd6939SJiyong Park
45*54fd6939SJiyong Park.. _misra-compliance:
46*54fd6939SJiyong Park
47*54fd6939SJiyong ParkMISRA Compliance
48*54fd6939SJiyong Park----------------
49*54fd6939SJiyong Park
50*54fd6939SJiyong ParkTF-A attempts to comply with the `MISRA C:2012 Guidelines`_. Coverity
51*54fd6939SJiyong ParkStatic Analysis is used to regularly generate a report of current MISRA defects
52*54fd6939SJiyong Parkand to prevent the addition of new ones.
53*54fd6939SJiyong Park
54*54fd6939SJiyong ParkIt is not possible for the project to follow all MISRA guidelines. We maintain
55*54fd6939SJiyong Park`a spreadsheet`_ that lists all rules and directives and whether we aim to
56*54fd6939SJiyong Parkcomply with them or not. A rationale is given for each deviation.
57*54fd6939SJiyong Park
58*54fd6939SJiyong Park.. note::
59*54fd6939SJiyong Park   Enforcing a rule does not mean that the codebase is free of defects
60*54fd6939SJiyong Park   of that rule, only that they would ideally be removed.
61*54fd6939SJiyong Park
62*54fd6939SJiyong Park.. note::
63*54fd6939SJiyong Park   Third-party libraries are not considered in our MISRA analysis and we do not
64*54fd6939SJiyong Park   intend to modify them to make them MISRA compliant.
65*54fd6939SJiyong Park
66*54fd6939SJiyong ParkIndentation
67*54fd6939SJiyong Park-----------
68*54fd6939SJiyong Park
69*54fd6939SJiyong ParkUse **tabs** for indentation. The use of spaces for indentation is forbidden
70*54fd6939SJiyong Parkexcept in the case where a term is being indented to a boundary that cannot be
71*54fd6939SJiyong Parkachieved using tabs alone.
72*54fd6939SJiyong Park
73*54fd6939SJiyong ParkTab spacing should be set to **8 characters**.
74*54fd6939SJiyong Park
75*54fd6939SJiyong ParkTrailing whitespace is not allowed and must be trimmed.
76*54fd6939SJiyong Park
77*54fd6939SJiyong ParkSpacing
78*54fd6939SJiyong Park-------
79*54fd6939SJiyong Park
80*54fd6939SJiyong ParkSingle spacing should be used around most operators, including:
81*54fd6939SJiyong Park
82*54fd6939SJiyong Park- Arithmetic operators (``+``, ``-``, ``/``, ``*``)
83*54fd6939SJiyong Park- Assignment operators (``=``, ``+=``, etc)
84*54fd6939SJiyong Park- Boolean operators (``&&``, ``||``)
85*54fd6939SJiyong Park- Comparison operators (``<``, ``>``, ``==``, etc)
86*54fd6939SJiyong Park
87*54fd6939SJiyong ParkA space should also be used to separate parentheses and braces when they are not
88*54fd6939SJiyong Parkalready separated by a newline, such as for the ``if`` statement in the
89*54fd6939SJiyong Parkfollowing example:
90*54fd6939SJiyong Park
91*54fd6939SJiyong Park.. code:: c
92*54fd6939SJiyong Park
93*54fd6939SJiyong Park  int function_foo(bool bar)
94*54fd6939SJiyong Park  {
95*54fd6939SJiyong Park      if (bar) {
96*54fd6939SJiyong Park          function_baz();
97*54fd6939SJiyong Park      }
98*54fd6939SJiyong Park  }
99*54fd6939SJiyong Park
100*54fd6939SJiyong ParkNote that there is no space between the name of a function and the following
101*54fd6939SJiyong Parkparentheses.
102*54fd6939SJiyong Park
103*54fd6939SJiyong ParkControl statements (``if``, ``for``, ``switch``, ``while``, etc) must be
104*54fd6939SJiyong Parkseparated from the following open parenthesis by a single space. The previous
105*54fd6939SJiyong Parkexample illustrates this for an ``if`` statement.
106*54fd6939SJiyong Park
107*54fd6939SJiyong ParkLine Length
108*54fd6939SJiyong Park-----------
109*54fd6939SJiyong Park
110*54fd6939SJiyong ParkLine length *should* be at most **80 characters**. This limit does not include
111*54fd6939SJiyong Parknon-printing characters such as the line feed.
112*54fd6939SJiyong Park
113*54fd6939SJiyong ParkThis rule is a *should*, not a must, and it is acceptable to exceed the limit
114*54fd6939SJiyong Park**slightly** where the readability of the code would otherwise be significantly
115*54fd6939SJiyong Parkreduced. Use your judgement in these cases.
116*54fd6939SJiyong Park
117*54fd6939SJiyong ParkBlank Lines
118*54fd6939SJiyong Park-----------
119*54fd6939SJiyong Park
120*54fd6939SJiyong ParkFunctions are usually separated by a single blank line. In certain cases it is
121*54fd6939SJiyong Parkacceptable to use additional blank lines for clarity, if required.
122*54fd6939SJiyong Park
123*54fd6939SJiyong ParkThe file must end with a single newline character. Many editors have the option
124*54fd6939SJiyong Parkto insert this automatically and to trim multiple blank lines at the end of the
125*54fd6939SJiyong Parkfile.
126*54fd6939SJiyong Park
127*54fd6939SJiyong ParkBraces
128*54fd6939SJiyong Park------
129*54fd6939SJiyong Park
130*54fd6939SJiyong ParkOpening Brace Placement
131*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^
132*54fd6939SJiyong Park
133*54fd6939SJiyong ParkBraces follow the **Kernighan and Ritchie (K&R)** style, where the opening brace
134*54fd6939SJiyong Parkis **not** placed on a new line.
135*54fd6939SJiyong Park
136*54fd6939SJiyong ParkExample for a ``while`` loop:
137*54fd6939SJiyong Park
138*54fd6939SJiyong Park.. code:: c
139*54fd6939SJiyong Park
140*54fd6939SJiyong Park  while (condition) {
141*54fd6939SJiyong Park      foo();
142*54fd6939SJiyong Park      bar();
143*54fd6939SJiyong Park  }
144*54fd6939SJiyong Park
145*54fd6939SJiyong ParkThis style applies to all blocks except for functions which, following the Linux
146*54fd6939SJiyong Parkstyle, **do** place the opening brace on a new line.
147*54fd6939SJiyong Park
148*54fd6939SJiyong ParkExample for a function:
149*54fd6939SJiyong Park
150*54fd6939SJiyong Park.. code:: c
151*54fd6939SJiyong Park
152*54fd6939SJiyong Park  int my_function(void)
153*54fd6939SJiyong Park  {
154*54fd6939SJiyong Park      int a;
155*54fd6939SJiyong Park
156*54fd6939SJiyong Park      a = 1;
157*54fd6939SJiyong Park      return a;
158*54fd6939SJiyong Park  }
159*54fd6939SJiyong Park
160*54fd6939SJiyong ParkConditional Statement Bodies
161*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^
162*54fd6939SJiyong Park
163*54fd6939SJiyong ParkWhere conditional statements (such as ``if``, ``for``, ``while`` and ``do``) are
164*54fd6939SJiyong Parkused, braces must be placed around the statements that form the body of the
165*54fd6939SJiyong Parkconditional. This is the case regardless of the number of statements in the
166*54fd6939SJiyong Parkbody.
167*54fd6939SJiyong Park
168*54fd6939SJiyong Park.. note::
169*54fd6939SJiyong Park  This is a notable departure from the Linux coding style that has been
170*54fd6939SJiyong Park  adopted to follow MISRA guidelines more closely and to help prevent errors.
171*54fd6939SJiyong Park
172*54fd6939SJiyong ParkFor example, use the following style:
173*54fd6939SJiyong Park
174*54fd6939SJiyong Park.. code:: c
175*54fd6939SJiyong Park
176*54fd6939SJiyong Park  if (condition) {
177*54fd6939SJiyong Park      foo++;
178*54fd6939SJiyong Park  }
179*54fd6939SJiyong Park
180*54fd6939SJiyong Parkinstead of omitting the optional braces around a single statement:
181*54fd6939SJiyong Park
182*54fd6939SJiyong Park.. code:: c
183*54fd6939SJiyong Park
184*54fd6939SJiyong Park  /* This is violating MISRA C 2012: Rule 15.6 */
185*54fd6939SJiyong Park  if (condition)
186*54fd6939SJiyong Park      foo++;
187*54fd6939SJiyong Park
188*54fd6939SJiyong ParkThe reason for this is to prevent accidental changes to control flow when
189*54fd6939SJiyong Parkmodifying the body of the conditional. For example, at a quick glance it is easy
190*54fd6939SJiyong Parkto think that the value of ``bar`` is only incremented if ``condition``
191*54fd6939SJiyong Parkevaluates to ``true`` but this is not the case - ``bar`` will always be
192*54fd6939SJiyong Parkincremented regardless of the condition evaluation. If the developer forgets to
193*54fd6939SJiyong Parkadd braces around the conditional body when adding the ``bar++;`` statement then
194*54fd6939SJiyong Parkthe program execution will not proceed as intended.
195*54fd6939SJiyong Park
196*54fd6939SJiyong Park.. code:: c
197*54fd6939SJiyong Park
198*54fd6939SJiyong Park  /* This is violating MISRA C 2012: Rule 15.6 */
199*54fd6939SJiyong Park  if (condition)
200*54fd6939SJiyong Park      foo++;
201*54fd6939SJiyong Park      bar++;
202*54fd6939SJiyong Park
203*54fd6939SJiyong ParkNaming
204*54fd6939SJiyong Park------
205*54fd6939SJiyong Park
206*54fd6939SJiyong ParkFunctions
207*54fd6939SJiyong Park^^^^^^^^^
208*54fd6939SJiyong Park
209*54fd6939SJiyong ParkUse lowercase for function names, separating multiple words with an underscore
210*54fd6939SJiyong Parkcharacter (``_``). This is sometimes referred to as *Snake Case*. An example is
211*54fd6939SJiyong Parkgiven below:
212*54fd6939SJiyong Park
213*54fd6939SJiyong Park.. code:: c
214*54fd6939SJiyong Park
215*54fd6939SJiyong Park  void bl2_arch_setup(void)
216*54fd6939SJiyong Park  {
217*54fd6939SJiyong Park      ...
218*54fd6939SJiyong Park  }
219*54fd6939SJiyong Park
220*54fd6939SJiyong ParkLocal Variables and Parameters
221*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
222*54fd6939SJiyong Park
223*54fd6939SJiyong ParkLocal variables and function parameters use the same format as function names:
224*54fd6939SJiyong Parklowercase with underscore separation between multiple words. An example is
225*54fd6939SJiyong Parkgiven below:
226*54fd6939SJiyong Park
227*54fd6939SJiyong Park.. code:: c
228*54fd6939SJiyong Park
229*54fd6939SJiyong Park  static void set_scr_el3_from_rm(uint32_t type,
230*54fd6939SJiyong Park                                  uint32_t interrupt_type_flags,
231*54fd6939SJiyong Park                                  uint32_t security_state)
232*54fd6939SJiyong Park  {
233*54fd6939SJiyong Park      uint32_t flag, bit_pos;
234*54fd6939SJiyong Park
235*54fd6939SJiyong Park      ...
236*54fd6939SJiyong Park
237*54fd6939SJiyong Park  }
238*54fd6939SJiyong Park
239*54fd6939SJiyong ParkPreprocessor Macros
240*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^
241*54fd6939SJiyong Park
242*54fd6939SJiyong ParkIdentifiers that are defined using preprocessor macros are written in all
243*54fd6939SJiyong Parkuppercase text.
244*54fd6939SJiyong Park
245*54fd6939SJiyong Park.. code:: c
246*54fd6939SJiyong Park
247*54fd6939SJiyong Park  #define BUFFER_SIZE_BYTES 64
248*54fd6939SJiyong Park
249*54fd6939SJiyong ParkFunction Attributes
250*54fd6939SJiyong Park-------------------
251*54fd6939SJiyong Park
252*54fd6939SJiyong ParkPlace any function attributes after the function type and before the function
253*54fd6939SJiyong Parkname.
254*54fd6939SJiyong Park
255*54fd6939SJiyong Park.. code:: c
256*54fd6939SJiyong Park
257*54fd6939SJiyong Park   void __init plat_arm_interconnect_init(void);
258*54fd6939SJiyong Park
259*54fd6939SJiyong ParkAlignment
260*54fd6939SJiyong Park---------
261*54fd6939SJiyong Park
262*54fd6939SJiyong ParkAlignment should be performed primarily with tabs, adding spaces if required to
263*54fd6939SJiyong Parkachieve a granularity that is smaller than the tab size. For example, with a tab
264*54fd6939SJiyong Parksize of eight columns it would be necessary to use one tab character and two
265*54fd6939SJiyong Parkspaces to indent text by ten columns.
266*54fd6939SJiyong Park
267*54fd6939SJiyong ParkSwitch Statement Alignment
268*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^
269*54fd6939SJiyong Park
270*54fd6939SJiyong ParkWhen using ``switch`` statements, align each ``case`` statement with the
271*54fd6939SJiyong Park``switch`` so that they are in the same column.
272*54fd6939SJiyong Park
273*54fd6939SJiyong Park.. code:: c
274*54fd6939SJiyong Park
275*54fd6939SJiyong Park  switch (condition) {
276*54fd6939SJiyong Park  case A:
277*54fd6939SJiyong Park      foo();
278*54fd6939SJiyong Park  case B:
279*54fd6939SJiyong Park      bar();
280*54fd6939SJiyong Park  default:
281*54fd6939SJiyong Park      baz();
282*54fd6939SJiyong Park  }
283*54fd6939SJiyong Park
284*54fd6939SJiyong ParkPointer Alignment
285*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^
286*54fd6939SJiyong Park
287*54fd6939SJiyong ParkThe reference and dereference operators (ampersand and *pointer star*) must be
288*54fd6939SJiyong Parkaligned with the name of the object on which they are operating, as opposed to
289*54fd6939SJiyong Parkthe type of the object.
290*54fd6939SJiyong Park
291*54fd6939SJiyong Park.. code:: c
292*54fd6939SJiyong Park
293*54fd6939SJiyong Park  uint8_t *foo;
294*54fd6939SJiyong Park
295*54fd6939SJiyong Park  foo = &bar;
296*54fd6939SJiyong Park
297*54fd6939SJiyong Park
298*54fd6939SJiyong ParkComments
299*54fd6939SJiyong Park--------
300*54fd6939SJiyong Park
301*54fd6939SJiyong ParkThe general rule for comments is that the double-slash style of comment (``//``)
302*54fd6939SJiyong Parkis not allowed. Examples of the allowed comment formats are shown below:
303*54fd6939SJiyong Park
304*54fd6939SJiyong Park.. code:: c
305*54fd6939SJiyong Park
306*54fd6939SJiyong Park  /*
307*54fd6939SJiyong Park   * This example illustrates the first allowed style for multi-line comments.
308*54fd6939SJiyong Park   *
309*54fd6939SJiyong Park   * Blank lines within multi-lines are allowed when they add clarity or when
310*54fd6939SJiyong Park   * they separate multiple contexts.
311*54fd6939SJiyong Park   *
312*54fd6939SJiyong Park   */
313*54fd6939SJiyong Park
314*54fd6939SJiyong Park.. code:: c
315*54fd6939SJiyong Park
316*54fd6939SJiyong Park  /**************************************************************************
317*54fd6939SJiyong Park   * This is the second allowed style for multi-line comments.
318*54fd6939SJiyong Park   *
319*54fd6939SJiyong Park   * In this style, the first and last lines use asterisks that run the full
320*54fd6939SJiyong Park   * width of the comment at its widest point.
321*54fd6939SJiyong Park   *
322*54fd6939SJiyong Park   * This style can be used for additional emphasis.
323*54fd6939SJiyong Park   *
324*54fd6939SJiyong Park   *************************************************************************/
325*54fd6939SJiyong Park
326*54fd6939SJiyong Park.. code:: c
327*54fd6939SJiyong Park
328*54fd6939SJiyong Park  /* Single line comments can use this format */
329*54fd6939SJiyong Park
330*54fd6939SJiyong Park.. code:: c
331*54fd6939SJiyong Park
332*54fd6939SJiyong Park  /***************************************************************************
333*54fd6939SJiyong Park   * This alternative single-line comment style can also be used for emphasis.
334*54fd6939SJiyong Park   **************************************************************************/
335*54fd6939SJiyong Park
336*54fd6939SJiyong ParkHeaders and inclusion
337*54fd6939SJiyong Park---------------------
338*54fd6939SJiyong Park
339*54fd6939SJiyong ParkHeader guards
340*54fd6939SJiyong Park^^^^^^^^^^^^^
341*54fd6939SJiyong Park
342*54fd6939SJiyong ParkFor a header file called "some_driver.h" the style used by |TF-A| is:
343*54fd6939SJiyong Park
344*54fd6939SJiyong Park.. code:: c
345*54fd6939SJiyong Park
346*54fd6939SJiyong Park  #ifndef SOME_DRIVER_H
347*54fd6939SJiyong Park  #define SOME_DRIVER_H
348*54fd6939SJiyong Park
349*54fd6939SJiyong Park  <header content>
350*54fd6939SJiyong Park
351*54fd6939SJiyong Park  #endif /* SOME_DRIVER_H */
352*54fd6939SJiyong Park
353*54fd6939SJiyong ParkInclude statement ordering
354*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^
355*54fd6939SJiyong Park
356*54fd6939SJiyong ParkAll header files that are included by a source file must use the following,
357*54fd6939SJiyong Parkgrouped ordering. This is to improve readability (by making it easier to quickly
358*54fd6939SJiyong Parkread through the list of headers) and maintainability.
359*54fd6939SJiyong Park
360*54fd6939SJiyong Park#. *System* includes: Header files from the standard *C* library, such as
361*54fd6939SJiyong Park   ``stddef.h`` and ``string.h``.
362*54fd6939SJiyong Park
363*54fd6939SJiyong Park#. *Project* includes: Header files under the ``include/`` directory within
364*54fd6939SJiyong Park   |TF-A| are *project* includes.
365*54fd6939SJiyong Park
366*54fd6939SJiyong Park#. *Platform* includes: Header files relating to a single, specific platform,
367*54fd6939SJiyong Park   and which are located under the ``plat/<platform_name>`` directory within
368*54fd6939SJiyong Park   |TF-A|, are *platform* includes.
369*54fd6939SJiyong Park
370*54fd6939SJiyong ParkWithin each group, ``#include`` statements must be in alphabetical order,
371*54fd6939SJiyong Parktaking both the file and directory names into account.
372*54fd6939SJiyong Park
373*54fd6939SJiyong ParkGroups must be separated by a single blank line for clarity.
374*54fd6939SJiyong Park
375*54fd6939SJiyong ParkThe example below illustrates the ordering rules using some contrived header
376*54fd6939SJiyong Parkfile names; this type of name reuse should be otherwise avoided.
377*54fd6939SJiyong Park
378*54fd6939SJiyong Park.. code:: c
379*54fd6939SJiyong Park
380*54fd6939SJiyong Park  #include <string.h>
381*54fd6939SJiyong Park
382*54fd6939SJiyong Park  #include <a_dir/example/a_header.h>
383*54fd6939SJiyong Park  #include <a_dir/example/b_header.h>
384*54fd6939SJiyong Park  #include <a_dir/test/a_header.h>
385*54fd6939SJiyong Park  #include <b_dir/example/a_header.h>
386*54fd6939SJiyong Park
387*54fd6939SJiyong Park  #include "a_header.h"
388*54fd6939SJiyong Park
389*54fd6939SJiyong ParkInclude statement variants
390*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^
391*54fd6939SJiyong Park
392*54fd6939SJiyong ParkTwo variants of the ``#include`` directive are acceptable in the |TF-A|
393*54fd6939SJiyong Parkcodebase. Correct use of the two styles improves readability by suggesting the
394*54fd6939SJiyong Parklocation of the included header and reducing ambiguity in cases where generic
395*54fd6939SJiyong Parkand platform-specific headers share a name.
396*54fd6939SJiyong Park
397*54fd6939SJiyong ParkFor header files that are in the same directory as the source file that is
398*54fd6939SJiyong Parkincluding them, use the ``"..."`` variant.
399*54fd6939SJiyong Park
400*54fd6939SJiyong ParkFor header files that are **not** in the same directory as the source file that
401*54fd6939SJiyong Parkis including them, use the ``<...>`` variant.
402*54fd6939SJiyong Park
403*54fd6939SJiyong ParkExample (bl1_fwu.c):
404*54fd6939SJiyong Park
405*54fd6939SJiyong Park.. code:: c
406*54fd6939SJiyong Park
407*54fd6939SJiyong Park  #include <assert.h>
408*54fd6939SJiyong Park  #include <errno.h>
409*54fd6939SJiyong Park  #include <string.h>
410*54fd6939SJiyong Park
411*54fd6939SJiyong Park  #include "bl1_private.h"
412*54fd6939SJiyong Park
413*54fd6939SJiyong ParkTypedefs
414*54fd6939SJiyong Park--------
415*54fd6939SJiyong Park
416*54fd6939SJiyong ParkAvoid anonymous typedefs of structs/enums in headers
417*54fd6939SJiyong Park^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
418*54fd6939SJiyong Park
419*54fd6939SJiyong ParkFor example, the following definition:
420*54fd6939SJiyong Park
421*54fd6939SJiyong Park.. code:: c
422*54fd6939SJiyong Park
423*54fd6939SJiyong Park  typedef struct {
424*54fd6939SJiyong Park          int arg1;
425*54fd6939SJiyong Park          int arg2;
426*54fd6939SJiyong Park  } my_struct_t;
427*54fd6939SJiyong Park
428*54fd6939SJiyong Park
429*54fd6939SJiyong Parkis better written as:
430*54fd6939SJiyong Park
431*54fd6939SJiyong Park.. code:: c
432*54fd6939SJiyong Park
433*54fd6939SJiyong Park  struct my_struct {
434*54fd6939SJiyong Park          int arg1;
435*54fd6939SJiyong Park          int arg2;
436*54fd6939SJiyong Park  };
437*54fd6939SJiyong Park
438*54fd6939SJiyong ParkThis allows function declarations in other header files that depend on the
439*54fd6939SJiyong Parkstruct/enum to forward declare the struct/enum instead of including the
440*54fd6939SJiyong Parkentire header:
441*54fd6939SJiyong Park
442*54fd6939SJiyong Park.. code:: c
443*54fd6939SJiyong Park
444*54fd6939SJiyong Park  struct my_struct;
445*54fd6939SJiyong Park  void my_func(struct my_struct *arg);
446*54fd6939SJiyong Park
447*54fd6939SJiyong Parkinstead of:
448*54fd6939SJiyong Park
449*54fd6939SJiyong Park.. code:: c
450*54fd6939SJiyong Park
451*54fd6939SJiyong Park  #include <my_struct.h>
452*54fd6939SJiyong Park  void my_func(my_struct_t *arg);
453*54fd6939SJiyong Park
454*54fd6939SJiyong ParkSome TF definitions use both a struct/enum name **and** a typedef name. This
455*54fd6939SJiyong Parkis discouraged for new definitions as it makes it difficult for TF to comply
456*54fd6939SJiyong Parkwith MISRA rule 8.3, which states that "All declarations of an object or
457*54fd6939SJiyong Parkfunction shall use the same names and type qualifiers".
458*54fd6939SJiyong Park
459*54fd6939SJiyong ParkThe Linux coding standards also discourage new typedefs and checkpatch emits
460*54fd6939SJiyong Parka warning for this.
461*54fd6939SJiyong Park
462*54fd6939SJiyong ParkExisting typedefs will be retained for compatibility.
463*54fd6939SJiyong Park
464*54fd6939SJiyong Park--------------
465*54fd6939SJiyong Park
466*54fd6939SJiyong Park*Copyright (c) 2020, Arm Limited. All rights reserved.*
467*54fd6939SJiyong Park
468*54fd6939SJiyong Park.. _`Linux kernel coding style`: https://www.kernel.org/doc/html/latest/process/coding-style.html
469*54fd6939SJiyong Park.. _`MISRA C:2012 Guidelines`: https://www.misra.org.uk/Activities/MISRAC/tabid/160/Default.aspx
470*54fd6939SJiyong Park.. _`a spreadsheet`: https://developer.trustedfirmware.org/file/download/lamajxif3w7c4mpjeoo5/PHID-FILE-fp7c7acszn6vliqomyhn/MISRA-and-TF-Analysis-v1.3.ods
471