blob: 3d2c067d0a4d5f01cb42383c9193258d7f1c22fa [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
14#include "SourceCoverageViewText.h"
Vedant Kumar861a19c2016-06-26 02:45:13 +000015#include "llvm/ADT/Optional.h"
Vedant Kumarf9151b92016-06-25 02:58:30 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/StringExtras.h"
18
19using namespace llvm;
20
Vedant Kumar9cbad2c2016-06-28 16:12:24 +000021Expected<CoveragePrinter::OwnedStream>
22CoveragePrinterText::createViewFile(StringRef Path, bool InToplevel) {
23 return createOutputStream(Path, "txt", InToplevel);
24}
25
26void CoveragePrinterText::closeViewFile(OwnedStream OS) {
27 OS->operator<<('\n');
28}
29
30Error CoveragePrinterText::createIndexFile(ArrayRef<StringRef> SourceFiles) {
31 auto OSOrErr = createOutputStream("index", "txt", /*InToplevel=*/true);
32 if (Error E = OSOrErr.takeError())
33 return E;
34 auto OS = std::move(OSOrErr.get());
35 raw_ostream &OSRef = *OS.get();
36
37 for (StringRef SF : SourceFiles)
38 OSRef << getOutputPath(SF, "txt", /*InToplevel=*/false) << '\n';
39
40 return Error::success();
41}
42
Vedant Kumarf9151b92016-06-25 02:58:30 +000043namespace {
44
Vedant Kumar4f4e8d82016-06-25 03:27:29 +000045static const unsigned LineCoverageColumnWidth = 7;
46static const unsigned LineNumberColumnWidth = 5;
Vedant Kumarf9151b92016-06-25 02:58:30 +000047
48/// \brief Get the width of the leading columns.
49unsigned getCombinedColumnWidth(const CoverageViewOptions &Opts) {
50 return (Opts.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
51 (Opts.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
52}
53
54/// \brief The width of the line that is used to divide between the view and
55/// the subviews.
56unsigned getDividerWidth(const CoverageViewOptions &Opts) {
57 return getCombinedColumnWidth(Opts) + 4;
58}
59
60} // anonymous namespace
61
Vedant Kumaraf0dd932016-07-06 22:02:55 +000062void SourceCoverageViewText::renderViewHeader(raw_ostream &) {}
Vedant Kumar8d74cb22016-06-29 00:38:21 +000063
Vedant Kumaraf0dd932016-07-06 22:02:55 +000064void SourceCoverageViewText::renderViewFooter(raw_ostream &) {}
Vedant Kumar8d74cb22016-06-29 00:38:21 +000065
Ying Yid36b47c2016-09-06 19:31:18 +000066void SourceCoverageViewText::renderSourceName(raw_ostream &OS, bool WholeFile,
67 unsigned FirstUncoveredLineNo) {
Vedant Kumar0053c0b2016-09-08 00:56:48 +000068 std::string ViewInfo = WholeFile ? getVerboseSourceName() : getSourceName();
69 getOptions().colored_ostream(OS, raw_ostream::CYAN) << ViewInfo << ":\n";
Vedant Kumarf9151b92016-06-25 02:58:30 +000070}
71
72void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS,
73 unsigned ViewDepth) {
74 for (unsigned I = 0; I < ViewDepth; ++I)
75 OS << " |";
76}
77
Vedant Kumaraf0dd932016-07-06 22:02:55 +000078void SourceCoverageViewText::renderLineSuffix(raw_ostream &, unsigned) {}
Vedant Kumar8d74cb22016-06-29 00:38:21 +000079
Vedant Kumarf9151b92016-06-25 02:58:30 +000080void SourceCoverageViewText::renderViewDivider(raw_ostream &OS,
81 unsigned ViewDepth) {
82 assert(ViewDepth != 0 && "Cannot render divider at top level");
83 renderLinePrefix(OS, ViewDepth - 1);
84 OS.indent(2);
85 unsigned Length = getDividerWidth(getOptions());
86 for (unsigned I = 0; I < Length; ++I)
87 OS << '-';
88 OS << '\n';
89}
90
91void SourceCoverageViewText::renderLine(
92 raw_ostream &OS, LineRef L,
93 const coverage::CoverageSegment *WrappedSegment,
94 CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) {
95 StringRef Line = L.Line;
96 unsigned LineNumber = L.LineNo;
97
98 Optional<raw_ostream::Colors> Highlight;
99 SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
100
101 // The first segment overlaps from a previous line, so we treat it specially.
102 if (WrappedSegment && WrappedSegment->HasCount && WrappedSegment->Count == 0)
103 Highlight = raw_ostream::RED;
104
105 // Output each segment of the line, possibly highlighted.
106 unsigned Col = 1;
107 for (const auto *S : Segments) {
108 unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1);
109 colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
110 getOptions().Colors && Highlight, /*Bold=*/false,
111 /*BG=*/true)
112 << Line.substr(Col - 1, End - Col);
113 if (getOptions().Debug && Highlight)
114 HighlightedRanges.push_back(std::make_pair(Col, End));
115 Col = End;
116 if (Col == ExpansionCol)
117 Highlight = raw_ostream::CYAN;
118 else if (S->HasCount && S->Count == 0)
119 Highlight = raw_ostream::RED;
120 else
121 Highlight = None;
122 }
123
124 // Show the rest of the line.
125 colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
126 getOptions().Colors && Highlight, /*Bold=*/false, /*BG=*/true)
127 << Line.substr(Col - 1, Line.size() - Col + 1);
128 OS << '\n';
129
130 if (getOptions().Debug) {
131 for (const auto &Range : HighlightedRanges)
132 errs() << "Highlighted line " << LineNumber << ", " << Range.first
133 << " -> " << Range.second << '\n';
134 if (Highlight)
135 errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n";
136 }
137}
138
139void SourceCoverageViewText::renderLineCoverageColumn(
140 raw_ostream &OS, const LineCoverageStats &Line) {
141 if (!Line.isMapped()) {
142 OS.indent(LineCoverageColumnWidth) << '|';
143 return;
144 }
145 std::string C = formatCount(Line.ExecutionCount);
146 OS.indent(LineCoverageColumnWidth - C.size());
147 colored_ostream(OS, raw_ostream::MAGENTA,
148 Line.hasMultipleRegions() && getOptions().Colors)
149 << C;
150 OS << '|';
151}
152
153void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS,
154 unsigned LineNo) {
155 SmallString<32> Buffer;
156 raw_svector_ostream BufferOS(Buffer);
157 BufferOS << LineNo;
158 auto Str = BufferOS.str();
159 // Trim and align to the right.
160 Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth));
161 OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
162}
163
164void SourceCoverageViewText::renderRegionMarkers(
165 raw_ostream &OS, CoverageSegmentArray Segments, unsigned ViewDepth) {
166 renderLinePrefix(OS, ViewDepth);
167 OS.indent(getCombinedColumnWidth(getOptions()));
168
169 unsigned PrevColumn = 1;
170 for (const auto *S : Segments) {
171 if (!S->IsRegionEntry)
172 continue;
173 // Skip to the new region.
174 if (S->Col > PrevColumn)
175 OS.indent(S->Col - PrevColumn);
176 PrevColumn = S->Col + 1;
177 std::string C = formatCount(S->Count);
178 PrevColumn += C.size();
179 OS << '^' << C;
180 }
181 OS << '\n';
182
183 if (getOptions().Debug)
184 for (const auto *S : Segments)
185 errs() << "Marker at " << S->Line << ":" << S->Col << " = "
186 << formatCount(S->Count) << (S->IsRegionEntry ? "\n" : " (pop)\n");
187}
188
Vedant Kumar861a19c2016-06-26 02:45:13 +0000189void SourceCoverageViewText::renderExpansionSite(
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000190 raw_ostream &OS, LineRef L, const coverage::CoverageSegment *WrappedSegment,
Vedant Kumar8b12ecb2016-06-25 05:48:59 +0000191 CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) {
Vedant Kumar861a19c2016-06-26 02:45:13 +0000192 renderLinePrefix(OS, ViewDepth);
193 OS.indent(getCombinedColumnWidth(getOptions()) + (ViewDepth == 0 ? 0 : 1));
194 renderLine(OS, L, WrappedSegment, Segments, ExpansionCol, ViewDepth);
195}
Vedant Kumarf9151b92016-06-25 02:58:30 +0000196
Vedant Kumar861a19c2016-06-26 02:45:13 +0000197void SourceCoverageViewText::renderExpansionView(raw_ostream &OS,
198 ExpansionView &ESV,
199 unsigned ViewDepth) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000200 // Render the child subview.
201 if (getOptions().Debug)
202 errs() << "Expansion at line " << ESV.getLine() << ", " << ESV.getStartCol()
203 << " -> " << ESV.getEndCol() << '\n';
204 ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
205 ViewDepth + 1);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000206}
207
208void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS,
209 InstantiationView &ISV,
210 unsigned ViewDepth) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000211 renderLinePrefix(OS, ViewDepth);
212 OS << ' ';
213 ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true, ViewDepth);
214}
Ying Yi84dc9712016-08-24 14:27:23 +0000215
216void SourceCoverageViewText::renderCellInTitle(raw_ostream &OS,
217 StringRef CellText) {
218 if (getOptions().hasProjectTitle())
219 getOptions().colored_ostream(OS, raw_ostream::CYAN)
220 << getOptions().ProjectTitle << "\n";
221
222 getOptions().colored_ostream(OS, raw_ostream::CYAN) << CellText << "\n";
223
224 if (getOptions().hasCreatedTime())
225 getOptions().colored_ostream(OS, raw_ostream::CYAN)
226 << getOptions().CreatedTimeStr << "\n";
227}
228
229void SourceCoverageViewText::renderTableHeader(raw_ostream &, unsigned) {}