0

I have project which i check with cppcheck 1.90, but i want to unset few defines, because it takes a lot of time to check with all defines.

Example: tests.cpp:

int main() {
  int x,y;
#ifdef A
  x = 5;
#else
  x = 10;
#endif

#ifdef B
  y = 5;
#else
  y = 10;
#endif

#ifdef C
  y = 5*x;
#else
  y = 10*x;
#endif

  int c = x+y;
}

When i work with file without project - there is no problem:

cppcheck ../tests.cpp --force -UB
Checking ../tests.cpp ...
Checking ../tests.cpp: A...
Checking ../tests.cpp: C...

I unset define B - and cppcheck don't check compile path with B. But how to do it with file generated from cmake project?

cmake ../ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
cppcheck --project=./compile_commands.json --force -UB
Checking /home/builder/bugs/test_cppcheck/tests.cpp ...
Checking /home/builder/bugs/test_cppcheck/tests.cpp: A...
Checking /home/builder/bugs/test_cppcheck/tests.cpp: B...
Checking /home/builder/bugs/test_cppcheck/tests.cpp: C...

As i can see cppcheck is ignored "-U" key. Is any way to unset define in that case?

Add cmakefile for test example:

CMakeLists.txt
cmake_minimum_required(VERSION 3.6)

project(tests)

set(SRC
    tests.cpp
)

set(NAME test)
add_executable(${NAME} ${SRC})

AlexBG
  • 386
  • 1
  • 7

3 Answers3

1

You have an option to edit the CMakeLists file only once and pass arguments as needed, To achieve this you should

In the CMakeLists file add:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} *hard_coded_flags*")

Command line :

When calling cmake from command line you add CMAKE_CXX_FLAGS = -UB=1 (or any other cppcheck flag), this will concatenate -UB=1 the to the hard_coded_flags.

joepol
  • 744
  • 6
  • 22
1

Another way is to have cmake run cppcheck itself. This can be done by setting CMAKE_CXX_CPPCHECK (see the documentation). You can either set CMAKE_CXX_CPPCHECK in your CMakeLists.txt, or you can set it on the command-line that invokes cmake.

This does have some disadvantages (for example, each source file is checked individually, so cross-file checking doesn't work).

But there are several advantages:

  • No need to invoke cppcheck separately.
  • If you set CMAKE_CXX_CPPCHECK in your CMakeLists.txt, you can also add cppcheck command-line options to control what it does. In your case, you could add -UB.
  • This helps encapsulate all knowledge of how to build and check your project into a single file (CMakeLists.txt).

I typically follow a pattern something like this in my CMakeLists.txt:

option(ENABLE_CODE_ANALYSIS "Run code analysis" OFF)
if(ENABLE_CODE_ANALYSIS)
    find_program(cppcheck cppcheck)
    message(STATUS "cppcheck    ${cppcheck}")
    if(NOT (cppcheck MATCHES "NOTFOUND"))
        set(CMAKE_CXX_CPPCHECK "${cppcheck}"
            "--enable=all"
            "--inconclusive"
            "--inline-suppr"
            "--quiet"
            "--suppress=missingInclude"
            "--suppress=unmatchedSuppression"
            "--suppress=unusedFunction"
            "--template='{file}:{line}: warning: {id} ({severity}): {message}'")
    endif()
endif(ENABLE_CODE_ANALYSIS)

Then you can do code analysis by doing cmake -DENABLE_CODE_ANALYSIS=ON.

Eric Backus
  • 1,614
  • 1
  • 12
  • 29
0

One of possible decision is change CMakeLists file(unsetting define):

cmake_minimum_required(VERSION 3.6)
project(tests)
set(SRC
    tests.cpp
)
set(CMAKE_CXX_FLAGS "-UB=1")
set(NAME test)
add_executable(${NAME} ${SRC})

And output is:

$ cmake ../ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-- Configuring done
-- Generating done
-- Build files have been written to: /home/builder/bugs/test_cppcheck/build
$ cppcheck --project=./compile_commands.json --force
Checking /home/builder/bugs/test_cppcheck/tests.cpp ...
Checking /home/builder/bugs/test_cppcheck/tests.cpp: A...
Checking /home/builder/bugs/test_cppcheck/tests.cpp: C...

There is no "B" path checking. But if exist solution without changing CMakeLists - it would be better.

AlexBG
  • 386
  • 1
  • 7