llvm-cov: Distinguish expansion/instantiation from SourceCoverageView
[oota-llvm.git] / tools / llvm-cov / SourceCoverageView.cpp
1 //===- 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
18 using namespace llvm;
19
20 void 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   if (Options.Debug) {
54     for (const auto &Range : Ranges) {
55       errs() << "Highlighted line " << Range.Line << ", " << Range.ColumnStart
56              << " -> ";
57       if (Range.ColumnEnd == std::numeric_limits<unsigned>::max()) {
58         errs() << "?\n";
59       } else {
60         errs() << Range.ColumnEnd << "\n";
61       }
62     }
63   }
64 }
65
66 void SourceCoverageView::renderIndent(raw_ostream &OS, unsigned Level) {
67   for (unsigned I = 0; I < Level; ++I)
68     OS << "  |";
69 }
70
71 void SourceCoverageView::renderViewDivider(unsigned Level, unsigned Length,
72                                            raw_ostream &OS) {
73   assert(Level != 0 && "Cannot render divider at top level");
74   renderIndent(OS, Level - 1);
75   OS.indent(2);
76   for (unsigned I = 0; I < Length; ++I)
77     OS << "-";
78 }
79
80 void
81 SourceCoverageView::renderLineCoverageColumn(raw_ostream &OS,
82                                              const LineCoverageInfo &Line) {
83   if (!Line.isMapped()) {
84     OS.indent(LineCoverageColumnWidth) << '|';
85     return;
86   }
87   SmallString<32> Buffer;
88   raw_svector_ostream BufferOS(Buffer);
89   BufferOS << Line.ExecutionCount;
90   auto Str = BufferOS.str();
91   // Trim
92   Str = Str.substr(0, std::min(Str.size(), (size_t)LineCoverageColumnWidth));
93   // Align to the right
94   OS.indent(LineCoverageColumnWidth - Str.size());
95   colored_ostream(OS, raw_ostream::MAGENTA,
96                   Line.hasMultipleRegions() && Options.Colors)
97       << Str;
98   OS << '|';
99 }
100
101 void SourceCoverageView::renderLineNumberColumn(raw_ostream &OS,
102                                                 unsigned LineNo) {
103   SmallString<32> Buffer;
104   raw_svector_ostream BufferOS(Buffer);
105   BufferOS << LineNo;
106   auto Str = BufferOS.str();
107   // Trim and align to the right
108   Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth));
109   OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
110 }
111
112 void SourceCoverageView::renderRegionMarkers(raw_ostream &OS,
113                                              ArrayRef<RegionMarker> Regions) {
114   SmallString<32> Buffer;
115   raw_svector_ostream BufferOS(Buffer);
116
117   unsigned PrevColumn = 1;
118   for (const auto &Region : Regions) {
119     // Skip to the new region
120     if (Region.Column > PrevColumn)
121       OS.indent(Region.Column - PrevColumn);
122     PrevColumn = Region.Column + 1;
123     BufferOS << Region.ExecutionCount;
124     StringRef Str = BufferOS.str();
125     // Trim the execution count
126     Str = Str.substr(0, std::min(Str.size(), (size_t)7));
127     PrevColumn += Str.size();
128     OS << '^' << Str;
129     Buffer.clear();
130   }
131   OS << "\n";
132
133   if (Options.Debug) {
134     for (const auto &Region : Regions) {
135       errs() << "Marker at " << Region.Line << ":" << Region.Column << " = "
136              << Region.ExecutionCount << "\n";
137     }
138   }
139 }
140
141 /// \brief Insert a new highlighting range into the line's highlighting ranges
142 /// Return line's new highlighting ranges in result.
143 static void insertExpansionHighlightRange(
144     ArrayRef<SourceCoverageView::HighlightRange> Ranges,
145     unsigned Line, unsigned StartCol, unsigned EndCol,
146     SmallVectorImpl<SourceCoverageView::HighlightRange> &Result) {
147   auto RangeToInsert = SourceCoverageView::HighlightRange(
148       Line, StartCol, EndCol, SourceCoverageView::HighlightRange::Expanded);
149   Result.clear();
150   size_t I = 0;
151   auto E = Ranges.size();
152   for (; I < E; ++I) {
153     if (RangeToInsert.ColumnStart < Ranges[I].ColumnEnd) {
154       const auto &Range = Ranges[I];
155       bool NextRangeContainsInserted = false;
156       // If the next range starts before the inserted range, move the end of the
157       // next range to the start of the inserted range.
158       if (Range.ColumnStart < RangeToInsert.ColumnStart) {
159         if (RangeToInsert.ColumnStart != Range.ColumnStart)
160           Result.push_back(SourceCoverageView::HighlightRange(
161               Range.Line, Range.ColumnStart, RangeToInsert.ColumnStart,
162               Range.Kind));
163         // If the next range also ends after the inserted range, keep this range
164         // and create a new range that starts at the inserted range and ends
165         // at the next range later.
166         if (Range.ColumnEnd > RangeToInsert.ColumnEnd)
167           NextRangeContainsInserted = true;
168       }
169       if (!NextRangeContainsInserted) {
170         ++I;
171         // Ignore ranges that are contained in inserted range
172         while (I < E && RangeToInsert.contains(Ranges[I]))
173           ++I;
174       }
175       break;
176     }
177     Result.push_back(Ranges[I]);
178   }
179   Result.push_back(RangeToInsert);
180   // If the next range starts before the inserted range end, move the start
181   // of the next range to the end of the inserted range.
182   if (I < E && Ranges[I].ColumnStart < RangeToInsert.ColumnEnd) {
183     const auto &Range = Ranges[I];
184     if (RangeToInsert.ColumnEnd != Range.ColumnEnd)
185       Result.push_back(SourceCoverageView::HighlightRange(
186           Range.Line, RangeToInsert.ColumnEnd, Range.ColumnEnd, Range.Kind));
187     ++I;
188   }
189   // Add the remaining ranges that are located after the inserted range
190   for (; I < E; ++I)
191     Result.push_back(Ranges[I]);
192 }
193
194 template <typename T>
195 ArrayRef<T> gatherLineItems(size_t &CurrentIdx, const std::vector<T> &Items,
196                             unsigned LineNo) {
197   auto PrevIdx = CurrentIdx;
198   auto E = Items.size();
199   while (CurrentIdx < E && Items[CurrentIdx].Line == LineNo)
200     ++CurrentIdx;
201   return ArrayRef<T>(Items.data() + PrevIdx, CurrentIdx - PrevIdx);
202 }
203
204 void SourceCoverageView::render(raw_ostream &OS, unsigned IndentLevel) {
205   SmallVector<HighlightRange, 8> AdjustedLineHighlightRanges;
206   size_t CurrentHighlightRange = 0;
207   size_t CurrentRegionMarker = 0;
208
209   line_iterator Lines(File);
210   // Advance the line iterator to the first line.
211   while (Lines.line_number() < LineOffset)
212     ++Lines;
213
214   // The width of the leading columns
215   unsigned CombinedColumnWidth =
216       (Options.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
217       (Options.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
218   // The width of the line that is used to divide between the view and the
219   // subviews.
220   unsigned DividerWidth = CombinedColumnWidth + 4;
221
222   // We need the expansions and instantiations sorted so we can go through them
223   // while we iterate lines.
224   std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
225   std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
226   auto NextESV = ExpansionSubViews.begin();
227   auto EndESV = ExpansionSubViews.end();
228   auto NextISV = InstantiationSubViews.begin();
229   auto EndISV = InstantiationSubViews.end();
230
231   for (size_t I = 0, E = LineStats.size(); I < E; ++I) {
232     unsigned LineNo = I + LineOffset;
233
234     renderIndent(OS, IndentLevel);
235     if (Options.ShowLineStats)
236       renderLineCoverageColumn(OS, LineStats[I]);
237     if (Options.ShowLineNumbers)
238       renderLineNumberColumn(OS, LineNo);
239
240     // Gather highlighting ranges.
241     auto LineHighlightRanges =
242         gatherLineItems(CurrentHighlightRange, HighlightRanges, LineNo);
243     auto LineRanges = LineHighlightRanges;
244     // Highlight the expansion range if there is an expansion subview on this
245     // line.
246     if (NextESV != EndESV && NextESV->getLine() == LineNo && Options.Colors) {
247       insertExpansionHighlightRange(
248           LineHighlightRanges, NextESV->getLine(), NextESV->getStartCol(),
249           NextESV->getEndCol(), AdjustedLineHighlightRanges);
250       LineRanges = AdjustedLineHighlightRanges;
251     }
252
253     // Display the source code for the current line.
254     StringRef Line = *Lines;
255     // Check if the line is empty, as line_iterator skips blank lines.
256     if (LineNo < Lines.line_number())
257       Line = "";
258     else if (!Lines.is_at_eof())
259       ++Lines;
260     renderLine(OS, Line, LineRanges);
261
262     // Show the region markers.
263     bool ShowMarkers = !Options.ShowLineStatsOrRegionMarkers ||
264                        LineStats[I].hasMultipleRegions();
265     auto LineMarkers = gatherLineItems(CurrentRegionMarker, Markers, LineNo);
266     if (ShowMarkers && !LineMarkers.empty()) {
267       renderIndent(OS, IndentLevel);
268       OS.indent(CombinedColumnWidth);
269       renderRegionMarkers(OS, LineMarkers);
270     }
271
272     // Show the expansions and instantiations for this line.
273     unsigned NestedIndent = IndentLevel + 1;
274     bool RenderedSubView = false;
275     for (; NextESV != EndESV && NextESV->getLine() == LineNo; ++NextESV) {
276       renderViewDivider(NestedIndent, DividerWidth, OS);
277       OS << "\n";
278       if (RenderedSubView) {
279         // Re-render the current line and highlight the expansion range for
280         // this subview.
281         insertExpansionHighlightRange(
282             LineHighlightRanges, NextESV->getLine(), NextESV->getStartCol(),
283             NextESV->getEndCol(), AdjustedLineHighlightRanges);
284         renderIndent(OS, IndentLevel);
285         OS.indent(CombinedColumnWidth + (IndentLevel == 0 ? 0 : 1));
286         renderLine(OS, Line, AdjustedLineHighlightRanges);
287         renderViewDivider(NestedIndent, DividerWidth, OS);
288         OS << "\n";
289       }
290       // Render the child subview
291       NextESV->View->render(OS, NestedIndent);
292       RenderedSubView = true;
293     }
294     for (; NextISV != EndISV && NextISV->Line == LineNo; ++NextISV) {
295       renderViewDivider(NestedIndent, DividerWidth, OS);
296       OS << "\n";
297       renderIndent(OS, NestedIndent);
298       OS << ' ';
299       Options.colored_ostream(OS, raw_ostream::CYAN) << NextISV->FunctionName
300                                                      << ":";
301       OS << "\n";
302       NextISV->View->render(OS, NestedIndent);
303       RenderedSubView = true;
304     }
305     if (RenderedSubView) {
306       renderViewDivider(NestedIndent, DividerWidth, OS);
307       OS << "\n";
308     }
309   }
310 }
311
312 void SourceCoverageView::setUpVisibleRange(SourceCoverageDataManager &Data) {
313   auto CountedRegions = Data.getSourceRegions();
314   if (!CountedRegions.size())
315     return;
316
317   unsigned Start = CountedRegions.front().LineStart, End = 0;
318   for (const auto &CR : CountedRegions) {
319     Start = std::min(Start, CR.LineStart);
320     End = std::max(End, CR.LineEnd);
321   }
322   LineOffset = Start;
323   LineStats.resize(End - Start + 1);
324 }
325
326 void
327 SourceCoverageView::createLineCoverageInfo(SourceCoverageDataManager &Data) {
328   auto CountedRegions = Data.getSourceRegions();
329   for (const auto &CR : CountedRegions) {
330     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)
334         LineStats[Line - LineOffset] = LineCoverageInfo();
335       continue;
336     }
337     LineStats[CR.LineStart - LineOffset].addRegionStartCount(CR.ExecutionCount);
338     for (unsigned Line = CR.LineStart + 1; Line <= CR.LineEnd; ++Line)
339       LineStats[Line - LineOffset].addRegionCount(CR.ExecutionCount);
340   }
341 }
342
343 void
344 SourceCoverageView::createHighlightRanges(SourceCoverageDataManager &Data) {
345   auto CountedRegions = Data.getSourceRegions();
346   std::vector<bool> AlreadyHighlighted;
347   AlreadyHighlighted.resize(CountedRegions.size(), false);
348
349   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)
353       continue;
354     if (AlreadyHighlighted[I])
355       continue;
356     for (size_t J = 0; J < S; ++J) {
357       if (CR.contains(CountedRegions[J])) {
358         AlreadyHighlighted[J] = true;
359       }
360     }
361     if (CR.LineStart == CR.LineEnd) {
362       HighlightRanges.push_back(HighlightRange(
363           CR.LineStart, CR.ColumnStart, CR.ColumnEnd));
364       continue;
365     }
366     HighlightRanges.push_back(
367         HighlightRange(CR.LineStart, CR.ColumnStart,
368                        std::numeric_limits<unsigned>::max()));
369     HighlightRanges.push_back(
370         HighlightRange(CR.LineEnd, 1, CR.ColumnEnd));
371     for (unsigned Line = CR.LineStart + 1; Line < CR.LineEnd;
372          ++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
381 void SourceCoverageView::createRegionMarkers(SourceCoverageDataManager &Data) {
382   for (const auto &CR : Data.getSourceRegions())
383     if (CR.Kind != coverage::CounterMappingRegion::SkippedRegion)
384       Markers.push_back(
385           RegionMarker(CR.LineStart, CR.ColumnStart, CR.ExecutionCount));
386 }
387
388 void SourceCoverageView::load(SourceCoverageDataManager &Data) {
389   setUpVisibleRange(Data);
390   if (Options.ShowLineStats)
391     createLineCoverageInfo(Data);
392   if (Options.Colors)
393     createHighlightRanges(Data);
394   if (Options.ShowRegionMarkers)
395     createRegionMarkers(Data);
396 }