9

I'm in the process of (finally!) setting up code coverage monitoring for my brand new C++ project. Due to the fact that I need some advanced C++20 features (read, coroutines), I am using clang 6 as compiler.

Now, I followed this guide on how to do basic code coverage for your project, and everything worked like magic. If I do:

clang++ -fprofile-instr-generate -fcoverage-mapping test.cpp -o test.out
LLVM_PROFILE_FILE="coverage/test.profraw" ./test.out
llvm-profdata merge -sparse coverage/test.profraw -o coverage/test.profdata
llvm-cov show ./test.out -instr-profile=coverage/test.profdata

I get a nice, colored report on my terminal that tells me what is covered and what is not.

So far so good! I thought I was close to what I wanted, but then the pain started when I tried to get the report uploaded to codecov.io.

I have tried a few things, including:

  • Running their https://codecov.io/bash script on my coverage folder in the hope that maybe it would catch on my test.profdata. No dice, and it makes sense, since even llvm-cov needs the path to the executable file to run.

  • Using the export functionality: when running llvm-cov export --instr-profile=coverage/test.profdata ./test.out I get a good-looking JSON file (via terminal). I tried throwing the output in a coverage.json file, which actually got uploaded, but then codecov just says that there was an error parsing it, with no further information.

I'm feeling completely lost. Everything seems so black-box-ish on their website that I just don't understand how to get anything done that doesn't by chance perfectly fit the cases that they can manage.

How can I get this working with codecov? If codecov can't handle my reports, is there any other equivalent online code coverage that I can use to get this to work?

4

2 回答 2

7

看起来 bash 脚本 codecov 用于将覆盖率数据上传到他们的站点,它会寻找与它所理解的格式相关的各种模式匹配的文件。这些记录很少,但是您至少可以通过查看Github 上的脚本来了解哪些模式是可行的。当然,这并不能告诉您 codecov 对匹配给定模式的文件格式有什么期望,正如您在coverage.json文件被拒绝时发现的那样。

通过反复试验,我发现以下内容会生成一个文件,当您运行 bash 脚本时,codecov 将正确解释该文件:

llvm-cov show ./test.out -instr-profile=default.profdata > coverage.txt

我尚未广泛测试允许使用哪些文件名,但似乎您可以在将覆盖数据传送到的文件名之间coverage和文件名中放置任何其他字符(例如,您可以调用它)。.txtcoverage_my_file_name.txt

编辑:以防万一这对任何人都有帮助,事实证明,上述的一个重要推论是,避免将任何不是覆盖报告的内容命名为与此模式匹配的内容至关重要。我刚刚处理了一个场景,其中我有一堆名为coverage_[more_text_here].out 的可执行文件与报告一起上传。事实证明,尝试将汇编代码解析为覆盖率报告可能会导致 codecov 神秘地失败而没有任何有用的错误。

于 2018-06-16T02:08:40.127 回答
1

另一种选择是使用 GCOV 分析,它比基于源的精确度稍差,但 codecov.io 支持。您需要“--coverage”编译器标志来启用它。

您可以使用grcov(也可以从https://github.com/mozilla/grcov/releases下载)解析 gcno/gcda 文件并通过 codecov.io bash 上传器上传它们:

grcov OBJ_DIR -s SRC_DIR -t lcov --branch > lcov.info
bash codecov.sh -f "lcov.info"

我计划为 grcov 添加对基于源的报告的支持,这也将更容易支持 codecov 上的格式。

于 2018-10-10T09:56:43.317 回答