2016-06-25 10:58:30 +08:00
|
|
|
//===- SourceCoverageViewText.cpp - A text-based code coverage view -------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2016-06-25 13:48:54 +08:00
|
|
|
///
|
|
|
|
/// \file This file implements the text-based coverage renderer.
|
|
|
|
///
|
2016-06-25 10:58:30 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-09-09 09:32:55 +08:00
|
|
|
#include "CoverageReport.h"
|
2016-06-25 10:58:30 +08:00
|
|
|
#include "SourceCoverageViewText.h"
|
2016-06-26 10:45:13 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2016-06-25 10:58:30 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-06-29 00:12:24 +08:00
|
|
|
Expected<CoveragePrinter::OwnedStream>
|
|
|
|
CoveragePrinterText::createViewFile(StringRef Path, bool InToplevel) {
|
|
|
|
return createOutputStream(Path, "txt", InToplevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoveragePrinterText::closeViewFile(OwnedStream OS) {
|
|
|
|
OS->operator<<('\n');
|
|
|
|
}
|
|
|
|
|
2016-09-09 09:32:55 +08:00
|
|
|
Error CoveragePrinterText::createIndexFile(
|
2017-10-19 07:58:27 +08:00
|
|
|
ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage,
|
2017-10-03 19:05:28 +08:00
|
|
|
const CoverageFiltersMatchAll &Filters) {
|
2016-06-29 00:12:24 +08:00
|
|
|
auto OSOrErr = createOutputStream("index", "txt", /*InToplevel=*/true);
|
|
|
|
if (Error E = OSOrErr.takeError())
|
|
|
|
return E;
|
|
|
|
auto OS = std::move(OSOrErr.get());
|
|
|
|
raw_ostream &OSRef = *OS.get();
|
|
|
|
|
2016-09-09 09:32:55 +08:00
|
|
|
CoverageReport Report(Opts, Coverage);
|
2017-09-28 18:07:30 +08:00
|
|
|
Report.renderFileReports(OSRef, SourceFiles, Filters);
|
2016-06-29 00:12:24 +08:00
|
|
|
|
2016-09-13 19:28:31 +08:00
|
|
|
Opts.colored_ostream(OSRef, raw_ostream::CYAN) << "\n"
|
|
|
|
<< Opts.getLLVMVersionString();
|
|
|
|
|
2016-06-29 00:12:24 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2016-06-25 10:58:30 +08:00
|
|
|
namespace {
|
|
|
|
|
2016-06-25 11:27:29 +08:00
|
|
|
static const unsigned LineCoverageColumnWidth = 7;
|
|
|
|
static const unsigned LineNumberColumnWidth = 5;
|
2016-06-25 10:58:30 +08:00
|
|
|
|
|
|
|
/// \brief Get the width of the leading columns.
|
|
|
|
unsigned getCombinedColumnWidth(const CoverageViewOptions &Opts) {
|
|
|
|
return (Opts.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
|
|
|
|
(Opts.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief The width of the line that is used to divide between the view and
|
|
|
|
/// the subviews.
|
|
|
|
unsigned getDividerWidth(const CoverageViewOptions &Opts) {
|
|
|
|
return getCombinedColumnWidth(Opts) + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-07-07 06:02:55 +08:00
|
|
|
void SourceCoverageViewText::renderViewHeader(raw_ostream &) {}
|
2016-06-29 08:38:21 +08:00
|
|
|
|
2016-09-14 07:00:13 +08:00
|
|
|
void SourceCoverageViewText::renderViewFooter(raw_ostream &) {}
|
2016-06-29 08:38:21 +08:00
|
|
|
|
2016-09-11 03:37:26 +08:00
|
|
|
void SourceCoverageViewText::renderSourceName(raw_ostream &OS, bool WholeFile) {
|
2016-10-25 08:08:33 +08:00
|
|
|
getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName()
|
|
|
|
<< ":\n";
|
2016-06-25 10:58:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS,
|
|
|
|
unsigned ViewDepth) {
|
|
|
|
for (unsigned I = 0; I < ViewDepth; ++I)
|
|
|
|
OS << " |";
|
|
|
|
}
|
|
|
|
|
2016-07-07 06:02:55 +08:00
|
|
|
void SourceCoverageViewText::renderLineSuffix(raw_ostream &, unsigned) {}
|
2016-06-29 08:38:21 +08:00
|
|
|
|
2016-06-25 10:58:30 +08:00
|
|
|
void SourceCoverageViewText::renderViewDivider(raw_ostream &OS,
|
|
|
|
unsigned ViewDepth) {
|
|
|
|
assert(ViewDepth != 0 && "Cannot render divider at top level");
|
|
|
|
renderLinePrefix(OS, ViewDepth - 1);
|
|
|
|
OS.indent(2);
|
|
|
|
unsigned Length = getDividerWidth(getOptions());
|
|
|
|
for (unsigned I = 0; I < Length; ++I)
|
|
|
|
OS << '-';
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2017-10-19 02:52:28 +08:00
|
|
|
void SourceCoverageViewText::renderLine(raw_ostream &OS, LineRef L,
|
|
|
|
const LineCoverageStats &LCS,
|
|
|
|
unsigned ExpansionCol,
|
|
|
|
unsigned ViewDepth) {
|
2016-06-25 10:58:30 +08:00
|
|
|
StringRef Line = L.Line;
|
|
|
|
unsigned LineNumber = L.LineNo;
|
2017-10-19 02:52:28 +08:00
|
|
|
auto *WrappedSegment = LCS.getWrappedSegment();
|
|
|
|
CoverageSegmentArray Segments = LCS.getLineSegments();
|
2016-06-25 10:58:30 +08:00
|
|
|
|
|
|
|
Optional<raw_ostream::Colors> Highlight;
|
|
|
|
SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
|
|
|
|
|
|
|
|
// The first segment overlaps from a previous line, so we treat it specially.
|
2017-11-09 10:33:43 +08:00
|
|
|
if (WrappedSegment && !WrappedSegment->IsGapRegion &&
|
|
|
|
WrappedSegment->HasCount && WrappedSegment->Count == 0)
|
2016-06-25 10:58:30 +08:00
|
|
|
Highlight = raw_ostream::RED;
|
|
|
|
|
|
|
|
// Output each segment of the line, possibly highlighted.
|
|
|
|
unsigned Col = 1;
|
|
|
|
for (const auto *S : Segments) {
|
|
|
|
unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1);
|
|
|
|
colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
|
|
|
|
getOptions().Colors && Highlight, /*Bold=*/false,
|
|
|
|
/*BG=*/true)
|
|
|
|
<< Line.substr(Col - 1, End - Col);
|
|
|
|
if (getOptions().Debug && Highlight)
|
|
|
|
HighlightedRanges.push_back(std::make_pair(Col, End));
|
|
|
|
Col = End;
|
2017-11-09 10:33:43 +08:00
|
|
|
if ((!S->IsGapRegion || (Highlight && *Highlight == raw_ostream::RED)) &&
|
|
|
|
S->HasCount && S->Count == 0)
|
2016-06-25 10:58:30 +08:00
|
|
|
Highlight = raw_ostream::RED;
|
2017-11-09 10:33:43 +08:00
|
|
|
else if (Col == ExpansionCol)
|
|
|
|
Highlight = raw_ostream::CYAN;
|
2016-06-25 10:58:30 +08:00
|
|
|
else
|
|
|
|
Highlight = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show the rest of the line.
|
|
|
|
colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
|
|
|
|
getOptions().Colors && Highlight, /*Bold=*/false, /*BG=*/true)
|
|
|
|
<< Line.substr(Col - 1, Line.size() - Col + 1);
|
|
|
|
OS << '\n';
|
|
|
|
|
|
|
|
if (getOptions().Debug) {
|
|
|
|
for (const auto &Range : HighlightedRanges)
|
|
|
|
errs() << "Highlighted line " << LineNumber << ", " << Range.first
|
|
|
|
<< " -> " << Range.second << '\n';
|
|
|
|
if (Highlight)
|
|
|
|
errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceCoverageViewText::renderLineCoverageColumn(
|
|
|
|
raw_ostream &OS, const LineCoverageStats &Line) {
|
|
|
|
if (!Line.isMapped()) {
|
|
|
|
OS.indent(LineCoverageColumnWidth) << '|';
|
|
|
|
return;
|
|
|
|
}
|
2017-10-14 10:27:29 +08:00
|
|
|
std::string C = formatCount(Line.getExecutionCount());
|
2016-06-25 10:58:30 +08:00
|
|
|
OS.indent(LineCoverageColumnWidth - C.size());
|
|
|
|
colored_ostream(OS, raw_ostream::MAGENTA,
|
|
|
|
Line.hasMultipleRegions() && getOptions().Colors)
|
|
|
|
<< C;
|
|
|
|
OS << '|';
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS,
|
|
|
|
unsigned LineNo) {
|
|
|
|
SmallString<32> Buffer;
|
|
|
|
raw_svector_ostream BufferOS(Buffer);
|
|
|
|
BufferOS << LineNo;
|
|
|
|
auto Str = BufferOS.str();
|
|
|
|
// Trim and align to the right.
|
|
|
|
Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth));
|
|
|
|
OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
|
|
|
|
}
|
|
|
|
|
2017-10-19 02:52:28 +08:00
|
|
|
void SourceCoverageViewText::renderRegionMarkers(raw_ostream &OS,
|
|
|
|
const LineCoverageStats &Line,
|
|
|
|
unsigned ViewDepth) {
|
2016-06-25 10:58:30 +08:00
|
|
|
renderLinePrefix(OS, ViewDepth);
|
|
|
|
OS.indent(getCombinedColumnWidth(getOptions()));
|
|
|
|
|
2017-10-19 02:52:28 +08:00
|
|
|
CoverageSegmentArray Segments = Line.getLineSegments();
|
|
|
|
|
2017-09-09 02:44:46 +08:00
|
|
|
// Just consider the segments which start *and* end on this line.
|
|
|
|
if (Segments.size() > 1)
|
|
|
|
Segments = Segments.drop_back();
|
|
|
|
|
2016-06-25 10:58:30 +08:00
|
|
|
unsigned PrevColumn = 1;
|
|
|
|
for (const auto *S : Segments) {
|
|
|
|
if (!S->IsRegionEntry)
|
|
|
|
continue;
|
2017-10-19 02:52:29 +08:00
|
|
|
if (S->Count == Line.getExecutionCount())
|
|
|
|
continue;
|
2016-06-25 10:58:30 +08:00
|
|
|
// Skip to the new region.
|
|
|
|
if (S->Col > PrevColumn)
|
|
|
|
OS.indent(S->Col - PrevColumn);
|
|
|
|
PrevColumn = S->Col + 1;
|
|
|
|
std::string C = formatCount(S->Count);
|
|
|
|
PrevColumn += C.size();
|
|
|
|
OS << '^' << C;
|
|
|
|
|
2017-09-09 02:44:46 +08:00
|
|
|
if (getOptions().Debug)
|
2016-06-25 10:58:30 +08:00
|
|
|
errs() << "Marker at " << S->Line << ":" << S->Col << " = "
|
2017-09-09 02:44:46 +08:00
|
|
|
<< formatCount(S->Count) << "\n";
|
|
|
|
}
|
|
|
|
OS << '\n';
|
2016-06-25 10:58:30 +08:00
|
|
|
}
|
|
|
|
|
2017-10-19 02:52:28 +08:00
|
|
|
void SourceCoverageViewText::renderExpansionSite(raw_ostream &OS, LineRef L,
|
|
|
|
const LineCoverageStats &LCS,
|
|
|
|
unsigned ExpansionCol,
|
|
|
|
unsigned ViewDepth) {
|
2016-06-26 10:45:13 +08:00
|
|
|
renderLinePrefix(OS, ViewDepth);
|
|
|
|
OS.indent(getCombinedColumnWidth(getOptions()) + (ViewDepth == 0 ? 0 : 1));
|
2017-10-19 02:52:28 +08:00
|
|
|
renderLine(OS, L, LCS, ExpansionCol, ViewDepth);
|
2016-06-26 10:45:13 +08:00
|
|
|
}
|
2016-06-25 10:58:30 +08:00
|
|
|
|
2016-06-26 10:45:13 +08:00
|
|
|
void SourceCoverageViewText::renderExpansionView(raw_ostream &OS,
|
|
|
|
ExpansionView &ESV,
|
|
|
|
unsigned ViewDepth) {
|
2016-06-25 10:58:30 +08:00
|
|
|
// Render the child subview.
|
|
|
|
if (getOptions().Debug)
|
|
|
|
errs() << "Expansion at line " << ESV.getLine() << ", " << ESV.getStartCol()
|
|
|
|
<< " -> " << ESV.getEndCol() << '\n';
|
|
|
|
ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
|
2017-09-28 18:07:30 +08:00
|
|
|
/*ShowTitle=*/false, ViewDepth + 1);
|
2016-06-25 10:58:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS,
|
|
|
|
InstantiationView &ISV,
|
|
|
|
unsigned ViewDepth) {
|
|
|
|
renderLinePrefix(OS, ViewDepth);
|
|
|
|
OS << ' ';
|
2016-09-15 14:44:51 +08:00
|
|
|
if (!ISV.View)
|
|
|
|
getOptions().colored_ostream(OS, raw_ostream::RED)
|
|
|
|
<< "Unexecuted instantiation: " << ISV.FunctionName << "\n";
|
|
|
|
else
|
|
|
|
ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true,
|
2017-09-28 18:07:30 +08:00
|
|
|
/*ShowTitle=*/false, ViewDepth);
|
2016-06-25 10:58:30 +08:00
|
|
|
}
|
2016-08-24 22:27:23 +08:00
|
|
|
|
2016-09-15 12:45:59 +08:00
|
|
|
void SourceCoverageViewText::renderTitle(raw_ostream &OS, StringRef Title) {
|
2016-08-24 22:27:23 +08:00
|
|
|
if (getOptions().hasProjectTitle())
|
|
|
|
getOptions().colored_ostream(OS, raw_ostream::CYAN)
|
|
|
|
<< getOptions().ProjectTitle << "\n";
|
|
|
|
|
2016-09-15 12:45:59 +08:00
|
|
|
getOptions().colored_ostream(OS, raw_ostream::CYAN) << Title << "\n";
|
2016-08-24 22:27:23 +08:00
|
|
|
|
|
|
|
if (getOptions().hasCreatedTime())
|
|
|
|
getOptions().colored_ostream(OS, raw_ostream::CYAN)
|
|
|
|
<< getOptions().CreatedTimeStr << "\n";
|
|
|
|
}
|
|
|
|
|
2016-09-11 03:37:26 +08:00
|
|
|
void SourceCoverageViewText::renderTableHeader(raw_ostream &, unsigned,
|
|
|
|
unsigned) {}
|