1 //===- SourceCoverageView.h - Code coverage view for source code ----------===//
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
7 //===----------------------------------------------------------------------===//
9 /// \file This class implements rendering for code coverage of source code.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_COV_SOURCECOVERAGEVIEW_H
14 #define LLVM_COV_SOURCECOVERAGEVIEW_H
16 #include "CoverageViewOptions.h"
17 #include "CoverageSummaryInfo.h"
18 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
19 #include "llvm/Support/MemoryBuffer.h"
24 using namespace coverage
;
26 class CoverageFiltersMatchAll
;
27 class SourceCoverageView
;
29 /// A view that represents a macro or include expansion.
30 struct ExpansionView
{
31 CounterMappingRegion Region
;
32 std::unique_ptr
<SourceCoverageView
> View
;
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
);
45 unsigned getLine() const { return Region
.LineStart
; }
46 unsigned getStartCol() const { return Region
.ColumnStart
; }
47 unsigned getEndCol() const { return Region
.ColumnEnd
; }
49 friend bool operator<(const ExpansionView
&LHS
, const ExpansionView
&RHS
) {
50 return LHS
.Region
.startLoc() < RHS
.Region
.startLoc();
54 /// A view that represents a function instantiation.
55 struct InstantiationView
{
56 StringRef FunctionName
;
58 std::unique_ptr
<SourceCoverageView
> View
;
60 InstantiationView(StringRef FunctionName
, unsigned Line
,
61 std::unique_ptr
<SourceCoverageView
> View
)
62 : FunctionName(FunctionName
), Line(Line
), View(std::move(View
)) {}
64 friend bool operator<(const InstantiationView
&LHS
,
65 const InstantiationView
&RHS
) {
66 return LHS
.Line
< RHS
.Line
;
70 /// A view that represents one or more branch regions on a given source line.
72 std::vector
<CountedRegion
> Regions
;
73 std::unique_ptr
<SourceCoverageView
> View
;
76 BranchView(unsigned Line
, ArrayRef
<CountedRegion
> Regions
,
77 std::unique_ptr
<SourceCoverageView
> View
)
78 : Regions(Regions
), View(std::move(View
)), Line(Line
) {}
80 unsigned getLine() const { return Line
; }
82 friend bool operator<(const BranchView
&LHS
, const BranchView
&RHS
) {
83 return LHS
.Line
< RHS
.Line
;
87 /// A file manager that handles format-aware file creation.
88 class CoveragePrinter
{
90 struct StreamDestructor
{
91 void operator()(raw_ostream
*OS
) const;
94 using OwnedStream
= std::unique_ptr
<raw_ostream
, StreamDestructor
>;
97 const CoverageViewOptions
&Opts
;
99 CoveragePrinter(const CoverageViewOptions
&Opts
) : Opts(Opts
) {}
101 /// Return `OutputDir/ToplevelDir/Path.Extension`. If \p InToplevel is
102 /// true, skip the ToplevelDir component. If \p Relative is true, skip the
103 /// OutputDir component.
104 std::string
getOutputPath(StringRef Path
, StringRef Extension
,
105 bool InToplevel
, bool Relative
= true) const;
107 /// If directory output is enabled, create a file in that directory
108 /// at the path given by getOutputPath(). Otherwise, return stdout.
109 Expected
<OwnedStream
> createOutputStream(StringRef Path
, StringRef Extension
,
110 bool InToplevel
) const;
112 /// Return the sub-directory name for file coverage reports.
113 static StringRef
getCoverageDir() { return "coverage"; }
116 static std::unique_ptr
<CoveragePrinter
>
117 create(const CoverageViewOptions
&Opts
);
119 virtual ~CoveragePrinter() {}
121 /// @name File Creation Interface
124 /// Create a file to print a coverage view into.
125 virtual Expected
<OwnedStream
> createViewFile(StringRef Path
,
126 bool InToplevel
) = 0;
128 /// Close a file which has been used to print a coverage view.
129 virtual void closeViewFile(OwnedStream OS
) = 0;
131 /// Create an index which lists reports for the given source files.
132 virtual Error
createIndexFile(ArrayRef
<std::string
> SourceFiles
,
133 const CoverageMapping
&Coverage
,
134 const CoverageFiltersMatchAll
&Filters
) = 0;
139 /// A code coverage view of a source file or function.
141 /// A source coverage view and its nested sub-views form a file-oriented
142 /// representation of code coverage data. This view can be printed out by a
143 /// renderer which implements the Rendering Interface.
144 class SourceCoverageView
{
145 /// A function or file name.
146 StringRef SourceName
;
148 /// A memory buffer backing the source on display.
149 const MemoryBuffer
&File
;
151 /// Various options to guide the coverage renderer.
152 const CoverageViewOptions
&Options
;
154 /// Complete coverage information about the source on display.
155 CoverageData CoverageInfo
;
157 /// A container for all expansions (e.g macros) in the source on display.
158 std::vector
<ExpansionView
> ExpansionSubViews
;
160 /// A container for all branches in the source on display.
161 std::vector
<BranchView
> BranchSubViews
;
163 /// A container for all instantiations (e.g template functions) in the source
165 std::vector
<InstantiationView
> InstantiationSubViews
;
167 /// Get the first uncovered line number for the source file.
168 unsigned getFirstUncoveredLineNo();
175 LineRef(StringRef Line
, int64_t LineNo
) : Line(Line
), LineNo(LineNo
) {}
178 using CoverageSegmentArray
= ArrayRef
<const CoverageSegment
*>;
180 /// @name Rendering Interface
183 /// Render a header for the view.
184 virtual void renderViewHeader(raw_ostream
&OS
) = 0;
186 /// Render a footer for the view.
187 virtual void renderViewFooter(raw_ostream
&OS
) = 0;
189 /// Render the source name for the view.
190 virtual void renderSourceName(raw_ostream
&OS
, bool WholeFile
) = 0;
192 /// Render the line prefix at the given \p ViewDepth.
193 virtual void renderLinePrefix(raw_ostream
&OS
, unsigned ViewDepth
) = 0;
195 /// Render the line suffix at the given \p ViewDepth.
196 virtual void renderLineSuffix(raw_ostream
&OS
, unsigned ViewDepth
) = 0;
198 /// Render a view divider at the given \p ViewDepth.
199 virtual void renderViewDivider(raw_ostream
&OS
, unsigned ViewDepth
) = 0;
201 /// Render a source line with highlighting.
202 virtual void renderLine(raw_ostream
&OS
, LineRef L
,
203 const LineCoverageStats
&LCS
, unsigned ExpansionCol
,
204 unsigned ViewDepth
) = 0;
206 /// Render the line's execution count column.
207 virtual void renderLineCoverageColumn(raw_ostream
&OS
,
208 const LineCoverageStats
&Line
) = 0;
210 /// Render the line number column.
211 virtual void renderLineNumberColumn(raw_ostream
&OS
, unsigned LineNo
) = 0;
213 /// Render all the region's execution counts on a line.
214 virtual void renderRegionMarkers(raw_ostream
&OS
,
215 const LineCoverageStats
&Line
,
216 unsigned ViewDepth
) = 0;
218 /// Render the site of an expansion.
219 virtual void renderExpansionSite(raw_ostream
&OS
, LineRef L
,
220 const LineCoverageStats
&LCS
,
221 unsigned ExpansionCol
,
222 unsigned ViewDepth
) = 0;
224 /// Render an expansion view and any nested views.
225 virtual void renderExpansionView(raw_ostream
&OS
, ExpansionView
&ESV
,
226 unsigned ViewDepth
) = 0;
228 /// Render an instantiation view and any nested views.
229 virtual void renderInstantiationView(raw_ostream
&OS
, InstantiationView
&ISV
,
230 unsigned ViewDepth
) = 0;
232 /// Render a branch view and any nested views.
233 virtual void renderBranchView(raw_ostream
&OS
, BranchView
&BRV
,
234 unsigned ViewDepth
) = 0;
236 /// Render \p Title, a project title if one is available, and the
238 virtual void renderTitle(raw_ostream
&OS
, StringRef CellText
) = 0;
240 /// Render the table header for a given source file.
241 virtual void renderTableHeader(raw_ostream
&OS
, unsigned FirstUncoveredLineNo
,
242 unsigned IndentLevel
) = 0;
246 /// Format a count using engineering notation with 3 significant
248 static std::string
formatCount(uint64_t N
);
250 /// Check if region marker output is expected for a line.
251 bool shouldRenderRegionMarkers(const LineCoverageStats
&LCS
) const;
253 /// Check if there are any sub-views attached to this view.
254 bool hasSubViews() const;
256 SourceCoverageView(StringRef SourceName
, const MemoryBuffer
&File
,
257 const CoverageViewOptions
&Options
,
258 CoverageData
&&CoverageInfo
)
259 : SourceName(SourceName
), File(File
), Options(Options
),
260 CoverageInfo(std::move(CoverageInfo
)) {}
263 static std::unique_ptr
<SourceCoverageView
>
264 create(StringRef SourceName
, const MemoryBuffer
&File
,
265 const CoverageViewOptions
&Options
, CoverageData
&&CoverageInfo
);
267 virtual ~SourceCoverageView() {}
269 /// Return the source name formatted for the host OS.
270 std::string
getSourceName() const;
272 const CoverageViewOptions
&getOptions() const { return Options
; }
274 /// Add an expansion subview to this view.
275 void addExpansion(const CounterMappingRegion
&Region
,
276 std::unique_ptr
<SourceCoverageView
> View
);
278 /// Add a function instantiation subview to this view.
279 void addInstantiation(StringRef FunctionName
, unsigned Line
,
280 std::unique_ptr
<SourceCoverageView
> View
);
282 /// Add a branch subview to this view.
283 void addBranch(unsigned Line
, ArrayRef
<CountedRegion
> Regions
,
284 std::unique_ptr
<SourceCoverageView
> View
);
286 /// Print the code coverage information for a specific portion of a
287 /// source file to the output stream.
288 void print(raw_ostream
&OS
, bool WholeFile
, bool ShowSourceName
,
289 bool ShowTitle
, unsigned ViewDepth
= 0);
294 #endif // LLVM_COV_SOURCECOVERAGEVIEW_H