How to see code coverage in Clion

5.4k Views Asked by At

I need to be able to check how well my C++ code covered by unit test in critical places. I'm using Clion as IDE based on Cmake project structure (not sure if something else supported). Is there any ways to get code coverage info with Clion?

2

There are 2 best solutions below

0
On BEST ANSWER

There is no such feature in CLion for now. The feature request exists. Also we are unaware of any existing plugin for code coverage in CLion.

0
On

It is possible with the latest vanilla versions of CLion (e.g. 2020.1.1) - no plugins required.


DETAILS

See official doc.

For example, on Linux (Fedora 31):

  • Depending on compiler, ensure CLion picks the right toolchain:

    Even though CMakeLists.txt sets compiler for the build, it may disagree with the one chosen by IDE to show the coverage (something to be improved).

  • Obviously, install necessary tools outside of IDE and ensure their versions match:

    sudo dnf install clang llvm # ...
    sudo dnf update
    

gcc

# CMakeLists.txt
set(CMAKE_C_COMPILER cc)
set(CMAKE_CXX_COMPILER c++)
set(COMPILE_FLAGS "--coverage")
set(CMAKE_EXE_LINKER_FLAGS "--coverage")

File / Settings... / Build, Execution, Deployment / Toolchain

gcc


clang

# CMakeLists.txt
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(COMPILE_FLAGS "-fprofile-instr-generate -fcoverage-mapping")
set(CMAKE_EXE_LINKER_FLAGS "-fprofile-instr-generate")

File / Settings... / Build, Execution, Deployment / Toolchain

clang