blob: b1bbff8e8046d8355b835f9bc7098b6eb556c2e5 [file] [log] [blame]
Alex Lorenze82d89c2014-08-22 22:56:03 +00001//===- SourceCoverageView.h - Code coverage view for source code ----------===//
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//===----------------------------------------------------------------------===//
Vedant Kumaree5a5e92016-06-25 05:48:54 +00009///
10/// \file This class implements rendering for code coverage of source code.
11///
Alex Lorenze82d89c2014-08-22 22:56:03 +000012//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_COV_SOURCECOVERAGEVIEW_H
15#define LLVM_COV_SOURCECOVERAGEVIEW_H
16
17#include "CoverageViewOptions.h"
Easwaran Ramandc707122016-04-29 18:53:05 +000018#include "llvm/ProfileData/Coverage/CoverageMapping.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000019#include "llvm/Support/MemoryBuffer.h"
20#include <vector>
21
22namespace llvm {
23
Justin Bogner5e1400a2014-09-17 05:33:20 +000024class SourceCoverageView;
25
Vedant Kumaree5a5e92016-06-25 05:48:54 +000026/// \brief A view that represents a macro or include expansion.
Justin Bogner5e1400a2014-09-17 05:33:20 +000027struct ExpansionView {
28 coverage::CounterMappingRegion Region;
29 std::unique_ptr<SourceCoverageView> View;
30
31 ExpansionView(const coverage::CounterMappingRegion &Region,
32 std::unique_ptr<SourceCoverageView> View)
33 : Region(Region), View(std::move(View)) {}
Justin Bogner99e95182014-09-17 06:32:48 +000034 ExpansionView(ExpansionView &&RHS)
35 : Region(std::move(RHS.Region)), View(std::move(RHS.View)) {}
36 ExpansionView &operator=(ExpansionView &&RHS) {
37 Region = std::move(RHS.Region);
38 View = std::move(RHS.View);
39 return *this;
40 }
Justin Bogner5e1400a2014-09-17 05:33:20 +000041
42 unsigned getLine() const { return Region.LineStart; }
43 unsigned getStartCol() const { return Region.ColumnStart; }
44 unsigned getEndCol() const { return Region.ColumnEnd; }
45
46 friend bool operator<(const ExpansionView &LHS, const ExpansionView &RHS) {
47 return LHS.Region.startLoc() < RHS.Region.startLoc();
48 }
49};
50
Vedant Kumaree5a5e92016-06-25 05:48:54 +000051/// \brief A view that represents a function instantiation.
Justin Bogner5e1400a2014-09-17 05:33:20 +000052struct InstantiationView {
53 StringRef FunctionName;
54 unsigned Line;
55 std::unique_ptr<SourceCoverageView> View;
56
57 InstantiationView(StringRef FunctionName, unsigned Line,
58 std::unique_ptr<SourceCoverageView> View)
59 : FunctionName(FunctionName), Line(Line), View(std::move(View)) {}
Justin Bogner99e95182014-09-17 06:32:48 +000060 InstantiationView(InstantiationView &&RHS)
61 : FunctionName(std::move(RHS.FunctionName)), Line(std::move(RHS.Line)),
62 View(std::move(RHS.View)) {}
63 InstantiationView &operator=(InstantiationView &&RHS) {
64 FunctionName = std::move(RHS.FunctionName);
65 Line = std::move(RHS.Line);
66 View = std::move(RHS.View);
67 return *this;
68 }
Justin Bogner5e1400a2014-09-17 05:33:20 +000069
70 friend bool operator<(const InstantiationView &LHS,
71 const InstantiationView &RHS) {
72 return LHS.Line < RHS.Line;
73 }
74};
75
Vedant Kumar60dcb482016-06-24 00:34:48 +000076/// \brief Coverage statistics for a single line.
77struct LineCoverageStats {
78 uint64_t ExecutionCount;
79 unsigned RegionCount;
80 bool Mapped;
81
82 LineCoverageStats() : ExecutionCount(0), RegionCount(0), Mapped(false) {}
83
84 bool isMapped() const { return Mapped; }
85
86 bool hasMultipleRegions() const { return RegionCount > 1; }
87
88 void addRegionStartCount(uint64_t Count) {
89 // The max of all region starts is the most interesting value.
90 addRegionCount(RegionCount ? std::max(ExecutionCount, Count) : Count);
91 ++RegionCount;
92 }
93
94 void addRegionCount(uint64_t Count) {
95 Mapped = true;
96 ExecutionCount = Count;
97 }
98};
99
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000100/// \brief A file manager that handles format-aware file creation.
101class CoveragePrinter {
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000102public:
103 struct StreamDestructor {
104 void operator()(raw_ostream *OS) const;
105 };
106
107 using OwnedStream = std::unique_ptr<raw_ostream, StreamDestructor>;
108
109protected:
Vedant Kumarc076c492016-07-21 23:26:15 +0000110 const CoverageViewOptions &Opts;
111
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000112 CoveragePrinter(const CoverageViewOptions &Opts) : Opts(Opts) {}
113
Vedant Kumard6d192c2016-06-29 21:55:46 +0000114 /// \brief Return `OutputDir/ToplevelDir/Path.Extension`. If \p InToplevel is
115 /// false, skip the ToplevelDir component. If \p Relative is false, skip the
116 /// OutputDir component.
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000117 std::string getOutputPath(StringRef Path, StringRef Extension,
Vedant Kumaraae0ba72016-09-09 01:32:51 +0000118 bool InToplevel, bool Relative = true) const;
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000119
120 /// \brief 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,
Vedant Kumaraae0ba72016-09-09 01:32:51 +0000123 bool InToplevel) const;
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000124
125 /// \brief 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 /// \brief Create a file to print a coverage view into.
138 virtual Expected<OwnedStream> createViewFile(StringRef Path,
139 bool InToplevel) = 0;
140
141 /// \brief Close a file which has been used to print a coverage view.
142 virtual void closeViewFile(OwnedStream OS) = 0;
143
144 /// \brief Create an index which lists reports for the given source files.
Vedant Kumara59334d2016-09-09 01:32:55 +0000145 virtual Error createIndexFile(ArrayRef<StringRef> SourceFiles,
146 const coverage::CoverageMapping &Coverage) = 0;
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000147
148 /// @}
149};
150
Vedant Kumaree5a5e92016-06-25 05:48:54 +0000151/// \brief A code coverage view of a source file or function.
152///
153/// A source coverage view and its nested sub-views form a file-oriented
154/// representation of code coverage data. This view can be printed out by a
Vedant Kumar9cbad2c2016-06-28 16:12:24 +0000155/// renderer which implements the Rendering Interface.
Alex Lorenze82d89c2014-08-22 22:56:03 +0000156class SourceCoverageView {
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000157 /// A function or file name.
158 StringRef SourceName;
159
160 /// A memory buffer backing the source on display.
Alex Lorenze82d89c2014-08-22 22:56:03 +0000161 const MemoryBuffer &File;
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000162
163 /// Various options to guide the coverage renderer.
Alex Lorenze82d89c2014-08-22 22:56:03 +0000164 const CoverageViewOptions &Options;
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000165
166 /// Complete coverage information about the source on display.
Justin Bogner953e2402014-09-20 15:31:56 +0000167 coverage::CoverageData CoverageInfo;
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000168
169 /// A container for all expansions (e.g macros) in the source on display.
Justin Bogner5e1400a2014-09-17 05:33:20 +0000170 std::vector<ExpansionView> ExpansionSubViews;
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000171
172 /// A container for all instantiations (e.g template functions) in the source
173 /// on display.
Justin Bogner5e1400a2014-09-17 05:33:20 +0000174 std::vector<InstantiationView> InstantiationSubViews;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000175
Ying Yid36b47c2016-09-06 19:31:18 +0000176 /// Get the first uncovered line number for the source file.
177 unsigned getFirstUncoveredLineNo();
178
Vedant Kumarf9151b92016-06-25 02:58:30 +0000179protected:
180 struct LineRef {
181 StringRef Line;
182 int64_t LineNo;
183
184 LineRef(StringRef Line, int64_t LineNo) : Line(Line), LineNo(LineNo) {}
185 };
186
187 using CoverageSegmentArray = ArrayRef<const coverage::CoverageSegment *>;
188
189 /// @name Rendering Interface
190 /// @{
191
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000192 /// \brief Render a header for the view.
193 virtual void renderViewHeader(raw_ostream &OS) = 0;
194
195 /// \brief Render a footer for the view.
196 virtual void renderViewFooter(raw_ostream &OS) = 0;
197
Vedant Kumarf9151b92016-06-25 02:58:30 +0000198 /// \brief Render the source name for the view.
Ying Yid36b47c2016-09-06 19:31:18 +0000199 virtual void renderSourceName(raw_ostream &OS, bool WholeFile,
200 unsigned FirstUncoveredLineNo) = 0;
Vedant Kumarf9151b92016-06-25 02:58:30 +0000201
202 /// \brief Render the line prefix at the given \p ViewDepth.
203 virtual void renderLinePrefix(raw_ostream &OS, unsigned ViewDepth) = 0;
204
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000205 /// \brief Render the line suffix at the given \p ViewDepth.
206 virtual void renderLineSuffix(raw_ostream &OS, unsigned ViewDepth) = 0;
207
Vedant Kumarf9151b92016-06-25 02:58:30 +0000208 /// \brief Render a view divider at the given \p ViewDepth.
209 virtual void renderViewDivider(raw_ostream &OS, unsigned ViewDepth) = 0;
210
Alex Lorenze82d89c2014-08-22 22:56:03 +0000211 /// \brief Render a source line with highlighting.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000212 virtual void renderLine(raw_ostream &OS, LineRef L,
213 const coverage::CoverageSegment *WrappedSegment,
214 CoverageSegmentArray Segments, unsigned ExpansionCol,
215 unsigned ViewDepth) = 0;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000216
217 /// \brief Render the line's execution count column.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000218 virtual void renderLineCoverageColumn(raw_ostream &OS,
219 const LineCoverageStats &Line) = 0;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000220
221 /// \brief Render the line number column.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000222 virtual void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000223
224 /// \brief Render all the region's execution counts on a line.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000225 virtual void renderRegionMarkers(raw_ostream &OS,
226 CoverageSegmentArray Segments,
227 unsigned ViewDepth) = 0;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000228
Vedant Kumar861a19c2016-06-26 02:45:13 +0000229 /// \brief Render the site of an expansion.
230 virtual void
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000231 renderExpansionSite(raw_ostream &OS, LineRef L,
Vedant Kumar8b12ecb2016-06-25 05:48:59 +0000232 const coverage::CoverageSegment *WrappedSegment,
233 CoverageSegmentArray Segments, unsigned ExpansionCol,
234 unsigned ViewDepth) = 0;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000235
Vedant Kumar861a19c2016-06-26 02:45:13 +0000236 /// \brief Render an expansion view and any nested views.
237 virtual void renderExpansionView(raw_ostream &OS, ExpansionView &ESV,
238 unsigned ViewDepth) = 0;
239
240 /// \brief Render an instantiation view and any nested views.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000241 virtual void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV,
242 unsigned ViewDepth) = 0;
243
Ying Yi84dc9712016-08-24 14:27:23 +0000244 /// \brief Render the project title, the report title \p CellText and the
245 /// created time for the view.
246 virtual void renderCellInTitle(raw_ostream &OS, StringRef CellText) = 0;
247
248 /// \brief Render the table header for a given source file
249 virtual void renderTableHeader(raw_ostream &OS, unsigned IndentLevel = 0) = 0;
250
Vedant Kumarf9151b92016-06-25 02:58:30 +0000251 /// @}
252
253 /// \brief Format a count using engineering notation with 3 significant
254 /// digits.
255 static std::string formatCount(uint64_t N);
256
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000257 /// \brief Check if region marker output is expected for a line.
258 bool shouldRenderRegionMarkers(bool LineHasMultipleRegions) const;
259
260 /// \brief Check if there are any sub-views attached to this view.
261 bool hasSubViews() const;
262
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000263 SourceCoverageView(StringRef SourceName, const MemoryBuffer &File,
Justin Bogner953e2402014-09-20 15:31:56 +0000264 const CoverageViewOptions &Options,
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000265 coverage::CoverageData &&CoverageInfo)
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000266 : SourceName(SourceName), File(File), Options(Options),
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000267 CoverageInfo(std::move(CoverageInfo)) {}
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000268
Vedant Kumarf9151b92016-06-25 02:58:30 +0000269public:
270 static std::unique_ptr<SourceCoverageView>
271 create(StringRef SourceName, const MemoryBuffer &File,
272 const CoverageViewOptions &Options,
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000273 coverage::CoverageData &&CoverageInfo);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000274
275 virtual ~SourceCoverageView() {}
276
Ying Yi24e91bd2016-09-06 21:41:38 +0000277 /// \brief Return the source name formatted for the host OS.
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000278 std::string getSourceName() const;
Ying Yi24e91bd2016-09-06 21:41:38 +0000279
Vedant Kumar0053c0b2016-09-08 00:56:48 +0000280 /// \brief Return a verbose description of the source name and the binary it
281 /// corresponds to.
282 std::string getVerboseSourceName() const;
Ying Yi84dc9712016-08-24 14:27:23 +0000283
Alex Lorenze82d89c2014-08-22 22:56:03 +0000284 const CoverageViewOptions &getOptions() const { return Options; }
285
Justin Bogner5e1400a2014-09-17 05:33:20 +0000286 /// \brief Add an expansion subview to this view.
287 void addExpansion(const coverage::CounterMappingRegion &Region,
Vedant Kumarf9151b92016-06-25 02:58:30 +0000288 std::unique_ptr<SourceCoverageView> View);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000289
Justin Bogner5e1400a2014-09-17 05:33:20 +0000290 /// \brief Add a function instantiation subview to this view.
291 void addInstantiation(StringRef FunctionName, unsigned Line,
Vedant Kumarf9151b92016-06-25 02:58:30 +0000292 std::unique_ptr<SourceCoverageView> View);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000293
Vedant Kumarf9151b92016-06-25 02:58:30 +0000294 /// \brief Print the code coverage information for a specific portion of a
295 /// source file to the output stream.
296 void print(raw_ostream &OS, bool WholeFile, bool ShowSourceName,
297 unsigned ViewDepth = 0);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000298};
299
300} // namespace llvm
301
302#endif // LLVM_COV_SOURCECOVERAGEVIEW_H