forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathSourceCoverageView.h
294 lines (221 loc) · 10.3 KB
/
SourceCoverageView.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//===- SourceCoverageView.h - Code coverage view for source code ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file This class implements rendering for code coverage of source code.
///
//===----------------------------------------------------------------------===//
#ifndef LLVM_COV_SOURCECOVERAGEVIEW_H
#defineLLVM_COV_SOURCECOVERAGEVIEW_H
#include"CoverageViewOptions.h"
#include"CoverageSummaryInfo.h"
#include"llvm/ProfileData/Coverage/CoverageMapping.h"
#include"llvm/Support/MemoryBuffer.h"
#include<vector>
namespacellvm {
usingnamespacecoverage;
classCoverageFiltersMatchAll;
classSourceCoverageView;
/// A view that represents a macro or include expansion.
structExpansionView {
CounterMappingRegion Region;
std::unique_ptr<SourceCoverageView> View;
ExpansionView(const CounterMappingRegion &Region,
std::unique_ptr<SourceCoverageView> View)
: Region(Region), View(std::move(View)) {}
ExpansionView(ExpansionView &&RHS)
: Region(std::move(RHS.Region)), View(std::move(RHS.View)) {}
ExpansionView &operator=(ExpansionView &&RHS) {
Region = std::move(RHS.Region);
View = std::move(RHS.View);
return *this;
}
unsignedgetLine() const { return Region.LineStart; }
unsignedgetStartCol() const { return Region.ColumnStart; }
unsignedgetEndCol() const { return Region.ColumnEnd; }
friendbooloperator<(const ExpansionView &LHS, const ExpansionView &RHS) {
return LHS.Region.startLoc() < RHS.Region.startLoc();
}
};
/// A view that represents a function instantiation.
structInstantiationView {
StringRef FunctionName;
unsigned Line;
std::unique_ptr<SourceCoverageView> View;
InstantiationView(StringRef FunctionName, unsigned Line,
std::unique_ptr<SourceCoverageView> View)
: FunctionName(FunctionName), Line(Line), View(std::move(View)) {}
friendbooloperator<(const InstantiationView &LHS,
const InstantiationView &RHS) {
return LHS.Line < RHS.Line;
}
};
/// A view that represents one or more branch regions on a given source line.
structBranchView {
std::vector<CountedRegion> Regions;
std::unique_ptr<SourceCoverageView> View;
unsigned Line;
BranchView(unsigned Line, ArrayRef<CountedRegion> Regions,
std::unique_ptr<SourceCoverageView> View)
: Regions(Regions), View(std::move(View)), Line(Line) {}
unsignedgetLine() const { return Line; }
friendbooloperator<(const BranchView &LHS, const BranchView &RHS) {
return LHS.Line < RHS.Line;
}
};
/// A file manager that handles format-aware file creation.
classCoveragePrinter {
public:
structStreamDestructor {
voidoperator()(raw_ostream *OS) const;
};
using OwnedStream = std::unique_ptr<raw_ostream, StreamDestructor>;
protected:
const CoverageViewOptions &Opts;
CoveragePrinter(const CoverageViewOptions &Opts) : Opts(Opts) {}
/// Return `OutputDir/ToplevelDir/Path.Extension`. If \p InToplevel is
/// false, skip the ToplevelDir component. If \p Relative is false, skip the
/// OutputDir component.
std::string getOutputPath(StringRef Path, StringRef Extension,
bool InToplevel, bool Relative = true) const;
/// If directory output is enabled, create a file in that directory
/// at the path given by getOutputPath(). Otherwise, return stdout.
Expected<OwnedStream> createOutputStream(StringRef Path, StringRef Extension,
bool InToplevel) const;
/// Return the sub-directory name for file coverage reports.
static StringRef getCoverageDir() { return"coverage"; }
public:
static std::unique_ptr<CoveragePrinter>
create(const CoverageViewOptions &Opts);
virtual~CoveragePrinter() {}
/// @name File Creation Interface
/// @{
/// Create a file to print a coverage view into.
virtual Expected<OwnedStream> createViewFile(StringRef Path,
bool InToplevel) = 0;
/// Close a file which has been used to print a coverage view.
virtualvoidcloseViewFile(OwnedStream OS) = 0;
/// Create an index which lists reports for the given source files.
virtual Error createIndexFile(ArrayRef<std::string> SourceFiles,
const CoverageMapping &Coverage,
const CoverageFiltersMatchAll &Filters) = 0;
/// @}
};
/// A code coverage view of a source file or function.
///
/// A source coverage view and its nested sub-views form a file-oriented
/// representation of code coverage data. This view can be printed out by a
/// renderer which implements the Rendering Interface.
classSourceCoverageView {
/// A function or file name.
StringRef SourceName;
/// A memory buffer backing the source on display.
const MemoryBuffer &File;
/// Various options to guide the coverage renderer.
const CoverageViewOptions &Options;
/// Complete coverage information about the source on display.
CoverageData CoverageInfo;
/// A container for all expansions (e.g macros) in the source on display.
std::vector<ExpansionView> ExpansionSubViews;
/// A container for all branches in the source on display.
std::vector<BranchView> BranchSubViews;
/// A container for all instantiations (e.g template functions) in the source
/// on display.
std::vector<InstantiationView> InstantiationSubViews;
/// Get the first uncovered line number for the source file.
unsignedgetFirstUncoveredLineNo();
protected:
structLineRef {
StringRef Line;
int64_t LineNo;
LineRef(StringRef Line, int64_t LineNo) : Line(Line), LineNo(LineNo) {}
};
using CoverageSegmentArray = ArrayRef<const CoverageSegment *>;
/// @name Rendering Interface
/// @{
/// Render a header for the view.
virtualvoidrenderViewHeader(raw_ostream &OS) = 0;
/// Render a footer for the view.
virtualvoidrenderViewFooter(raw_ostream &OS) = 0;
/// Render the source name for the view.
virtualvoidrenderSourceName(raw_ostream &OS, bool WholeFile) = 0;
/// Render the line prefix at the given \p ViewDepth.
virtualvoidrenderLinePrefix(raw_ostream &OS, unsigned ViewDepth) = 0;
/// Render the line suffix at the given \p ViewDepth.
virtualvoidrenderLineSuffix(raw_ostream &OS, unsigned ViewDepth) = 0;
/// Render a view divider at the given \p ViewDepth.
virtualvoidrenderViewDivider(raw_ostream &OS, unsigned ViewDepth) = 0;
/// Render a source line with highlighting.
virtualvoidrenderLine(raw_ostream &OS, LineRef L,
const LineCoverageStats &LCS, unsigned ExpansionCol,
unsigned ViewDepth) = 0;
/// Render the line's execution count column.
virtualvoidrenderLineCoverageColumn(raw_ostream &OS,
const LineCoverageStats &Line) = 0;
/// Render the line number column.
virtualvoidrenderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0;
/// Render all the region's execution counts on a line.
virtualvoidrenderRegionMarkers(raw_ostream &OS,
const LineCoverageStats &Line,
unsigned ViewDepth) = 0;
/// Render the site of an expansion.
virtualvoidrenderExpansionSite(raw_ostream &OS, LineRef L,
const LineCoverageStats &LCS,
unsigned ExpansionCol,
unsigned ViewDepth) = 0;
/// Render an expansion view and any nested views.
virtualvoidrenderExpansionView(raw_ostream &OS, ExpansionView &ESV,
unsigned ViewDepth) = 0;
/// Render an instantiation view and any nested views.
virtualvoidrenderInstantiationView(raw_ostream &OS, InstantiationView &ISV,
unsigned ViewDepth) = 0;
/// Render a branch view and any nested views.
virtualvoidrenderBranchView(raw_ostream &OS, BranchView &BRV,
unsigned ViewDepth) = 0;
/// Render \p Title, a project title if one is available, and the
/// created time.
virtualvoidrenderTitle(raw_ostream &OS, StringRef CellText) = 0;
/// Render the table header for a given source file.
virtualvoidrenderTableHeader(raw_ostream &OS, unsigned FirstUncoveredLineNo,
unsigned IndentLevel) = 0;
/// @}
/// Format a count using engineering notation with 3 significant
/// digits.
static std::string formatCount(uint64_t N);
/// Check if region marker output is expected for a line.
boolshouldRenderRegionMarkers(const LineCoverageStats &LCS) const;
/// Check if there are any sub-views attached to this view.
boolhasSubViews() const;
SourceCoverageView(StringRef SourceName, const MemoryBuffer &File,
const CoverageViewOptions &Options,
CoverageData &&CoverageInfo)
: SourceName(SourceName), File(File), Options(Options),
CoverageInfo(std::move(CoverageInfo)) {}
public:
static std::unique_ptr<SourceCoverageView>
create(StringRef SourceName, const MemoryBuffer &File,
const CoverageViewOptions &Options, CoverageData &&CoverageInfo);
virtual~SourceCoverageView() {}
/// Return the source name formatted for the host OS.
std::string getSourceName() const;
const CoverageViewOptions &getOptions() const { return Options; }
/// Add an expansion subview to this view.
voidaddExpansion(const CounterMappingRegion &Region,
std::unique_ptr<SourceCoverageView> View);
/// Add a function instantiation subview to this view.
voidaddInstantiation(StringRef FunctionName, unsigned Line,
std::unique_ptr<SourceCoverageView> View);
/// Add a branch subview to this view.
voidaddBranch(unsigned Line, ArrayRef<CountedRegion> Regions,
std::unique_ptr<SourceCoverageView> View);
/// Print the code coverage information for a specific portion of a
/// source file to the output stream.
voidprint(raw_ostream &OS, bool WholeFile, bool ShowSourceName,
bool ShowTitle, unsigned ViewDepth = 0);
};
} // namespace llvm
#endif// LLVM_COV_SOURCECOVERAGEVIEW_H