views:

625

answers:

3

Currently we use gcov with our testing suite for Linux C++ application and it does a good job at measuring line coverage.

Can gcov produce function/method coverage report in addition to line coverage?

Looking at the parameters gcov accepts I do not think it is possible, but I may be missing something. Or, probably, is there any other tool that can produce function/method coverage report out of statistics generated by gcc?

Update: By function/method coverage I mean percentage of functions that get executed during tests.

+4  A: 

I guess what you mean is the -f option, which will give you the percentage of lines covered per function. There is an interesting article about gcov at Dr. Dobb's which might be helpful. If "man gcov" doesn't show the -f flag, check if you have a reasobably recent version of the gcc suite.

Edit: to get the percentage of functions not executed you can simply parse through the function coverage output, as 0.00% coverage should be pretty much equivalent to not called. This small script prints the percentage of functions not executed:

#!/bin/bash

if test -z "$1"
then
    echo "First argument must be function coverage file"
else
    notExecuted=`cat $1 | grep "^0.00%" | wc -l`
    executed=`cat $1 | grep -v "^0.00%" | wc -l`

    percentage=$(echo "scale=2; $notExecuted / ($notExecuted + $executed) * 100" |bc)

    echo $percentage
fi
VolkA
-f option is not exactly what I want. It gives line coverage by function, but I want function/method coverage. I added clarification to the question.
Dima Malenko
+5  A: 

We have started to use gcov and lcov together. The results from lcov do include the percentage of functions that are executed for the "module" you're looking at.

EDIT: The module can go from directories down to files.

I also want to add that if you are already using the GNU compiler tools, then gcov/lcov won't be too difficult for you to get running and the results it produces are very impressive.

Richard Corden
+1  A: 

The lcov utility is nice, and we use it. But I'm not sure if you need it for what you want.

We

  1. Use ctags (wikipedia; sourceforge) to find all the functions declared in the relevant header files.

  2. Run GCOV to get line coverage for every function in the binary.

  3. Compare the list of functions from 1 & 2 to produce "Functions Called" / "Functions Available".

We call it "API coverage" since we apply step #1 only to public API headers. But you can do it on all headers or only a subset as you choose. I think the ratio we produce in this manner is the ratio you are looking for.

talkaboutquality