]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-cov/CoverageSummaryInfo.h
Merge ^/head r312207 through r312308.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-cov / CoverageSummaryInfo.h
1 //===- CoverageSummaryInfo.h - Coverage summary for function/file ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // These structures are used to represent code coverage metrics
11 // for functions/files.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_COV_COVERAGESUMMARYINFO_H
16 #define LLVM_COV_COVERAGESUMMARYINFO_H
17
18 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
19 #include "llvm/Support/raw_ostream.h"
20
21 namespace llvm {
22
23 /// \brief Provides information about region coverage for a function/file.
24 struct RegionCoverageInfo {
25   /// \brief The number of regions that were executed at least once.
26   size_t Covered;
27
28   /// \brief The number of regions that weren't executed.
29   size_t NotCovered;
30
31   /// \brief The total number of regions in a function/file.
32   size_t NumRegions;
33
34   RegionCoverageInfo() : Covered(0), NotCovered(0), NumRegions(0) {}
35
36   RegionCoverageInfo(size_t Covered, size_t NumRegions)
37       : Covered(Covered), NotCovered(NumRegions - Covered),
38         NumRegions(NumRegions) {}
39
40   RegionCoverageInfo &operator+=(const RegionCoverageInfo &RHS) {
41     Covered += RHS.Covered;
42     NotCovered += RHS.NotCovered;
43     NumRegions += RHS.NumRegions;
44     return *this;
45   }
46
47   bool isFullyCovered() const { return Covered == NumRegions; }
48
49   double getPercentCovered() const {
50     if (NumRegions == 0)
51       return 0.0;
52     return double(Covered) / double(NumRegions) * 100.0;
53   }
54 };
55
56 /// \brief Provides information about line coverage for a function/file.
57 struct LineCoverageInfo {
58   /// \brief The number of lines that were executed at least once.
59   size_t Covered;
60
61   /// \brief The number of lines that weren't executed.
62   size_t NotCovered;
63
64   /// \brief The total number of lines in a function/file.
65   size_t NumLines;
66
67   LineCoverageInfo() : Covered(0), NotCovered(0), NumLines(0) {}
68
69   LineCoverageInfo(size_t Covered, size_t NumLines)
70       : Covered(Covered), NotCovered(NumLines - Covered), NumLines(NumLines) {}
71
72   LineCoverageInfo &operator+=(const LineCoverageInfo &RHS) {
73     Covered += RHS.Covered;
74     NotCovered += RHS.NotCovered;
75     NumLines += RHS.NumLines;
76     return *this;
77   }
78
79   bool isFullyCovered() const { return Covered == NumLines; }
80
81   double getPercentCovered() const {
82     if (NumLines == 0)
83       return 0.0;
84     return double(Covered) / double(NumLines) * 100.0;
85   }
86 };
87
88 /// \brief Provides information about function coverage for a file.
89 struct FunctionCoverageInfo {
90   /// \brief The number of functions that were executed.
91   size_t Executed;
92
93   /// \brief The total number of functions in this file.
94   size_t NumFunctions;
95
96   FunctionCoverageInfo() : Executed(0), NumFunctions(0) {}
97
98   FunctionCoverageInfo(size_t Executed, size_t NumFunctions)
99       : Executed(Executed), NumFunctions(NumFunctions) {}
100
101   void addFunction(bool Covered) {
102     if (Covered)
103       ++Executed;
104     ++NumFunctions;
105   }
106
107   bool isFullyCovered() const { return Executed == NumFunctions; }
108
109   double getPercentCovered() const {
110     if (NumFunctions == 0)
111       return 0.0;
112     return double(Executed) / double(NumFunctions) * 100.0;
113   }
114 };
115
116 /// \brief A summary of function's code coverage.
117 struct FunctionCoverageSummary {
118   StringRef Name;
119   uint64_t ExecutionCount;
120   RegionCoverageInfo RegionCoverage;
121   LineCoverageInfo LineCoverage;
122
123   FunctionCoverageSummary(StringRef Name) : Name(Name), ExecutionCount(0) {}
124
125   FunctionCoverageSummary(StringRef Name, uint64_t ExecutionCount,
126                           const RegionCoverageInfo &RegionCoverage,
127                           const LineCoverageInfo &LineCoverage)
128       : Name(Name), ExecutionCount(ExecutionCount),
129         RegionCoverage(RegionCoverage), LineCoverage(LineCoverage) {
130   }
131
132   /// \brief Compute the code coverage summary for the given function coverage
133   /// mapping record.
134   static FunctionCoverageSummary
135   get(const coverage::FunctionRecord &Function);
136
137   /// \brief Update the summary with information from another instantiation
138   /// of this function.
139   void update(const FunctionCoverageSummary &Summary);
140 };
141
142 /// \brief A summary of file's code coverage.
143 struct FileCoverageSummary {
144   StringRef Name;
145   RegionCoverageInfo RegionCoverage;
146   LineCoverageInfo LineCoverage;
147   FunctionCoverageInfo FunctionCoverage;
148   FunctionCoverageInfo InstantiationCoverage;
149
150   FileCoverageSummary(StringRef Name) : Name(Name) {}
151
152   void addFunction(const FunctionCoverageSummary &Function) {
153     RegionCoverage += Function.RegionCoverage;
154     LineCoverage += Function.LineCoverage;
155     FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
156   }
157
158   void addInstantiation(const FunctionCoverageSummary &Function) {
159     InstantiationCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
160   }
161 };
162
163 } // namespace llvm
164
165 #endif // LLVM_COV_COVERAGESUMMARYINFO_H