[llvm-objcopy] [COFF] Fix warnings abuilt missing field initialization. NFC.
[llvm-complete.git] / tools / llvm-cov / SourceCoverageView.h
blobe3a2f9e5c0b43fe864f90c52d7e3dda429e33f1e
1 //===- 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 //===----------------------------------------------------------------------===//
9 ///
10 /// \file This class implements rendering for code coverage of source code.
11 ///
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_COV_SOURCECOVERAGEVIEW_H
15 #define LLVM_COV_SOURCECOVERAGEVIEW_H
17 #include "CoverageViewOptions.h"
18 #include "CoverageSummaryInfo.h"
19 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include <vector>
23 namespace llvm {
25 using namespace coverage;
27 class CoverageFiltersMatchAll;
28 class SourceCoverageView;
30 /// A view that represents a macro or include expansion.
31 struct ExpansionView {
32 CounterMappingRegion Region;
33 std::unique_ptr<SourceCoverageView> View;
35 ExpansionView(const CounterMappingRegion &Region,
36 std::unique_ptr<SourceCoverageView> View)
37 : Region(Region), View(std::move(View)) {}
38 ExpansionView(ExpansionView &&RHS)
39 : Region(std::move(RHS.Region)), View(std::move(RHS.View)) {}
40 ExpansionView &operator=(ExpansionView &&RHS) {
41 Region = std::move(RHS.Region);
42 View = std::move(RHS.View);
43 return *this;
46 unsigned getLine() const { return Region.LineStart; }
47 unsigned getStartCol() const { return Region.ColumnStart; }
48 unsigned getEndCol() const { return Region.ColumnEnd; }
50 friend bool operator<(const ExpansionView &LHS, const ExpansionView &RHS) {
51 return LHS.Region.startLoc() < RHS.Region.startLoc();
55 /// A view that represents a function instantiation.
56 struct InstantiationView {
57 StringRef FunctionName;
58 unsigned Line;
59 std::unique_ptr<SourceCoverageView> View;
61 InstantiationView(StringRef FunctionName, unsigned Line,
62 std::unique_ptr<SourceCoverageView> View)
63 : FunctionName(FunctionName), Line(Line), View(std::move(View)) {}
65 friend bool operator<(const InstantiationView &LHS,
66 const InstantiationView &RHS) {
67 return LHS.Line < RHS.Line;
71 /// A file manager that handles format-aware file creation.
72 class CoveragePrinter {
73 public:
74 struct StreamDestructor {
75 void operator()(raw_ostream *OS) const;
78 using OwnedStream = std::unique_ptr<raw_ostream, StreamDestructor>;
80 protected:
81 const CoverageViewOptions &Opts;
83 CoveragePrinter(const CoverageViewOptions &Opts) : Opts(Opts) {}
85 /// Return `OutputDir/ToplevelDir/Path.Extension`. If \p InToplevel is
86 /// false, skip the ToplevelDir component. If \p Relative is false, skip the
87 /// OutputDir component.
88 std::string getOutputPath(StringRef Path, StringRef Extension,
89 bool InToplevel, bool Relative = true) const;
91 /// If directory output is enabled, create a file in that directory
92 /// at the path given by getOutputPath(). Otherwise, return stdout.
93 Expected<OwnedStream> createOutputStream(StringRef Path, StringRef Extension,
94 bool InToplevel) const;
96 /// Return the sub-directory name for file coverage reports.
97 static StringRef getCoverageDir() { return "coverage"; }
99 public:
100 static std::unique_ptr<CoveragePrinter>
101 create(const CoverageViewOptions &Opts);
103 virtual ~CoveragePrinter() {}
105 /// @name File Creation Interface
106 /// @{
108 /// Create a file to print a coverage view into.
109 virtual Expected<OwnedStream> createViewFile(StringRef Path,
110 bool InToplevel) = 0;
112 /// Close a file which has been used to print a coverage view.
113 virtual void closeViewFile(OwnedStream OS) = 0;
115 /// Create an index which lists reports for the given source files.
116 virtual Error createIndexFile(ArrayRef<std::string> SourceFiles,
117 const CoverageMapping &Coverage,
118 const CoverageFiltersMatchAll &Filters) = 0;
120 /// @}
123 /// A code coverage view of a source file or function.
125 /// A source coverage view and its nested sub-views form a file-oriented
126 /// representation of code coverage data. This view can be printed out by a
127 /// renderer which implements the Rendering Interface.
128 class SourceCoverageView {
129 /// A function or file name.
130 StringRef SourceName;
132 /// A memory buffer backing the source on display.
133 const MemoryBuffer &File;
135 /// Various options to guide the coverage renderer.
136 const CoverageViewOptions &Options;
138 /// Complete coverage information about the source on display.
139 CoverageData CoverageInfo;
141 /// A container for all expansions (e.g macros) in the source on display.
142 std::vector<ExpansionView> ExpansionSubViews;
144 /// A container for all instantiations (e.g template functions) in the source
145 /// on display.
146 std::vector<InstantiationView> InstantiationSubViews;
148 /// Get the first uncovered line number for the source file.
149 unsigned getFirstUncoveredLineNo();
151 protected:
152 struct LineRef {
153 StringRef Line;
154 int64_t LineNo;
156 LineRef(StringRef Line, int64_t LineNo) : Line(Line), LineNo(LineNo) {}
159 using CoverageSegmentArray = ArrayRef<const CoverageSegment *>;
161 /// @name Rendering Interface
162 /// @{
164 /// Render a header for the view.
165 virtual void renderViewHeader(raw_ostream &OS) = 0;
167 /// Render a footer for the view.
168 virtual void renderViewFooter(raw_ostream &OS) = 0;
170 /// Render the source name for the view.
171 virtual void renderSourceName(raw_ostream &OS, bool WholeFile) = 0;
173 /// Render the line prefix at the given \p ViewDepth.
174 virtual void renderLinePrefix(raw_ostream &OS, unsigned ViewDepth) = 0;
176 /// Render the line suffix at the given \p ViewDepth.
177 virtual void renderLineSuffix(raw_ostream &OS, unsigned ViewDepth) = 0;
179 /// Render a view divider at the given \p ViewDepth.
180 virtual void renderViewDivider(raw_ostream &OS, unsigned ViewDepth) = 0;
182 /// Render a source line with highlighting.
183 virtual void renderLine(raw_ostream &OS, LineRef L,
184 const LineCoverageStats &LCS, unsigned ExpansionCol,
185 unsigned ViewDepth) = 0;
187 /// Render the line's execution count column.
188 virtual void renderLineCoverageColumn(raw_ostream &OS,
189 const LineCoverageStats &Line) = 0;
191 /// Render the line number column.
192 virtual void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0;
194 /// Render all the region's execution counts on a line.
195 virtual void renderRegionMarkers(raw_ostream &OS,
196 const LineCoverageStats &Line,
197 unsigned ViewDepth) = 0;
199 /// Render the site of an expansion.
200 virtual void renderExpansionSite(raw_ostream &OS, LineRef L,
201 const LineCoverageStats &LCS,
202 unsigned ExpansionCol,
203 unsigned ViewDepth) = 0;
205 /// Render an expansion view and any nested views.
206 virtual void renderExpansionView(raw_ostream &OS, ExpansionView &ESV,
207 unsigned ViewDepth) = 0;
209 /// Render an instantiation view and any nested views.
210 virtual void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV,
211 unsigned ViewDepth) = 0;
213 /// Render \p Title, a project title if one is available, and the
214 /// created time.
215 virtual void renderTitle(raw_ostream &OS, StringRef CellText) = 0;
217 /// Render the table header for a given source file.
218 virtual void renderTableHeader(raw_ostream &OS, unsigned FirstUncoveredLineNo,
219 unsigned IndentLevel) = 0;
221 /// @}
223 /// Format a count using engineering notation with 3 significant
224 /// digits.
225 static std::string formatCount(uint64_t N);
227 /// Check if region marker output is expected for a line.
228 bool shouldRenderRegionMarkers(const LineCoverageStats &LCS) const;
230 /// Check if there are any sub-views attached to this view.
231 bool hasSubViews() const;
233 SourceCoverageView(StringRef SourceName, const MemoryBuffer &File,
234 const CoverageViewOptions &Options,
235 CoverageData &&CoverageInfo)
236 : SourceName(SourceName), File(File), Options(Options),
237 CoverageInfo(std::move(CoverageInfo)) {}
239 public:
240 static std::unique_ptr<SourceCoverageView>
241 create(StringRef SourceName, const MemoryBuffer &File,
242 const CoverageViewOptions &Options, CoverageData &&CoverageInfo);
244 virtual ~SourceCoverageView() {}
246 /// Return the source name formatted for the host OS.
247 std::string getSourceName() const;
249 const CoverageViewOptions &getOptions() const { return Options; }
251 /// Add an expansion subview to this view.
252 void addExpansion(const CounterMappingRegion &Region,
253 std::unique_ptr<SourceCoverageView> View);
255 /// Add a function instantiation subview to this view.
256 void addInstantiation(StringRef FunctionName, unsigned Line,
257 std::unique_ptr<SourceCoverageView> View);
259 /// Print the code coverage information for a specific portion of a
260 /// source file to the output stream.
261 void print(raw_ostream &OS, bool WholeFile, bool ShowSourceName,
262 bool ShowTitle, unsigned ViewDepth = 0);
265 } // namespace llvm
267 #endif // LLVM_COV_SOURCECOVERAGEVIEW_H