5

I am trying to set up a basic project, and I want to use gcov. When I use g++, it works:

g++ main.cpp whatever.cpp -fprofile-arcs -ftest-coverage

The output of gcov is OK:

gcov main.gcno 
main.gcda:cannot open data file, assuming not executed
File 'main.cpp'
Lines executed:0.00% of 20
Creating 'main.cpp.gcov'

File '/usr/include/c++/7/iostream'
Lines executed:0.00% of 1
Creating 'iostream.gcov'

However, I need to use clang. After running the compile command:

clang++-6.0 main.cpp whatever.cpp -fprofile-arcs -ftest-coverage

I get the following error:

main.gcno:version '402*', prefer 'A73*'

gcov: out of memory allocating 16158246392 bytes after a total of 0 bytes

My gcov version is 7.3.0, same as gcc and g++.

Any idea what is wrong and what I can do about it?

Thanks!

Serban Stoenescu
  • 3,136
  • 3
  • 22
  • 41
  • 1
    Did you mean `llvm-cov`? Clang has it's own coverage source code intrumentation method, currently I don't know if it is compatible with gcov. – AmeyaVS Jun 19 '19 at 10:40

2 Answers2

11

I faced a similar mismatching version issue when trying to generate html reports with lcov and xml reports with gcovr.

lcov

I added the parameter --gcov-tool gcov_for_clang.sh.

gcovr

I added the parameter --gcov-executable gcov_for_clang.sh.

gcov_for_clang.sh

Contains:

#!/bin/bash
exec llvm-cov-6.0 gcov "$@"

Rationale: lcov's --gcov-tool cannot handle the space between llvm-cov-6.0 and gcov, hence the bash script indirection. Don't forget to make the file executable!

Back to your question

I guess you simply need to replace gcov main.gcno by llvm-cov-6.0 gcov main.gcno. This isn't using LLVM's genuine source-based coverage facility but its gcov-compatible coverage implementation.

Note on compiler options

In my setup I simply pass --coverage as compiler option, this should be sufficient.

Roland Sarrazin
  • 1,203
  • 11
  • 29
0

Here's reference to get Source based code coverage report from Clang and associated utilities.

Update: As noted by @Lothar in the comment the above-mentioned link now points to Source-Based Code Coverage supported by Clang Tooling, gcov compatible example section has been removed.

For gcov compatible documentation one can refer here for the llvm-cov gcov command line invocation.

AmeyaVS
  • 814
  • 10
  • 26
  • 2
    This once pointed to a valid version of the Clang manual but now at CLang 12 it only contains a description of the new sanitizer coverage "-fprofile-instr-generate" not the gcov format anymore. – Lothar Dec 07 '20 at 02:55
  • Thanks for the heads up @Lothar , have updated the answer to point to `llvm-cov gcov` documentation. But the other answer might be more relevant. – AmeyaVS Dec 07 '20 at 11:45