blob: 87a68e0db032ec30266f661f66c96160c258ec7a [file] [log] [blame]
Alex Lorenze82d89c2014-08-22 22:56:03 +00001//===- SourceCoverageView.cpp - 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#include "SourceCoverageView.h"
15#include "llvm/ADT/SmallString.h"
16#include "llvm/Support/LineIterator.h"
17
18using namespace llvm;
19
20void SourceCoverageView::renderLine(raw_ostream &OS, StringRef Line,
21 ArrayRef<HighlightRange> Ranges) {
22 if (Ranges.empty()) {
23 OS << Line << "\n";
24 return;
25 }
26 if (Line.empty())
27 Line = " ";
28
29 unsigned PrevColumnStart = 0;
30 unsigned Start = 1;
31 for (const auto &Range : Ranges) {
32 if (PrevColumnStart == Range.ColumnStart)
33 continue;
34
35 // Show the unhighlighted part
36 unsigned ColumnStart = PrevColumnStart = Range.ColumnStart;
37 OS << Line.substr(Start - 1, ColumnStart - Start);
38
39 // Show the highlighted part
40 auto Color = Range.Kind == HighlightRange::NotCovered ? raw_ostream::RED
41 : raw_ostream::CYAN;
42 OS.changeColor(Color, false, true);
43 unsigned ColumnEnd = std::min(Range.ColumnEnd, (unsigned)Line.size() + 1);
44 OS << Line.substr(ColumnStart - 1, ColumnEnd - ColumnStart);
45 Start = ColumnEnd;
46 OS.resetColor();
47 }
48
49 // Show the rest of the line
50 OS << Line.substr(Start - 1, Line.size() - Start + 1);
51 OS << "\n";
52}
53
54void SourceCoverageView::renderOffset(raw_ostream &OS, unsigned I) {
55 for (unsigned J = 0; J < I; ++J)
56 OS << " |";
57}
58
59void SourceCoverageView::renderViewDivider(unsigned Offset, unsigned Length,
60 raw_ostream &OS) {
61 for (unsigned J = 1; J < Offset; ++J)
62 OS << " |";
63 if (Offset != 0)
64 OS.indent(2);
65 for (unsigned I = 0; I < Length; ++I)
66 OS << "-";
67}
68
69void
70SourceCoverageView::renderLineCoverageColumn(raw_ostream &OS,
71 const LineCoverageInfo &Line) {
72 if (!Line.isMapped()) {
73 OS.indent(LineCoverageColumnWidth) << '|';
74 return;
75 }
76 SmallString<32> Buffer;
77 raw_svector_ostream BufferOS(Buffer);
78 BufferOS << Line.ExecutionCount;
79 auto Str = BufferOS.str();
80 // Trim
81 Str = Str.substr(0, std::min(Str.size(), (size_t)LineCoverageColumnWidth));
82 // Align to the right
83 OS.indent(LineCoverageColumnWidth - Str.size());
84 colored_ostream(OS, raw_ostream::MAGENTA,
85 Line.hasMultipleRegions() && Options.Colors)
86 << Str;
87 OS << '|';
88}
89
90void SourceCoverageView::renderLineNumberColumn(raw_ostream &OS,
91 unsigned LineNo) {
92 SmallString<32> Buffer;
93 raw_svector_ostream BufferOS(Buffer);
94 BufferOS << LineNo;
95 auto Str = BufferOS.str();
96 // Trim and align to the right
97 Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth));
98 OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
99}
100
101void SourceCoverageView::renderRegionMarkers(raw_ostream &OS,
102 ArrayRef<RegionMarker> Regions) {
103 SmallString<32> Buffer;
104 raw_svector_ostream BufferOS(Buffer);
105
106 unsigned PrevColumn = 1;
107 for (const auto &Region : Regions) {
108 // Skip to the new region
109 if (Region.Column > PrevColumn)
110 OS.indent(Region.Column - PrevColumn);
111 PrevColumn = Region.Column + 1;
112 BufferOS << Region.ExecutionCount;
113 StringRef Str = BufferOS.str();
114 // Trim the execution count
115 Str = Str.substr(0, std::min(Str.size(), (size_t)7));
116 PrevColumn += Str.size();
117 OS << '^' << Str;
118 Buffer.clear();
119 }
120 OS << "\n";
121}
122
123/// \brief Insert a new highlighting range into the line's highlighting ranges
124/// Return line's new highlighting ranges in result.
125static void insertHighlightRange(
126 ArrayRef<SourceCoverageView::HighlightRange> Ranges,
127 SourceCoverageView::HighlightRange RangeToInsert,
128 SmallVectorImpl<SourceCoverageView::HighlightRange> &Result) {
129 Result.clear();
130 size_t I = 0;
131 auto E = Ranges.size();
132 for (; I < E; ++I) {
133 if (RangeToInsert.ColumnStart < Ranges[I].ColumnEnd) {
134 const auto &Range = Ranges[I];
135 bool NextRangeContainsInserted = false;
136 // If the next range starts before the inserted range, move the end of the
137 // next range to the start of the inserted range.
138 if (Range.ColumnStart < RangeToInsert.ColumnStart) {
139 if (RangeToInsert.ColumnStart != Range.ColumnStart)
140 Result.push_back(SourceCoverageView::HighlightRange(
141 Range.Line, Range.ColumnStart, RangeToInsert.ColumnStart,
142 Range.Kind));
143 // If the next range also ends after the inserted range, keep this range
144 // and create a new range that starts at the inserted range and ends
145 // at the next range later.
146 if (Range.ColumnEnd > RangeToInsert.ColumnEnd)
147 NextRangeContainsInserted = true;
148 }
149 if (!NextRangeContainsInserted) {
150 ++I;
151 // Ignore ranges that are contained in inserted range
152 while (I < E && RangeToInsert.contains(Ranges[I]))
153 ++I;
154 }
155 break;
156 }
157 Result.push_back(Ranges[I]);
158 }
159 Result.push_back(RangeToInsert);
160 // If the next range starts before the inserted range end, move the start
161 // of the next range to the end of the inserted range.
162 if (I < E && Ranges[I].ColumnStart < RangeToInsert.ColumnEnd) {
163 const auto &Range = Ranges[I];
164 if (RangeToInsert.ColumnEnd != Range.ColumnEnd)
165 Result.push_back(SourceCoverageView::HighlightRange(
166 Range.Line, RangeToInsert.ColumnEnd, Range.ColumnEnd, Range.Kind));
167 ++I;
168 }
169 // Add the remaining ranges that are located after the inserted range
170 for (; I < E; ++I)
171 Result.push_back(Ranges[I]);
172}
173
174void SourceCoverageView::sortChildren() {
175 for (auto &I : Children)
176 I->sortChildren();
177 std::sort(Children.begin(), Children.end(),
178 [](const std::unique_ptr<SourceCoverageView> &LHS,
179 const std::unique_ptr<SourceCoverageView> &RHS) {
180 return LHS->ExpansionRegion < RHS->ExpansionRegion;
181 });
182}
183
184SourceCoverageView::HighlightRange
185SourceCoverageView::getExpansionHighlightRange() const {
186 return HighlightRange(ExpansionRegion.LineStart, ExpansionRegion.ColumnStart,
187 ExpansionRegion.ColumnEnd, HighlightRange::Expanded);
188}
189
190template <typename T>
191ArrayRef<T> gatherLineItems(size_t &CurrentIdx, const std::vector<T> &Items,
192 unsigned LineNo) {
193 auto PrevIdx = CurrentIdx;
194 auto E = Items.size();
195 while (CurrentIdx < E && Items[CurrentIdx].Line == LineNo)
196 ++CurrentIdx;
197 return ArrayRef<T>(Items.data() + PrevIdx, CurrentIdx - PrevIdx);
198}
199
200ArrayRef<std::unique_ptr<SourceCoverageView>>
201gatherLineSubViews(size_t &CurrentIdx,
202 ArrayRef<std::unique_ptr<SourceCoverageView>> Items,
203 unsigned LineNo) {
204 auto PrevIdx = CurrentIdx;
205 auto E = Items.size();
206 while (CurrentIdx < E &&
207 Items[CurrentIdx]->getSubViewsExpansionLine() == LineNo)
208 ++CurrentIdx;
Justin Bogner3f81d492014-09-10 06:06:07 +0000209 return Items.slice(PrevIdx, CurrentIdx - PrevIdx);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000210}
211
212void SourceCoverageView::render(raw_ostream &OS, unsigned Offset) {
213 // Make sure that the children are in sorted order.
214 sortChildren();
215
216 SmallVector<HighlightRange, 8> AdjustedLineHighlightRanges;
217 size_t CurrentChild = 0;
218 size_t CurrentHighlightRange = 0;
219 size_t CurrentRegionMarker = 0;
220
221 line_iterator Lines(File);
222 // Advance the line iterator to the first line.
Justin Bogner7dad93b2014-09-15 03:41:04 +0000223 while (Lines.line_number() < LineOffset)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000224 ++Lines;
225
226 // The width of the leading columns
227 unsigned CombinedColumnWidth =
228 (Options.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
229 (Options.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
230 // The width of the line that is used to divide between the view and the
231 // subviews.
232 unsigned DividerWidth = CombinedColumnWidth + 4;
233
Justin Bogner7dad93b2014-09-15 03:41:04 +0000234 for (size_t I = 0, E = LineStats.size(); I < E; ++I) {
235 unsigned LineNo = I + LineOffset;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000236
237 // Gather the child subviews that are visible on this line.
238 auto LineSubViews = gatherLineSubViews(CurrentChild, Children, LineNo);
239
240 renderOffset(OS, Offset);
241 if (Options.ShowLineStats)
242 renderLineCoverageColumn(OS, LineStats[I]);
243 if (Options.ShowLineNumbers)
244 renderLineNumberColumn(OS, LineNo);
245
246 // Gather highlighting ranges.
247 auto LineHighlightRanges =
248 gatherLineItems(CurrentHighlightRange, HighlightRanges, LineNo);
249 auto LineRanges = LineHighlightRanges;
250 // Highlight the expansion range if there is an expansion subview on this
251 // line.
252 if (!LineSubViews.empty() && LineSubViews.front()->isExpansionSubView() &&
253 Options.Colors) {
254 insertHighlightRange(LineHighlightRanges,
255 LineSubViews.front()->getExpansionHighlightRange(),
256 AdjustedLineHighlightRanges);
257 LineRanges = AdjustedLineHighlightRanges;
258 }
259
260 // Display the source code for the current line.
261 StringRef Line = *Lines;
262 // Check if the line is empty, as line_iterator skips blank lines.
263 if (LineNo < Lines.line_number())
264 Line = "";
265 else if (!Lines.is_at_eof())
266 ++Lines;
267 renderLine(OS, Line, LineRanges);
268
269 // Show the region markers.
270 bool ShowMarkers = !Options.ShowLineStatsOrRegionMarkers ||
271 LineStats[I].hasMultipleRegions();
272 auto LineMarkers = gatherLineItems(CurrentRegionMarker, Markers, LineNo);
273 if (ShowMarkers && !LineMarkers.empty()) {
274 renderOffset(OS, Offset);
275 OS.indent(CombinedColumnWidth);
276 renderRegionMarkers(OS, LineMarkers);
277 }
278
279 // Show the line's expanded child subviews.
280 bool FirstChildExpansion = true;
281 if (LineSubViews.empty())
282 continue;
283 unsigned NewOffset = Offset + 1;
284 renderViewDivider(NewOffset, DividerWidth, OS);
285 OS << "\n";
286 for (const auto &Child : LineSubViews) {
287 // If this subview shows a function instantiation, render the function's
288 // name.
289 if (Child->isInstantiationSubView()) {
290 renderOffset(OS, NewOffset);
291 OS << ' ';
292 Options.colored_ostream(OS, raw_ostream::CYAN) << Child->FunctionName
293 << ":";
294 OS << "\n";
295 } else {
296 if (!FirstChildExpansion) {
297 // Re-render the current line and highlight the expansion range for
298 // this
299 // subview.
300 insertHighlightRange(LineHighlightRanges,
301 Child->getExpansionHighlightRange(),
302 AdjustedLineHighlightRanges);
303 renderOffset(OS, Offset);
304 OS.indent(CombinedColumnWidth + (Offset == 0 ? 0 : 1));
305 renderLine(OS, Line, AdjustedLineHighlightRanges);
306 renderViewDivider(NewOffset, DividerWidth, OS);
307 OS << "\n";
308 } else
309 FirstChildExpansion = false;
310 }
311 // Render the child subview
312 Child->render(OS, NewOffset);
313 renderViewDivider(NewOffset, DividerWidth, OS);
314 OS << "\n";
315 }
316 }
317}
318
319void
320SourceCoverageView::createLineCoverageInfo(SourceCoverageDataManager &Data) {
Justin Bogner7dad93b2014-09-15 03:41:04 +0000321 auto CountedRegions = Data.getSourceRegions();
322 if (!CountedRegions.size())
323 return;
324
325 LineOffset = CountedRegions.front().LineStart;
326 LineStats.resize(CountedRegions.front().LineEnd - LineOffset + 1);
327 for (const auto &CR : CountedRegions) {
328 if (CR.LineEnd > LineStats.size())
329 LineStats.resize(CR.LineEnd - LineOffset + 1);
Justin Bognere53be062014-09-09 05:32:18 +0000330 if (CR.Kind == coverage::CounterMappingRegion::SkippedRegion) {
331 // Reset the line stats for skipped regions.
332 for (unsigned Line = CR.LineStart; Line <= CR.LineEnd;
333 ++Line)
Justin Bogner7dad93b2014-09-15 03:41:04 +0000334 LineStats[Line - LineOffset] = LineCoverageInfo();
Justin Bognere53be062014-09-09 05:32:18 +0000335 continue;
336 }
Justin Bogner7dad93b2014-09-15 03:41:04 +0000337 LineStats[CR.LineStart - LineOffset].addRegionStartCount(CR.ExecutionCount);
Justin Bognere53be062014-09-09 05:32:18 +0000338 for (unsigned Line = CR.LineStart + 1; Line <= CR.LineEnd; ++Line)
Justin Bogner7dad93b2014-09-15 03:41:04 +0000339 LineStats[Line - LineOffset].addRegionCount(CR.ExecutionCount);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000340 }
341}
342
343void
344SourceCoverageView::createHighlightRanges(SourceCoverageDataManager &Data) {
Justin Bognere53be062014-09-09 05:32:18 +0000345 auto CountedRegions = Data.getSourceRegions();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000346 std::vector<bool> AlreadyHighlighted;
Justin Bognere53be062014-09-09 05:32:18 +0000347 AlreadyHighlighted.resize(CountedRegions.size(), false);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000348
Justin Bognere53be062014-09-09 05:32:18 +0000349 for (size_t I = 0, S = CountedRegions.size(); I < S; ++I) {
350 const auto &CR = CountedRegions[I];
351 if (CR.Kind == coverage::CounterMappingRegion::SkippedRegion ||
352 CR.ExecutionCount != 0)
Alex Lorenze82d89c2014-08-22 22:56:03 +0000353 continue;
354 if (AlreadyHighlighted[I])
355 continue;
356 for (size_t J = 0; J < S; ++J) {
Justin Bognere53be062014-09-09 05:32:18 +0000357 if (CR.contains(CountedRegions[J])) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000358 AlreadyHighlighted[J] = true;
359 }
360 }
Justin Bognere53be062014-09-09 05:32:18 +0000361 if (CR.LineStart == CR.LineEnd) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000362 HighlightRanges.push_back(HighlightRange(
Justin Bognere53be062014-09-09 05:32:18 +0000363 CR.LineStart, CR.ColumnStart, CR.ColumnEnd));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000364 continue;
365 }
366 HighlightRanges.push_back(
Justin Bognere53be062014-09-09 05:32:18 +0000367 HighlightRange(CR.LineStart, CR.ColumnStart,
Alex Lorenze82d89c2014-08-22 22:56:03 +0000368 std::numeric_limits<unsigned>::max()));
369 HighlightRanges.push_back(
Justin Bognere53be062014-09-09 05:32:18 +0000370 HighlightRange(CR.LineEnd, 1, CR.ColumnEnd));
371 for (unsigned Line = CR.LineStart + 1; Line < CR.LineEnd;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000372 ++Line) {
373 HighlightRanges.push_back(
374 HighlightRange(Line, 1, std::numeric_limits<unsigned>::max()));
375 }
376 }
377
378 std::sort(HighlightRanges.begin(), HighlightRanges.end());
379
380 if (Options.Debug) {
381 for (const auto &Range : HighlightRanges) {
382 outs() << "Highlighted line " << Range.Line << ", " << Range.ColumnStart
383 << " -> ";
384 if (Range.ColumnEnd == std::numeric_limits<unsigned>::max()) {
385 outs() << "?\n";
386 } else {
387 outs() << Range.ColumnEnd << "\n";
388 }
389 }
390 }
391}
392
393void SourceCoverageView::createRegionMarkers(SourceCoverageDataManager &Data) {
Justin Bogner7dad93b2014-09-15 03:41:04 +0000394 for (const auto &CR : Data.getSourceRegions())
395 if (CR.Kind != coverage::CounterMappingRegion::SkippedRegion)
Justin Bognere53be062014-09-09 05:32:18 +0000396 Markers.push_back(
397 RegionMarker(CR.LineStart, CR.ColumnStart, CR.ExecutionCount));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000398
399 if (Options.Debug) {
400 for (const auto &Marker : Markers) {
401 outs() << "Marker at " << Marker.Line << ":" << Marker.Column << " = "
402 << Marker.ExecutionCount << "\n";
403 }
404 }
405}
406
407void SourceCoverageView::load(SourceCoverageDataManager &Data) {
408 if (Options.ShowLineStats)
409 createLineCoverageInfo(Data);
410 if (Options.Colors)
411 createHighlightRanges(Data);
412 if (Options.ShowRegionMarkers)
413 createRegionMarkers(Data);
414}