blob: 0845e2ce2e776fe2e6f686d91cf64d900f4d700e [file] [log] [blame]
Alex Lorenz6c7a6a12014-08-22 22:56:03 +00001//===- 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
Easwaran Ramana96d53702016-04-29 18:53:05 +000018#include "llvm/ProfileData/Coverage/CoverageMapping.h"
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000019#include "llvm/Support/raw_ostream.h"
20
21namespace llvm {
22
Adrian Prantl26b584c2018-05-01 15:54:18 +000023/// Provides information about region coverage for a function/file.
Vedant Kumarc94f3922017-09-15 23:00:01 +000024class RegionCoverageInfo {
Adrian Prantl26b584c2018-05-01 15:54:18 +000025 /// The number of regions that were executed at least once.
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000026 size_t Covered;
27
Adrian Prantl26b584c2018-05-01 15:54:18 +000028 /// The total number of regions in a function/file.
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000029 size_t NumRegions;
30
Vedant Kumarc94f3922017-09-15 23:00:01 +000031public:
Vedant Kumar9f82f262017-09-15 23:00:00 +000032 RegionCoverageInfo() : Covered(0), NumRegions(0) {}
Justin Bogner9bdb1942015-02-14 02:01:24 +000033
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000034 RegionCoverageInfo(size_t Covered, size_t NumRegions)
Vedant Kumar310c2802017-09-15 23:00:02 +000035 : Covered(Covered), NumRegions(NumRegions) {
36 assert(Covered <= NumRegions && "Covered regions over-counted");
37 }
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000038
Justin Bogner9bdb1942015-02-14 02:01:24 +000039 RegionCoverageInfo &operator+=(const RegionCoverageInfo &RHS) {
40 Covered += RHS.Covered;
Justin Bogner9bdb1942015-02-14 02:01:24 +000041 NumRegions += RHS.NumRegions;
42 return *this;
43 }
44
Vedant Kumarc94f3922017-09-15 23:00:01 +000045 void merge(const RegionCoverageInfo &RHS) {
46 Covered = std::max(Covered, RHS.Covered);
Vedant Kumar310c2802017-09-15 23:00:02 +000047 NumRegions = std::max(NumRegions, RHS.NumRegions);
Vedant Kumarc94f3922017-09-15 23:00:01 +000048 }
49
50 size_t getCovered() const { return Covered; }
51
52 size_t getNumRegions() const { return NumRegions; }
53
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000054 bool isFullyCovered() const { return Covered == NumRegions; }
55
56 double getPercentCovered() const {
Vedant Kumar310c2802017-09-15 23:00:02 +000057 assert(Covered <= NumRegions && "Covered regions over-counted");
Vedant Kumar17d25e32016-04-29 01:31:49 +000058 if (NumRegions == 0)
59 return 0.0;
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000060 return double(Covered) / double(NumRegions) * 100.0;
61 }
62};
63
Adrian Prantl26b584c2018-05-01 15:54:18 +000064/// Provides information about line coverage for a function/file.
Vedant Kumarc94f3922017-09-15 23:00:01 +000065class LineCoverageInfo {
Adrian Prantl26b584c2018-05-01 15:54:18 +000066 /// The number of lines that were executed at least once.
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000067 size_t Covered;
68
Adrian Prantl26b584c2018-05-01 15:54:18 +000069 /// The total number of lines in a function/file.
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000070 size_t NumLines;
71
Vedant Kumarc94f3922017-09-15 23:00:01 +000072public:
Vedant Kumar9f82f262017-09-15 23:00:00 +000073 LineCoverageInfo() : Covered(0), NumLines(0) {}
Justin Bogner9bdb1942015-02-14 02:01:24 +000074
Vedant Kumar1b70b5d2016-09-19 01:46:01 +000075 LineCoverageInfo(size_t Covered, size_t NumLines)
Vedant Kumar310c2802017-09-15 23:00:02 +000076 : Covered(Covered), NumLines(NumLines) {
77 assert(Covered <= NumLines && "Covered lines over-counted");
78 }
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000079
Justin Bogner9bdb1942015-02-14 02:01:24 +000080 LineCoverageInfo &operator+=(const LineCoverageInfo &RHS) {
81 Covered += RHS.Covered;
Justin Bogner9bdb1942015-02-14 02:01:24 +000082 NumLines += RHS.NumLines;
83 return *this;
84 }
85
Vedant Kumarc94f3922017-09-15 23:00:01 +000086 void merge(const LineCoverageInfo &RHS) {
87 Covered = std::max(Covered, RHS.Covered);
Vedant Kumar310c2802017-09-15 23:00:02 +000088 NumLines = std::max(NumLines, RHS.NumLines);
Vedant Kumarc94f3922017-09-15 23:00:01 +000089 }
90
91 size_t getCovered() const { return Covered; }
92
93 size_t getNumLines() const { return NumLines; }
94
Vedant Kumar1b70b5d2016-09-19 01:46:01 +000095 bool isFullyCovered() const { return Covered == NumLines; }
Alex Lorenz6c7a6a12014-08-22 22:56:03 +000096
97 double getPercentCovered() const {
Vedant Kumar310c2802017-09-15 23:00:02 +000098 assert(Covered <= NumLines && "Covered lines over-counted");
Vedant Kumar1b70b5d2016-09-19 01:46:01 +000099 if (NumLines == 0)
Vedant Kumar17d25e32016-04-29 01:31:49 +0000100 return 0.0;
Vedant Kumar1b70b5d2016-09-19 01:46:01 +0000101 return double(Covered) / double(NumLines) * 100.0;
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000102 }
103};
104
Adrian Prantl26b584c2018-05-01 15:54:18 +0000105/// Provides information about function coverage for a file.
Vedant Kumarc94f3922017-09-15 23:00:01 +0000106class FunctionCoverageInfo {
Adrian Prantl26b584c2018-05-01 15:54:18 +0000107 /// The number of functions that were executed.
Alex Lorenz38c59de2014-09-30 12:45:13 +0000108 size_t Executed;
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000109
Adrian Prantl26b584c2018-05-01 15:54:18 +0000110 /// The total number of functions in this file.
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000111 size_t NumFunctions;
112
Vedant Kumarc94f3922017-09-15 23:00:01 +0000113public:
Justin Bogner9bdb1942015-02-14 02:01:24 +0000114 FunctionCoverageInfo() : Executed(0), NumFunctions(0) {}
115
Alex Lorenz38c59de2014-09-30 12:45:13 +0000116 FunctionCoverageInfo(size_t Executed, size_t NumFunctions)
117 : Executed(Executed), NumFunctions(NumFunctions) {}
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000118
Max Morozd6352ca2018-01-05 16:15:07 +0000119 FunctionCoverageInfo &operator+=(const FunctionCoverageInfo &RHS) {
120 Executed += RHS.Executed;
121 NumFunctions += RHS.NumFunctions;
122 return *this;
123 }
124
Justin Bogner9bdb1942015-02-14 02:01:24 +0000125 void addFunction(bool Covered) {
126 if (Covered)
127 ++Executed;
128 ++NumFunctions;
129 }
130
Vedant Kumarc94f3922017-09-15 23:00:01 +0000131 size_t getExecuted() const { return Executed; }
132
133 size_t getNumFunctions() const { return NumFunctions; }
134
Alex Lorenz38c59de2014-09-30 12:45:13 +0000135 bool isFullyCovered() const { return Executed == NumFunctions; }
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000136
137 double getPercentCovered() const {
Vedant Kumar310c2802017-09-15 23:00:02 +0000138 assert(Executed <= NumFunctions && "Covered functions over-counted");
Vedant Kumar17d25e32016-04-29 01:31:49 +0000139 if (NumFunctions == 0)
140 return 0.0;
Alex Lorenz38c59de2014-09-30 12:45:13 +0000141 return double(Executed) / double(NumFunctions) * 100.0;
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000142 }
143};
144
Adrian Prantl26b584c2018-05-01 15:54:18 +0000145/// A summary of function's code coverage.
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000146struct FunctionCoverageSummary {
Vedant Kumara8dfa812017-08-02 23:35:25 +0000147 std::string Name;
Alex Lorenz38c59de2014-09-30 12:45:13 +0000148 uint64_t ExecutionCount;
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000149 RegionCoverageInfo RegionCoverage;
150 LineCoverageInfo LineCoverage;
151
Vedant Kumar31b24fc2017-09-08 18:44:49 +0000152 FunctionCoverageSummary(const std::string &Name)
Vedant Kumar310c2802017-09-15 23:00:02 +0000153 : Name(Name), ExecutionCount(0), RegionCoverage(), LineCoverage() {}
Justin Bogner9bdb1942015-02-14 02:01:24 +0000154
Vedant Kumar31b24fc2017-09-08 18:44:49 +0000155 FunctionCoverageSummary(const std::string &Name, uint64_t ExecutionCount,
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000156 const RegionCoverageInfo &RegionCoverage,
157 const LineCoverageInfo &LineCoverage)
Alex Lorenz38c59de2014-09-30 12:45:13 +0000158 : Name(Name), ExecutionCount(ExecutionCount),
Vedant Kumar31b24fc2017-09-08 18:44:49 +0000159 RegionCoverage(RegionCoverage), LineCoverage(LineCoverage) {}
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000160
Adrian Prantl26b584c2018-05-01 15:54:18 +0000161 /// Compute the code coverage summary for the given function coverage
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000162 /// mapping record.
Vedant Kumar1ef0d5a2017-09-19 02:00:12 +0000163 static FunctionCoverageSummary get(const coverage::CoverageMapping &CM,
164 const coverage::FunctionRecord &Function);
Vedant Kumare17f26f2016-09-19 00:38:23 +0000165
Vedant Kumara8dfa812017-08-02 23:35:25 +0000166 /// Compute the code coverage summary for an instantiation group \p Group,
167 /// given a list of summaries for each instantiation in \p Summaries.
168 static FunctionCoverageSummary
169 get(const coverage::InstantiationGroup &Group,
170 ArrayRef<FunctionCoverageSummary> Summaries);
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000171};
172
Adrian Prantl26b584c2018-05-01 15:54:18 +0000173/// A summary of file's code coverage.
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000174struct FileCoverageSummary {
175 StringRef Name;
176 RegionCoverageInfo RegionCoverage;
177 LineCoverageInfo LineCoverage;
178 FunctionCoverageInfo FunctionCoverage;
Vedant Kumare17f26f2016-09-19 00:38:23 +0000179 FunctionCoverageInfo InstantiationCoverage;
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000180
Vedant Kumar310c2802017-09-15 23:00:02 +0000181 FileCoverageSummary(StringRef Name)
182 : Name(Name), RegionCoverage(), LineCoverage(), FunctionCoverage(),
183 InstantiationCoverage() {}
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000184
Max Morozd6352ca2018-01-05 16:15:07 +0000185 FileCoverageSummary &operator+=(const FileCoverageSummary &RHS) {
186 RegionCoverage += RHS.RegionCoverage;
187 LineCoverage += RHS.LineCoverage;
188 FunctionCoverage += RHS.FunctionCoverage;
189 InstantiationCoverage += RHS.InstantiationCoverage;
190 return *this;
191 }
192
Justin Bogner9bdb1942015-02-14 02:01:24 +0000193 void addFunction(const FunctionCoverageSummary &Function) {
194 RegionCoverage += Function.RegionCoverage;
195 LineCoverage += Function.LineCoverage;
196 FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
197 }
Vedant Kumare17f26f2016-09-19 00:38:23 +0000198
199 void addInstantiation(const FunctionCoverageSummary &Function) {
200 InstantiationCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
201 }
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000202};
203
Adrian Prantl26b584c2018-05-01 15:54:18 +0000204/// A cache for demangled symbols.
Vedant Kumar48584472017-02-05 20:10:58 +0000205struct DemangleCache {
206 StringMap<std::string> DemangledNames;
207
Adrian Prantl26b584c2018-05-01 15:54:18 +0000208 /// Demangle \p Sym if possible. Otherwise, just return \p Sym.
Vedant Kumar48584472017-02-05 20:10:58 +0000209 StringRef demangle(StringRef Sym) const {
210 const auto DemangledName = DemangledNames.find(Sym);
211 if (DemangledName == DemangledNames.end())
212 return Sym;
213 return DemangledName->getValue();
214 }
215};
216
Alex Lorenz6c7a6a12014-08-22 22:56:03 +0000217} // namespace llvm
218
219#endif // LLVM_COV_COVERAGESUMMARYINFO_H