This is an archive of the discontinued LLVM Phabricator instance.

[llvm-cov gcov] Fix calculating coverage statistics of template functions
ClosedPublic

Authored by ikudrin on Mar 10 2022, 9:50 AM.

Details

Summary

Template functions share the same lines in source files, so the common container of lines' properties cannot be used to calculate the coverage statistics of individual functions.

> cat tmpl.cpp
template <int N> int test() { return N; }
int main() { return test<1>() + test<2>(); }
> clang++ --coverage tmpl.cpp -o tmpl
> ./tmpl
> llvm-cov gcov tmpl.cpp -f
...
Function '_Z4testILi1EEiv'
Lines executed:100.00% of 1

Function '_Z4testILi2EEiv'
Lines executed:-nan% of 0
...
> llvm-cov-patched gcov tmpl.cpp -f
...
Function '_Z4testILi1EEiv'
Lines executed:100.00% of 1

Function '_Z4testILi2EEiv'
Lines executed:100.00% of 1
...

Diff Detail

Event Timeline

ikudrin created this revision.Mar 10 2022, 9:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 10 2022, 9:50 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
ikudrin requested review of this revision.Mar 10 2022, 9:50 AM
jhenderson resigned from this revision.Mar 10 2022, 10:41 PM

I don't know a nything about how this works unfortunately, so can't review it really.

FYI, there's a pre-merge test failure and clang-format issue.

curl -L 'https://reviews.llvm.org/D121390?download=1' does not include the contents for tmpl.gcno and tmpl.gcda.

So I just run:

clang++ --coverage tmpl.cpp -o tmpl
./tmpl
llvm-cov gcov tmpl.cpp -t

I do not see a difference with and w/o the patch.

Template functions share the same lines in source files, so the common container of lines' properties cannot be used to calculate the coverage of individual functions.

Suppose there is indeed a difference. It's useful to attach a C++ program and line execution counts before and after this patch, for the relevant lines.

llvm/test/tools/llvm-cov/gcov/tmpl.cpp
1 ↗(On Diff #414399)

llvm-cov gcov %s --gcno=%p/Inputs/tmpl.gcno --gcda=%p/Inputs/tmpl.gcda

14 ↗(On Diff #414399)

It's useful to dump llvm-cov gcov -t and test the line counts for the three lines, to better leverage this test file.

ikudrin edited the summary of this revision. (Show Details)Mar 11 2022, 10:48 AM
ikudrin updated this revision to Diff 414932.Mar 13 2022, 7:13 AM
ikudrin marked an inline comment as done.
ikudrin retitled this revision from [llvm-cov gcov] Fix calculating coverage of template functions to [llvm-cov gcov] Fix calculating coverage statistics of template functions.
ikudrin edited the summary of this revision. (Show Details)
ikudrin added a subscriber: jhenderson.
  • Fixed a formatting issue
  • Updated the test

FYI, there's a pre-merge test failure

That is probably because tmpl.gcda and tmpl.gcno are binaries and I do not know how to upload them to the review. I have tried git diff --binary for this patch, but with little success.

and clang-format issue.

Thanks! Fixed.

curl -L 'https://reviews.llvm.org/D121390?download=1' does not include the contents for tmpl.gcno and tmpl.gcda.

So I just run:

clang++ --coverage tmpl.cpp -o tmpl
./tmpl
llvm-cov gcov tmpl.cpp -t

I do not see a difference with and w/o the patch.

The patch fixes -f, not -t.

Template functions share the same lines in source files, so the common container of lines' properties cannot be used to calculate the coverage of individual functions.

Suppose there is indeed a difference. It's useful to attach a C++ program and line execution counts before and after this patch, for the relevant lines.

Added an example into the description.

llvm/test/tools/llvm-cov/gcov/tmpl.cpp
14 ↗(On Diff #414399)

I'm not sure I follow you. The patch fixes and the test checks the coverage statistics for template functions (-f), not execution counts (-t).

MaskRay accepted this revision.Mar 13 2022, 3:14 PM
> llvm-cov gcov tmpl.cpp -f
...
Function '_Z4testILi2EEiv'
Lines executed:-nan% of 0

Perhaps include

Function '_Z4testILi1EEiv'
Lines executed:100.00% of 1

in the message as well.

llvm/lib/ProfileData/GCOV.cpp
666

Optional: llvm:: is usually omitted for llvm/lib files.

llvm/test/tools/llvm-cov/gcov/tmpl.cpp
14 ↗(On Diff #414399)

I suggest using llvm-cov gcov -t -f to better leverage the test. I know this patch fixes an issue. But with little efforts (adding line execution counts for -t) this patch can add more coverage that we currently lack (there are very little C++ template tests): test<1>() + test<2>(); on the same line may be interesting for the line execution counts.

This revision is now accepted and ready to land.Mar 13 2022, 3:14 PM
ikudrin updated this revision to Diff 415104.Mar 14 2022, 8:47 AM
ikudrin marked 3 inline comments as done.
ikudrin edited the summary of this revision. (Show Details)
  • Removed llvm::
  • Update the test
llvm/test/tools/llvm-cov/gcov/tmpl.cpp
14 ↗(On Diff #414399)

-t suppresses -f, and a passed source file name is ignored if --gcno=/--gcda= are specified, so I have to rework the test

This revision was landed with ongoing or failed builds.Mar 15 2022, 9:48 AM
This revision was automatically updated to reflect the committed changes.