Coverage Report

Created: 2018-01-17 21:32

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/llvm-cov/CoverageSummaryInfo.h
Line
Count
Source (jump to first uncovered line)
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
196
  RegionCoverageInfo() : Covered(0), NumRegions(0) {}
33
34
  RegionCoverageInfo(size_t Covered, size_t NumRegions)
35
109
      : Covered(Covered), NumRegions(NumRegions) {
36
109
    assert(Covered <= NumRegions && "Covered regions over-counted");
37
109
  }
38
39
169
  RegionCoverageInfo &operator+=(const RegionCoverageInfo &RHS) {
40
169
    Covered += RHS.Covered;
41
169
    NumRegions += RHS.NumRegions;
42
169
    return *this;
43
169
  }
44
45
19
  void merge(const RegionCoverageInfo &RHS) {
46
19
    Covered = std::max(Covered, RHS.Covered);
47
19
    NumRegions = std::max(NumRegions, RHS.NumRegions);
48
19
  }
49
50
153
  size_t getCovered() const { return Covered; }
51
52
258
  size_t getNumRegions() const { return NumRegions; }
53
54
66
  bool isFullyCovered() const { return Covered == NumRegions; }
55
56
153
  double getPercentCovered() const {
57
153
    assert(Covered <= NumRegions && "Covered regions over-counted");
58
153
    if (NumRegions == 0)
59
0
      return 0.0;
60
153
    return double(Covered) / double(NumRegions) * 100.0;
61
153
  }
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
196
  LineCoverageInfo() : Covered(0), NumLines(0) {}
74
75
  LineCoverageInfo(size_t Covered, size_t NumLines)
76
109
      : Covered(Covered), NumLines(NumLines) {
77
109
    assert(Covered <= NumLines && "Covered lines over-counted");
78
109
  }
79
80
169
  LineCoverageInfo &operator+=(const LineCoverageInfo &RHS) {
81
169
    Covered += RHS.Covered;
82
169
    NumLines += RHS.NumLines;
83
169
    return *this;
84
169
  }
85
86
19
  void merge(const LineCoverageInfo &RHS) {
87
19
    Covered = std::max(Covered, RHS.Covered);
88
19
    NumLines = std::max(NumLines, RHS.NumLines);
89
19
  }
90
91
119
  size_t getCovered() const { return Covered; }
92
93
224
  size_t getNumLines() const { return NumLines; }
94
95
66
  bool isFullyCovered() const { return Covered == NumLines; }
96
97
151
  double getPercentCovered() const {
98
151
    assert(Covered <= NumLines && "Covered lines over-counted");
99
151
    if (NumLines == 0)
100
0
      return 0.0;
101
151
    return double(Covered) / double(NumLines) * 100.0;
102
151
  }
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
224
  FunctionCoverageInfo() : Executed(0), NumFunctions(0) {}
115
116
  FunctionCoverageInfo(size_t Executed, size_t NumFunctions)
117
0
      : Executed(Executed), NumFunctions(NumFunctions) {}
118
119
160
  FunctionCoverageInfo &operator+=(const FunctionCoverageInfo &RHS) {
120
160
    Executed += RHS.Executed;
121
160
    NumFunctions += RHS.NumFunctions;
122
160
    return *this;
123
160
  }
124
125
184
  void addFunction(bool Covered) {
126
184
    if (Covered)
127
161
      ++Executed;
128
184
    ++NumFunctions;
129
184
  }
130
131
146
  size_t getExecuted() const { return Executed; }
132
133
308
  size_t getNumFunctions() const { return NumFunctions; }
134
135
96
  bool isFullyCovered() const { return Executed == NumFunctions; }
136
137
168
  double getPercentCovered() const {
138
168
    assert(Executed <= NumFunctions && "Covered functions over-counted");
139
168
    if (NumFunctions == 0)
140
0
      return 0.0;
141
168
    return double(Executed) / double(NumFunctions) * 100.0;
142
168
  }
143
};
144
145
/// \brief A summary of function's code coverage.
146
struct FunctionCoverageSummary {
147
  std::string Name;
148
  uint64_t ExecutionCount;
149
  RegionCoverageInfo RegionCoverage;
150
  LineCoverageInfo LineCoverage;
151
152
  FunctionCoverageSummary(const std::string &Name)
153
85
      : Name(Name), ExecutionCount(0), RegionCoverage(), LineCoverage() {}
154
155
  FunctionCoverageSummary(const std::string &Name, uint64_t ExecutionCount,
156
                          const RegionCoverageInfo &RegionCoverage,
157
                          const LineCoverageInfo &LineCoverage)
158
      : Name(Name), ExecutionCount(ExecutionCount),
159
109
        RegionCoverage(RegionCoverage), LineCoverage(LineCoverage) {}
160
161
  /// \brief Compute the code coverage summary for the given function coverage
162
  /// mapping record.
163
  static FunctionCoverageSummary get(const coverage::CoverageMapping &CM,
164
                                     const coverage::FunctionRecord &Function);
165
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);
171
};
172
173
/// \brief A summary of file's code coverage.
174
struct FileCoverageSummary {
175
  StringRef Name;
176
  RegionCoverageInfo RegionCoverage;
177
  LineCoverageInfo LineCoverage;
178
  FunctionCoverageInfo FunctionCoverage;
179
  FunctionCoverageInfo InstantiationCoverage;
180
181
  FileCoverageSummary(StringRef Name)
182
      : Name(Name), RegionCoverage(), LineCoverage(), FunctionCoverage(),
183
112
        InstantiationCoverage() {}
184
185
80
  FileCoverageSummary &operator+=(const FileCoverageSummary &RHS) {
186
80
    RegionCoverage += RHS.RegionCoverage;
187
80
    LineCoverage += RHS.LineCoverage;
188
80
    FunctionCoverage += RHS.FunctionCoverage;
189
80
    InstantiationCoverage += RHS.InstantiationCoverage;
190
80
    return *this;
191
80
  }
192
193
82
  void addFunction(const FunctionCoverageSummary &Function) {
194
82
    RegionCoverage += Function.RegionCoverage;
195
82
    LineCoverage += Function.LineCoverage;
196
82
    FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
197
82
  }
198
199
102
  void addInstantiation(const FunctionCoverageSummary &Function) {
200
102
    InstantiationCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
201
102
  }
202
};
203
204
/// \brief A cache for demangled symbols.
205
struct DemangleCache {
206
  StringMap<std::string> DemangledNames;
207
208
  /// \brief Demangle \p Sym if possible. Otherwise, just return \p Sym.
209
65
  StringRef demangle(StringRef Sym) const {
210
65
    const auto DemangledName = DemangledNames.find(Sym);
211
65
    if (DemangledName == DemangledNames.end())
212
57
      return Sym;
213
8
    return DemangledName->getValue();
214
8
  }
215
};
216
217
} // namespace llvm
218
219
#endif // LLVM_COV_COVERAGESUMMARYINFO_H