blob: 0777c23a855afdae1547cc71aa9582a0c26119be [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//===----------------------------------------------------------------------===//
9//
10// This class implements rendering for code coverage of source code.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_COV_SOURCECOVERAGEVIEW_H
15#define LLVM_COV_SOURCECOVERAGEVIEW_H
16
17#include "CoverageViewOptions.h"
18#include "SourceCoverageDataManager.h"
19#include "llvm/ProfileData/CoverageMapping.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include <vector>
22
23namespace llvm {
24
25/// \brief A code coverage view of a specific source file.
26/// It can have embedded coverage views.
27class SourceCoverageView {
28public:
29 enum SubViewKind { View, ExpansionView, InstantiationView };
30
31 /// \brief Coverage information for a single line.
32 struct LineCoverageInfo {
33 uint64_t ExecutionCount;
34 unsigned RegionCount;
35 bool Mapped;
36
37 LineCoverageInfo() : ExecutionCount(0), RegionCount(0), Mapped(false) {}
38
39 bool isMapped() const { return Mapped; }
40
41 bool hasMultipleRegions() const { return RegionCount > 1; }
42
43 void addRegionStartCount(uint64_t Count) {
44 Mapped = true;
45 ExecutionCount = Count;
46 ++RegionCount;
47 }
48
49 void addRegionCount(uint64_t Count) {
50 Mapped = true;
51 ExecutionCount = Count;
52 }
53 };
54
55 /// \brief A marker that points at the start
56 /// of a specific mapping region.
57 struct RegionMarker {
58 unsigned Line, Column;
59 uint64_t ExecutionCount;
60
61 RegionMarker(unsigned Line, unsigned Column, uint64_t Value)
62 : Line(Line), Column(Column), ExecutionCount(Value) {}
63 };
64
65 /// \brief A single line source range used to
66 /// render highlighted text.
67 struct HighlightRange {
68 enum HighlightKind {
69 /// The code that wasn't executed.
70 NotCovered,
71
72 /// The region of code that was expanded.
73 Expanded
74 };
75 HighlightKind Kind;
76 unsigned Line;
77 unsigned ColumnStart;
78 unsigned ColumnEnd;
79
80 HighlightRange(unsigned Line, unsigned ColumnStart, unsigned ColumnEnd,
81 HighlightKind Kind = NotCovered)
82 : Kind(Kind), Line(Line), ColumnStart(ColumnStart),
83 ColumnEnd(ColumnEnd) {}
84
85 bool operator<(const HighlightRange &Other) const {
86 if (Line == Other.Line)
87 return ColumnStart < Other.ColumnStart;
88 return Line < Other.Line;
89 }
90
91 bool columnStartOverlaps(const HighlightRange &Other) const {
92 return ColumnStart <= Other.ColumnStart && ColumnEnd > Other.ColumnStart;
93 }
94 bool columnEndOverlaps(const HighlightRange &Other) const {
95 return ColumnEnd >= Other.ColumnEnd && ColumnStart < Other.ColumnEnd;
96 }
97 bool contains(const HighlightRange &Other) const {
98 if (Line != Other.Line)
99 return false;
100 return ColumnStart <= Other.ColumnStart && ColumnEnd >= Other.ColumnEnd;
101 }
102
103 bool overlaps(const HighlightRange &Other) const {
104 if (Line != Other.Line)
105 return false;
106 return columnStartOverlaps(Other) || columnEndOverlaps(Other);
107 }
108 };
109
110private:
111 const MemoryBuffer &File;
112 const CoverageViewOptions &Options;
Justin Bogner7dad93b2014-09-15 03:41:04 +0000113 unsigned LineOffset;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000114 SubViewKind Kind;
115 coverage::CounterMappingRegion ExpansionRegion;
116 std::vector<std::unique_ptr<SourceCoverageView>> Children;
117 std::vector<LineCoverageInfo> LineStats;
118 std::vector<HighlightRange> HighlightRanges;
119 std::vector<RegionMarker> Markers;
120 StringRef FunctionName;
121
Justin Bogner0b3614f2014-09-15 22:12:28 +0000122 /// \brief Initialize the visible source range for this view.
123 void setUpVisibleRange(SourceCoverageDataManager &Data);
124
Alex Lorenze82d89c2014-08-22 22:56:03 +0000125 /// \brief Create the line coverage information using the coverage data.
126 void createLineCoverageInfo(SourceCoverageDataManager &Data);
127
128 /// \brief Create the line highlighting ranges using the coverage data.
129 void createHighlightRanges(SourceCoverageDataManager &Data);
130
131 /// \brief Create the region markers using the coverage data.
132 void createRegionMarkers(SourceCoverageDataManager &Data);
133
134 /// \brief Sort children by the starting location.
135 void sortChildren();
136
137 /// \brief Return a highlight range for the expansion region of this view.
138 HighlightRange getExpansionHighlightRange() const;
139
140 /// \brief Render a source line with highlighting.
141 void renderLine(raw_ostream &OS, StringRef Line,
142 ArrayRef<HighlightRange> Ranges);
143
Justin Bogner76e251c2014-09-16 06:21:57 +0000144 void renderIndent(raw_ostream &OS, unsigned Level);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000145
146 void renderViewDivider(unsigned Offset, unsigned Length, raw_ostream &OS);
147
148 /// \brief Render the line's execution count column.
149 void renderLineCoverageColumn(raw_ostream &OS, const LineCoverageInfo &Line);
150
151 /// \brief Render the line number column.
152 void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo);
153
154 /// \brief Render all the region's execution counts on a line.
155 void renderRegionMarkers(raw_ostream &OS, ArrayRef<RegionMarker> Regions);
156
157 static const unsigned LineCoverageColumnWidth = 7;
158 static const unsigned LineNumberColumnWidth = 5;
159
160public:
161 SourceCoverageView(const MemoryBuffer &File,
162 const CoverageViewOptions &Options)
Justin Bogner7dad93b2014-09-15 03:41:04 +0000163 : File(File), Options(Options), LineOffset(0), Kind(View),
Alex Lorenze82d89c2014-08-22 22:56:03 +0000164 ExpansionRegion(coverage::Counter(), 0, 0, 0, 0, 0) {}
165
Justin Bogner7dad93b2014-09-15 03:41:04 +0000166 SourceCoverageView(SourceCoverageView &Parent, StringRef FunctionName)
167 : File(Parent.File), Options(Parent.Options), LineOffset(0),
168 Kind(InstantiationView),
169 ExpansionRegion(coverage::Counter(), 0, 0, 0, 0, 0),
Alex Lorenze82d89c2014-08-22 22:56:03 +0000170 FunctionName(FunctionName) {}
171
172 SourceCoverageView(const MemoryBuffer &File,
Justin Bogner7dad93b2014-09-15 03:41:04 +0000173 const CoverageViewOptions &Options,
Alex Lorenze82d89c2014-08-22 22:56:03 +0000174 const coverage::CounterMappingRegion &ExpansionRegion)
Justin Bogner7dad93b2014-09-15 03:41:04 +0000175 : File(File), Options(Options), LineOffset(0), Kind(ExpansionView),
Alex Lorenze82d89c2014-08-22 22:56:03 +0000176 ExpansionRegion(ExpansionRegion) {}
177
178 const CoverageViewOptions &getOptions() const { return Options; }
179
180 bool isExpansionSubView() const { return Kind == ExpansionView; }
181
182 bool isInstantiationSubView() const { return Kind == InstantiationView; }
183
184 /// \brief Return the line number after which the subview expansion is shown.
185 unsigned getSubViewsExpansionLine() const {
186 return ExpansionRegion.LineStart;
187 }
188
189 void addChild(std::unique_ptr<SourceCoverageView> View) {
190 Children.push_back(std::move(View));
191 }
192
193 /// \brief Print the code coverage information for a specific
194 /// portion of a source file to the output stream.
Justin Bogner76e251c2014-09-16 06:21:57 +0000195 void render(raw_ostream &OS, unsigned IndentLevel = 0);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000196
197 /// \brief Load the coverage information required for rendering
198 /// from the mapping regions in the data manager.
199 void load(SourceCoverageDataManager &Data);
200};
201
202} // namespace llvm
203
204#endif // LLVM_COV_SOURCECOVERAGEVIEW_H