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