xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-cov/CoverageSummaryInfo.cpp (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 //===- CoverageSummaryInfo.cpp - Coverage summary for function/file -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // These structures are used to represent code coverage metrics
10 // for functions/files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CoverageSummaryInfo.h"
15 
16 using namespace llvm;
17 using namespace coverage;
18 
19 FunctionCoverageSummary
20 FunctionCoverageSummary::get(const CoverageMapping &CM,
21                              const coverage::FunctionRecord &Function) {
22   // Compute the region coverage.
23   size_t NumCodeRegions = 0, CoveredRegions = 0;
24   for (auto &CR : Function.CountedRegions) {
25     if (CR.Kind != CounterMappingRegion::CodeRegion)
26       continue;
27     ++NumCodeRegions;
28     if (CR.ExecutionCount != 0)
29       ++CoveredRegions;
30   }
31 
32   // Compute the line coverage
33   size_t NumLines = 0, CoveredLines = 0;
34   CoverageData CD = CM.getCoverageForFunction(Function);
35   for (const auto &LCS : getLineCoverageStats(CD)) {
36     if (!LCS.isMapped())
37       continue;
38     ++NumLines;
39     if (LCS.getExecutionCount())
40       ++CoveredLines;
41   }
42 
43   return FunctionCoverageSummary(
44       Function.Name, Function.ExecutionCount,
45       RegionCoverageInfo(CoveredRegions, NumCodeRegions),
46       LineCoverageInfo(CoveredLines, NumLines));
47 }
48 
49 FunctionCoverageSummary
50 FunctionCoverageSummary::get(const InstantiationGroup &Group,
51                              ArrayRef<FunctionCoverageSummary> Summaries) {
52   std::string Name;
53   if (Group.hasName()) {
54     Name = std::string(Group.getName());
55   } else {
56     llvm::raw_string_ostream OS(Name);
57     OS << "Definition at line " << Group.getLine() << ", column "
58        << Group.getColumn();
59   }
60 
61   FunctionCoverageSummary Summary(Name);
62   Summary.ExecutionCount = Group.getTotalExecutionCount();
63   Summary.RegionCoverage = Summaries[0].RegionCoverage;
64   Summary.LineCoverage = Summaries[0].LineCoverage;
65   for (const auto &FCS : Summaries.drop_front()) {
66     Summary.RegionCoverage.merge(FCS.RegionCoverage);
67     Summary.LineCoverage.merge(FCS.LineCoverage);
68   }
69   return Summary;
70 }
71