[llvm-dev] Code coverage for member functions that are defined inside the class

Chen, Yuanfang via llvm-dev llvm-dev at lists.llvm.org
Tue Jun 2 16:43:46 PDT 2020


Hello,

We have a user that wants to get the code coverage report for his library without turning on instrumentation for the library clients or change how they are built (only the library is instrumented). It seems like the inline member functions defined in headers are not instrumented in this case because the clients are not instrumented. The library itself does not have a copy of the inline methods either.

In below example,
> clang++ -fprofile-instr-generate -fcoverage-mapping -c foobar.cpp -o foobar.o
> clang++ main.cpp foobar.o -o foobar
> LLVM_PROFILE_FILE="foo.profraw" ./foobar
> llvm-profdata merge -sparse foo.profraw -o foo.profdata
> llvm-cov show ./foobar -instr-profile=foo.profdata

"""
    1|       |#include "foobar.h"
    2|       |
    3|       |
    4|       |void FooBar::foo(void)
    5|      1|{
    6|      1|  printf("foo\n");
    7|      1|}
    8|       |
"""



foobar.h
"""
#pragma once
#include <stdio.h>

class FooBar {
public:
     void foo(void);
     void bar(void) {
           printf("bar\n");
     }
};
"""

foobar.cpp
"""
#include "foobar.h"
void FooBar::foo(void) {
        printf("foo\n");
}
"""

main.cpp
"""
#include "foobar.h"
int main(int argc, const char *argv[]) {
    FooBar foobar;
    foobar.foo();
    foobar.bar();
    return 0
}
"""

My question are:
- Is there an existing solution to this use case?
- If not, is there a compiler switch to use when compiling foobar library to make inline function methods behave like an out-of-line non-inline function? (there are hundreds of inline function methods which makes doing this manually hard)
- Is there some function attribute such as [[instrument-for-coverage]] to say: I want this function to be instrumented no matter what the command switches are? If not, is it a good idea?


Thanks,
- Yuanfang


More information about the llvm-dev mailing list