尝试使用llvm cov查看代码覆盖率

尝试使用llvm cov查看代码覆盖率,llvm,llvm-clang,Llvm,Llvm Clang,我正在用一些*.c文件构建一个库,并且在子文件夹test中有一个测试文件。构建工具有CMake、CLang和ninja。它在Windows 10和Unbuntu 16.04上运行。我正在尝试生成代码覆盖率以供查看 在Unbuntu上,我的CMakeLists.txt包含以下行 set(CMAKE_CXX_FLAGS "-g -O0 -Wall -fprofile-arcs -ftest-coverage -fprofile-instr-generate -fcoverage-mapping -p

我正在用一些
*.c
文件构建一个库,并且在子文件夹
test
中有一个测试文件。构建工具有CMake、CLang和ninja。它在Windows 10和Unbuntu 16.04上运行。我正在尝试生成代码覆盖率以供查看

在Unbuntu上,我的CMakeLists.txt包含以下行

set(CMAKE_CXX_FLAGS "-g -O0 -Wall -fprofile-arcs -ftest-coverage -fprofile-instr-generate -fcoverage-mapping -pthread")
我不知道这些选项有什么作用

编译器生成
test.cpp.o
test.cpp.gcno
。运行测试程序会生成
test.cpp.gcda

使用这些文件运行
llvm cov

llvm-cov show: for the -instr-profile option: must be specified at least once!
我也看到过类似这样的错误

llvm-cov gcov: Not enough positional command line arguments specified!
我明白了
用法:llvm cov gcov[options]SOURCEFILE
但从未见过
SOURCEFILE
是什么的解释或示例


我感兴趣的是看哪行代码至少被使用一次。我缺少什么?

尝试运行此命令

llvm-cov gcov *.gcno
这将显示所有已编译源文件的覆盖范围

有关参数的其他信息:

-fprofile-arcs
Add code so that program flow arcs are instrumented. During execution the program records how many times each branch and call is executed and how many times it is taken or returns. On targets that support constructors with priority support, profiling properly handles constructors, destructors and C++ constructors (and destructors) of classes which are used as a type of a global variable.

When the compiled program exits it saves this data to a file called auxname.gcda for each source file. The data may be used for profile-directed optimizations (-fbranch-probabilities), or for test coverage analysis (-ftest-coverage). Each object file’s auxname is generated from the name of the output file, if explicitly specified and it is not the final executable, otherwise it is the basename of the source file. In both cases any suffix is removed (e.g. foo.gcda for input file dir/foo.c, or dir/foo.gcda for output file specified as -o dir/foo.o). See Cross-profiling.

--coverage
This option is used to compile and link code instrumented for coverage analysis. The option is a synonym for -fprofile-arcs -ftest-coverage (when compiling) and -lgcov (when linking). See the documentation for those options for more details.

Compile the source files with -fprofile-arcs plus optimization and code generation options. For test coverage analysis, use the additional -ftest-coverage option. You do not need to profile every source file in a program.
Compile the source files additionally with -fprofile-abs-path to create absolute path names in the .gcno files. This allows gcov to find the correct sources in projects where compilations occur with different working directories.
Link your object files with -lgcov or -fprofile-arcs (the latter implies the former).
Run the program on a representative workload to generate the arc profile information. This may be repeated any number of times. You can run concurrent instances of your program, and provided that the file system supports locking, the data files will be correctly updated. Unless a strict ISO C dialect option is in effect, fork calls are detected and correctly handled without double counting.
For profile-directed optimizations, compile the source files again with the same optimization and code generation options plus -fbranch-probabilities (see Options that Control Optimization).
For test coverage analysis, use gcov to produce human readable information from the .gcno and .gcda files. Refer to the gcov documentation for further information.
With -fprofile-arcs, for each function of your program GCC creates a program flow graph, then finds a spanning tree for the graph. Only arcs that are not on the spanning tree have to be instrumented: the compiler adds code to count the number of times that these arcs are executed. When an arc is the only exit or only entrance to a block, the instrumentation code can be added to the block; otherwise, a new basic block must be created to hold the instrumentation code.

-ftest-coverage
Produce a notes file that the gcov code-coverage utility (see gcov—a Test Coverage Program) can use to show program coverage. Each source file’s note file is called auxname.gcno. Refer to the -fprofile-arcs option above for a description of auxname and instructions on how to generate test coverage data. Coverage data matches the source files more closely if you do not optimize.