Skip to content
  • Lingkai Dong's avatar
    ci: Run cppcheck using list of files and flags from CMake · 74437922
    Lingkai Dong authored
    
    
    The latest cppcheck supports reading `compile_commands.json` from the
    CMake build directory to get a list of files to check and compiler flags
    to use. This is better than simply checking all files recursively
    without dedicated flags for each file, which we have been doing before,
    because
    * when checking all files recursively in one invocation of cppcheck,
    cppcheck treats code from all examples as part of one big project and
    incorrectly warns of duplicated class definitions from different
    examples
    * the compiler flags from `compile_commands.json` provide more complete
    information to cppcheck such as header include paths (among others), so
    that cppcheck can, for example, perform complete analysis of both class
    declaration in header and definition in source in one combined context,
    catching problems that have been missed before.
    
    Thus, run cppcheck using `compile_commands.json` generated by CMake, in
    each build job in CI. Disable the pre-commit hook that checks on all
    files recursively in one go.
    
    Notes:
    * We suppress a few types of errors:
      - `internalAstError`: cppcheck internal error, which we can't handle
      - `preprocessorErrorDirective`: `#include` failure in glue code, due
      to missing config headers (e.g. `mbedtls_config.h`) and macros when
      the current example (e.g. littlefs) doesn't really need to provide the
      header
      - `unmatchedSuppression`: the two aforementioned suppressions are not
      matched/needed
    * CMake has built-in support for cppcheck, which can be enabled via the
    variables `CMAKE_{C,CXX}_CPPCHECK`. But if we use it, we would need to
    set those variables wherever we add our own code and unset it wherever
    we make available fetched third-party code (which likely fails checks).
    It is more intrusive and less handy than passing `compile_commands.json`
    directly to cppcheck and filtering out files from the build directory.
    
    Signed-off-by: Lingkai Dong's avatarLingkai Dong <lingkai.dong@arm.com>
    74437922
Loading