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