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