blob: 83d228e367039e15e65bb785855dca24cb40af49 [file] [log] [blame]
Vedant Kumarf9151b92016-06-25 02:58:30 +00001//===- SourceCoverageViewText.cpp - A text-based code coverage view -------===//
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//===----------------------------------------------------------------------===//
Vedant Kumaree5a5e92016-06-25 05:48:54 +00009///
10/// \file This file implements the text-based coverage renderer.
11///
Vedant Kumarf9151b92016-06-25 02:58:30 +000012//===----------------------------------------------------------------------===//
13
Vedant Kumara59334d2016-09-09 01:32:55 +000014#include "CoverageReport.h"
Vedant Kumarf9151b92016-06-25 02:58:30 +000015#include "SourceCoverageViewText.h"
Vedant Kumar861a19c2016-06-26 02:45:13 +000016#include "llvm/ADT/Optional.h"
Vedant Kumarf9151b92016-06-25 02:58:30 +000017#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/StringExtras.h"
19
20using namespace llvm;
21
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000022Expected<CoveragePrinter::OwnedStream>
23CoveragePrinterText::createViewFile(StringRef Path, bool InToplevel) {
24 return createOutputStream(Path, "txt", InToplevel);
25}
26
27void CoveragePrinterText::closeViewFile(OwnedStream OS) {
28 OS->operator<<('\n');
29}
30
Vedant Kumara59334d2016-09-09 01:32:55 +000031Error CoveragePrinterText::createIndexFile(
Vedant Kumarbc647982016-09-23 18:57:32 +000032 ArrayRef<std::string> SourceFiles,
Sean Evesond932b2d2017-10-03 11:05:28 +000033 const coverage::CoverageMapping &Coverage,
34 const CoverageFiltersMatchAll &Filters) {
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000035 auto OSOrErr = createOutputStream("index", "txt", /*InToplevel=*/true);
36 if (Error E = OSOrErr.takeError())
37 return E;
38 auto OS = std::move(OSOrErr.get());
39 raw_ostream &OSRef = *OS.get();
40
Vedant Kumara59334d2016-09-09 01:32:55 +000041 CoverageReport Report(Opts, Coverage);
Sean Evesonfa8ef352017-09-28 10:07:30 +000042 Report.renderFileReports(OSRef, SourceFiles, Filters);
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000043
Ying Yi544b1df2016-09-13 11:28:31 +000044 Opts.colored_ostream(OSRef, raw_ostream::CYAN) << "\n"
45 << Opts.getLLVMVersionString();
46
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000047 return Error::success();
48}
49
Vedant Kumarf9151b92016-06-25 02:58:30 +000050namespace {
51
Vedant Kumar4f4e8d82016-06-25 03:27:29 +000052static const unsigned LineCoverageColumnWidth = 7;
53static const unsigned LineNumberColumnWidth = 5;
Vedant Kumarf9151b92016-06-25 02:58:30 +000054
55/// \brief Get the width of the leading columns.
56unsigned getCombinedColumnWidth(const CoverageViewOptions &Opts) {
57 return (Opts.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
58 (Opts.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
59}
60
61/// \brief The width of the line that is used to divide between the view and
62/// the subviews.
63unsigned getDividerWidth(const CoverageViewOptions &Opts) {
64 return getCombinedColumnWidth(Opts) + 4;
65}
66
67} // anonymous namespace
68
Vedant Kumaraf0dd932016-07-06 22:02:55 +000069void SourceCoverageViewText::renderViewHeader(raw_ostream &) {}
Vedant Kumar8d74cb22016-06-29 00:38:21 +000070
Vedant Kumar84a280a2016-09-13 23:00:13 +000071void SourceCoverageViewText::renderViewFooter(raw_ostream &) {}
Vedant Kumar8d74cb22016-06-29 00:38:21 +000072
Vedant Kumarb1c174a2016-09-10 19:37:26 +000073void SourceCoverageViewText::renderSourceName(raw_ostream &OS, bool WholeFile) {
Vedant Kumar5c61c702016-10-25 00:08:33 +000074 getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName()
75 << ":\n";
Vedant Kumarf9151b92016-06-25 02:58:30 +000076}
77
78void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS,
79 unsigned ViewDepth) {
80 for (unsigned I = 0; I < ViewDepth; ++I)
81 OS << " |";
82}
83
Vedant Kumaraf0dd932016-07-06 22:02:55 +000084void SourceCoverageViewText::renderLineSuffix(raw_ostream &, unsigned) {}
Vedant Kumar8d74cb22016-06-29 00:38:21 +000085
Vedant Kumarf9151b92016-06-25 02:58:30 +000086void SourceCoverageViewText::renderViewDivider(raw_ostream &OS,
87 unsigned ViewDepth) {
88 assert(ViewDepth != 0 && "Cannot render divider at top level");
89 renderLinePrefix(OS, ViewDepth - 1);
90 OS.indent(2);
91 unsigned Length = getDividerWidth(getOptions());
92 for (unsigned I = 0; I < Length; ++I)
93 OS << '-';
94 OS << '\n';
95}
96
Vedant Kumar08a0a312017-10-18 18:52:28 +000097void SourceCoverageViewText::renderLine(raw_ostream &OS, LineRef L,
98 const LineCoverageStats &LCS,
99 unsigned ExpansionCol,
100 unsigned ViewDepth) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000101 StringRef Line = L.Line;
102 unsigned LineNumber = L.LineNo;
Vedant Kumar08a0a312017-10-18 18:52:28 +0000103 auto *WrappedSegment = LCS.getWrappedSegment();
104 CoverageSegmentArray Segments = LCS.getLineSegments();
Vedant Kumarf9151b92016-06-25 02:58:30 +0000105
106 Optional<raw_ostream::Colors> Highlight;
107 SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
108
109 // The first segment overlaps from a previous line, so we treat it specially.
110 if (WrappedSegment && WrappedSegment->HasCount && WrappedSegment->Count == 0)
111 Highlight = raw_ostream::RED;
112
113 // Output each segment of the line, possibly highlighted.
114 unsigned Col = 1;
115 for (const auto *S : Segments) {
116 unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1);
117 colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
118 getOptions().Colors && Highlight, /*Bold=*/false,
119 /*BG=*/true)
120 << Line.substr(Col - 1, End - Col);
121 if (getOptions().Debug && Highlight)
122 HighlightedRanges.push_back(std::make_pair(Col, End));
123 Col = End;
124 if (Col == ExpansionCol)
125 Highlight = raw_ostream::CYAN;
Vedant Kumar988faf82017-10-18 18:52:27 +0000126 else if ((!S->IsGapRegion || Highlight == raw_ostream::RED) &&
127 S->HasCount && S->Count == 0)
Vedant Kumarf9151b92016-06-25 02:58:30 +0000128 Highlight = raw_ostream::RED;
129 else
130 Highlight = None;
131 }
132
133 // Show the rest of the line.
134 colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
135 getOptions().Colors && Highlight, /*Bold=*/false, /*BG=*/true)
136 << Line.substr(Col - 1, Line.size() - Col + 1);
137 OS << '\n';
138
139 if (getOptions().Debug) {
140 for (const auto &Range : HighlightedRanges)
141 errs() << "Highlighted line " << LineNumber << ", " << Range.first
142 << " -> " << Range.second << '\n';
143 if (Highlight)
144 errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n";
145 }
146}
147
148void SourceCoverageViewText::renderLineCoverageColumn(
149 raw_ostream &OS, const LineCoverageStats &Line) {
150 if (!Line.isMapped()) {
151 OS.indent(LineCoverageColumnWidth) << '|';
152 return;
153 }
Vedant Kumar1963f512017-10-14 02:27:29 +0000154 std::string C = formatCount(Line.getExecutionCount());
Vedant Kumarf9151b92016-06-25 02:58:30 +0000155 OS.indent(LineCoverageColumnWidth - C.size());
156 colored_ostream(OS, raw_ostream::MAGENTA,
157 Line.hasMultipleRegions() && getOptions().Colors)
158 << C;
159 OS << '|';
160}
161
162void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS,
163 unsigned LineNo) {
164 SmallString<32> Buffer;
165 raw_svector_ostream BufferOS(Buffer);
166 BufferOS << LineNo;
167 auto Str = BufferOS.str();
168 // Trim and align to the right.
169 Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth));
170 OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
171}
172
Vedant Kumar08a0a312017-10-18 18:52:28 +0000173void SourceCoverageViewText::renderRegionMarkers(raw_ostream &OS,
174 const LineCoverageStats &Line,
175 unsigned ViewDepth) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000176 renderLinePrefix(OS, ViewDepth);
177 OS.indent(getCombinedColumnWidth(getOptions()));
178
Vedant Kumar08a0a312017-10-18 18:52:28 +0000179 CoverageSegmentArray Segments = Line.getLineSegments();
180
Vedant Kumar933b37f2017-09-08 18:44:46 +0000181 // Just consider the segments which start *and* end on this line.
182 if (Segments.size() > 1)
183 Segments = Segments.drop_back();
184
Vedant Kumarf9151b92016-06-25 02:58:30 +0000185 unsigned PrevColumn = 1;
186 for (const auto *S : Segments) {
187 if (!S->IsRegionEntry)
188 continue;
189 // Skip to the new region.
190 if (S->Col > PrevColumn)
191 OS.indent(S->Col - PrevColumn);
192 PrevColumn = S->Col + 1;
193 std::string C = formatCount(S->Count);
194 PrevColumn += C.size();
195 OS << '^' << C;
Vedant Kumar933b37f2017-09-08 18:44:46 +0000196
197 if (getOptions().Debug)
198 errs() << "Marker at " << S->Line << ":" << S->Col << " = "
199 << formatCount(S->Count) << "\n";
Vedant Kumarf9151b92016-06-25 02:58:30 +0000200 }
201 OS << '\n';
Vedant Kumarf9151b92016-06-25 02:58:30 +0000202}
203
Vedant Kumar08a0a312017-10-18 18:52:28 +0000204void SourceCoverageViewText::renderExpansionSite(raw_ostream &OS, LineRef L,
205 const LineCoverageStats &LCS,
206 unsigned ExpansionCol,
207 unsigned ViewDepth) {
Vedant Kumar861a19c2016-06-26 02:45:13 +0000208 renderLinePrefix(OS, ViewDepth);
209 OS.indent(getCombinedColumnWidth(getOptions()) + (ViewDepth == 0 ? 0 : 1));
Vedant Kumar08a0a312017-10-18 18:52:28 +0000210 renderLine(OS, L, LCS, ExpansionCol, ViewDepth);
Vedant Kumar861a19c2016-06-26 02:45:13 +0000211}
Vedant Kumarf9151b92016-06-25 02:58:30 +0000212
Vedant Kumar861a19c2016-06-26 02:45:13 +0000213void SourceCoverageViewText::renderExpansionView(raw_ostream &OS,
214 ExpansionView &ESV,
215 unsigned ViewDepth) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000216 // Render the child subview.
217 if (getOptions().Debug)
218 errs() << "Expansion at line " << ESV.getLine() << ", " << ESV.getStartCol()
219 << " -> " << ESV.getEndCol() << '\n';
220 ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
Sean Evesonfa8ef352017-09-28 10:07:30 +0000221 /*ShowTitle=*/false, ViewDepth + 1);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000222}
223
224void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS,
225 InstantiationView &ISV,
226 unsigned ViewDepth) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000227 renderLinePrefix(OS, ViewDepth);
228 OS << ' ';
Vedant Kumara8c396d2016-09-15 06:44:51 +0000229 if (!ISV.View)
230 getOptions().colored_ostream(OS, raw_ostream::RED)
231 << "Unexecuted instantiation: " << ISV.FunctionName << "\n";
232 else
233 ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true,
Sean Evesonfa8ef352017-09-28 10:07:30 +0000234 /*ShowTitle=*/false, ViewDepth);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000235}
Ying Yi84dc9712016-08-24 14:27:23 +0000236
Vedant Kumarb2edd112016-09-15 04:45:59 +0000237void SourceCoverageViewText::renderTitle(raw_ostream &OS, StringRef Title) {
Ying Yi84dc9712016-08-24 14:27:23 +0000238 if (getOptions().hasProjectTitle())
239 getOptions().colored_ostream(OS, raw_ostream::CYAN)
240 << getOptions().ProjectTitle << "\n";
241
Vedant Kumarb2edd112016-09-15 04:45:59 +0000242 getOptions().colored_ostream(OS, raw_ostream::CYAN) << Title << "\n";
Ying Yi84dc9712016-08-24 14:27:23 +0000243
244 if (getOptions().hasCreatedTime())
245 getOptions().colored_ostream(OS, raw_ostream::CYAN)
246 << getOptions().CreatedTimeStr << "\n";
247}
248
Vedant Kumarb1c174a2016-09-10 19:37:26 +0000249void SourceCoverageViewText::renderTableHeader(raw_ostream &, unsigned,
250 unsigned) {}