1 //===- SourceCoverageView.cpp - Code coverage view for source code --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements rendering for code coverage of source code.
12 //===----------------------------------------------------------------------===//
14 #include "SourceCoverageView.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Support/LineIterator.h"
20 void SourceCoverageView::renderLine(raw_ostream &OS, StringRef Line,
21 ArrayRef<HighlightRange> Ranges) {
29 unsigned PrevColumnStart = 0;
31 for (const auto &Range : Ranges) {
32 if (PrevColumnStart == Range.ColumnStart)
35 // Show the unhighlighted part
36 unsigned ColumnStart = PrevColumnStart = Range.ColumnStart;
37 OS << Line.substr(Start - 1, ColumnStart - Start);
39 // Show the highlighted part
40 auto Color = Range.Kind == HighlightRange::NotCovered ? raw_ostream::RED
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);
49 // Show the rest of the line
50 OS << Line.substr(Start - 1, Line.size() - Start + 1);
54 void SourceCoverageView::renderOffset(raw_ostream &OS, unsigned I) {
55 for (unsigned J = 0; J < I; ++J)
59 void SourceCoverageView::renderViewDivider(unsigned Offset, unsigned Length,
61 for (unsigned J = 1; J < Offset; ++J)
65 for (unsigned I = 0; I < Length; ++I)
70 SourceCoverageView::renderLineCoverageColumn(raw_ostream &OS,
71 const LineCoverageInfo &Line) {
72 if (!Line.isMapped()) {
73 OS.indent(LineCoverageColumnWidth) << '|';
76 SmallString<32> Buffer;
77 raw_svector_ostream BufferOS(Buffer);
78 BufferOS << Line.ExecutionCount;
79 auto Str = BufferOS.str();
81 Str = Str.substr(0, std::min(Str.size(), (size_t)LineCoverageColumnWidth));
83 OS.indent(LineCoverageColumnWidth - Str.size());
84 colored_ostream(OS, raw_ostream::MAGENTA,
85 Line.hasMultipleRegions() && Options.Colors)
90 void SourceCoverageView::renderLineNumberColumn(raw_ostream &OS,
92 SmallString<32> Buffer;
93 raw_svector_ostream BufferOS(Buffer);
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 << '|';
101 void SourceCoverageView::renderRegionMarkers(raw_ostream &OS,
102 ArrayRef<RegionMarker> Regions) {
103 SmallString<32> Buffer;
104 raw_svector_ostream BufferOS(Buffer);
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();
123 /// \brief Insert a new highlighting range into the line's highlighting ranges
124 /// Return line's new highlighting ranges in result.
125 static void insertHighlightRange(
126 ArrayRef<SourceCoverageView::HighlightRange> Ranges,
127 SourceCoverageView::HighlightRange RangeToInsert,
128 SmallVectorImpl<SourceCoverageView::HighlightRange> &Result) {
131 auto E = Ranges.size();
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,
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;
149 if (!NextRangeContainsInserted) {
151 // Ignore ranges that are contained in inserted range
152 while (I < E && RangeToInsert.contains(Ranges[I]))
157 Result.push_back(Ranges[I]);
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));
169 // Add the remaining ranges that are located after the inserted range
171 Result.push_back(Ranges[I]);
174 void SourceCoverageView::sortChildren() {
175 for (auto &I : Children)
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;
184 SourceCoverageView::HighlightRange
185 SourceCoverageView::getExpansionHighlightRange() const {
186 return HighlightRange(ExpansionRegion.LineStart, ExpansionRegion.ColumnStart,
187 ExpansionRegion.ColumnEnd, HighlightRange::Expanded);
190 template <typename T>
191 ArrayRef<T> gatherLineItems(size_t &CurrentIdx, const std::vector<T> &Items,
193 auto PrevIdx = CurrentIdx;
194 auto E = Items.size();
195 while (CurrentIdx < E && Items[CurrentIdx].Line == LineNo)
197 return ArrayRef<T>(Items.data() + PrevIdx, CurrentIdx - PrevIdx);
200 ArrayRef<std::unique_ptr<SourceCoverageView>>
201 gatherLineSubViews(size_t &CurrentIdx,
202 ArrayRef<std::unique_ptr<SourceCoverageView>> Items,
204 auto PrevIdx = CurrentIdx;
205 auto E = Items.size();
206 while (CurrentIdx < E &&
207 Items[CurrentIdx]->getSubViewsExpansionLine() == LineNo)
209 return ArrayRef<std::unique_ptr<SourceCoverageView>>(Items.data() + PrevIdx,
210 CurrentIdx - PrevIdx);
213 void SourceCoverageView::render(raw_ostream &OS, unsigned Offset) {
214 // Make sure that the children are in sorted order.
217 SmallVector<HighlightRange, 8> AdjustedLineHighlightRanges;
218 size_t CurrentChild = 0;
219 size_t CurrentHighlightRange = 0;
220 size_t CurrentRegionMarker = 0;
222 line_iterator Lines(File);
223 // Advance the line iterator to the first line.
224 while (Lines.line_number() < LineStart)
227 // The width of the leading columns
228 unsigned CombinedColumnWidth =
229 (Options.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
230 (Options.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
231 // The width of the line that is used to divide between the view and the
233 unsigned DividerWidth = CombinedColumnWidth + 4;
235 for (size_t I = 0; I < LineCount; ++I) {
236 unsigned LineNo = I + LineStart;
238 // Gather the child subviews that are visible on this line.
239 auto LineSubViews = gatherLineSubViews(CurrentChild, Children, LineNo);
241 renderOffset(OS, Offset);
242 if (Options.ShowLineStats)
243 renderLineCoverageColumn(OS, LineStats[I]);
244 if (Options.ShowLineNumbers)
245 renderLineNumberColumn(OS, LineNo);
247 // Gather highlighting ranges.
248 auto LineHighlightRanges =
249 gatherLineItems(CurrentHighlightRange, HighlightRanges, LineNo);
250 auto LineRanges = LineHighlightRanges;
251 // Highlight the expansion range if there is an expansion subview on this
253 if (!LineSubViews.empty() && LineSubViews.front()->isExpansionSubView() &&
255 insertHighlightRange(LineHighlightRanges,
256 LineSubViews.front()->getExpansionHighlightRange(),
257 AdjustedLineHighlightRanges);
258 LineRanges = AdjustedLineHighlightRanges;
261 // Display the source code for the current line.
262 StringRef Line = *Lines;
263 // Check if the line is empty, as line_iterator skips blank lines.
264 if (LineNo < Lines.line_number())
266 else if (!Lines.is_at_eof())
268 renderLine(OS, Line, LineRanges);
270 // Show the region markers.
271 bool ShowMarkers = !Options.ShowLineStatsOrRegionMarkers ||
272 LineStats[I].hasMultipleRegions();
273 auto LineMarkers = gatherLineItems(CurrentRegionMarker, Markers, LineNo);
274 if (ShowMarkers && !LineMarkers.empty()) {
275 renderOffset(OS, Offset);
276 OS.indent(CombinedColumnWidth);
277 renderRegionMarkers(OS, LineMarkers);
280 // Show the line's expanded child subviews.
281 bool FirstChildExpansion = true;
282 if (LineSubViews.empty())
284 unsigned NewOffset = Offset + 1;
285 renderViewDivider(NewOffset, DividerWidth, OS);
287 for (const auto &Child : LineSubViews) {
288 // If this subview shows a function instantiation, render the function's
290 if (Child->isInstantiationSubView()) {
291 renderOffset(OS, NewOffset);
293 Options.colored_ostream(OS, raw_ostream::CYAN) << Child->FunctionName
297 if (!FirstChildExpansion) {
298 // Re-render the current line and highlight the expansion range for
301 insertHighlightRange(LineHighlightRanges,
302 Child->getExpansionHighlightRange(),
303 AdjustedLineHighlightRanges);
304 renderOffset(OS, Offset);
305 OS.indent(CombinedColumnWidth + (Offset == 0 ? 0 : 1));
306 renderLine(OS, Line, AdjustedLineHighlightRanges);
307 renderViewDivider(NewOffset, DividerWidth, OS);
310 FirstChildExpansion = false;
312 // Render the child subview
313 Child->render(OS, NewOffset);
314 renderViewDivider(NewOffset, DividerWidth, OS);
321 SourceCoverageView::createLineCoverageInfo(SourceCoverageDataManager &Data) {
322 LineStats.resize(LineCount);
323 for (const auto &CR : Data.getSourceRegions()) {
324 if (CR.Kind == coverage::CounterMappingRegion::SkippedRegion) {
325 // Reset the line stats for skipped regions.
326 for (unsigned Line = CR.LineStart; Line <= CR.LineEnd;
328 LineStats[Line - LineStart] = LineCoverageInfo();
331 LineStats[CR.LineStart - LineStart].addRegionStartCount(CR.ExecutionCount);
332 for (unsigned Line = CR.LineStart + 1; Line <= CR.LineEnd; ++Line)
333 LineStats[Line - LineStart].addRegionCount(CR.ExecutionCount);
338 SourceCoverageView::createHighlightRanges(SourceCoverageDataManager &Data) {
339 auto CountedRegions = Data.getSourceRegions();
340 std::vector<bool> AlreadyHighlighted;
341 AlreadyHighlighted.resize(CountedRegions.size(), false);
343 for (size_t I = 0, S = CountedRegions.size(); I < S; ++I) {
344 const auto &CR = CountedRegions[I];
345 if (CR.Kind == coverage::CounterMappingRegion::SkippedRegion ||
346 CR.ExecutionCount != 0)
348 if (AlreadyHighlighted[I])
350 for (size_t J = 0; J < S; ++J) {
351 if (CR.contains(CountedRegions[J])) {
352 AlreadyHighlighted[J] = true;
355 if (CR.LineStart == CR.LineEnd) {
356 HighlightRanges.push_back(HighlightRange(
357 CR.LineStart, CR.ColumnStart, CR.ColumnEnd));
360 HighlightRanges.push_back(
361 HighlightRange(CR.LineStart, CR.ColumnStart,
362 std::numeric_limits<unsigned>::max()));
363 HighlightRanges.push_back(
364 HighlightRange(CR.LineEnd, 1, CR.ColumnEnd));
365 for (unsigned Line = CR.LineStart + 1; Line < CR.LineEnd;
367 HighlightRanges.push_back(
368 HighlightRange(Line, 1, std::numeric_limits<unsigned>::max()));
372 std::sort(HighlightRanges.begin(), HighlightRanges.end());
375 for (const auto &Range : HighlightRanges) {
376 outs() << "Highlighted line " << Range.Line << ", " << Range.ColumnStart
378 if (Range.ColumnEnd == std::numeric_limits<unsigned>::max()) {
381 outs() << Range.ColumnEnd << "\n";
387 void SourceCoverageView::createRegionMarkers(SourceCoverageDataManager &Data) {
388 for (const auto &CR : Data.getSourceRegions()) {
389 if (CR.Kind == coverage::CounterMappingRegion::SkippedRegion)
391 if (CR.LineStart >= LineStart)
393 RegionMarker(CR.LineStart, CR.ColumnStart, CR.ExecutionCount));
397 for (const auto &Marker : Markers) {
398 outs() << "Marker at " << Marker.Line << ":" << Marker.Column << " = "
399 << Marker.ExecutionCount << "\n";
404 void SourceCoverageView::load(SourceCoverageDataManager &Data) {
405 if (Options.ShowLineStats)
406 createLineCoverageInfo(Data);
408 createHighlightRanges(Data);
409 if (Options.ShowRegionMarkers)
410 createRegionMarkers(Data);