【问题标题】:clang -coverage marks function signatures as not coveredclang -coverage 将函数签名标记为未覆盖
【发布时间】:2015-07-19 23:39:35
【问题描述】:

我正在尝试使用 clang -coverage 为我的 C 项目生成覆盖信息,但所有函数签名都被标记为未覆盖,而我希望它们根本不会被检测。

小例子——这个文件是hello.c:

extern int puts(const char*);

int main(int argc, char *argv[]) {
  puts("hello, world");
  return 0;
}

在此示例中,我希望将第 4 行和第 5 行标记为可执行,但不是第 3 行。我像这样运行 clang -coveragelcov 以获得初始覆盖:

$ cc -coverage hello.c
$ lcov -c -i -d . -o coverage.base
Capturing coverage data from .
Found gcov version: 3.6.0
Found LLVM gcov version 3.4, which emulates gcov version 4.2
Scanning . for .gcno files ...
Found 1 graph files in .
Processing hello.gcno
Finished .info-file creation

生成的覆盖率文件如下所示:

TN:
SF:/Users/isbadawi/cov/hello.c
FN:3,main
FNDA:0,main
FNF:1
FNH:0
DA:3,0
DA:4,0
DA:5,0
LF:3
LH:0
end_of_record

我希望DA:3,0 行不存在——或者至少,如果存在,我希望它会在最终覆盖报告中被标记。当我继续运行 a.out 并再次运行 lcov 以生成完整的覆盖率报告时,如下所示:

$ ./a.out
$ lcov -q -c -d . -o coverage.run
$ lcov -q -d . -a coverage.base -a coverage.run -o coverage.total

coverage.total 看起来像这样:

TN:
SF:/Users/isbadawi/cov/hello.c
FN:3,main
FNDA:1,main
FNF:1
FNH:1
DA:3,0
DA:4,1
DA:5,1
LF:3
LH:2
end_of_record

我最终得到 66.7% 的覆盖率,而不是 100%。我做错了什么?

我在 OS X Yosemite 上使用系统叮当声:

Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix

用 gcc 做同样的事情(用 4.8.2 试过),该行仍然被标记为可执行,但最终的覆盖率报告已经被执行了。

【问题讨论】:

    标签: c clang llvm code-coverage lcov


    【解决方案1】:

    编译器生成的代码并不总是与您的代码行直接对应。这可能是这里的问题。当 LCOV 报告没有覆盖块(函数或其他)的 右大括号 时,我遇到了类似的挫败感,例如:

    if (x < lim) {
      process_x(x);
      ++x_processed;
    } // <== this line is listed as NOT COVERED
    

    因此,解决方法是使用 LCOV 关键字标记。在这种情况下:

    if (x < lim) {
      process_x(x);
      ++x_processed;
    } // LCOV_EXCL_LINE
    

    如果您在第 3 行使用上述标记,那应该可以解决您的报告问题。

    int main(int argc, char *argv[]) {   // LCOV_EXCL_LINE
    

    有相应的标记来排除一系列线,以及排除一条线或一系列线的分支覆盖。查看geninfo 联机帮助页了解完整的详细信息。这是一个在线版本:http://ltp.sourceforge.net/coverage/lcov/geninfo.1.php

    注意:这些关键字可以与有意义的 cmets 共享线路(即,它们不必为了正常运行而单独在线上)。

    【讨论】:

      【解决方案2】:

      这个错误应该在 Clang 8 中修复。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-30
        • 2017-07-09
        • 1970-01-01
        相关资源
        最近更新 更多