]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-cov/CoverageSummaryInfo.h
Update Apache Serf to 1.3.9 to support OpenSSL 1.1.1.
[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 class RegionCoverageInfo {
25   /// \brief The number of regions that were executed at least once.
26   size_t Covered;
27
28   /// \brief The total number of regions in a function/file.
29   size_t NumRegions;
30
31 public:
32   RegionCoverageInfo() : Covered(0), NumRegions(0) {}
33
34   RegionCoverageInfo(size_t Covered, size_t NumRegions)
35       : Covered(Covered), NumRegions(NumRegions) {
36     assert(Covered <= NumRegions && "Covered regions over-counted");
37   }
38
39   RegionCoverageInfo &operator+=(const RegionCoverageInfo &RHS) {
40     Covered += RHS.Covered;
41     NumRegions += RHS.NumRegions;
42     return *this;
43   }
44
45   void merge(const RegionCoverageInfo &RHS) {
46     Covered = std::max(Covered, RHS.Covered);
47     NumRegions = std::max(NumRegions, RHS.NumRegions);
48   }
49
50   size_t getCovered() const { return Covered; }
51
52   size_t getNumRegions() const { return NumRegions; }
53
54   bool isFullyCovered() const { return Covered == NumRegions; }
55
56   double getPercentCovered() const {
57     assert(Covered <= NumRegions && "Covered regions over-counted");
58     if (NumRegions == 0)
59       return 0.0;
60     return double(Covered) / double(NumRegions) * 100.0;
61   }
62 };
63
64 /// \brief Provides information about line coverage for a function/file.
65 class LineCoverageInfo {
66   /// \brief The number of lines that were executed at least once.
67   size_t Covered;
68
69   /// \brief The total number of lines in a function/file.
70   size_t NumLines;
71
72 public:
73   LineCoverageInfo() : Covered(0), NumLines(0) {}
74
75   LineCoverageInfo(size_t Covered, size_t NumLines)
76       : Covered(Covered), NumLines(NumLines) {
77     assert(Covered <= NumLines && "Covered lines over-counted");
78   }
79
80   LineCoverageInfo &operator+=(const LineCoverageInfo &RHS) {
81     Covered += RHS.Covered;
82     NumLines += RHS.NumLines;
83     return *this;
84   }
85
86   void merge(const LineCoverageInfo &RHS) {
87     Covered = std::max(Covered, RHS.Covered);
88     NumLines = std::max(NumLines, RHS.NumLines);
89   }
90
91   size_t getCovered() const { return Covered; }
92
93   size_t getNumLines() const { return NumLines; }
94
95   bool isFullyCovered() const { return Covered == NumLines; }
96
97   double getPercentCovered() const {
98     assert(Covered <= NumLines && "Covered lines over-counted");
99     if (NumLines == 0)
100       return 0.0;
101     return double(Covered) / double(NumLines) * 100.0;
102   }
103 };
104
105 /// \brief Provides information about function coverage for a file.
106 class FunctionCoverageInfo {
107   /// \brief The number of functions that were executed.
108   size_t Executed;
109
110   /// \brief The total number of functions in this file.
111   size_t NumFunctions;
112
113 public:
114   FunctionCoverageInfo() : Executed(0), NumFunctions(0) {}
115
116   FunctionCoverageInfo(size_t Executed, size_t NumFunctions)
117       : Executed(Executed), NumFunctions(NumFunctions) {}
118
119   void addFunction(bool Covered) {
120     if (Covered)
121       ++Executed;
122     ++NumFunctions;
123   }
124
125   size_t getExecuted() const { return Executed; }
126
127   size_t getNumFunctions() const { return NumFunctions; }
128
129   bool isFullyCovered() const { return Executed == NumFunctions; }
130
131   double getPercentCovered() const {
132     assert(Executed <= NumFunctions && "Covered functions over-counted");
133     if (NumFunctions == 0)
134       return 0.0;
135     return double(Executed) / double(NumFunctions) * 100.0;
136   }
137 };
138
139 /// \brief A summary of function's code coverage.
140 struct FunctionCoverageSummary {
141   std::string Name;
142   uint64_t ExecutionCount;
143   RegionCoverageInfo RegionCoverage;
144   LineCoverageInfo LineCoverage;
145
146   FunctionCoverageSummary(const std::string &Name)
147       : Name(Name), ExecutionCount(0), RegionCoverage(), LineCoverage() {}
148
149   FunctionCoverageSummary(const std::string &Name, uint64_t ExecutionCount,
150                           const RegionCoverageInfo &RegionCoverage,
151                           const LineCoverageInfo &LineCoverage)
152       : Name(Name), ExecutionCount(ExecutionCount),
153         RegionCoverage(RegionCoverage), LineCoverage(LineCoverage) {}
154
155   /// \brief Compute the code coverage summary for the given function coverage
156   /// mapping record.
157   static FunctionCoverageSummary get(const coverage::CoverageMapping &CM,
158                                      const coverage::FunctionRecord &Function);
159
160   /// Compute the code coverage summary for an instantiation group \p Group,
161   /// given a list of summaries for each instantiation in \p Summaries.
162   static FunctionCoverageSummary
163   get(const coverage::InstantiationGroup &Group,
164       ArrayRef<FunctionCoverageSummary> Summaries);
165 };
166
167 /// \brief A summary of file's code coverage.
168 struct FileCoverageSummary {
169   StringRef Name;
170   RegionCoverageInfo RegionCoverage;
171   LineCoverageInfo LineCoverage;
172   FunctionCoverageInfo FunctionCoverage;
173   FunctionCoverageInfo InstantiationCoverage;
174
175   FileCoverageSummary(StringRef Name)
176       : Name(Name), RegionCoverage(), LineCoverage(), FunctionCoverage(),
177         InstantiationCoverage() {}
178
179   void addFunction(const FunctionCoverageSummary &Function) {
180     RegionCoverage += Function.RegionCoverage;
181     LineCoverage += Function.LineCoverage;
182     FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
183   }
184
185   void addInstantiation(const FunctionCoverageSummary &Function) {
186     InstantiationCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
187   }
188 };
189
190 /// \brief A cache for demangled symbols.
191 struct DemangleCache {
192   StringMap<std::string> DemangledNames;
193
194   /// \brief Demangle \p Sym if possible. Otherwise, just return \p Sym.
195   StringRef demangle(StringRef Sym) const {
196     const auto DemangledName = DemangledNames.find(Sym);
197     if (DemangledName == DemangledNames.end())
198       return Sym;
199     return DemangledName->getValue();
200   }
201 };
202
203 } // namespace llvm
204
205 #endif // LLVM_COV_COVERAGESUMMARYINFO_H