1f9151b93SVedant Kumar //===- SourceCoverageViewText.cpp - A text-based code coverage view -------===//
2f9151b93SVedant Kumar //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f9151b93SVedant Kumar //
7f9151b93SVedant Kumar //===----------------------------------------------------------------------===//
8ee5a5e9bSVedant Kumar ///
9ee5a5e9bSVedant Kumar /// \file This file implements the text-based coverage renderer.
10ee5a5e9bSVedant Kumar ///
11f9151b93SVedant Kumar //===----------------------------------------------------------------------===//
12f9151b93SVedant Kumar
13f9151b93SVedant Kumar #include "SourceCoverageViewText.h"
14*9f2967bcSAlan Phipps #include "CoverageReport.h"
15861a19c2SVedant Kumar #include "llvm/ADT/Optional.h"
16f9151b93SVedant Kumar #include "llvm/ADT/SmallString.h"
17f9151b93SVedant Kumar #include "llvm/ADT/StringExtras.h"
18*9f2967bcSAlan Phipps #include "llvm/Support/Format.h"
19f9151b93SVedant Kumar
20f9151b93SVedant Kumar using namespace llvm;
21f9151b93SVedant Kumar
229cbad2c2SVedant Kumar Expected<CoveragePrinter::OwnedStream>
createViewFile(StringRef Path,bool InToplevel)239cbad2c2SVedant Kumar CoveragePrinterText::createViewFile(StringRef Path, bool InToplevel) {
249cbad2c2SVedant Kumar return createOutputStream(Path, "txt", InToplevel);
259cbad2c2SVedant Kumar }
269cbad2c2SVedant Kumar
closeViewFile(OwnedStream OS)279cbad2c2SVedant Kumar void CoveragePrinterText::closeViewFile(OwnedStream OS) {
289cbad2c2SVedant Kumar OS->operator<<('\n');
299cbad2c2SVedant Kumar }
309cbad2c2SVedant Kumar
createIndexFile(ArrayRef<std::string> SourceFiles,const CoverageMapping & Coverage,const CoverageFiltersMatchAll & Filters)31a59334daSVedant Kumar Error CoveragePrinterText::createIndexFile(
32e955f618SVedant Kumar ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage,
33d932b2d7SSean Eveson const CoverageFiltersMatchAll &Filters) {
349cbad2c2SVedant Kumar auto OSOrErr = createOutputStream("index", "txt", /*InToplevel=*/true);
359cbad2c2SVedant Kumar if (Error E = OSOrErr.takeError())
369cbad2c2SVedant Kumar return E;
379cbad2c2SVedant Kumar auto OS = std::move(OSOrErr.get());
389cbad2c2SVedant Kumar raw_ostream &OSRef = *OS.get();
399cbad2c2SVedant Kumar
40a59334daSVedant Kumar CoverageReport Report(Opts, Coverage);
41fa8ef35eSSean Eveson Report.renderFileReports(OSRef, SourceFiles, Filters);
429cbad2c2SVedant Kumar
43544b1df6SYing Yi Opts.colored_ostream(OSRef, raw_ostream::CYAN) << "\n"
44544b1df6SYing Yi << Opts.getLLVMVersionString();
45544b1df6SYing Yi
469cbad2c2SVedant Kumar return Error::success();
479cbad2c2SVedant Kumar }
489cbad2c2SVedant Kumar
49f9151b93SVedant Kumar namespace {
50f9151b93SVedant Kumar
514f4e8d81SVedant Kumar static const unsigned LineCoverageColumnWidth = 7;
524f4e8d81SVedant Kumar static const unsigned LineNumberColumnWidth = 5;
53f9151b93SVedant Kumar
545f8f34e4SAdrian Prantl /// Get the width of the leading columns.
getCombinedColumnWidth(const CoverageViewOptions & Opts)55f9151b93SVedant Kumar unsigned getCombinedColumnWidth(const CoverageViewOptions &Opts) {
56f9151b93SVedant Kumar return (Opts.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
57f9151b93SVedant Kumar (Opts.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
58f9151b93SVedant Kumar }
59f9151b93SVedant Kumar
605f8f34e4SAdrian Prantl /// The width of the line that is used to divide between the view and
61f9151b93SVedant Kumar /// the subviews.
getDividerWidth(const CoverageViewOptions & Opts)62f9151b93SVedant Kumar unsigned getDividerWidth(const CoverageViewOptions &Opts) {
63f9151b93SVedant Kumar return getCombinedColumnWidth(Opts) + 4;
64f9151b93SVedant Kumar }
65f9151b93SVedant Kumar
66f9151b93SVedant Kumar } // anonymous namespace
67f9151b93SVedant Kumar
renderViewHeader(raw_ostream &)68af0dd931SVedant Kumar void SourceCoverageViewText::renderViewHeader(raw_ostream &) {}
698d74cb27SVedant Kumar
renderViewFooter(raw_ostream &)7084a280adSVedant Kumar void SourceCoverageViewText::renderViewFooter(raw_ostream &) {}
718d74cb27SVedant Kumar
renderSourceName(raw_ostream & OS,bool WholeFile)72b1c174aaSVedant Kumar void SourceCoverageViewText::renderSourceName(raw_ostream &OS, bool WholeFile) {
735c61c703SVedant Kumar getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName()
745c61c703SVedant Kumar << ":\n";
75f9151b93SVedant Kumar }
76f9151b93SVedant Kumar
renderLinePrefix(raw_ostream & OS,unsigned ViewDepth)77f9151b93SVedant Kumar void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS,
78f9151b93SVedant Kumar unsigned ViewDepth) {
79f9151b93SVedant Kumar for (unsigned I = 0; I < ViewDepth; ++I)
80f9151b93SVedant Kumar OS << " |";
81f9151b93SVedant Kumar }
82f9151b93SVedant Kumar
renderLineSuffix(raw_ostream &,unsigned)83af0dd931SVedant Kumar void SourceCoverageViewText::renderLineSuffix(raw_ostream &, unsigned) {}
848d74cb27SVedant Kumar
renderViewDivider(raw_ostream & OS,unsigned ViewDepth)85f9151b93SVedant Kumar void SourceCoverageViewText::renderViewDivider(raw_ostream &OS,
86f9151b93SVedant Kumar unsigned ViewDepth) {
87f9151b93SVedant Kumar assert(ViewDepth != 0 && "Cannot render divider at top level");
88f9151b93SVedant Kumar renderLinePrefix(OS, ViewDepth - 1);
89f9151b93SVedant Kumar OS.indent(2);
90f9151b93SVedant Kumar unsigned Length = getDividerWidth(getOptions());
91f9151b93SVedant Kumar for (unsigned I = 0; I < Length; ++I)
92f9151b93SVedant Kumar OS << '-';
93f9151b93SVedant Kumar OS << '\n';
94f9151b93SVedant Kumar }
95f9151b93SVedant Kumar
renderLine(raw_ostream & OS,LineRef L,const LineCoverageStats & LCS,unsigned ExpansionCol,unsigned ViewDepth)9608a0a310SVedant Kumar void SourceCoverageViewText::renderLine(raw_ostream &OS, LineRef L,
9708a0a310SVedant Kumar const LineCoverageStats &LCS,
9808a0a310SVedant Kumar unsigned ExpansionCol,
9908a0a310SVedant Kumar unsigned ViewDepth) {
100f9151b93SVedant Kumar StringRef Line = L.Line;
101f9151b93SVedant Kumar unsigned LineNumber = L.LineNo;
10208a0a310SVedant Kumar auto *WrappedSegment = LCS.getWrappedSegment();
10308a0a310SVedant Kumar CoverageSegmentArray Segments = LCS.getLineSegments();
104f9151b93SVedant Kumar
1054d41c332SRui Ueyama Optional<raw_ostream::Colors> Highlight;
106f9151b93SVedant Kumar SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
107f9151b93SVedant Kumar
108f9151b93SVedant Kumar // The first segment overlaps from a previous line, so we treat it specially.
10943247f05SVedant Kumar if (WrappedSegment && !WrappedSegment->IsGapRegion &&
11043247f05SVedant Kumar WrappedSegment->HasCount && WrappedSegment->Count == 0)
111f9151b93SVedant Kumar Highlight = raw_ostream::RED;
112f9151b93SVedant Kumar
113f9151b93SVedant Kumar // Output each segment of the line, possibly highlighted.
114f9151b93SVedant Kumar unsigned Col = 1;
115f9151b93SVedant Kumar for (const auto *S : Segments) {
116f9151b93SVedant Kumar unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1);
117f9151b93SVedant Kumar colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
118f9151b93SVedant Kumar getOptions().Colors && Highlight, /*Bold=*/false,
119f9151b93SVedant Kumar /*BG=*/true)
120f9151b93SVedant Kumar << Line.substr(Col - 1, End - Col);
121f9151b93SVedant Kumar if (getOptions().Debug && Highlight)
122f9151b93SVedant Kumar HighlightedRanges.push_back(std::make_pair(Col, End));
123f9151b93SVedant Kumar Col = End;
12443247f05SVedant Kumar if ((!S->IsGapRegion || (Highlight && *Highlight == raw_ostream::RED)) &&
125988faf87SVedant Kumar S->HasCount && S->Count == 0)
126f9151b93SVedant Kumar Highlight = raw_ostream::RED;
12743247f05SVedant Kumar else if (Col == ExpansionCol)
12843247f05SVedant Kumar Highlight = raw_ostream::CYAN;
129f9151b93SVedant Kumar else
130f9151b93SVedant Kumar Highlight = None;
131f9151b93SVedant Kumar }
132f9151b93SVedant Kumar
133f9151b93SVedant Kumar // Show the rest of the line.
134f9151b93SVedant Kumar colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
135f9151b93SVedant Kumar getOptions().Colors && Highlight, /*Bold=*/false, /*BG=*/true)
136f9151b93SVedant Kumar << Line.substr(Col - 1, Line.size() - Col + 1);
137f9151b93SVedant Kumar OS << '\n';
138f9151b93SVedant Kumar
139f9151b93SVedant Kumar if (getOptions().Debug) {
140f9151b93SVedant Kumar for (const auto &Range : HighlightedRanges)
141f9151b93SVedant Kumar errs() << "Highlighted line " << LineNumber << ", " << Range.first
142f9151b93SVedant Kumar << " -> " << Range.second << '\n';
143f9151b93SVedant Kumar if (Highlight)
144f9151b93SVedant Kumar errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n";
145f9151b93SVedant Kumar }
146f9151b93SVedant Kumar }
147f9151b93SVedant Kumar
renderLineCoverageColumn(raw_ostream & OS,const LineCoverageStats & Line)148f9151b93SVedant Kumar void SourceCoverageViewText::renderLineCoverageColumn(
149f9151b93SVedant Kumar raw_ostream &OS, const LineCoverageStats &Line) {
150f9151b93SVedant Kumar if (!Line.isMapped()) {
151f9151b93SVedant Kumar OS.indent(LineCoverageColumnWidth) << '|';
152f9151b93SVedant Kumar return;
153f9151b93SVedant Kumar }
1541963f51fSVedant Kumar std::string C = formatCount(Line.getExecutionCount());
155f9151b93SVedant Kumar OS.indent(LineCoverageColumnWidth - C.size());
156f9151b93SVedant Kumar colored_ostream(OS, raw_ostream::MAGENTA,
157f9151b93SVedant Kumar Line.hasMultipleRegions() && getOptions().Colors)
158f9151b93SVedant Kumar << C;
159f9151b93SVedant Kumar OS << '|';
160f9151b93SVedant Kumar }
161f9151b93SVedant Kumar
renderLineNumberColumn(raw_ostream & OS,unsigned LineNo)162f9151b93SVedant Kumar void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS,
163f9151b93SVedant Kumar unsigned LineNo) {
164f9151b93SVedant Kumar SmallString<32> Buffer;
165f9151b93SVedant Kumar raw_svector_ostream BufferOS(Buffer);
166f9151b93SVedant Kumar BufferOS << LineNo;
167f9151b93SVedant Kumar auto Str = BufferOS.str();
168f9151b93SVedant Kumar // Trim and align to the right.
169f9151b93SVedant Kumar Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth));
170f9151b93SVedant Kumar OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
171f9151b93SVedant Kumar }
172f9151b93SVedant Kumar
renderRegionMarkers(raw_ostream & OS,const LineCoverageStats & Line,unsigned ViewDepth)17308a0a310SVedant Kumar void SourceCoverageViewText::renderRegionMarkers(raw_ostream &OS,
17408a0a310SVedant Kumar const LineCoverageStats &Line,
17508a0a310SVedant Kumar unsigned ViewDepth) {
176f9151b93SVedant Kumar renderLinePrefix(OS, ViewDepth);
177f9151b93SVedant Kumar OS.indent(getCombinedColumnWidth(getOptions()));
178f9151b93SVedant Kumar
17908a0a310SVedant Kumar CoverageSegmentArray Segments = Line.getLineSegments();
18008a0a310SVedant Kumar
181933b37f9SVedant Kumar // Just consider the segments which start *and* end on this line.
182933b37f9SVedant Kumar if (Segments.size() > 1)
183933b37f9SVedant Kumar Segments = Segments.drop_back();
184933b37f9SVedant Kumar
185f9151b93SVedant Kumar unsigned PrevColumn = 1;
186f9151b93SVedant Kumar for (const auto *S : Segments) {
187f9151b93SVedant Kumar if (!S->IsRegionEntry)
188f9151b93SVedant Kumar continue;
1899cbd33feSVedant Kumar if (S->Count == Line.getExecutionCount())
1909cbd33feSVedant Kumar continue;
191f9151b93SVedant Kumar // Skip to the new region.
192f9151b93SVedant Kumar if (S->Col > PrevColumn)
193f9151b93SVedant Kumar OS.indent(S->Col - PrevColumn);
194f9151b93SVedant Kumar PrevColumn = S->Col + 1;
195f9151b93SVedant Kumar std::string C = formatCount(S->Count);
196f9151b93SVedant Kumar PrevColumn += C.size();
197f9151b93SVedant Kumar OS << '^' << C;
198f9151b93SVedant Kumar
199f9151b93SVedant Kumar if (getOptions().Debug)
200f9151b93SVedant Kumar errs() << "Marker at " << S->Line << ":" << S->Col << " = "
201933b37f9SVedant Kumar << formatCount(S->Count) << "\n";
202933b37f9SVedant Kumar }
203933b37f9SVedant Kumar OS << '\n';
204f9151b93SVedant Kumar }
205f9151b93SVedant Kumar
renderExpansionSite(raw_ostream & OS,LineRef L,const LineCoverageStats & LCS,unsigned ExpansionCol,unsigned ViewDepth)20608a0a310SVedant Kumar void SourceCoverageViewText::renderExpansionSite(raw_ostream &OS, LineRef L,
20708a0a310SVedant Kumar const LineCoverageStats &LCS,
20808a0a310SVedant Kumar unsigned ExpansionCol,
20908a0a310SVedant Kumar unsigned ViewDepth) {
210f9151b93SVedant Kumar renderLinePrefix(OS, ViewDepth);
211f9151b93SVedant Kumar OS.indent(getCombinedColumnWidth(getOptions()) + (ViewDepth == 0 ? 0 : 1));
21208a0a310SVedant Kumar renderLine(OS, L, LCS, ExpansionCol, ViewDepth);
213f9151b93SVedant Kumar }
214f9151b93SVedant Kumar
renderExpansionView(raw_ostream & OS,ExpansionView & ESV,unsigned ViewDepth)215861a19c2SVedant Kumar void SourceCoverageViewText::renderExpansionView(raw_ostream &OS,
216861a19c2SVedant Kumar ExpansionView &ESV,
217861a19c2SVedant Kumar unsigned ViewDepth) {
218f9151b93SVedant Kumar // Render the child subview.
219f9151b93SVedant Kumar if (getOptions().Debug)
220f9151b93SVedant Kumar errs() << "Expansion at line " << ESV.getLine() << ", " << ESV.getStartCol()
221f9151b93SVedant Kumar << " -> " << ESV.getEndCol() << '\n';
222f9151b93SVedant Kumar ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
223fa8ef35eSSean Eveson /*ShowTitle=*/false, ViewDepth + 1);
224f9151b93SVedant Kumar }
225f9151b93SVedant Kumar
renderBranchView(raw_ostream & OS,BranchView & BRV,unsigned ViewDepth)226*9f2967bcSAlan Phipps void SourceCoverageViewText::renderBranchView(raw_ostream &OS, BranchView &BRV,
227*9f2967bcSAlan Phipps unsigned ViewDepth) {
228*9f2967bcSAlan Phipps // Render the child subview.
229*9f2967bcSAlan Phipps if (getOptions().Debug)
230*9f2967bcSAlan Phipps errs() << "Branch at line " << BRV.getLine() << '\n';
231*9f2967bcSAlan Phipps
232*9f2967bcSAlan Phipps for (const auto &R : BRV.Regions) {
233*9f2967bcSAlan Phipps double TruePercent = 0.0;
234*9f2967bcSAlan Phipps double FalsePercent = 0.0;
235*9f2967bcSAlan Phipps unsigned Total = R.ExecutionCount + R.FalseExecutionCount;
236*9f2967bcSAlan Phipps
237*9f2967bcSAlan Phipps if (!getOptions().ShowBranchCounts && Total != 0) {
238*9f2967bcSAlan Phipps TruePercent = ((double)(R.ExecutionCount) / (double)Total) * 100.0;
239*9f2967bcSAlan Phipps FalsePercent = ((double)(R.FalseExecutionCount) / (double)Total) * 100.0;
240*9f2967bcSAlan Phipps }
241*9f2967bcSAlan Phipps
242*9f2967bcSAlan Phipps renderLinePrefix(OS, ViewDepth);
243*9f2967bcSAlan Phipps OS << " Branch (" << R.LineStart << ":" << R.ColumnStart << "): [";
244*9f2967bcSAlan Phipps
245*9f2967bcSAlan Phipps if (R.Folded) {
246*9f2967bcSAlan Phipps OS << "Folded - Ignored]\n";
247*9f2967bcSAlan Phipps continue;
248*9f2967bcSAlan Phipps }
249*9f2967bcSAlan Phipps
250*9f2967bcSAlan Phipps colored_ostream(OS, raw_ostream::RED,
251*9f2967bcSAlan Phipps getOptions().Colors && !R.ExecutionCount,
252*9f2967bcSAlan Phipps /*Bold=*/false, /*BG=*/true)
253*9f2967bcSAlan Phipps << "True";
254*9f2967bcSAlan Phipps
255*9f2967bcSAlan Phipps if (getOptions().ShowBranchCounts)
256*9f2967bcSAlan Phipps OS << ": " << formatCount(R.ExecutionCount) << ", ";
257*9f2967bcSAlan Phipps else
258*9f2967bcSAlan Phipps OS << ": " << format("%0.2f", TruePercent) << "%, ";
259*9f2967bcSAlan Phipps
260*9f2967bcSAlan Phipps colored_ostream(OS, raw_ostream::RED,
261*9f2967bcSAlan Phipps getOptions().Colors && !R.FalseExecutionCount,
262*9f2967bcSAlan Phipps /*Bold=*/false, /*BG=*/true)
263*9f2967bcSAlan Phipps << "False";
264*9f2967bcSAlan Phipps
265*9f2967bcSAlan Phipps if (getOptions().ShowBranchCounts)
266*9f2967bcSAlan Phipps OS << ": " << formatCount(R.FalseExecutionCount);
267*9f2967bcSAlan Phipps else
268*9f2967bcSAlan Phipps OS << ": " << format("%0.2f", FalsePercent) << "%";
269*9f2967bcSAlan Phipps OS << "]\n";
270*9f2967bcSAlan Phipps }
271*9f2967bcSAlan Phipps }
272*9f2967bcSAlan Phipps
renderInstantiationView(raw_ostream & OS,InstantiationView & ISV,unsigned ViewDepth)273f9151b93SVedant Kumar void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS,
274f9151b93SVedant Kumar InstantiationView &ISV,
275f9151b93SVedant Kumar unsigned ViewDepth) {
276f9151b93SVedant Kumar renderLinePrefix(OS, ViewDepth);
277f9151b93SVedant Kumar OS << ' ';
278a8c396d9SVedant Kumar if (!ISV.View)
279a8c396d9SVedant Kumar getOptions().colored_ostream(OS, raw_ostream::RED)
280a8c396d9SVedant Kumar << "Unexecuted instantiation: " << ISV.FunctionName << "\n";
281a8c396d9SVedant Kumar else
282a8c396d9SVedant Kumar ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true,
283fa8ef35eSSean Eveson /*ShowTitle=*/false, ViewDepth);
284f9151b93SVedant Kumar }
28584dc971eSYing Yi
renderTitle(raw_ostream & OS,StringRef Title)286b2edd11fSVedant Kumar void SourceCoverageViewText::renderTitle(raw_ostream &OS, StringRef Title) {
28784dc971eSYing Yi if (getOptions().hasProjectTitle())
28884dc971eSYing Yi getOptions().colored_ostream(OS, raw_ostream::CYAN)
28984dc971eSYing Yi << getOptions().ProjectTitle << "\n";
29084dc971eSYing Yi
291b2edd11fSVedant Kumar getOptions().colored_ostream(OS, raw_ostream::CYAN) << Title << "\n";
29284dc971eSYing Yi
29384dc971eSYing Yi if (getOptions().hasCreatedTime())
29484dc971eSYing Yi getOptions().colored_ostream(OS, raw_ostream::CYAN)
29584dc971eSYing Yi << getOptions().CreatedTimeStr << "\n";
29684dc971eSYing Yi }
29784dc971eSYing Yi
renderTableHeader(raw_ostream &,unsigned,unsigned)298b1c174aaSVedant Kumar void SourceCoverageViewText::renderTableHeader(raw_ostream &, unsigned,
299b1c174aaSVedant Kumar unsigned) {}
300