diff --git a/llvm/lib/ProfileData/GCOV.cpp b/llvm/lib/ProfileData/GCOV.cpp index 22062e60af2ab9..feacf40b8d0ae6 100644 --- a/llvm/lib/ProfileData/GCOV.cpp +++ b/llvm/lib/ProfileData/GCOV.cpp @@ -13,6 +13,7 @@ #include "llvm/ProfileData/GCOV.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallSet.h" #include "llvm/Config/llvm-config.h" #include "llvm/Demangle/Demangle.h" #include "llvm/Support/Debug.h" @@ -662,6 +663,8 @@ void Context::collectFunction(GCOVFunction &f, Summary &summary) { if (f.startLine >= si.startLineToFunctions.size()) si.startLineToFunctions.resize(f.startLine + 1); si.startLineToFunctions[f.startLine].push_back(&f); + SmallSet lines; + SmallSet linesExec; for (const GCOVBlock &b : f.blocksRange()) { if (b.lines.empty()) continue; @@ -670,9 +673,9 @@ void Context::collectFunction(GCOVFunction &f, Summary &summary) { si.lines.resize(maxLineNum + 1); for (uint32_t lineNum : b.lines) { LineInfo &line = si.lines[lineNum]; - if (!line.exists) + if (lines.insert(lineNum).second) ++summary.lines; - if (line.count == 0 && b.count) + if (b.count && linesExec.insert(lineNum).second) ++summary.linesExec; line.exists = true; line.count += b.count; diff --git a/llvm/test/tools/llvm-cov/gcov/Inputs/tmpl.cpp b/llvm/test/tools/llvm-cov/gcov/Inputs/tmpl.cpp new file mode 100644 index 00000000000000..287b541443a4ba --- /dev/null +++ b/llvm/test/tools/llvm-cov/gcov/Inputs/tmpl.cpp @@ -0,0 +1,4 @@ +template +int test() { return N; } + +int main() { return test<1>() + test<2>(); } diff --git a/llvm/test/tools/llvm-cov/gcov/Inputs/tmpl.gcda b/llvm/test/tools/llvm-cov/gcov/Inputs/tmpl.gcda new file mode 100644 index 00000000000000..d235d3c3de2390 Binary files /dev/null and b/llvm/test/tools/llvm-cov/gcov/Inputs/tmpl.gcda differ diff --git a/llvm/test/tools/llvm-cov/gcov/Inputs/tmpl.gcno b/llvm/test/tools/llvm-cov/gcov/Inputs/tmpl.gcno new file mode 100644 index 00000000000000..13fab3e7f77a8a Binary files /dev/null and b/llvm/test/tools/llvm-cov/gcov/Inputs/tmpl.gcno differ diff --git a/llvm/test/tools/llvm-cov/gcov/tmpl.test b/llvm/test/tools/llvm-cov/gcov/tmpl.test new file mode 100644 index 00000000000000..7494ef33f64303 --- /dev/null +++ b/llvm/test/tools/llvm-cov/gcov/tmpl.test @@ -0,0 +1,19 @@ +# Check that the coverage statistics for template functions are calculated as expected. + +RUN: rm -rf %t +RUN: mkdir %t +RUN: cd %t +RUN: cp %p/Inputs/tmpl* . + +RUN: llvm-cov gcov tmpl.cpp -f | FileCheck %s --check-prefix=F +RUN: llvm-cov gcov tmpl.cpp -t | FileCheck %s --check-prefix=T + +F: Function '_Z4testILi1EEiv' +F-NEXT: Lines executed:100.00% of 1 +F: Function '_Z4testILi2EEiv' +F-NEXT: Lines executed:100.00% of 1 + +T: -: 1:template +T-NEXT: 2: 2:int test() { return N; } +T-NEXT: -: 3: +T-NEXT: 1: 4:int main() { return test<1>() + test<2>(); }