问题
I developed a simple example to test gcovr and gcov:
#include <iostream>
int main (int argc, const char * argv[])
{
std::cout << argc << std::endl;
if(argc == 1)
{
int y = 1;
std::cout << "Argc > 1" << std::endl;
}
if(argc == 2) std::cout << "Argc > 2" << std::endl;
if(argc == 3)
{
std::cout << "Argc > 3" << std::endl;
}
int i = 34;
i = i * i;
return 0;
}
And a script for coverage report generation:
#! /bin/bash
rm -rf build-run
mkdir build-run
cd build-run
g++ -O6 -DDEBUG=0 --coverage -ftest-coverage -fprofile-arcs -c -o main.o ../main.cpp
g++ -O6 -DDEBUG=0 --coverage -fprofile-arcs -ftest-coverage -lgcov -o coverage ./main.o
./coverage > out
./coverage --help > out
./coverage --help --out > out
gcovr -v -kpbu -r .. -o ../branch-report.txt
gcovr -v -kpu -r .. -o ../report.txt
I got coverage 80% using -b option and it point me on the last line in main block. It seems to me that it should be 100% for such scenario or not?
回答1:
This is an issue with gcov. If you look at the underlying gcov output [which the example driver is so courteous to leave for us as build-run/^#main.cpp.gcov], you see:
[…snip…]
3: 21: return 0;
function _Z41__static_initialization_and_destruction_0ii called 3 returned 100% blocks executed 100%
6: 22:}
branch 0 taken 3 (fallthrough)
branch 1 taken 0
branch 2 taken 3 (fallthrough)
branch 3 taken 0
function _GLOBAL__I_main called 3 returned 100% blocks executed 100%
3: 23:/*EOF*/
call 0 returned 3
I think what is being reported is branch coverage for the destructors of static members of objects in the iostream library. … while we try and filter out most of the gcov weirdness through gcovr, this is one of the cases that we cannot reliably ignore.
Bill Hart John Siirola
P.S. I encourage you to submit gcovr tickets on the gcovr Trac page: https://software.sandia.gov/trac/fast/newticket
来源:https://stackoverflow.com/questions/9475970/gcovr-branch-coverage-for-simple-case