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 struct RegionCoverageInfo {
25 /// \brief The number of regions that were executed at least once.
28 /// \brief The number of regions that weren't executed.
31 /// \brief The total number of regions in a function/file.
34 RegionCoverageInfo() : Covered(0), NotCovered(0), NumRegions(0) {}
36 RegionCoverageInfo(size_t Covered, size_t NumRegions)
37 : Covered(Covered), NotCovered(NumRegions - Covered),
38 NumRegions(NumRegions) {}
40 RegionCoverageInfo &operator+=(const RegionCoverageInfo &RHS) {
41 Covered += RHS.Covered;
42 NotCovered += RHS.NotCovered;
43 NumRegions += RHS.NumRegions;
47 bool isFullyCovered() const { return Covered == NumRegions; }
49 double getPercentCovered() const {
52 return double(Covered) / double(NumRegions) * 100.0;
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.
61 /// \brief The number of lines that weren't executed.
64 /// \brief The total number of lines in a function/file.
67 LineCoverageInfo() : Covered(0), NotCovered(0), NumLines(0) {}
69 LineCoverageInfo(size_t Covered, size_t NumLines)
70 : Covered(Covered), NotCovered(NumLines - Covered), NumLines(NumLines) {}
72 LineCoverageInfo &operator+=(const LineCoverageInfo &RHS) {
73 Covered += RHS.Covered;
74 NotCovered += RHS.NotCovered;
75 NumLines += RHS.NumLines;
79 bool isFullyCovered() const { return Covered == NumLines; }
81 double getPercentCovered() const {
84 return double(Covered) / double(NumLines) * 100.0;
88 /// \brief Provides information about function coverage for a file.
89 struct FunctionCoverageInfo {
90 /// \brief The number of functions that were executed.
93 /// \brief The total number of functions in this file.
96 FunctionCoverageInfo() : Executed(0), NumFunctions(0) {}
98 FunctionCoverageInfo(size_t Executed, size_t NumFunctions)
99 : Executed(Executed), NumFunctions(NumFunctions) {}
101 void addFunction(bool Covered) {
107 bool isFullyCovered() const { return Executed == NumFunctions; }
109 double getPercentCovered() const {
110 if (NumFunctions == 0)
112 return double(Executed) / double(NumFunctions) * 100.0;
116 /// \brief A summary of function's code coverage.
117 struct FunctionCoverageSummary {
119 uint64_t ExecutionCount;
120 RegionCoverageInfo RegionCoverage;
121 LineCoverageInfo LineCoverage;
123 FunctionCoverageSummary(StringRef Name) : Name(Name), ExecutionCount(0) {}
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) {
132 /// \brief Compute the code coverage summary for the given function coverage
134 static FunctionCoverageSummary
135 get(const coverage::FunctionRecord &Function);
137 /// \brief Update the summary with information from another instantiation
138 /// of this function.
139 void update(const FunctionCoverageSummary &Summary);
142 /// \brief A summary of file's code coverage.
143 struct FileCoverageSummary {
145 RegionCoverageInfo RegionCoverage;
146 LineCoverageInfo LineCoverage;
147 FunctionCoverageInfo FunctionCoverage;
148 FunctionCoverageInfo InstantiationCoverage;
150 FileCoverageSummary(StringRef Name) : Name(Name) {}
152 void addFunction(const FunctionCoverageSummary &Function) {
153 RegionCoverage += Function.RegionCoverage;
154 LineCoverage += Function.LineCoverage;
155 FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
158 void addInstantiation(const FunctionCoverageSummary &Function) {
159 InstantiationCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
163 /// \brief A cache for demangled symbols.
164 struct DemangleCache {
165 StringMap<std::string> DemangledNames;
167 /// \brief Demangle \p Sym if possible. Otherwise, just return \p Sym.
168 StringRef demangle(StringRef Sym) const {
169 const auto DemangledName = DemangledNames.find(Sym);
170 if (DemangledName == DemangledNames.end())
172 return DemangledName->getValue();
178 #endif // LLVM_COV_COVERAGESUMMARYINFO_H