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

标签 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 -pthread")

我不确定这些选项的作用。

编译器生成test.cpp.otest.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.

关于llvm - 尝试使用 llvm-cov 查看代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58400297/

相关文章:

gcc - Address Sanitizer 可以在检测到错误后立即中止吗?

xcode - clang : error: '-I-' not supported, 请改用 -iquote (Xcode 4)

c++ - Windows 上的 Clang/LLVM 6.0.0 不需要静态数据成员声明的定义

c++ - 使用 LLVM pass 加载和存储变量

llvm 传递给扁平化(一些)嵌套循环

Rust:在 LLVM Bitcode 中包含依赖项

llvm - 大函数不内联 llvm -inline pass

llvm - LLVM 构建中的 *.inc 文件是什么

compilation - 如何为堆栈机器编写 LLVM 后端?

c - `__heap_base`在clang 9.0.0中好像不见了,有没有替代品?