Shruti Gupta | 57b8038 | 2024-04-24 11:35:18 +0100 | [diff] [blame] | 1 | .. SPDX-License-Identifier: BSD-3-Clause |
| 2 | .. SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 3 | |
| 4 | ************************* |
| 5 | Cppcheck Application Note |
| 6 | ************************* |
| 7 | |
| 8 | Cppcheck is an open source static analysis tool for C/C++. In addition to |
| 9 | various static analysis, it also has an addon to verify compliance with MISRA |
| 10 | C 2012. Please refer to `Cppcheck Project Page`_ for details on Cppcheck. |
| 11 | |
| 12 | Cppcheck can be run standalone or along with MISRA addon from within the RMM |
| 13 | build system. TF-RMM aims to have 0 outstanding errors with the recommended |
Soby Mathew | 78bdfb1 | 2024-05-09 16:58:04 +0100 | [diff] [blame] | 14 | Cppcheck version mentioned in :ref:`tool_dependencies`. |
Shruti Gupta | 57b8038 | 2024-04-24 11:35:18 +0100 | [diff] [blame] | 15 | |
| 16 | Installing Cppcheck |
| 17 | =================== |
| 18 | |
| 19 | Cppcheck can be installed directly from various package managers or built from |
| 20 | source. However installing from package manager can get you an outdated |
| 21 | version. |
| 22 | |
| 23 | For building from source, please refer to `Cppcheck GitHub`_ for downloading |
| 24 | recommended version and build guidelines. Once Cppcheck is built, add both |
Soby Mathew | 78bdfb1 | 2024-05-09 16:58:04 +0100 | [diff] [blame] | 25 | Cppcheck binary folder and Cppcheck-htmlreport folder to PATH. The latter |
Shruti Gupta | 57b8038 | 2024-04-24 11:35:18 +0100 | [diff] [blame] | 26 | is used to convert Cppcheck XML output into user friendly html report. |
Soby Mathew | 78bdfb1 | 2024-05-09 16:58:04 +0100 | [diff] [blame] | 27 | Asssuming that `build` is the output folder for Cppcheck build: |
Shruti Gupta | 57b8038 | 2024-04-24 11:35:18 +0100 | [diff] [blame] | 28 | |
| 29 | .. code-block:: bash |
| 30 | |
| 31 | export PATH=$cppcheck_root/build/bin:$cppcheck_root/htmlreport:$PATH |
| 32 | cppcheck --version |
| 33 | |
| 34 | The Cppcheck version should report the recommended version. |
| 35 | |
| 36 | Invoking Cppcheck rule within TF-RMM build system |
| 37 | ================================================= |
| 38 | |
| 39 | If you own a valid copy of a MISRA rules file, copy the file to the below |
| 40 | location as it will give a more descriptive error message on detecting MISRA |
| 41 | errors. |
| 42 | |
| 43 | .. code-block:: bash |
| 44 | |
| 45 | cp -a <path to the misra rules file>/<file name> ${RMM_SOURCE_DIR}/tools/cppcheck/misra.rules |
| 46 | |
| 47 | To invoke the standard Cppcheck static analysis build rule on TF-RMM, run the |
| 48 | `cppcheck` build target after TF-RMM configuration : |
| 49 | |
| 50 | .. code-block:: bash |
| 51 | |
| 52 | cd $rmm_root |
| 53 | cmake -DRMM_CONFIG=fvp_defcfg -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON |
| 54 | cmake --build build -- cppcheck |
| 55 | |
| 56 | The `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON` generates a compile_commands.json |
| 57 | file containing the exact compiler calls for all translation units of the |
| 58 | project in machine-readable form. |
| 59 | |
| 60 | The successful execution of the build target will generate `cppcheck.xml` |
| 61 | in `build/tools/cppcheck` folder. |
| 62 | |
| 63 | To invoke the Cppcheck static analysis with MISRA addon, run the |
| 64 | `cppcheck-misra` build target: |
| 65 | |
| 66 | .. code-block:: bash |
| 67 | |
| 68 | cd $rmm_root |
| 69 | cmake -DRMM_CONFIG=fvp_defcfg -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON |
| 70 | cmake --build build -- cppcheck-misra |
| 71 | |
| 72 | This will generate `cppcheck_misra.xml` in `build/tools/cppcheck` folder. |
| 73 | |
Sona Mathew | 4471356 | 2024-08-11 23:18:32 -0500 | [diff] [blame] | 74 | If the above generated xml contains error tags detected by Cppcheck and |
| 75 | if the installed version of Cppcheck matches or exceeds the recommended |
| 76 | version mentioned in :ref:`tool_dependencies`, the build fails and prints |
| 77 | the error count. However, if the installed version is less than the |
| 78 | recommended version, a warning is generated and the output is not parsed |
| 79 | for errors. |
| 80 | |
Shruti Gupta | 57b8038 | 2024-04-24 11:35:18 +0100 | [diff] [blame] | 81 | Generating the Cppcheck HTML report |
| 82 | =================================== |
| 83 | |
| 84 | To generate html report in current directory after the Cppcheck build target |
| 85 | has executed, run `cppcheck-htmlreport` tool with the genenerated xml file as |
| 86 | input. For example, after the `cppcheck-misra` build target has completed, |
| 87 | use the below cmd line to generate the html report : |
| 88 | |
| 89 | .. code-block:: bash |
| 90 | |
| 91 | cppcheck-htmlreport --file=./build/tools/cppcheck/cppcheck_misra.xml --report-dir=test --source-dir=. |
| 92 | |
| 93 | The output will be generated in the specified `report-dir` and, for the above |
| 94 | command, the html report can be found at `./test/index.html`. |
| 95 | |
| 96 | Cppcheck Error Suppression |
| 97 | ========================== |
| 98 | |
| 99 | TF-RMM as a project has decided to suppress some rules because either the rule |
| 100 | is not found to be useful for the project or there are too many false positives |
| 101 | generated by the rule. The global suppression rules are specified via |
| 102 | `suppressions.txt` file present in `tools/cppcheck` directory. |
| 103 | |
| 104 | If more suppressions need to be added for Cppcheck, it can be done by adding it |
| 105 | to the suppression rules file. For example, to skip `ext` folder from Cppcheck |
| 106 | analysis, add the following line to the file : |
| 107 | |
| 108 | .. code-block:: bash |
| 109 | |
| 110 | *:*/ext/* |
| 111 | |
| 112 | Suppression can be added inline to code as a comment. For example, to suppress |
| 113 | the `uninitvar` rule on a particular line, add the following comment above the |
| 114 | line : |
| 115 | |
| 116 | .. code-block:: C |
| 117 | |
| 118 | /* cppcheck-suppress uninitvar */ |
| 119 | |
| 120 | Multiple rules can be disabled via this method, as shown in example below : |
| 121 | |
| 122 | .. code-block:: C |
| 123 | |
| 124 | /* cppcheck-suppress [arrayIndexOutOfBounds, uninitvar] */ |
| 125 | |
| 126 | If a certain rule needs to be suppressed for a block of code, the block |
| 127 | suppression format can be used as shown in example below: |
| 128 | |
| 129 | .. code-block:: C |
| 130 | |
Soby Mathew | 78bdfb1 | 2024-05-09 16:58:04 +0100 | [diff] [blame] | 131 | /* cppcheck-suppress-begin uninitvar */ |
Shruti Gupta | 57b8038 | 2024-04-24 11:35:18 +0100 | [diff] [blame] | 132 | block_of_code; |
Soby Mathew | 78bdfb1 | 2024-05-09 16:58:04 +0100 | [diff] [blame] | 133 | /* cppcheck-suppress-end uninitvar */ |
Shruti Gupta | 57b8038 | 2024-04-24 11:35:18 +0100 | [diff] [blame] | 134 | |
| 135 | .. _Cppcheck Project Page: https://cppcheck.sourceforge.io/ |
| 136 | .. _Cppcheck GitHub: https://github.com/danmar/cppcheck |