Mo Kanj
Mo Kanj

Reputation: 127

Cppcheck integration with CMake for SOME targets

What I couldn't figure out is that if I had a CMake project with subfolders holding their own CMakeLists.txt and their own targets: I want to integrate Cppcheck for specific targets and not all of them. So What would I need to include in the root CMakeLists.txt and what would change in the CMakeLists of the subfolders in this case?

Upvotes: 2

Views: 2232

Answers (1)

KamilCuk
KamilCuk

Reputation: 140880

Set <LANG>_CPPCHECK target property for every target you want to and clear it from every target you do not want to check with cppcheck.


The following CMakeLists.txt file:

cmake_minimum_required (VERSION 3.11)
project(test)
file(WRITE a.c "int main() { int a[10]; a[10] = 0; }")
file(WRITE b.c "int main() { int b[10]; b[10] = 0; }")
file(WRITE c.c "int main() { int c[10]; c[10] = 0; }")
file(WRITE a.h "static inline void f() { int a[10]; a[10] = 0; }")
file(WRITE b.h "static inline void f() { int b[10]; b[10] = 0; }")
file(WRITE c.h "static inline void f() { int c[10]; c[10] = 0; }")
add_executable(a a.c a.h)
add_executable(b b.c b.h)
add_executable(c c.c c.h)
set(cppcheck
  cppcheck
  "--enable=warning"
  "--inconclusive"
  "--force" 
  "--inline-suppr"
)
set_target_properties(a PROPERTIES C_CPPCHECK ${cppcheck})
set_target_properties(b PROPERTIES C_CPPCHECK ${cppcheck})

Results in that only a.c and b.c files are checked, and c.c file is not checked.

$ cmake -S. -B_build -GNinja ; cmake --build _build
-- The C compiler identification is GNU 11.1.0
-- The CXX compiler identification is GNU 11.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /dev/shm/.1000.home.tmp.dir/10/_build
[3/6] Building C object CMakeFiles/b.dir/b.c.o
Checking ../b.c ...
../b.c:1:26: error: Array 'b[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]
int main() { int b[10]; b[10] = 0; }
                         ^
[4/6] Building C object CMakeFiles/a.dir/a.c.o
Checking ../a.c ...
../a.c:1:26: error: Array 'a[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]
int main() { int a[10]; a[10] = 0; }
                         ^
[6/6] Linking C executable a

however, if I wanna execute cppcheck --project=compile_commands.json, it would still output style issues in targets I ignored.

Yes, cppcheck gets all the files from compile_commands.json and checks them. If you want to use --project, parse compile_commands.json with a JSON tools to filter only the files you are interested in and remove the files you are not interested in checking, and then pass the resulting file to cppcheck.

Upvotes: 3

Related Questions