Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 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 | |
Easwaran Raman | a96d5370 | 2016-04-29 18:53:05 +0000 | [diff] [blame] | 18 | #include "llvm/ProfileData/Coverage/CoverageMapping.h" |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 23 | /// Provides information about region coverage for a function/file. |
Vedant Kumar | c94f392 | 2017-09-15 23:00:01 +0000 | [diff] [blame] | 24 | class RegionCoverageInfo { |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 25 | /// The number of regions that were executed at least once. |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 26 | size_t Covered; |
| 27 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 28 | /// The total number of regions in a function/file. |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 29 | size_t NumRegions; |
| 30 | |
Vedant Kumar | c94f392 | 2017-09-15 23:00:01 +0000 | [diff] [blame] | 31 | public: |
Vedant Kumar | 9f82f26 | 2017-09-15 23:00:00 +0000 | [diff] [blame] | 32 | RegionCoverageInfo() : Covered(0), NumRegions(0) {} |
Justin Bogner | 9bdb194 | 2015-02-14 02:01:24 +0000 | [diff] [blame] | 33 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 34 | RegionCoverageInfo(size_t Covered, size_t NumRegions) |
Vedant Kumar | 310c280 | 2017-09-15 23:00:02 +0000 | [diff] [blame] | 35 | : Covered(Covered), NumRegions(NumRegions) { |
| 36 | assert(Covered <= NumRegions && "Covered regions over-counted"); |
| 37 | } |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 38 | |
Justin Bogner | 9bdb194 | 2015-02-14 02:01:24 +0000 | [diff] [blame] | 39 | RegionCoverageInfo &operator+=(const RegionCoverageInfo &RHS) { |
| 40 | Covered += RHS.Covered; |
Justin Bogner | 9bdb194 | 2015-02-14 02:01:24 +0000 | [diff] [blame] | 41 | NumRegions += RHS.NumRegions; |
| 42 | return *this; |
| 43 | } |
| 44 | |
Vedant Kumar | c94f392 | 2017-09-15 23:00:01 +0000 | [diff] [blame] | 45 | void merge(const RegionCoverageInfo &RHS) { |
| 46 | Covered = std::max(Covered, RHS.Covered); |
Vedant Kumar | 310c280 | 2017-09-15 23:00:02 +0000 | [diff] [blame] | 47 | NumRegions = std::max(NumRegions, RHS.NumRegions); |
Vedant Kumar | c94f392 | 2017-09-15 23:00:01 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | size_t getCovered() const { return Covered; } |
| 51 | |
| 52 | size_t getNumRegions() const { return NumRegions; } |
| 53 | |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 54 | bool isFullyCovered() const { return Covered == NumRegions; } |
| 55 | |
| 56 | double getPercentCovered() const { |
Vedant Kumar | 310c280 | 2017-09-15 23:00:02 +0000 | [diff] [blame] | 57 | assert(Covered <= NumRegions && "Covered regions over-counted"); |
Vedant Kumar | 17d25e3 | 2016-04-29 01:31:49 +0000 | [diff] [blame] | 58 | if (NumRegions == 0) |
| 59 | return 0.0; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 60 | return double(Covered) / double(NumRegions) * 100.0; |
| 61 | } |
| 62 | }; |
| 63 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 64 | /// Provides information about line coverage for a function/file. |
Vedant Kumar | c94f392 | 2017-09-15 23:00:01 +0000 | [diff] [blame] | 65 | class LineCoverageInfo { |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 66 | /// The number of lines that were executed at least once. |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 67 | size_t Covered; |
| 68 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 69 | /// The total number of lines in a function/file. |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 70 | size_t NumLines; |
| 71 | |
Vedant Kumar | c94f392 | 2017-09-15 23:00:01 +0000 | [diff] [blame] | 72 | public: |
Vedant Kumar | 9f82f26 | 2017-09-15 23:00:00 +0000 | [diff] [blame] | 73 | LineCoverageInfo() : Covered(0), NumLines(0) {} |
Justin Bogner | 9bdb194 | 2015-02-14 02:01:24 +0000 | [diff] [blame] | 74 | |
Vedant Kumar | 1b70b5d | 2016-09-19 01:46:01 +0000 | [diff] [blame] | 75 | LineCoverageInfo(size_t Covered, size_t NumLines) |
Vedant Kumar | 310c280 | 2017-09-15 23:00:02 +0000 | [diff] [blame] | 76 | : Covered(Covered), NumLines(NumLines) { |
| 77 | assert(Covered <= NumLines && "Covered lines over-counted"); |
| 78 | } |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 79 | |
Justin Bogner | 9bdb194 | 2015-02-14 02:01:24 +0000 | [diff] [blame] | 80 | LineCoverageInfo &operator+=(const LineCoverageInfo &RHS) { |
| 81 | Covered += RHS.Covered; |
Justin Bogner | 9bdb194 | 2015-02-14 02:01:24 +0000 | [diff] [blame] | 82 | NumLines += RHS.NumLines; |
| 83 | return *this; |
| 84 | } |
| 85 | |
Vedant Kumar | c94f392 | 2017-09-15 23:00:01 +0000 | [diff] [blame] | 86 | void merge(const LineCoverageInfo &RHS) { |
| 87 | Covered = std::max(Covered, RHS.Covered); |
Vedant Kumar | 310c280 | 2017-09-15 23:00:02 +0000 | [diff] [blame] | 88 | NumLines = std::max(NumLines, RHS.NumLines); |
Vedant Kumar | c94f392 | 2017-09-15 23:00:01 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | size_t getCovered() const { return Covered; } |
| 92 | |
| 93 | size_t getNumLines() const { return NumLines; } |
| 94 | |
Vedant Kumar | 1b70b5d | 2016-09-19 01:46:01 +0000 | [diff] [blame] | 95 | bool isFullyCovered() const { return Covered == NumLines; } |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 96 | |
| 97 | double getPercentCovered() const { |
Vedant Kumar | 310c280 | 2017-09-15 23:00:02 +0000 | [diff] [blame] | 98 | assert(Covered <= NumLines && "Covered lines over-counted"); |
Vedant Kumar | 1b70b5d | 2016-09-19 01:46:01 +0000 | [diff] [blame] | 99 | if (NumLines == 0) |
Vedant Kumar | 17d25e3 | 2016-04-29 01:31:49 +0000 | [diff] [blame] | 100 | return 0.0; |
Vedant Kumar | 1b70b5d | 2016-09-19 01:46:01 +0000 | [diff] [blame] | 101 | return double(Covered) / double(NumLines) * 100.0; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 102 | } |
| 103 | }; |
| 104 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 105 | /// Provides information about function coverage for a file. |
Vedant Kumar | c94f392 | 2017-09-15 23:00:01 +0000 | [diff] [blame] | 106 | class FunctionCoverageInfo { |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 107 | /// The number of functions that were executed. |
Alex Lorenz | 38c59de | 2014-09-30 12:45:13 +0000 | [diff] [blame] | 108 | size_t Executed; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 109 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 110 | /// The total number of functions in this file. |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 111 | size_t NumFunctions; |
| 112 | |
Vedant Kumar | c94f392 | 2017-09-15 23:00:01 +0000 | [diff] [blame] | 113 | public: |
Justin Bogner | 9bdb194 | 2015-02-14 02:01:24 +0000 | [diff] [blame] | 114 | FunctionCoverageInfo() : Executed(0), NumFunctions(0) {} |
| 115 | |
Alex Lorenz | 38c59de | 2014-09-30 12:45:13 +0000 | [diff] [blame] | 116 | FunctionCoverageInfo(size_t Executed, size_t NumFunctions) |
| 117 | : Executed(Executed), NumFunctions(NumFunctions) {} |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 118 | |
Max Moroz | d6352ca | 2018-01-05 16:15:07 +0000 | [diff] [blame] | 119 | FunctionCoverageInfo &operator+=(const FunctionCoverageInfo &RHS) { |
| 120 | Executed += RHS.Executed; |
| 121 | NumFunctions += RHS.NumFunctions; |
| 122 | return *this; |
| 123 | } |
| 124 | |
Justin Bogner | 9bdb194 | 2015-02-14 02:01:24 +0000 | [diff] [blame] | 125 | void addFunction(bool Covered) { |
| 126 | if (Covered) |
| 127 | ++Executed; |
| 128 | ++NumFunctions; |
| 129 | } |
| 130 | |
Vedant Kumar | c94f392 | 2017-09-15 23:00:01 +0000 | [diff] [blame] | 131 | size_t getExecuted() const { return Executed; } |
| 132 | |
| 133 | size_t getNumFunctions() const { return NumFunctions; } |
| 134 | |
Alex Lorenz | 38c59de | 2014-09-30 12:45:13 +0000 | [diff] [blame] | 135 | bool isFullyCovered() const { return Executed == NumFunctions; } |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 136 | |
| 137 | double getPercentCovered() const { |
Vedant Kumar | 310c280 | 2017-09-15 23:00:02 +0000 | [diff] [blame] | 138 | assert(Executed <= NumFunctions && "Covered functions over-counted"); |
Vedant Kumar | 17d25e3 | 2016-04-29 01:31:49 +0000 | [diff] [blame] | 139 | if (NumFunctions == 0) |
| 140 | return 0.0; |
Alex Lorenz | 38c59de | 2014-09-30 12:45:13 +0000 | [diff] [blame] | 141 | return double(Executed) / double(NumFunctions) * 100.0; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 142 | } |
| 143 | }; |
| 144 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 145 | /// A summary of function's code coverage. |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 146 | struct FunctionCoverageSummary { |
Vedant Kumar | a8dfa81 | 2017-08-02 23:35:25 +0000 | [diff] [blame] | 147 | std::string Name; |
Alex Lorenz | 38c59de | 2014-09-30 12:45:13 +0000 | [diff] [blame] | 148 | uint64_t ExecutionCount; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 149 | RegionCoverageInfo RegionCoverage; |
| 150 | LineCoverageInfo LineCoverage; |
| 151 | |
Vedant Kumar | 31b24fc | 2017-09-08 18:44:49 +0000 | [diff] [blame] | 152 | FunctionCoverageSummary(const std::string &Name) |
Vedant Kumar | 310c280 | 2017-09-15 23:00:02 +0000 | [diff] [blame] | 153 | : Name(Name), ExecutionCount(0), RegionCoverage(), LineCoverage() {} |
Justin Bogner | 9bdb194 | 2015-02-14 02:01:24 +0000 | [diff] [blame] | 154 | |
Vedant Kumar | 31b24fc | 2017-09-08 18:44:49 +0000 | [diff] [blame] | 155 | FunctionCoverageSummary(const std::string &Name, uint64_t ExecutionCount, |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 156 | const RegionCoverageInfo &RegionCoverage, |
| 157 | const LineCoverageInfo &LineCoverage) |
Alex Lorenz | 38c59de | 2014-09-30 12:45:13 +0000 | [diff] [blame] | 158 | : Name(Name), ExecutionCount(ExecutionCount), |
Vedant Kumar | 31b24fc | 2017-09-08 18:44:49 +0000 | [diff] [blame] | 159 | RegionCoverage(RegionCoverage), LineCoverage(LineCoverage) {} |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 160 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 161 | /// Compute the code coverage summary for the given function coverage |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 162 | /// mapping record. |
Vedant Kumar | 1ef0d5a | 2017-09-19 02:00:12 +0000 | [diff] [blame] | 163 | static FunctionCoverageSummary get(const coverage::CoverageMapping &CM, |
| 164 | const coverage::FunctionRecord &Function); |
Vedant Kumar | e17f26f | 2016-09-19 00:38:23 +0000 | [diff] [blame] | 165 | |
Vedant Kumar | a8dfa81 | 2017-08-02 23:35:25 +0000 | [diff] [blame] | 166 | /// 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 Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 173 | /// A summary of file's code coverage. |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 174 | struct FileCoverageSummary { |
| 175 | StringRef Name; |
| 176 | RegionCoverageInfo RegionCoverage; |
| 177 | LineCoverageInfo LineCoverage; |
| 178 | FunctionCoverageInfo FunctionCoverage; |
Vedant Kumar | e17f26f | 2016-09-19 00:38:23 +0000 | [diff] [blame] | 179 | FunctionCoverageInfo InstantiationCoverage; |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 180 | |
Vedant Kumar | 310c280 | 2017-09-15 23:00:02 +0000 | [diff] [blame] | 181 | FileCoverageSummary(StringRef Name) |
| 182 | : Name(Name), RegionCoverage(), LineCoverage(), FunctionCoverage(), |
| 183 | InstantiationCoverage() {} |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 184 | |
Max Moroz | d6352ca | 2018-01-05 16:15:07 +0000 | [diff] [blame] | 185 | 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 Bogner | 9bdb194 | 2015-02-14 02:01:24 +0000 | [diff] [blame] | 193 | void addFunction(const FunctionCoverageSummary &Function) { |
| 194 | RegionCoverage += Function.RegionCoverage; |
| 195 | LineCoverage += Function.LineCoverage; |
| 196 | FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0); |
| 197 | } |
Vedant Kumar | e17f26f | 2016-09-19 00:38:23 +0000 | [diff] [blame] | 198 | |
| 199 | void addInstantiation(const FunctionCoverageSummary &Function) { |
| 200 | InstantiationCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0); |
| 201 | } |
Alex Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 204 | /// A cache for demangled symbols. |
Vedant Kumar | 4858447 | 2017-02-05 20:10:58 +0000 | [diff] [blame] | 205 | struct DemangleCache { |
| 206 | StringMap<std::string> DemangledNames; |
| 207 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 208 | /// Demangle \p Sym if possible. Otherwise, just return \p Sym. |
Vedant Kumar | 4858447 | 2017-02-05 20:10:58 +0000 | [diff] [blame] | 209 | 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 Lorenz | 6c7a6a1 | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 217 | } // namespace llvm |
| 218 | |
| 219 | #endif // LLVM_COV_COVERAGESUMMARYINFO_H |