blob: 3be54f828da797b8f3a91166a36e5f5e5763e069 [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"
Justin Bognerfe357c02014-09-17 18:23:47 +000015#include "llvm/ADT/Optional.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/Support/LineIterator.h"
18
19using namespace llvm;
20
21void SourceCoverageView::renderLine(raw_ostream &OS, StringRef Line,
Justin Bognerfe357c02014-09-17 18:23:47 +000022 int64_t LineNumber,
23 const CoverageSegment *WrappedSegment,
24 ArrayRef<const CoverageSegment *> Segments,
25 unsigned ExpansionCol) {
26 Optional<raw_ostream::Colors> Highlight;
27 SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
Alex Lorenze82d89c2014-08-22 22:56:03 +000028
Justin Bognerfe357c02014-09-17 18:23:47 +000029 // The first segment overlaps from a previous line, so we treat it specially.
30 if (WrappedSegment && WrappedSegment->HasCount && WrappedSegment->Count == 0)
31 Highlight = raw_ostream::RED;
Alex Lorenze82d89c2014-08-22 22:56:03 +000032
Justin Bognerfe357c02014-09-17 18:23:47 +000033 // Output each segment of the line, possibly highlighted.
34 unsigned Col = 1;
35 for (const auto *S : Segments) {
36 unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1);
37 colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
38 Options.Colors && Highlight, /*Bold=*/false, /*BG=*/true)
39 << Line.substr(Col - 1, End - Col);
40 if (Options.Debug && Highlight)
41 HighlightedRanges.push_back(std::make_pair(Col, End));
42 Col = End;
43 if (Col == ExpansionCol)
44 Highlight = raw_ostream::CYAN;
45 else if (S->HasCount && S->Count == 0)
46 Highlight = raw_ostream::RED;
47 else
48 Highlight = None;
Alex Lorenze82d89c2014-08-22 22:56:03 +000049 }
50
51 // Show the rest of the line
Justin Bognerfe357c02014-09-17 18:23:47 +000052 colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
53 Options.Colors && Highlight, /*Bold=*/false, /*BG=*/true)
54 << Line.substr(Col - 1, Line.size() - Col + 1);
Alex Lorenze82d89c2014-08-22 22:56:03 +000055 OS << "\n";
Justin Bogner92bb3022014-09-15 22:23:29 +000056
57 if (Options.Debug) {
Justin Bognerfe357c02014-09-17 18:23:47 +000058 for (const auto &Range : HighlightedRanges)
59 errs() << "Highlighted line " << LineNumber << ", " << Range.first
60 << " -> " << Range.second << "\n";
61 if (Highlight)
62 errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n";
Justin Bogner92bb3022014-09-15 22:23:29 +000063 }
Alex Lorenze82d89c2014-08-22 22:56:03 +000064}
65
Justin Bogner76e251c2014-09-16 06:21:57 +000066void SourceCoverageView::renderIndent(raw_ostream &OS, unsigned Level) {
67 for (unsigned I = 0; I < Level; ++I)
Alex Lorenze82d89c2014-08-22 22:56:03 +000068 OS << " |";
69}
70
Justin Bogner76e251c2014-09-16 06:21:57 +000071void SourceCoverageView::renderViewDivider(unsigned Level, unsigned Length,
Alex Lorenze82d89c2014-08-22 22:56:03 +000072 raw_ostream &OS) {
Justin Bogner76e251c2014-09-16 06:21:57 +000073 assert(Level != 0 && "Cannot render divider at top level");
74 renderIndent(OS, Level - 1);
75 OS.indent(2);
Alex Lorenze82d89c2014-08-22 22:56:03 +000076 for (unsigned I = 0; I < Length; ++I)
77 OS << "-";
78}
79
80void
81SourceCoverageView::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
101void 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
Justin Bognerfe357c02014-09-17 18:23:47 +0000112void SourceCoverageView::renderRegionMarkers(
113 raw_ostream &OS, ArrayRef<const CoverageSegment *> Segments) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000114 SmallString<32> Buffer;
115 raw_svector_ostream BufferOS(Buffer);
116
117 unsigned PrevColumn = 1;
Justin Bognerfe357c02014-09-17 18:23:47 +0000118 for (const auto *S : Segments) {
119 if (!S->IsRegionEntry)
120 continue;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000121 // Skip to the new region
Justin Bognerfe357c02014-09-17 18:23:47 +0000122 if (S->Col > PrevColumn)
123 OS.indent(S->Col - PrevColumn);
124 PrevColumn = S->Col + 1;
125 BufferOS << S->Count;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000126 StringRef Str = BufferOS.str();
127 // Trim the execution count
128 Str = Str.substr(0, std::min(Str.size(), (size_t)7));
129 PrevColumn += Str.size();
130 OS << '^' << Str;
131 Buffer.clear();
132 }
133 OS << "\n";
Justin Bogner0b3614f2014-09-15 22:12:28 +0000134
Justin Bognerfe357c02014-09-17 18:23:47 +0000135 if (Options.Debug)
136 for (const auto *S : Segments)
137 errs() << "Marker at " << S->Line << ":" << S->Col << " = " << S->Count
138 << (S->IsRegionEntry ? "\n" : " (pop)\n");
Alex Lorenze82d89c2014-08-22 22:56:03 +0000139}
140
Justin Bognerfe357c02014-09-17 18:23:47 +0000141void SourceCoverageView::render(raw_ostream &OS, bool WholeFile,
142 unsigned IndentLevel) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000143 // The width of the leading columns
144 unsigned CombinedColumnWidth =
145 (Options.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
146 (Options.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
147 // The width of the line that is used to divide between the view and the
148 // subviews.
149 unsigned DividerWidth = CombinedColumnWidth + 4;
150
Justin Bogner5e1400a2014-09-17 05:33:20 +0000151 // We need the expansions and instantiations sorted so we can go through them
152 // while we iterate lines.
153 std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
154 std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
155 auto NextESV = ExpansionSubViews.begin();
156 auto EndESV = ExpansionSubViews.end();
157 auto NextISV = InstantiationSubViews.begin();
158 auto EndISV = InstantiationSubViews.end();
159
Justin Bognerfe357c02014-09-17 18:23:47 +0000160 // Get the coverage information for the file.
161 auto CoverageSegments = RegionManager->getCoverageSegments();
162 assert(CoverageSegments.size() && "View with no coverage?");
163 auto NextSegment = CoverageSegments.begin();
164 auto EndSegment = CoverageSegments.end();
Alex Lorenze82d89c2014-08-22 22:56:03 +0000165
Justin Bogner13ba23b2014-09-19 08:13:16 +0000166 unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0;
Justin Bognerfe357c02014-09-17 18:23:47 +0000167 const CoverageSegment *WrappedSegment = nullptr;
168 SmallVector<const CoverageSegment *, 8> LineSegments;
169 for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) {
170 // If we aren't rendering the whole file, we need to filter out the prologue
171 // and epilogue.
172 if (!WholeFile) {
173 if (NextSegment == EndSegment)
174 break;
Justin Bogner13ba23b2014-09-19 08:13:16 +0000175 else if (LI.line_number() < FirstLine)
Justin Bognerfe357c02014-09-17 18:23:47 +0000176 continue;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000177 }
178
Justin Bognerfe357c02014-09-17 18:23:47 +0000179 // Collect the coverage information relevant to this line.
180 if (LineSegments.size())
181 WrappedSegment = LineSegments.back();
182 LineSegments.clear();
183 while (NextSegment != EndSegment && NextSegment->Line == LI.line_number())
184 LineSegments.push_back(&*NextSegment++);
185
186 // Calculate a count to be for the line as a whole.
187 LineCoverageInfo LineCount;
188 if (WrappedSegment && WrappedSegment->HasCount)
189 LineCount.addRegionCount(WrappedSegment->Count);
190 for (const auto *S : LineSegments)
191 if (S->HasCount && S->IsRegionEntry)
192 LineCount.addRegionStartCount(S->Count);
193
194 // Render the line prefix.
195 renderIndent(OS, IndentLevel);
196 if (Options.ShowLineStats)
197 renderLineCoverageColumn(OS, LineCount);
198 if (Options.ShowLineNumbers)
199 renderLineNumberColumn(OS, LI.line_number());
200
201 // If there are expansion subviews, we want to highlight the first one.
202 unsigned ExpansionColumn = 0;
203 if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
204 Options.Colors)
205 ExpansionColumn = NextESV->getStartCol();
206
Alex Lorenze82d89c2014-08-22 22:56:03 +0000207 // Display the source code for the current line.
Justin Bognerfe357c02014-09-17 18:23:47 +0000208 renderLine(OS, *LI, LI.line_number(), WrappedSegment, LineSegments,
209 ExpansionColumn);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000210
211 // Show the region markers.
Justin Bognerfe357c02014-09-17 18:23:47 +0000212 if (Options.ShowRegionMarkers && (!Options.ShowLineStatsOrRegionMarkers ||
213 LineCount.hasMultipleRegions()) &&
214 !LineSegments.empty()) {
Justin Bogner76e251c2014-09-16 06:21:57 +0000215 renderIndent(OS, IndentLevel);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000216 OS.indent(CombinedColumnWidth);
Justin Bognerfe357c02014-09-17 18:23:47 +0000217 renderRegionMarkers(OS, LineSegments);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000218 }
219
Justin Bogner5e1400a2014-09-17 05:33:20 +0000220 // Show the expansions and instantiations for this line.
Justin Bogner76e251c2014-09-16 06:21:57 +0000221 unsigned NestedIndent = IndentLevel + 1;
Justin Bogner5e1400a2014-09-17 05:33:20 +0000222 bool RenderedSubView = false;
Justin Bognerfe357c02014-09-17 18:23:47 +0000223 for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
224 ++NextESV) {
Justin Bogner5e1400a2014-09-17 05:33:20 +0000225 renderViewDivider(NestedIndent, DividerWidth, OS);
226 OS << "\n";
227 if (RenderedSubView) {
228 // Re-render the current line and highlight the expansion range for
229 // this subview.
Justin Bognerfe357c02014-09-17 18:23:47 +0000230 ExpansionColumn = NextESV->getStartCol();
Justin Bogner5e1400a2014-09-17 05:33:20 +0000231 renderIndent(OS, IndentLevel);
232 OS.indent(CombinedColumnWidth + (IndentLevel == 0 ? 0 : 1));
Justin Bognerfe357c02014-09-17 18:23:47 +0000233 renderLine(OS, *LI, LI.line_number(), WrappedSegment, LineSegments,
234 ExpansionColumn);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000235 renderViewDivider(NestedIndent, DividerWidth, OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000236 OS << "\n";
Alex Lorenze82d89c2014-08-22 22:56:03 +0000237 }
238 // Render the child subview
Justin Bogner5cbed6e2014-09-17 21:48:52 +0000239 if (Options.Debug)
240 errs() << "Expansion at line " << NextESV->getLine() << ", "
241 << NextESV->getStartCol() << " -> " << NextESV->getEndCol()
242 << "\n";
Justin Bognerfe357c02014-09-17 18:23:47 +0000243 NextESV->View->render(OS, false, NestedIndent);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000244 RenderedSubView = true;
245 }
Justin Bognerfe357c02014-09-17 18:23:47 +0000246 for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
Justin Bogner5e1400a2014-09-17 05:33:20 +0000247 renderViewDivider(NestedIndent, DividerWidth, OS);
248 OS << "\n";
249 renderIndent(OS, NestedIndent);
250 OS << ' ';
251 Options.colored_ostream(OS, raw_ostream::CYAN) << NextISV->FunctionName
252 << ":";
253 OS << "\n";
Justin Bognerfe357c02014-09-17 18:23:47 +0000254 NextISV->View->render(OS, false, NestedIndent);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000255 RenderedSubView = true;
256 }
257 if (RenderedSubView) {
Justin Bogner76e251c2014-09-16 06:21:57 +0000258 renderViewDivider(NestedIndent, DividerWidth, OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000259 OS << "\n";
260 }
261 }
262}