1 //===- CoverageSummaryInfo.h - Coverage summary for function/file ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These structures are used to represent code coverage metrics
11 // for functions/files.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_COV_COVERAGESUMMARYINFO_H
16 #define LLVM_COV_COVERAGESUMMARYINFO_H
18 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
19 #include "llvm/Support/raw_ostream.h"
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.
28 /// \brief The total number of regions in a function/file.
32 RegionCoverageInfo() : Covered(0), NumRegions(0) {}
34 RegionCoverageInfo(size_t Covered, size_t NumRegions)
35 : Covered(Covered), NumRegions(NumRegions) {
36 assert(Covered <= NumRegions && "Covered regions over-counted");
39 RegionCoverageInfo &operator+=(const RegionCoverageInfo &RHS) {
40 Covered += RHS.Covered;
41 NumRegions += RHS.NumRegions;
45 void merge(const RegionCoverageInfo &RHS) {
46 Covered = std::max(Covered, RHS.Covered);
47 NumRegions = std::max(NumRegions, RHS.NumRegions);
50 size_t getCovered() const { return Covered; }
52 size_t getNumRegions() const { return NumRegions; }
54 bool isFullyCovered() const { return Covered == NumRegions; }
56 double getPercentCovered() const {
57 assert(Covered <= NumRegions && "Covered regions over-counted");
60 return double(Covered) / double(NumRegions) * 100.0;
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.
69 /// \brief The total number of lines in a function/file.
73 LineCoverageInfo() : Covered(0), NumLines(0) {}
75 LineCoverageInfo(size_t Covered, size_t NumLines)
76 : Covered(Covered), NumLines(NumLines) {
77 assert(Covered <= NumLines && "Covered lines over-counted");
80 LineCoverageInfo &operator+=(const LineCoverageInfo &RHS) {
81 Covered += RHS.Covered;
82 NumLines += RHS.NumLines;
86 void merge(const LineCoverageInfo &RHS) {
87 Covered = std::max(Covered, RHS.Covered);
88 NumLines = std::max(NumLines, RHS.NumLines);
91 size_t getCovered() const { return Covered; }
93 size_t getNumLines() const { return NumLines; }
95 bool isFullyCovered() const { return Covered == NumLines; }
97 double getPercentCovered() const {
98 assert(Covered <= NumLines && "Covered lines over-counted");
101 return double(Covered) / double(NumLines) * 100.0;
105 /// \brief Provides information about function coverage for a file.
106 class FunctionCoverageInfo {
107 /// \brief The number of functions that were executed.
110 /// \brief The total number of functions in this file.
114 FunctionCoverageInfo() : Executed(0), NumFunctions(0) {}
116 FunctionCoverageInfo(size_t Executed, size_t NumFunctions)
117 : Executed(Executed), NumFunctions(NumFunctions) {}
119 void addFunction(bool Covered) {
125 size_t getExecuted() const { return Executed; }
127 size_t getNumFunctions() const { return NumFunctions; }
129 bool isFullyCovered() const { return Executed == NumFunctions; }
131 double getPercentCovered() const {
132 assert(Executed <= NumFunctions && "Covered functions over-counted");
133 if (NumFunctions == 0)
135 return double(Executed) / double(NumFunctions) * 100.0;
139 /// \brief A summary of function's code coverage.
140 struct FunctionCoverageSummary {
142 uint64_t ExecutionCount;
143 RegionCoverageInfo RegionCoverage;
144 LineCoverageInfo LineCoverage;
146 FunctionCoverageSummary(const std::string &Name)
147 : Name(Name), ExecutionCount(0), RegionCoverage(), LineCoverage() {}
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) {}
155 /// \brief Compute the code coverage summary for the given function coverage
157 static FunctionCoverageSummary get(const coverage::CoverageMapping &CM,
158 const coverage::FunctionRecord &Function);
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);
167 /// \brief A summary of file's code coverage.
168 struct FileCoverageSummary {
170 RegionCoverageInfo RegionCoverage;
171 LineCoverageInfo LineCoverage;
172 FunctionCoverageInfo FunctionCoverage;
173 FunctionCoverageInfo InstantiationCoverage;
175 FileCoverageSummary(StringRef Name)
176 : Name(Name), RegionCoverage(), LineCoverage(), FunctionCoverage(),
177 InstantiationCoverage() {}
179 void addFunction(const FunctionCoverageSummary &Function) {
180 RegionCoverage += Function.RegionCoverage;
181 LineCoverage += Function.LineCoverage;
182 FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
185 void addInstantiation(const FunctionCoverageSummary &Function) {
186 InstantiationCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
190 /// \brief A cache for demangled symbols.
191 struct DemangleCache {
192 StringMap<std::string> DemangledNames;
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())
199 return DemangledName->getValue();
205 #endif // LLVM_COV_COVERAGESUMMARYINFO_H