blob: 763529a476bafc570eb3d88237f77c641a198fa9 [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"
Vedant Kumarf9151b92016-06-25 02:58:30 +000019#include "llvm/ADT/Optional.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000020#include "llvm/Support/MemoryBuffer.h"
21#include <vector>
22
23namespace llvm {
24
Justin Bogner5e1400a2014-09-17 05:33:20 +000025class SourceCoverageView;
26
Vedant Kumaree5a5e92016-06-25 05:48:54 +000027/// \brief A view that represents a macro or include expansion.
Justin Bogner5e1400a2014-09-17 05:33:20 +000028struct ExpansionView {
29 coverage::CounterMappingRegion Region;
30 std::unique_ptr<SourceCoverageView> View;
31
32 ExpansionView(const coverage::CounterMappingRegion &Region,
33 std::unique_ptr<SourceCoverageView> View)
34 : Region(Region), View(std::move(View)) {}
Justin Bogner99e95182014-09-17 06:32:48 +000035 ExpansionView(ExpansionView &&RHS)
36 : Region(std::move(RHS.Region)), View(std::move(RHS.View)) {}
37 ExpansionView &operator=(ExpansionView &&RHS) {
38 Region = std::move(RHS.Region);
39 View = std::move(RHS.View);
40 return *this;
41 }
Justin Bogner5e1400a2014-09-17 05:33:20 +000042
43 unsigned getLine() const { return Region.LineStart; }
44 unsigned getStartCol() const { return Region.ColumnStart; }
45 unsigned getEndCol() const { return Region.ColumnEnd; }
46
47 friend bool operator<(const ExpansionView &LHS, const ExpansionView &RHS) {
48 return LHS.Region.startLoc() < RHS.Region.startLoc();
49 }
50};
51
Vedant Kumaree5a5e92016-06-25 05:48:54 +000052/// \brief A view that represents a function instantiation.
Justin Bogner5e1400a2014-09-17 05:33:20 +000053struct InstantiationView {
54 StringRef FunctionName;
55 unsigned Line;
56 std::unique_ptr<SourceCoverageView> View;
57
58 InstantiationView(StringRef FunctionName, unsigned Line,
59 std::unique_ptr<SourceCoverageView> View)
60 : FunctionName(FunctionName), Line(Line), View(std::move(View)) {}
Justin Bogner99e95182014-09-17 06:32:48 +000061 InstantiationView(InstantiationView &&RHS)
62 : FunctionName(std::move(RHS.FunctionName)), Line(std::move(RHS.Line)),
63 View(std::move(RHS.View)) {}
64 InstantiationView &operator=(InstantiationView &&RHS) {
65 FunctionName = std::move(RHS.FunctionName);
66 Line = std::move(RHS.Line);
67 View = std::move(RHS.View);
68 return *this;
69 }
Justin Bogner5e1400a2014-09-17 05:33:20 +000070
71 friend bool operator<(const InstantiationView &LHS,
72 const InstantiationView &RHS) {
73 return LHS.Line < RHS.Line;
74 }
75};
76
Vedant Kumar60dcb482016-06-24 00:34:48 +000077/// \brief Coverage statistics for a single line.
78struct LineCoverageStats {
79 uint64_t ExecutionCount;
80 unsigned RegionCount;
81 bool Mapped;
82
83 LineCoverageStats() : ExecutionCount(0), RegionCount(0), Mapped(false) {}
84
85 bool isMapped() const { return Mapped; }
86
87 bool hasMultipleRegions() const { return RegionCount > 1; }
88
89 void addRegionStartCount(uint64_t Count) {
90 // The max of all region starts is the most interesting value.
91 addRegionCount(RegionCount ? std::max(ExecutionCount, Count) : Count);
92 ++RegionCount;
93 }
94
95 void addRegionCount(uint64_t Count) {
96 Mapped = true;
97 ExecutionCount = Count;
98 }
99};
100
Vedant Kumaree5a5e92016-06-25 05:48:54 +0000101/// \brief A code coverage view of a source file or function.
102///
103/// A source coverage view and its nested sub-views form a file-oriented
104/// representation of code coverage data. This view can be printed out by a
105/// renderer which implements the Rendering Interface.
Alex Lorenze82d89c2014-08-22 22:56:03 +0000106class SourceCoverageView {
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000107 /// A function or file name.
108 StringRef SourceName;
109
110 /// A memory buffer backing the source on display.
Alex Lorenze82d89c2014-08-22 22:56:03 +0000111 const MemoryBuffer &File;
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000112
113 /// Various options to guide the coverage renderer.
Alex Lorenze82d89c2014-08-22 22:56:03 +0000114 const CoverageViewOptions &Options;
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000115
116 /// Complete coverage information about the source on display.
Justin Bogner953e2402014-09-20 15:31:56 +0000117 coverage::CoverageData CoverageInfo;
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000118
119 /// A container for all expansions (e.g macros) in the source on display.
Justin Bogner5e1400a2014-09-17 05:33:20 +0000120 std::vector<ExpansionView> ExpansionSubViews;
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000121
122 /// A container for all instantiations (e.g template functions) in the source
123 /// on display.
Justin Bogner5e1400a2014-09-17 05:33:20 +0000124 std::vector<InstantiationView> InstantiationSubViews;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000125
Vedant Kumarf9151b92016-06-25 02:58:30 +0000126protected:
127 struct LineRef {
128 StringRef Line;
129 int64_t LineNo;
130
131 LineRef(StringRef Line, int64_t LineNo) : Line(Line), LineNo(LineNo) {}
132 };
133
134 using CoverageSegmentArray = ArrayRef<const coverage::CoverageSegment *>;
135
136 /// @name Rendering Interface
137 /// @{
138
139 /// \brief Render the source name for the view.
140 virtual void renderSourceName(raw_ostream &OS) = 0;
141
142 /// \brief Render the line prefix at the given \p ViewDepth.
143 virtual void renderLinePrefix(raw_ostream &OS, unsigned ViewDepth) = 0;
144
145 /// \brief Render a view divider at the given \p ViewDepth.
146 virtual void renderViewDivider(raw_ostream &OS, unsigned ViewDepth) = 0;
147
Alex Lorenze82d89c2014-08-22 22:56:03 +0000148 /// \brief Render a source line with highlighting.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000149 virtual void renderLine(raw_ostream &OS, LineRef L,
150 const coverage::CoverageSegment *WrappedSegment,
151 CoverageSegmentArray Segments, unsigned ExpansionCol,
152 unsigned ViewDepth) = 0;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000153
154 /// \brief Render the line's execution count column.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000155 virtual void renderLineCoverageColumn(raw_ostream &OS,
156 const LineCoverageStats &Line) = 0;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000157
158 /// \brief Render the line number column.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000159 virtual void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000160
161 /// \brief Render all the region's execution counts on a line.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000162 virtual void renderRegionMarkers(raw_ostream &OS,
163 CoverageSegmentArray Segments,
164 unsigned ViewDepth) = 0;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000165
Vedant Kumar8b12ecb2016-06-25 05:48:59 +0000166 /// \brief Render an expansion view. If the expansion site must be re-rendered
167 /// for clarity, it is passed in via \p FirstLine.
168 virtual unsigned
169 renderExpansionView(raw_ostream &OS, ExpansionView &ESV,
170 Optional<LineRef> FirstLine,
171 const coverage::CoverageSegment *WrappedSegment,
172 CoverageSegmentArray Segments, unsigned ExpansionCol,
173 unsigned ViewDepth) = 0;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000174
Vedant Kumarf9151b92016-06-25 02:58:30 +0000175 /// \brief Render an instantiation view.
176 virtual void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV,
177 unsigned ViewDepth) = 0;
178
179 /// @}
180
181 /// \brief Format a count using engineering notation with 3 significant
182 /// digits.
183 static std::string formatCount(uint64_t N);
184
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000185 SourceCoverageView(StringRef SourceName, const MemoryBuffer &File,
Justin Bogner953e2402014-09-20 15:31:56 +0000186 const CoverageViewOptions &Options,
187 coverage::CoverageData &&CoverageInfo)
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000188 : SourceName(SourceName), File(File), Options(Options),
189 CoverageInfo(std::move(CoverageInfo)) {}
190
Vedant Kumarf9151b92016-06-25 02:58:30 +0000191public:
192 static std::unique_ptr<SourceCoverageView>
193 create(StringRef SourceName, const MemoryBuffer &File,
194 const CoverageViewOptions &Options,
195 coverage::CoverageData &&CoverageInfo);
196
197 virtual ~SourceCoverageView() {}
198
Vedant Kumar9d70d0b2016-06-24 00:34:51 +0000199 StringRef getSourceName() const { return SourceName; }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000200
201 const CoverageViewOptions &getOptions() const { return Options; }
202
Justin Bogner5e1400a2014-09-17 05:33:20 +0000203 /// \brief Add an expansion subview to this view.
204 void addExpansion(const coverage::CounterMappingRegion &Region,
Vedant Kumarf9151b92016-06-25 02:58:30 +0000205 std::unique_ptr<SourceCoverageView> View);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000206
Justin Bogner5e1400a2014-09-17 05:33:20 +0000207 /// \brief Add a function instantiation subview to this view.
208 void addInstantiation(StringRef FunctionName, unsigned Line,
Vedant Kumarf9151b92016-06-25 02:58:30 +0000209 std::unique_ptr<SourceCoverageView> View);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000210
Vedant Kumarf9151b92016-06-25 02:58:30 +0000211 /// \brief Print the code coverage information for a specific portion of a
212 /// source file to the output stream.
213 void print(raw_ostream &OS, bool WholeFile, bool ShowSourceName,
214 unsigned ViewDepth = 0);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000215};
216
217} // namespace llvm
218
219#endif // LLVM_COV_SOURCECOVERAGEVIEW_H