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