Search code examples
ccode-coveragegcov

GCOV static library coverage for C source code


I want to perform code coverage on a static library. For this I wrote test cases using boost. In my library I have many functions defined in header files.

For example in a header file accuracy.h I have the following functions

static float absf( float x )
{
    return (x >= 0.0f) ? x : -x;
}

static boolean almost_zero( float n, float tol )
{
    return (boolean)(absf( n ) <= tol);
}

I have written test cases for these functions. But the problem is GCOV shows these functions are not covered. If I move the function definition to C file then I get the proper coverage results.

I have used -fprofile-arcs -ftest-coverag for performing coverage. Does anyone has any idea on this issue.

Note:
Test cases are executed properly. I have confirmed it by debugging.
I am using MinGW gcc version 4.8.1 (GCC).


Solution

  • Functions in header files are difficult for coverage. It's not just a technical difficulty - it's also a presentation difficulty. These functions are copied every time the header is #included. Does full coverage require that all copies are covered? Or that one instance is covered?

    From the user's perspective, both answers may be wrong.

    Also, there are likely to be functions lurking in header files that the user does not care about. For instance, ctype.h has a few of these.

    That's probably why coverage tools tend to ignore them entirely.

    I work on a coverage tool, RapiCover, and our approach is to ignore them by default but provide an option to turn on coverage for headers. The option can be used on a file-by-file basis, and you can also specifically name the functions that you want coverage for. We found that this was the best way to support typical customer requirements.

    I suggest you try forcing gcov to believe that the functions are defined in C source code rather than the header. To do this, preprocess your source file (e.g. -E option for GCC) and then filter out the # markers that indicate files and line numbers. Then do gcov on this preprocessed, filtered file. It should see all functions as part of the source code. This trick would also work with RapiCover, though it would not be necessary there.