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