1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
//===- SourceCoverageViewText.cpp - A text-based code coverage view -------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file This file implements the text-based coverage renderer.
///
//===----------------------------------------------------------------------===//
#include "SourceCoverageViewText.h"
#include "CoverageReport.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Format.h"
using namespace llvm;
Expected<CoveragePrinter::OwnedStream>
CoveragePrinterText::createViewFile(StringRef Path, bool InToplevel) {
return createOutputStream(Path, "txt", InToplevel);
}
void CoveragePrinterText::closeViewFile(OwnedStream OS) {
OS->operator<<('\n');
}
Error CoveragePrinterText::createIndexFile(
ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage,
const CoverageFiltersMatchAll &Filters) {
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();
CoverageReport Report(Opts, Coverage);
Report.renderFileReports(OSRef, SourceFiles, Filters);
Opts.colored_ostream(OSRef, raw_ostream::CYAN) << "\n"
<< Opts.getLLVMVersionString();
return Error::success();
}
namespace {
static const unsigned LineCoverageColumnWidth = 7;
static const unsigned LineNumberColumnWidth = 5;
/// Get the width of the leading columns.
unsigned getCombinedColumnWidth(const CoverageViewOptions &Opts) {
return (Opts.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
(Opts.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
}
/// 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
void SourceCoverageViewText::renderViewHeader(raw_ostream &) {}
void SourceCoverageViewText::renderViewFooter(raw_ostream &) {}
void SourceCoverageViewText::renderSourceName(raw_ostream &OS, bool WholeFile) {
getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName()
<< ":\n";
}
void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS,
unsigned ViewDepth) {
for (unsigned I = 0; I < ViewDepth; ++I)
OS << " |";
}
void SourceCoverageViewText::renderLineSuffix(raw_ostream &, unsigned) {}
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';
}
void SourceCoverageViewText::renderLine(raw_ostream &OS, LineRef L,
const LineCoverageStats &LCS,
unsigned ExpansionCol,
unsigned ViewDepth) {
StringRef Line = L.Line;
unsigned LineNumber = L.LineNo;
auto *WrappedSegment = LCS.getWrappedSegment();
CoverageSegmentArray Segments = LCS.getLineSegments();
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->IsGapRegion &&
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 ((!S->IsGapRegion || (Highlight && *Highlight == raw_ostream::RED)) &&
S->HasCount && S->Count == 0)
Highlight = raw_ostream::RED;
else if (Col == ExpansionCol)
Highlight = raw_ostream::CYAN;
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;
}
std::string C = formatCount(Line.getExecutionCount());
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 << '|';
}
void SourceCoverageViewText::renderRegionMarkers(raw_ostream &OS,
const LineCoverageStats &Line,
unsigned ViewDepth) {
renderLinePrefix(OS, ViewDepth);
OS.indent(getCombinedColumnWidth(getOptions()));
CoverageSegmentArray Segments = Line.getLineSegments();
// Just consider the segments which start *and* end on this line.
if (Segments.size() > 1)
Segments = Segments.drop_back();
unsigned PrevColumn = 1;
for (const auto *S : Segments) {
if (!S->IsRegionEntry)
continue;
if (S->Count == Line.getExecutionCount())
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;
if (getOptions().Debug)
errs() << "Marker at " << S->Line << ":" << S->Col << " = "
<< formatCount(S->Count) << "\n";
}
OS << '\n';
}
void SourceCoverageViewText::renderExpansionSite(raw_ostream &OS, LineRef L,
const LineCoverageStats &LCS,
unsigned ExpansionCol,
unsigned ViewDepth) {
renderLinePrefix(OS, ViewDepth);
OS.indent(getCombinedColumnWidth(getOptions()) + (ViewDepth == 0 ? 0 : 1));
renderLine(OS, L, LCS, ExpansionCol, ViewDepth);
}
void SourceCoverageViewText::renderExpansionView(raw_ostream &OS,
ExpansionView &ESV,
unsigned ViewDepth) {
// 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,
/*ShowTitle=*/false, ViewDepth + 1);
}
void SourceCoverageViewText::renderBranchView(raw_ostream &OS, BranchView &BRV,
unsigned ViewDepth) {
// Render the child subview.
if (getOptions().Debug)
errs() << "Branch at line " << BRV.getLine() << '\n';
for (const auto &R : BRV.Regions) {
double TruePercent = 0.0;
double FalsePercent = 0.0;
unsigned Total = R.ExecutionCount + R.FalseExecutionCount;
if (!getOptions().ShowBranchCounts && Total != 0) {
TruePercent = ((double)(R.ExecutionCount) / (double)Total) * 100.0;
FalsePercent = ((double)(R.FalseExecutionCount) / (double)Total) * 100.0;
}
renderLinePrefix(OS, ViewDepth);
OS << " Branch (" << R.LineStart << ":" << R.ColumnStart << "): [";
if (R.Folded) {
OS << "Folded - Ignored]\n";
continue;
}
colored_ostream(OS, raw_ostream::RED,
getOptions().Colors && !R.ExecutionCount,
/*Bold=*/false, /*BG=*/true)
<< "True";
if (getOptions().ShowBranchCounts)
OS << ": " << formatCount(R.ExecutionCount) << ", ";
else
OS << ": " << format("%0.2f", TruePercent) << "%, ";
colored_ostream(OS, raw_ostream::RED,
getOptions().Colors && !R.FalseExecutionCount,
/*Bold=*/false, /*BG=*/true)
<< "False";
if (getOptions().ShowBranchCounts)
OS << ": " << formatCount(R.FalseExecutionCount);
else
OS << ": " << format("%0.2f", FalsePercent) << "%";
OS << "]\n";
}
}
void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS,
InstantiationView &ISV,
unsigned ViewDepth) {
renderLinePrefix(OS, ViewDepth);
OS << ' ';
if (!ISV.View)
getOptions().colored_ostream(OS, raw_ostream::RED)
<< "Unexecuted instantiation: " << ISV.FunctionName << "\n";
else
ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true,
/*ShowTitle=*/false, ViewDepth);
}
void SourceCoverageViewText::renderTitle(raw_ostream &OS, StringRef Title) {
if (getOptions().hasProjectTitle())
getOptions().colored_ostream(OS, raw_ostream::CYAN)
<< getOptions().ProjectTitle << "\n";
getOptions().colored_ostream(OS, raw_ostream::CYAN) << Title << "\n";
if (getOptions().hasCreatedTime())
getOptions().colored_ostream(OS, raw_ostream::CYAN)
<< getOptions().CreatedTimeStr << "\n";
}
void SourceCoverageViewText::renderTableHeader(raw_ostream &, unsigned,
unsigned) {}
|