Search code examples
c++cmakecppcheck

Cppcheck + CMake - skip third-party sources


I am working on a large codebase consisting of Modern C++, Legacy C++ (30+ years old MFC) and a number of third-party libraries (Boost, range-v3, Catch2 etc.).

The code is built using CMake and targeted to Windows and Linux (x86 & ARM).
For the modern code, I am using Cppcheck for static analysis. Unfortunately, when I build with Cppcheck enabled, a lot of time is spent running Cppcheck over the third-party source (*.cpp) files.

The third party sources are included via the CMake FetchContent syntax, e.g.

Include(FetchContent)
FetchContent_Declare(
  Catch2
  GIT_REPOSITORY https://github.com/catchorg/Catch2.git
  GIT_TAG        v3.0.1
  SYSTEM
)
FetchContent_MakeAvailable(Catch2)

Cppcheck is enabled via:

set(CMAKE_CXX_CPPCHECK
                       --suppress=missingInclude
                       --suppress=missingIncludeSystem
                       --suppress=unusedFunction
                       --suppress=unmatchedSuppression
                       --suppress=functionStatic
                       --suppress=funcArgNamesDifferent
                       --suppress=*:*_deps/*
                       --enable=all
                       --inline-suppr
                       --inconclusive
                       --force
                       -i ${CMAKE_SOURCE_DIR}/imgui/lib
                       )

The _deps directory is the location where third party sources are fetched and built, and the line --suppress=*:*_deps/* disables Cppcheck for the headers but does not prevent it from running when each third party library is built.

How can I prevent Cppcheck from being run over the third party libraries?

Is there something I can add to the CMake section which pulls in the library to disable it? Such as clearing CMAKE_CXX_CPPCHECK before the call to FetchContent_Declare and setting it again after the call FetchContent_MakeAvailable


Solution

  • Such as clearing CMAKE_CXX_CPPCHECK before the call to FetchContent_Declare and setting it again after the call FetchContent_MakeAvailable

    That's the best way to do it to prevent cmake from running Cppcheck on sources you don't want it to.

    Try this:

    set(old_cmake_cxx_cppcheck "${CMAKE_CXX_CPPCHECK}")
    set(CMAKE_CXX_CPPCHECK "")
    // FetchContent stuff here...
    set(CMAKE_CXX_CPPCHECK "${old_cmake_cxx_cppcheck")