blob: ddfc690c4822cab589f730ff9fcbaef136501663 [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;
113 unsigned LineStart, LineCount;
114 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
122 /// \brief Create the line coverage information using the coverage data.
123 void createLineCoverageInfo(SourceCoverageDataManager &Data);
124
125 /// \brief Create the line highlighting ranges using the coverage data.
126 void createHighlightRanges(SourceCoverageDataManager &Data);
127
128 /// \brief Create the region markers using the coverage data.
129 void createRegionMarkers(SourceCoverageDataManager &Data);
130
131 /// \brief Sort children by the starting location.
132 void sortChildren();
133
134 /// \brief Return a highlight range for the expansion region of this view.
135 HighlightRange getExpansionHighlightRange() const;
136
137 /// \brief Render a source line with highlighting.
138 void renderLine(raw_ostream &OS, StringRef Line,
139 ArrayRef<HighlightRange> Ranges);
140
141 void renderOffset(raw_ostream &OS, unsigned I);
142
143 void renderViewDivider(unsigned Offset, unsigned Length, raw_ostream &OS);
144
145 /// \brief Render the line's execution count column.
146 void renderLineCoverageColumn(raw_ostream &OS, const LineCoverageInfo &Line);
147
148 /// \brief Render the line number column.
149 void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo);
150
151 /// \brief Render all the region's execution counts on a line.
152 void renderRegionMarkers(raw_ostream &OS, ArrayRef<RegionMarker> Regions);
153
154 static const unsigned LineCoverageColumnWidth = 7;
155 static const unsigned LineNumberColumnWidth = 5;
156
157public:
158 SourceCoverageView(const MemoryBuffer &File,
159 const CoverageViewOptions &Options)
160 : File(File), Options(Options), LineStart(1), Kind(View),
161 ExpansionRegion(coverage::Counter(), 0, 0, 0, 0, 0) {
162 LineCount = File.getBuffer().count('\n') + 1;
163 }
164
165 SourceCoverageView(const MemoryBuffer &File,
166 const CoverageViewOptions &Options, unsigned LineStart,
167 unsigned LineEnd)
168 : File(File), Options(Options), LineStart(LineStart),
169 LineCount(LineEnd - LineStart + 1), Kind(View),
170 ExpansionRegion(coverage::Counter(), 0, 0, 0, 0, 0) {}
171
172 SourceCoverageView(SourceCoverageView &Parent, unsigned LineStart,
173 unsigned LineEnd, StringRef FunctionName)
174 : File(Parent.File), Options(Parent.Options), LineStart(LineStart),
175 LineCount(LineEnd - LineStart + 1), Kind(InstantiationView),
176 ExpansionRegion(coverage::Counter(), 0, LineEnd, 0, LineEnd, 0),
177 FunctionName(FunctionName) {}
178
179 SourceCoverageView(const MemoryBuffer &File,
180 const CoverageViewOptions &Options, unsigned LineStart,
181 unsigned LineEnd,
182 const coverage::CounterMappingRegion &ExpansionRegion)
183 : File(File), Options(Options), LineStart(LineStart),
184 LineCount(LineEnd - LineStart + 1), Kind(ExpansionView),
185 ExpansionRegion(ExpansionRegion) {}
186
187 const CoverageViewOptions &getOptions() const { return Options; }
188
189 bool isExpansionSubView() const { return Kind == ExpansionView; }
190
191 bool isInstantiationSubView() const { return Kind == InstantiationView; }
192
193 /// \brief Return the line number after which the subview expansion is shown.
194 unsigned getSubViewsExpansionLine() const {
195 return ExpansionRegion.LineStart;
196 }
197
198 void addChild(std::unique_ptr<SourceCoverageView> View) {
199 Children.push_back(std::move(View));
200 }
201
202 /// \brief Print the code coverage information for a specific
203 /// portion of a source file to the output stream.
204 void render(raw_ostream &OS, unsigned Offset = 0);
205
206 /// \brief Load the coverage information required for rendering
207 /// from the mapping regions in the data manager.
208 void load(SourceCoverageDataManager &Data);
209};
210
211} // namespace llvm
212
213#endif // LLVM_COV_SOURCECOVERAGEVIEW_H