summaryrefslogtreecommitdiff
path: root/tools/llvm-cov/SourceCoverageView.cpp
diff options
context:
space:
mode:
authorVedant Kumar <vsk@apple.com>2016-06-25 02:58:30 +0000
committerVedant Kumar <vsk@apple.com>2016-06-25 02:58:30 +0000
commita9bf312be4c22287bc30af0c730f4c2544cfb018 (patch)
treeec10c5bcbaddc2a4b160f1df645e31f372115128 /tools/llvm-cov/SourceCoverageView.cpp
parent2feb69ebcdc98a2325c26fc0f57988bf4b48148c (diff)
[llvm-cov] Separate presentation logic from formatting logic, NFC
This makes it easier to add renderers for new kinds of output formats. - Define and document a pure-virtual coverage rendering interface. - Move the text-based rendering logic into its a new file. - Re-work the API to better reflect the presentation/formatting split. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273767 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/llvm-cov/SourceCoverageView.cpp')
-rw-r--r--tools/llvm-cov/SourceCoverageView.cpp191
1 files changed, 34 insertions, 157 deletions
diff --git a/tools/llvm-cov/SourceCoverageView.cpp b/tools/llvm-cov/SourceCoverageView.cpp
index 8aa99295a0f..c393ad42f4c 100644
--- a/tools/llvm-cov/SourceCoverageView.cpp
+++ b/tools/llvm-cov/SourceCoverageView.cpp
@@ -12,75 +12,14 @@
//===----------------------------------------------------------------------===//
#include "SourceCoverageView.h"
-#include "llvm/ADT/Optional.h"
+#include "SourceCoverageViewText.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/LineIterator.h"
using namespace llvm;
-void SourceCoverageView::renderLine(
- raw_ostream &OS, StringRef Line, int64_t LineNumber,
- const coverage::CoverageSegment *WrappedSegment,
- ArrayRef<const coverage::CoverageSegment *> Segments,
- unsigned ExpansionCol) {
- 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,
- 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;
- if (Col == ExpansionCol)
- Highlight = raw_ostream::CYAN;
- else if (S->HasCount && S->Count == 0)
- Highlight = raw_ostream::RED;
- 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 SourceCoverageView::renderIndent(raw_ostream &OS, unsigned Level) {
- for (unsigned I = 0; I < Level; ++I)
- OS << " |";
-}
-
-void SourceCoverageView::renderViewDivider(unsigned Level, unsigned Length,
- raw_ostream &OS) {
- assert(Level != 0 && "Cannot render divider at top level");
- renderIndent(OS, Level - 1);
- OS.indent(2);
- for (unsigned I = 0; I < Length; ++I)
- OS << "-";
-}
-
-/// Format a count using engineering notation with 3 significant digits.
-static std::string formatCount(uint64_t N) {
+std::string SourceCoverageView::formatCount(uint64_t N) {
std::string Number = utostr(N);
int Len = Number.size();
if (Len <= 3)
@@ -95,63 +34,30 @@ static std::string formatCount(uint64_t N) {
return Result;
}
-void
-SourceCoverageView::renderLineCoverageColumn(raw_ostream &OS,
- const LineCoverageStats &Line) {
- if (!Line.isMapped()) {
- OS.indent(LineCoverageColumnWidth) << '|';
- return;
- }
- std::string C = formatCount(Line.ExecutionCount);
- OS.indent(LineCoverageColumnWidth - C.size());
- colored_ostream(OS, raw_ostream::MAGENTA,
- Line.hasMultipleRegions() && getOptions().Colors)
- << C;
- OS << '|';
+void SourceCoverageView::addExpansion(
+ const coverage::CounterMappingRegion &Region,
+ std::unique_ptr<SourceCoverageView> View) {
+ ExpansionSubViews.emplace_back(Region, std::move(View));
}
-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 << '|';
+void SourceCoverageView::addInstantiation(
+ StringRef FunctionName, unsigned Line,
+ std::unique_ptr<SourceCoverageView> View) {
+ InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
}
-void SourceCoverageView::renderRegionMarkers(
- raw_ostream &OS, ArrayRef<const coverage::CoverageSegment *> Segments) {
- unsigned PrevColumn = 1;
- for (const auto *S : Segments) {
- if (!S->IsRegionEntry)
- continue;
- // 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;
- }
- OS << "\n";
-
- if (getOptions().Debug)
- for (const auto *S : Segments)
- errs() << "Marker at " << S->Line << ":" << S->Col << " = "
- << formatCount(S->Count) << (S->IsRegionEntry ? "\n" : " (pop)\n");
+std::unique_ptr<SourceCoverageView>
+SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
+ const CoverageViewOptions &Options,
+ coverage::CoverageData &&CoverageInfo) {
+ return llvm::make_unique<SourceCoverageViewText>(SourceName, File, Options,
+ std::move(CoverageInfo));
}
-void SourceCoverageView::render(raw_ostream &OS, bool WholeFile,
- unsigned IndentLevel) {
- // The width of the leading columns
- unsigned CombinedColumnWidth =
- (getOptions().ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
- (getOptions().ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
- // The width of the line that is used to divide between the view and the
- // subviews.
- unsigned DividerWidth = CombinedColumnWidth + 4;
+void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
+ bool ShowSourceName, unsigned ViewDepth) {
+ if (ShowSourceName)
+ renderSourceName(OS);
// We need the expansions and instantiations sorted so we can go through them
// while we iterate lines.
@@ -192,10 +98,9 @@ void SourceCoverageView::render(raw_ostream &OS, bool WholeFile,
LineCount.addRegionCount(WrappedSegment->Count);
for (const auto *S : LineSegments)
if (S->HasCount && S->IsRegionEntry)
- LineCount.addRegionStartCount(S->Count);
+ LineCount.addRegionStartCount(S->Count);
- // Render the line prefix.
- renderIndent(OS, IndentLevel);
+ renderLinePrefix(OS, ViewDepth);
if (getOptions().ShowLineStats)
renderLineCoverageColumn(OS, LineCount);
if (getOptions().ShowLineNumbers)
@@ -208,62 +113,34 @@ void SourceCoverageView::render(raw_ostream &OS, bool WholeFile,
ExpansionColumn = NextESV->getStartCol();
// Display the source code for the current line.
- renderLine(OS, *LI, LI.line_number(), WrappedSegment, LineSegments,
- ExpansionColumn);
+ renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
+ ExpansionColumn, ViewDepth);
// Show the region markers.
if (getOptions().ShowRegionMarkers &&
(!getOptions().ShowLineStatsOrRegionMarkers ||
LineCount.hasMultipleRegions()) &&
!LineSegments.empty()) {
- renderIndent(OS, IndentLevel);
- OS.indent(CombinedColumnWidth);
- renderRegionMarkers(OS, LineSegments);
+ renderRegionMarkers(OS, LineSegments, ViewDepth);
}
// Show the expansions and instantiations for this line.
- unsigned NestedIndent = IndentLevel + 1;
bool RenderedSubView = false;
for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
++NextESV) {
- renderViewDivider(NestedIndent, DividerWidth, OS);
- OS << "\n";
- if (RenderedSubView) {
- // Re-render the current line and highlight the expansion range for
- // this subview.
- ExpansionColumn = NextESV->getStartCol();
- renderIndent(OS, IndentLevel);
- OS.indent(CombinedColumnWidth + (IndentLevel == 0 ? 0 : 1));
- renderLine(OS, *LI, LI.line_number(), WrappedSegment, LineSegments,
- ExpansionColumn);
- renderViewDivider(NestedIndent, DividerWidth, OS);
- OS << "\n";
- }
- // Render the child subview
- if (getOptions().Debug)
- errs() << "Expansion at line " << NextESV->getLine() << ", "
- << NextESV->getStartCol() << " -> " << NextESV->getEndCol()
- << "\n";
- NextESV->View->render(OS, false, NestedIndent);
+ renderViewDivider(OS, ViewDepth + 1);
+ ExpansionColumn = renderExpansionView(
+ OS, *NextESV,
+ RenderedSubView ? Optional<LineRef>({*LI, LI.line_number()})
+ : Optional<LineRef>(),
+ ExpansionColumn, WrappedSegment, LineSegments, ViewDepth);
RenderedSubView = true;
}
for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
- renderViewDivider(NestedIndent, DividerWidth, OS);
- OS << "\n";
- renderIndent(OS, NestedIndent);
- OS << ' ';
- NextISV->View->renderSourceName(OS);
- NextISV->View->render(OS, false, NestedIndent);
+ renderInstantiationView(OS, *NextISV, ViewDepth + 1);
RenderedSubView = true;
}
- if (RenderedSubView) {
- renderViewDivider(NestedIndent, DividerWidth, OS);
- OS << "\n";
- }
+ if (RenderedSubView)
+ renderViewDivider(OS, ViewDepth + 1);
}
}
-
-void SourceCoverageView::renderSourceName(raw_ostream &OS) {
- getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName()
- << ":\n";
-}