gcovr icon indicating copy to clipboard operation
gcovr copied to clipboard

How to get function coverage from .gcda ?

Open Toolazy2cruel opened this issue 5 years ago • 11 comments

We can add function coverage statistics in html.how to do?

Toolazy2cruel avatar Jul 25 '19 13:07 Toolazy2cruel

Currently, gcovr does not support function coverage. In the meanwhile, you would have to use lcov for coverage reports, which does support this.

The underlying data from GCC/gcov does include per-function statistics, but right now gcovr just ignores those lines. A pull request that parses this data (without directly rendering it in reports) would be most welcome.

A similar features was requested in #68.

latk avatar Jul 25 '19 13:07 latk

Thank you very much for your answer, I will try to solve this problem and hope that interested partners can work together.

Toolazy2cruel avatar Jul 25 '19 13:07 Toolazy2cruel

Thanks! If you'd like to try doing this, look at the contribution guidelines for how to set up a development environment etc.

A couple of pointers:

  • we'd have to add the -f/--function-summaries and -m/--demangled-names option when calling gcov (around here)

  • the function parsing code goes here

  • A function line in the coverage data looks like this:

    function main called 1 returned 1 blocks executed 75%
    

    Note that the function name can look more complex, e.g. Foo<int>::inc()

  • a FileCoverage object only stores LineCoverage. We'd have to add a new FunctionCoverage class similar to the LineCoverage and BranchCoverage objects. The FunctionCoverage would need to keep track of the function name, and the number of calls.

  • the most up to date gcov documentation is here: https://gcc.gnu.org/onlinedocs/gcc/Gcov.html

latk avatar Jul 25 '19 13:07 latk

In older version of gcov tool(4.3.3) the -m/ --demangled-names option doesn't exists wouldnt it be better to pass these flags externally?

k10blogger avatar Sep 21 '19 07:09 k10blogger

In older version of gcov tool(4.3.3) the -m/ --demangled-names option doesn't exists wouldnt it be better to pass these flags externally?

I also found this problem.My solution is to use the shell instruction "c++filt" to get the function name.You can refer to this from my commit ,but there may be a better way.

Toolazy2cruel avatar Sep 22 '19 17:09 Toolazy2cruel

Hello there, does the GCC also generate association between lines, branches and functions, so that one could produce branch and line-coverage per function? I could only find lcov examples that would merely show whether a function was hit or not.

decimad avatar Nov 07 '19 21:11 decimad

@decimad GCC/gcov does know where functions start and end. However, the human-readable .gcov format that we parse does not include this information. At most, we could count lines towards the preceding function, but that will fail in more complex scenarios (e.g. nested functions, templates since GCC 8).

The GCC 9 JSON format and the previous intermediate format do include all the necessary information such as start/end line numbers for functions, but we don't yet have a parser for those formats (#282). The GCC JSON format also explicitly associates each line with a function.

If someone is working on this, I'd be fine with approaches that provide different levels of detail depending on the GCC version, e.g. only summary coverage statistics of functions for GCC 4–8 but detailed data for GCC 9+, and demangled names only for GCC 5+. Of course, it's not necessary to implement all of that at once – even basic function coverage data is still an improvement.

latk avatar Nov 08 '19 11:11 latk

@Toolazy2cruel It seems you have implemented a parser for function coverage? If you could open a pull request with that code, I'd love to merge it into gcovr. Once function coverage is part of the internal data model, we can see how to include it in the various output formats.

latk avatar Nov 08 '19 11:11 latk

@latk Thank you for the clarifications. I poked around the various GCC version's gcno file specs and the version we're stuck with basically only stores function entry lines. However, we're considering C only, in which there are no nested functions and the likes. So I think gcovr output together with that versions gcov intermediate output (which contains the function summaries along with entry line) lets us associciate metrics with functions.

decimad avatar Nov 08 '19 16:11 decimad

@latk : I have started yesterday working on this and have a PR almost ready. I just need to publish the results to all reports (currently only HTML). Some issues:

  • "--function-summaries" has no effect in my gcov version.
  • I have demangled the CPP names with the python module "cpp-demangle" instead of using the build-in options which might be supported in old GCOV

See: https://github.com/gcovr/gcovr/pull/362

gobater avatar Apr 17 '20 07:04 gobater

Hi, Any update on when you are going to support this coverage type?

itayave avatar Jan 24 '21 11:01 itayave

Introduced with #362.

Spacetown avatar Dec 30 '22 21:12 Spacetown