Is it possible to measure function coverage using gcov?

We are currently using gcov with our Linux C ++ testing kit, and it does an excellent job of measuring line coverage.

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

Looking at the parameters accepted by gcov, I do not think it is possible, but I can miss something. Or maybe there is another tool that can generate a coverage report of functions / methods from statistics generated by gcc?

Update: By scope of a function / method, I mean the percentage of functions that are performed during the tests.

+7
c ++ unit-testing code-coverage gcov
source share
3 answers

I assume you mean the -f option, which will give you the percentage of lines covered by each function. There is an interesting article about gcov in Dr. Dobb's that may be helpful. If "man gcov" does not show the -f flag, check if you have an updated version of the gcc package.

Edit: to get the percentage of functions that are not being executed, you can simply analyze the output of the function, since a coverage of 0.00% should be almost equivalent to not called. This small script prints the percentage of functions performed:

#!/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 
+6
source share

We started using gcov and lcov together. Lcov results include the percentage of functions that are performed for the “module” you are looking for.

EDIT: The module can go from directories to files.

I also want to add that if you are already using the GNU compiler tools, then gcov / lcov will not be too complicated for you, and the results it produces are very impressive.

+6
source share

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

We

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

  • Run GCOV to get the line coverage for each function in binary format.

  • Compare the list of functions from 1 and 2 to create “Assigned Functions” / “Available Functions”.

We call this “API coverage” since we apply step # 1 only to public API headers. But you can do this in all headers or just a subset of your choice. I think that we produce in this way the attitude you are looking for.

+5
source share

All Articles