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