CLion 2020.2 Help

Code Coverage

In CLion, you can run CMake applications and tests with code coverage measurements. Code coverage results provide the percentage of code lines executed during a run and the number of times a particular line was hit.

CLion relies on llvm-cov/gcov integration to collect and show code coverage data. These tools require special coverage compiler flags, which you can pass manually or let CLion add them automatically.

Running CMake applications or tests with coverage

The Run with Coverage action is available for CMake Application and test configurations (Boost.Test, Google Test, or Catch).

You can call Run with Coverage from the toolbar next to the configuration switcher or from the gutter menu:

Run with Coverage

Run with Coverage

When you call Run with Coverage, but no coverage files (.gcov or .profraw) are found, CLion suggests adding the coverage flags:

Coverage notification

If you click Fix and rerun, CLion will do the following:

  1. Search for an existing CMake profile, which has the same build type, toolchain, and CMake options as in the currently used profile, but with added compiler options for coverage (see Coverage compiler flags). If one is found, CLion will switch to it and use it to run your configuration with coverage.

  2. If no such profile is found, CLion will create a new one, copying the Build type, Toolchain, and CMake Options parameters from the current profile and passing the coverage flags as CMAKE_CXX_FLAGS and CMAKE_C_FLAGS in CMake options. Then CLion will switch to this newly created profile and use it to run you configuration with coverage.

    Automatically added CMake profile with coverage flags

Coverage compiler flags

If you pass coverage flags manually, you can use one of the following options depending on the compiler and coverage tooling you prefer:

  • GCC

    -fprofile-arcs -ftest-coverage or --coverage

    In this case, the gcov tool will be used.

  • Clang / Clang-cl

    Two options here:

You can provide the flags by setting the CMAKE_CXX_FLAGS variable (CMAKE_C_FLAGS for C projects) or using other alternatives like the add_compile_options command.

Reading coverage results

  • When coverage data is ready, the Coverage tool window opens up automatically. It shows the percentage of the files per folder and lines per file covered during the launch:

    Coverage tool window

  • In any file, you can check the gutter indicator and click it next to a particular line to learn how many times it was hit:

    Coverage line hits

    A line executed fully is marked green. Yellow marker means that it was only partically executed.

    To modify the colors of coverage highlighting, click icons.general.settings.png or go to Settings / Preferences | Editor | Color Scheme | General and expand the Line Coverage node.

  • CLion also shows coverage statistics in the Project view:

    Code coverage results in Project view

Merging results from several runs

When you rerun coverage analysis, CLion prompts you to choose how you prefer the new results to be presented:

Merging several coverage suites

Add to active suites – new results will be added to the previously collected statistics.

Replace active suites – the already collected data will be overwritten.

Do not apply collected coverage data – new results will be ignored.

Coverage settings

Code coverage settings are gathered in the Settings / Preferences | Build, Execution, Deployment | Coverage:

Code coverage settings

In the Tools section, you can provide custom paths to gcov / llvm-cov / llvm-profdata. By default, CLion takes the paths from the PATH environment varible.

Troubleshooting and current limitations

  • If you get empty coverage reports, check whether your compiler version matches the version of gcov / llvm-cov tool (default or custom) that you are using.

    Compiler and coverage tool mismatch notification

    Keep this in mind when changing compilers or switching from one Windows toolchain to another.

  • Code coverage is not yet supported for the Remote toolchain (CPP-17709).

Last modified: 16 July 2020