blob: 70ded87c495515951d0c0f46056f9bea31506331 [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 Kumar8d74cb22016-06-29 00:38:21 +000062void SourceCoverageViewText::renderViewHeader(raw_ostream &OS) { (void)OS; }
63
64void SourceCoverageViewText::renderViewFooter(raw_ostream &OS) { (void)OS; }
65
Vedant Kumarf9151b92016-06-25 02:58:30 +000066void SourceCoverageViewText::renderSourceName(raw_ostream &OS) {
67 getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName()
68 << ":\n";
69}
70
71void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS,
72 unsigned ViewDepth) {
73 for (unsigned I = 0; I < ViewDepth; ++I)
74 OS << " |";
75}
76
Vedant Kumar8d74cb22016-06-29 00:38:21 +000077void SourceCoverageViewText::renderLineSuffix(raw_ostream &OS,
78 unsigned ViewDepth) {
79 (void)OS;
80 (void)ViewDepth;
81}
82
Vedant Kumarf9151b92016-06-25 02:58:30 +000083void SourceCoverageViewText::renderViewDivider(raw_ostream &OS,
84 unsigned ViewDepth) {
85 assert(ViewDepth != 0 && "Cannot render divider at top level");
86 renderLinePrefix(OS, ViewDepth - 1);
87 OS.indent(2);
88 unsigned Length = getDividerWidth(getOptions());
89 for (unsigned I = 0; I < Length; ++I)
90 OS << '-';
91 OS << '\n';
92}
93
94void SourceCoverageViewText::renderLine(
95 raw_ostream &OS, LineRef L,
96 const coverage::CoverageSegment *WrappedSegment,
97 CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) {
98 StringRef Line = L.Line;
99 unsigned LineNumber = L.LineNo;
100
101 Optional<raw_ostream::Colors> Highlight;
102 SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
103
104 // The first segment overlaps from a previous line, so we treat it specially.
105 if (WrappedSegment && WrappedSegment->HasCount && WrappedSegment->Count == 0)
106 Highlight = raw_ostream::RED;
107
108 // Output each segment of the line, possibly highlighted.
109 unsigned Col = 1;
110 for (const auto *S : Segments) {
111 unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1);
112 colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
113 getOptions().Colors && Highlight, /*Bold=*/false,
114 /*BG=*/true)
115 << Line.substr(Col - 1, End - Col);
116 if (getOptions().Debug && Highlight)
117 HighlightedRanges.push_back(std::make_pair(Col, End));
118 Col = End;
119 if (Col == ExpansionCol)
120 Highlight = raw_ostream::CYAN;
121 else if (S->HasCount && S->Count == 0)
122 Highlight = raw_ostream::RED;
123 else
124 Highlight = None;
125 }
126
127 // Show the rest of the line.
128 colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
129 getOptions().Colors && Highlight, /*Bold=*/false, /*BG=*/true)
130 << Line.substr(Col - 1, Line.size() - Col + 1);
131 OS << '\n';
132
133 if (getOptions().Debug) {
134 for (const auto &Range : HighlightedRanges)
135 errs() << "Highlighted line " << LineNumber << ", " << Range.first
136 << " -> " << Range.second << '\n';
137 if (Highlight)
138 errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n";
139 }
140}
141
142void SourceCoverageViewText::renderLineCoverageColumn(
143 raw_ostream &OS, const LineCoverageStats &Line) {
144 if (!Line.isMapped()) {
145 OS.indent(LineCoverageColumnWidth) << '|';
146 return;
147 }
148 std::string C = formatCount(Line.ExecutionCount);
149 OS.indent(LineCoverageColumnWidth - C.size());
150 colored_ostream(OS, raw_ostream::MAGENTA,
151 Line.hasMultipleRegions() && getOptions().Colors)
152 << C;
153 OS << '|';
154}
155
156void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS,
157 unsigned LineNo) {
158 SmallString<32> Buffer;
159 raw_svector_ostream BufferOS(Buffer);
160 BufferOS << LineNo;
161 auto Str = BufferOS.str();
162 // Trim and align to the right.
163 Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth));
164 OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
165}
166
167void SourceCoverageViewText::renderRegionMarkers(
168 raw_ostream &OS, CoverageSegmentArray Segments, unsigned ViewDepth) {
169 renderLinePrefix(OS, ViewDepth);
170 OS.indent(getCombinedColumnWidth(getOptions()));
171
172 unsigned PrevColumn = 1;
173 for (const auto *S : Segments) {
174 if (!S->IsRegionEntry)
175 continue;
176 // Skip to the new region.
177 if (S->Col > PrevColumn)
178 OS.indent(S->Col - PrevColumn);
179 PrevColumn = S->Col + 1;
180 std::string C = formatCount(S->Count);
181 PrevColumn += C.size();
182 OS << '^' << C;
183 }
184 OS << '\n';
185
186 if (getOptions().Debug)
187 for (const auto *S : Segments)
188 errs() << "Marker at " << S->Line << ":" << S->Col << " = "
189 << formatCount(S->Count) << (S->IsRegionEntry ? "\n" : " (pop)\n");
190}
191
Vedant Kumar861a19c2016-06-26 02:45:13 +0000192void SourceCoverageViewText::renderExpansionSite(
Vedant Kumar8d74cb22016-06-29 00:38:21 +0000193 raw_ostream &OS, LineRef L, const coverage::CoverageSegment *WrappedSegment,
Vedant Kumar8b12ecb2016-06-25 05:48:59 +0000194 CoverageSegmentArray Segments, unsigned ExpansionCol, unsigned ViewDepth) {
Vedant Kumar861a19c2016-06-26 02:45:13 +0000195 renderLinePrefix(OS, ViewDepth);
196 OS.indent(getCombinedColumnWidth(getOptions()) + (ViewDepth == 0 ? 0 : 1));
197 renderLine(OS, L, WrappedSegment, Segments, ExpansionCol, ViewDepth);
198}
Vedant Kumarf9151b92016-06-25 02:58:30 +0000199
Vedant Kumar861a19c2016-06-26 02:45:13 +0000200void SourceCoverageViewText::renderExpansionView(raw_ostream &OS,
201 ExpansionView &ESV,
202 unsigned ViewDepth) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000203 // Render the child subview.
204 if (getOptions().Debug)
205 errs() << "Expansion at line " << ESV.getLine() << ", " << ESV.getStartCol()
206 << " -> " << ESV.getEndCol() << '\n';
207 ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
208 ViewDepth + 1);
Vedant Kumarf9151b92016-06-25 02:58:30 +0000209}
210
211void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS,
212 InstantiationView &ISV,
213 unsigned ViewDepth) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000214 renderLinePrefix(OS, ViewDepth);
215 OS << ' ';
216 ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true, ViewDepth);
217}