Is it possible to measure function coverage with gcov?

Dima Malenko picture Dima Malenko · Feb 10, 2009 · Viewed 7.6k times · Source

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.

Answer

VolkA picture VolkA · Feb 10, 2009

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