2014-08-23 06:56:03 +08:00
|
|
|
//===- SourceCoverageView.cpp - Code coverage view for source code --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This class implements rendering for code coverage of source code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SourceCoverageView.h"
|
2014-09-18 02:23:47 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2014-08-23 06:56:03 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2015-05-14 06:41:48 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2014-08-23 06:56:03 +08:00
|
|
|
#include "llvm/Support/LineIterator.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-09-20 23:31:56 +08:00
|
|
|
void SourceCoverageView::renderLine(
|
|
|
|
raw_ostream &OS, StringRef Line, int64_t LineNumber,
|
|
|
|
const coverage::CoverageSegment *WrappedSegment,
|
|
|
|
ArrayRef<const coverage::CoverageSegment *> Segments,
|
|
|
|
unsigned ExpansionCol) {
|
2014-09-18 02:23:47 +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.
|
|
|
|
if (WrappedSegment && WrappedSegment->HasCount && WrappedSegment->Count == 0)
|
|
|
|
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,
|
|
|
|
Options.Colors && Highlight, /*Bold=*/false, /*BG=*/true)
|
|
|
|
<< Line.substr(Col - 1, End - Col);
|
|
|
|
if (Options.Debug && Highlight)
|
|
|
|
HighlightedRanges.push_back(std::make_pair(Col, End));
|
|
|
|
Col = End;
|
|
|
|
if (Col == ExpansionCol)
|
|
|
|
Highlight = raw_ostream::CYAN;
|
|
|
|
else if (S->HasCount && S->Count == 0)
|
|
|
|
Highlight = raw_ostream::RED;
|
|
|
|
else
|
|
|
|
Highlight = None;
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show the rest of the line
|
2014-09-18 02:23:47 +08:00
|
|
|
colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
|
|
|
|
Options.Colors && Highlight, /*Bold=*/false, /*BG=*/true)
|
|
|
|
<< Line.substr(Col - 1, Line.size() - Col + 1);
|
2014-08-23 06:56:03 +08:00
|
|
|
OS << "\n";
|
2014-09-16 06:23:29 +08:00
|
|
|
|
|
|
|
if (Options.Debug) {
|
2014-09-18 02:23:47 +08:00
|
|
|
for (const auto &Range : HighlightedRanges)
|
|
|
|
errs() << "Highlighted line " << LineNumber << ", " << Range.first
|
|
|
|
<< " -> " << Range.second << "\n";
|
|
|
|
if (Highlight)
|
|
|
|
errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n";
|
2014-09-16 06:23:29 +08:00
|
|
|
}
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
2014-09-16 14:21:57 +08:00
|
|
|
void SourceCoverageView::renderIndent(raw_ostream &OS, unsigned Level) {
|
|
|
|
for (unsigned I = 0; I < Level; ++I)
|
2014-08-23 06:56:03 +08:00
|
|
|
OS << " |";
|
|
|
|
}
|
|
|
|
|
2014-09-16 14:21:57 +08:00
|
|
|
void SourceCoverageView::renderViewDivider(unsigned Level, unsigned Length,
|
2014-08-23 06:56:03 +08:00
|
|
|
raw_ostream &OS) {
|
2014-09-16 14:21:57 +08:00
|
|
|
assert(Level != 0 && "Cannot render divider at top level");
|
|
|
|
renderIndent(OS, Level - 1);
|
|
|
|
OS.indent(2);
|
2014-08-23 06:56:03 +08:00
|
|
|
for (unsigned I = 0; I < Length; ++I)
|
|
|
|
OS << "-";
|
|
|
|
}
|
|
|
|
|
2015-05-14 06:41:48 +08:00
|
|
|
/// Format a count using engineering notation with 3 significant digits.
|
|
|
|
static std::string formatCount(uint64_t N) {
|
|
|
|
std::string Number = utostr(N);
|
|
|
|
int Len = Number.size();
|
|
|
|
if (Len <= 3)
|
|
|
|
return Number;
|
|
|
|
int IntLen = Len % 3 == 0 ? 3 : Len % 3;
|
|
|
|
std::string Result(Number.data(), IntLen);
|
|
|
|
if (IntLen != 3) {
|
|
|
|
Result.push_back('.');
|
|
|
|
Result += Number.substr(IntLen, 3 - IntLen);
|
|
|
|
}
|
|
|
|
Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2014-08-23 06:56:03 +08:00
|
|
|
void
|
|
|
|
SourceCoverageView::renderLineCoverageColumn(raw_ostream &OS,
|
|
|
|
const LineCoverageInfo &Line) {
|
|
|
|
if (!Line.isMapped()) {
|
|
|
|
OS.indent(LineCoverageColumnWidth) << '|';
|
|
|
|
return;
|
|
|
|
}
|
2015-05-14 06:41:48 +08:00
|
|
|
std::string C = formatCount(Line.ExecutionCount);
|
|
|
|
OS.indent(LineCoverageColumnWidth - C.size());
|
2014-08-23 06:56:03 +08:00
|
|
|
colored_ostream(OS, raw_ostream::MAGENTA,
|
|
|
|
Line.hasMultipleRegions() && Options.Colors)
|
2015-05-14 06:41:48 +08:00
|
|
|
<< C;
|
2014-08-23 06:56:03 +08:00
|
|
|
OS << '|';
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceCoverageView::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 << '|';
|
|
|
|
}
|
|
|
|
|
2014-09-18 02:23:47 +08:00
|
|
|
void SourceCoverageView::renderRegionMarkers(
|
2014-09-20 23:31:56 +08:00
|
|
|
raw_ostream &OS, ArrayRef<const coverage::CoverageSegment *> Segments) {
|
2014-08-23 06:56:03 +08:00
|
|
|
unsigned PrevColumn = 1;
|
2014-09-18 02:23:47 +08:00
|
|
|
for (const auto *S : Segments) {
|
|
|
|
if (!S->IsRegionEntry)
|
|
|
|
continue;
|
2014-08-23 06:56:03 +08:00
|
|
|
// Skip to the new region
|
2014-09-18 02:23:47 +08:00
|
|
|
if (S->Col > PrevColumn)
|
|
|
|
OS.indent(S->Col - PrevColumn);
|
|
|
|
PrevColumn = S->Col + 1;
|
2015-05-14 06:41:48 +08:00
|
|
|
std::string C = formatCount(S->Count);
|
|
|
|
PrevColumn += C.size();
|
|
|
|
OS << '^' << C;
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
OS << "\n";
|
2014-09-16 06:12:28 +08:00
|
|
|
|
2014-09-18 02:23:47 +08:00
|
|
|
if (Options.Debug)
|
|
|
|
for (const auto *S : Segments)
|
2015-05-14 06:41:48 +08:00
|
|
|
errs() << "Marker at " << S->Line << ":" << S->Col << " = "
|
|
|
|
<< formatCount(S->Count) << (S->IsRegionEntry ? "\n" : " (pop)\n");
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
2014-09-18 02:23:47 +08:00
|
|
|
void SourceCoverageView::render(raw_ostream &OS, bool WholeFile,
|
|
|
|
unsigned IndentLevel) {
|
2014-08-23 06:56:03 +08:00
|
|
|
// The width of the leading columns
|
|
|
|
unsigned CombinedColumnWidth =
|
|
|
|
(Options.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
|
|
|
|
(Options.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
|
|
|
|
// The width of the line that is used to divide between the view and the
|
|
|
|
// subviews.
|
|
|
|
unsigned DividerWidth = CombinedColumnWidth + 4;
|
|
|
|
|
2014-09-17 13:33:20 +08:00
|
|
|
// We need the expansions and instantiations sorted so we can go through them
|
|
|
|
// while we iterate lines.
|
|
|
|
std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
|
|
|
|
std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
|
|
|
|
auto NextESV = ExpansionSubViews.begin();
|
|
|
|
auto EndESV = ExpansionSubViews.end();
|
|
|
|
auto NextISV = InstantiationSubViews.begin();
|
|
|
|
auto EndISV = InstantiationSubViews.end();
|
|
|
|
|
2014-09-18 02:23:47 +08:00
|
|
|
// Get the coverage information for the file.
|
2014-09-20 23:31:56 +08:00
|
|
|
auto NextSegment = CoverageInfo.begin();
|
|
|
|
auto EndSegment = CoverageInfo.end();
|
2014-09-18 02:23:47 +08:00
|
|
|
|
2014-09-19 16:13:16 +08:00
|
|
|
unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0;
|
2014-09-20 23:31:56 +08:00
|
|
|
const coverage::CoverageSegment *WrappedSegment = nullptr;
|
|
|
|
SmallVector<const coverage::CoverageSegment *, 8> LineSegments;
|
2014-09-18 02:23:47 +08:00
|
|
|
for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) {
|
|
|
|
// If we aren't rendering the whole file, we need to filter out the prologue
|
|
|
|
// and epilogue.
|
|
|
|
if (!WholeFile) {
|
|
|
|
if (NextSegment == EndSegment)
|
|
|
|
break;
|
2014-09-19 16:13:16 +08:00
|
|
|
else if (LI.line_number() < FirstLine)
|
2014-09-18 02:23:47 +08:00
|
|
|
continue;
|
|
|
|
}
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2014-09-18 02:23:47 +08:00
|
|
|
// Collect the coverage information relevant to this line.
|
|
|
|
if (LineSegments.size())
|
|
|
|
WrappedSegment = LineSegments.back();
|
|
|
|
LineSegments.clear();
|
|
|
|
while (NextSegment != EndSegment && NextSegment->Line == LI.line_number())
|
|
|
|
LineSegments.push_back(&*NextSegment++);
|
|
|
|
|
|
|
|
// Calculate a count to be for the line as a whole.
|
|
|
|
LineCoverageInfo LineCount;
|
|
|
|
if (WrappedSegment && WrappedSegment->HasCount)
|
|
|
|
LineCount.addRegionCount(WrappedSegment->Count);
|
|
|
|
for (const auto *S : LineSegments)
|
|
|
|
if (S->HasCount && S->IsRegionEntry)
|
|
|
|
LineCount.addRegionStartCount(S->Count);
|
|
|
|
|
|
|
|
// Render the line prefix.
|
2014-09-16 14:21:57 +08:00
|
|
|
renderIndent(OS, IndentLevel);
|
2014-08-23 06:56:03 +08:00
|
|
|
if (Options.ShowLineStats)
|
2014-09-18 02:23:47 +08:00
|
|
|
renderLineCoverageColumn(OS, LineCount);
|
2014-08-23 06:56:03 +08:00
|
|
|
if (Options.ShowLineNumbers)
|
2014-09-18 02:23:47 +08:00
|
|
|
renderLineNumberColumn(OS, LI.line_number());
|
2014-08-23 06:56:03 +08:00
|
|
|
|
2014-09-18 02:23:47 +08:00
|
|
|
// If there are expansion subviews, we want to highlight the first one.
|
|
|
|
unsigned ExpansionColumn = 0;
|
|
|
|
if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
|
|
|
|
Options.Colors)
|
|
|
|
ExpansionColumn = NextESV->getStartCol();
|
2014-08-23 06:56:03 +08:00
|
|
|
|
|
|
|
// Display the source code for the current line.
|
2014-09-18 02:23:47 +08:00
|
|
|
renderLine(OS, *LI, LI.line_number(), WrappedSegment, LineSegments,
|
|
|
|
ExpansionColumn);
|
2014-08-23 06:56:03 +08:00
|
|
|
|
|
|
|
// Show the region markers.
|
2014-09-18 02:23:47 +08:00
|
|
|
if (Options.ShowRegionMarkers && (!Options.ShowLineStatsOrRegionMarkers ||
|
|
|
|
LineCount.hasMultipleRegions()) &&
|
|
|
|
!LineSegments.empty()) {
|
2014-09-16 14:21:57 +08:00
|
|
|
renderIndent(OS, IndentLevel);
|
2014-08-23 06:56:03 +08:00
|
|
|
OS.indent(CombinedColumnWidth);
|
2014-09-18 02:23:47 +08:00
|
|
|
renderRegionMarkers(OS, LineSegments);
|
2014-08-23 06:56:03 +08:00
|
|
|
}
|
|
|
|
|
2014-09-17 13:33:20 +08:00
|
|
|
// Show the expansions and instantiations for this line.
|
2014-09-16 14:21:57 +08:00
|
|
|
unsigned NestedIndent = IndentLevel + 1;
|
2014-09-17 13:33:20 +08:00
|
|
|
bool RenderedSubView = false;
|
2014-09-18 02:23:47 +08:00
|
|
|
for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
|
|
|
|
++NextESV) {
|
2014-09-17 13:33:20 +08:00
|
|
|
renderViewDivider(NestedIndent, DividerWidth, OS);
|
|
|
|
OS << "\n";
|
|
|
|
if (RenderedSubView) {
|
|
|
|
// Re-render the current line and highlight the expansion range for
|
|
|
|
// this subview.
|
2014-09-18 02:23:47 +08:00
|
|
|
ExpansionColumn = NextESV->getStartCol();
|
2014-09-17 13:33:20 +08:00
|
|
|
renderIndent(OS, IndentLevel);
|
|
|
|
OS.indent(CombinedColumnWidth + (IndentLevel == 0 ? 0 : 1));
|
2014-09-18 02:23:47 +08:00
|
|
|
renderLine(OS, *LI, LI.line_number(), WrappedSegment, LineSegments,
|
|
|
|
ExpansionColumn);
|
2014-09-17 13:33:20 +08:00
|
|
|
renderViewDivider(NestedIndent, DividerWidth, OS);
|
2014-08-23 06:56:03 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
// Render the child subview
|
2014-09-18 05:48:52 +08:00
|
|
|
if (Options.Debug)
|
|
|
|
errs() << "Expansion at line " << NextESV->getLine() << ", "
|
|
|
|
<< NextESV->getStartCol() << " -> " << NextESV->getEndCol()
|
|
|
|
<< "\n";
|
2014-09-18 02:23:47 +08:00
|
|
|
NextESV->View->render(OS, false, NestedIndent);
|
2014-09-17 13:33:20 +08:00
|
|
|
RenderedSubView = true;
|
|
|
|
}
|
2014-09-18 02:23:47 +08:00
|
|
|
for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
|
2014-09-17 13:33:20 +08:00
|
|
|
renderViewDivider(NestedIndent, DividerWidth, OS);
|
|
|
|
OS << "\n";
|
|
|
|
renderIndent(OS, NestedIndent);
|
|
|
|
OS << ' ';
|
|
|
|
Options.colored_ostream(OS, raw_ostream::CYAN) << NextISV->FunctionName
|
|
|
|
<< ":";
|
|
|
|
OS << "\n";
|
2014-09-18 02:23:47 +08:00
|
|
|
NextISV->View->render(OS, false, NestedIndent);
|
2014-09-17 13:33:20 +08:00
|
|
|
RenderedSubView = true;
|
|
|
|
}
|
|
|
|
if (RenderedSubView) {
|
2014-09-16 14:21:57 +08:00
|
|
|
renderViewDivider(NestedIndent, DividerWidth, OS);
|
2014-08-23 06:56:03 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|