blob: a78c0575cdc0315dada669780db4e7dbe1439ef7 [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 Eveson1439fa62017-09-27 16:20:07 +000033 const coverage::CoverageMapping &Coverage) {
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000034 auto OSOrErr = createOutputStream("index", "txt", /*InToplevel=*/true);
35 if (Error E = OSOrErr.takeError())
36 return E;
37 auto OS = std::move(OSOrErr.get());
38 raw_ostream &OSRef = *OS.get();
39
Vedant Kumara59334d2016-09-09 01:32:55 +000040 CoverageReport Report(Opts, Coverage);
Sean Eveson1439fa62017-09-27 16:20:07 +000041 Report.renderFileReports(OSRef, SourceFiles);
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000042
Ying Yi544b1df2016-09-13 11:28:31 +000043 Opts.colored_ostream(OSRef, raw_ostream::CYAN) << "\n"
44 << Opts.getLLVMVersionString();
45
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000046 return Error::success();
47}
48
Vedant Kumarf9151b92016-06-25 02:58:30 +000049namespace {
50
Vedant Kumar4f4e8d82016-06-25 03:27:29 +000051static const unsigned LineCoverageColumnWidth = 7;
52static const unsigned LineNumberColumnWidth = 5;
Vedant Kumarf9151b92016-06-25 02:58:30 +000053
54/// \brief Get the width of the leading columns.
55unsigned getCombinedColumnWidth(const CoverageViewOptions &Opts) {
56 return (Opts.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
57 (Opts.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
58}
59
60/// \brief The width of the line that is used to divide between the view and
61/// the subviews.
62unsigned getDividerWidth(const CoverageViewOptions &Opts) {
63 return getCombinedColumnWidth(Opts) + 4;
64}
65
66} // anonymous namespace
67
Vedant Kumaraf0dd932016-07-06 22:02:55 +000068void SourceCoverageViewText::renderViewHeader(raw_ostream &) {}
Vedant Kumar8d74cb22016-06-29 00:38:21 +000069
Vedant Kumar84a280a2016-09-13 23:00:13 +000070void SourceCoverageViewText::renderViewFooter(raw_ostream &) {}
Vedant Kumar8d74cb22016-06-29 00:38:21 +000071
Vedant Kumarb1c174a2016-09-10 19:37:26 +000072void SourceCoverageViewText::renderSourceName(raw_ostream &OS, bool WholeFile) {
Vedant Kumar5c61c702016-10-25 00:08:33 +000073 getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName()
74 << ":\n";
Vedant Kumarf9151b92016-06-25 02:58:30 +000075}
76
77void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS,
78 unsigned ViewDepth) {
79 for (unsigned I = 0; I < ViewDepth; ++I)
80 OS << " |";
81}
82
Vedant Kumaraf0dd932016-07-06 22:02:55 +000083void SourceCoverageViewText::renderLineSuffix(raw_ostream &, unsigned) {}
Vedant Kumar8d74cb22016-06-29 00:38:21 +000084
Vedant Kumarf9151b92016-06-25 02:58:30 +000085void SourceCoverageViewText::renderViewDivider(raw_ostream &OS,
86 unsigned ViewDepth) {
87 assert(ViewDepth != 0 && "Cannot render divider at top level");
88 renderLinePrefix(OS, ViewDepth - 1);
89 OS.indent(2);
90 unsigned Length = getDividerWidth(getOptions());
91 for (unsigned I = 0; I < Length; ++I)
92 OS << '-';
93 OS << '\n';
94}
95
96void SourceCoverageViewText::renderLine(
97 raw_ostream &OS, LineRef L,
98 const coverage::CoverageSegment *WrappedSegment,
99 CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) {
100 StringRef Line = L.Line;
101 unsigned LineNumber = L.LineNo;
102
103 Optional<raw_ostream::Colors> Highlight;
104 SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
105
106 // The first segment overlaps from a previous line, so we treat it specially.
107 if (WrappedSegment && WrappedSegment->HasCount && WrappedSegment->Count == 0)
108 Highlight = raw_ostream::RED;
109
110 // Output each segment of the line, possibly highlighted.
111 unsigned Col = 1;
112 for (const auto *S : Segments) {
113 unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1);
114 colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
115 getOptions().Colors && Highlight, /*Bold=*/false,
116 /*BG=*/true)
117 << Line.substr(Col - 1, End - Col);
118 if (getOptions().Debug && Highlight)
119 HighlightedRanges.push_back(std::make_pair(Col, End));
120 Col = End;
121 if (Col == ExpansionCol)
122 Highlight = raw_ostream::CYAN;
Vedant Kumarad8f6372017-09-18 23:37:28 +0000123 else if (!S->IsGapRegion && S->HasCount && S->Count == 0)
Vedant Kumarf9151b92016-06-25 02:58:30 +0000124 Highlight = raw_ostream::RED;
125 else
126 Highlight = None;
127 }
128
129 // Show the rest of the line.
130 colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
131 getOptions().Colors && Highlight, /*Bold=*/false, /*BG=*/true)
132 << Line.substr(Col - 1, Line.size() - Col + 1);
133 OS << '\n';
134
135 if (getOptions().Debug) {
136 for (const auto &Range : HighlightedRanges)
137 errs() << "Highlighted line " << LineNumber << ", " << Range.first
138 << " -> " << Range.second << '\n';
139 if (Highlight)
140 errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n";
141 }
142}
143
144void SourceCoverageViewText::renderLineCoverageColumn(
145 raw_ostream &OS, const LineCoverageStats &Line) {
146 if (!Line.isMapped()) {
147 OS.indent(LineCoverageColumnWidth) << '|';
148 return;
149 }
150 std::string C = formatCount(Line.ExecutionCount);
151 OS.indent(LineCoverageColumnWidth - C.size());
152 colored_ostream(OS, raw_ostream::MAGENTA,
153 Line.hasMultipleRegions() && getOptions().Colors)
154 << C;
155 OS << '|';
156}
157
158void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS,
159 unsigned LineNo) {
160 SmallString<32> Buffer;
161 raw_svector_ostream BufferOS(Buffer);
162 BufferOS << LineNo;
163 auto Str = BufferOS.str();
164 // Trim and align to the right.
165 Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth));
166 OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
167}
168
169void SourceCoverageViewText::renderRegionMarkers(
170 raw_ostream &OS, CoverageSegmentArray Segments, unsigned ViewDepth) {
171 renderLinePrefix(OS, ViewDepth);
172 OS.indent(getCombinedColumnWidth(getOptions()));
173
Vedant Kumar933b37f2017-09-08 18:44:46 +0000174 // Just consider the segments which start *and* end on this line.
175 if (Segments.size() > 1)
176 Segments = Segments.drop_back();
177
Vedant Kumarf9151b92016-06-25 02:58:30 +0000178 unsigned PrevColumn = 1;
179 for (const auto *S : Segments) {
180 if (!S->IsRegionEntry)
181 continue;
182 // Skip to the new region.
183 if (S->Col > PrevColumn)
184 OS.indent(S->Col - PrevColumn);
185 PrevColumn = S->Col + 1;
186 std::string C = formatCount(S->Count);
187 PrevColumn += C.size();
188 OS << '^' << C;
Vedant Kumar933b37f2017-09-08 18:44:46 +0000189
190 if (getOptions().Debug)
191 errs() << "Marker at " << S->Line << ":" << S->Col << " = "
192 << formatCount(S->Count) << "\n";
Vedant Kumarf9151b92016-06-25 02:58:30 +0000193 }
194 OS << '\n';
Vedant Kumarf9151b92016-06-25 02:58:30 +0000195}
196
Vedant Kumar861a19c2016-06-26 02:45:13 +0000197void SourceCoverageViewText::renderExpansionSite(
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000198 raw_ostream &OS, LineRef L, const coverage::CoverageSegment *WrappedSegment,
Vedant Kumar8b12ecb2016-06-25 05:48:59 +0000199 CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) {
Vedant Kumar861a19c2016-06-26 02:45:13 +0000200 renderLinePrefix(OS, ViewDepth);
201 OS.indent(getCombinedColumnWidth(getOptions()) + (ViewDepth == 0 ? 0 : 1));
202 renderLine(OS, L, WrappedSegment, Segments, ExpansionCol, ViewDepth);
203}
Vedant Kumarf9151b92016-06-25 02:58:30 +0000204
Vedant Kumar861a19c2016-06-26 02:45:13 +0000205void SourceCoverageViewText::renderExpansionView(raw_ostream &OS,
206 ExpansionView &ESV,
207 unsigned ViewDepth) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000208 // Render the child subview.
209 if (getOptions().Debug)
210 errs() << "Expansion at line " << ESV.getLine() << ", " << ESV.getStartCol()
211 << " -> " << ESV.getEndCol() << '\n';
212 ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
Sean Eveson1439fa62017-09-27 16:20:07 +0000213 ViewDepth + 1);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000214}
215
216void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS,
217 InstantiationView &ISV,
218 unsigned ViewDepth) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000219 renderLinePrefix(OS, ViewDepth);
220 OS << ' ';
Vedant Kumara8c396d2016-09-15 06:44:51 +0000221 if (!ISV.View)
222 getOptions().colored_ostream(OS, raw_ostream::RED)
223 << "Unexecuted instantiation: " << ISV.FunctionName << "\n";
224 else
225 ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true,
Sean Eveson1439fa62017-09-27 16:20:07 +0000226 ViewDepth);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000227}
Ying Yi84dc9712016-08-24 14:27:23 +0000228
Vedant Kumarb2edd112016-09-15 04:45:59 +0000229void SourceCoverageViewText::renderTitle(raw_ostream &OS, StringRef Title) {
Ying Yi84dc9712016-08-24 14:27:23 +0000230 if (getOptions().hasProjectTitle())
231 getOptions().colored_ostream(OS, raw_ostream::CYAN)
232 << getOptions().ProjectTitle << "\n";
233
Vedant Kumarb2edd112016-09-15 04:45:59 +0000234 getOptions().colored_ostream(OS, raw_ostream::CYAN) << Title << "\n";
Ying Yi84dc9712016-08-24 14:27:23 +0000235
236 if (getOptions().hasCreatedTime())
237 getOptions().colored_ostream(OS, raw_ostream::CYAN)
238 << getOptions().CreatedTimeStr << "\n";
239}
240
Vedant Kumarb1c174a2016-09-10 19:37:26 +0000241void SourceCoverageViewText::renderTableHeader(raw_ostream &, unsigned,
242 unsigned) {}