2014-08-23 00:56:03 +02:00
|
|
|
//===- SourceCoverageView.cpp - Code coverage view for source code --------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// 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
|
2014-08-23 00:56:03 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2016-06-25 07:48:54 +02:00
|
|
|
///
|
|
|
|
/// \file This class implements rendering for code coverage of source code.
|
|
|
|
///
|
2014-08-23 00:56:03 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SourceCoverageView.h"
|
2016-07-06 23:44:05 +02:00
|
|
|
#include "SourceCoverageViewHTML.h"
|
2016-06-25 04:58:30 +02:00
|
|
|
#include "SourceCoverageViewText.h"
|
2014-08-23 00:56:03 +02:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2015-05-14 00:41:48 +02:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2016-06-28 04:09:39 +02:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2014-08-23 00:56:03 +02:00
|
|
|
#include "llvm/Support/LineIterator.h"
|
2016-06-28 04:09:39 +02:00
|
|
|
#include "llvm/Support/Path.h"
|
2014-08-23 00:56:03 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-06-28 18:12:24 +02:00
|
|
|
void CoveragePrinter::StreamDestructor::operator()(raw_ostream *OS) const {
|
2016-06-28 04:09:39 +02:00
|
|
|
if (OS == &outs())
|
|
|
|
return;
|
|
|
|
delete OS;
|
|
|
|
}
|
|
|
|
|
2016-06-28 18:12:24 +02:00
|
|
|
std::string CoveragePrinter::getOutputPath(StringRef Path, StringRef Extension,
|
2016-09-09 03:32:51 +02:00
|
|
|
bool InToplevel,
|
|
|
|
bool Relative) const {
|
2018-12-20 01:57:06 +01:00
|
|
|
assert(!Extension.empty() && "The file extension may not be empty");
|
2016-06-28 04:09:39 +02:00
|
|
|
|
2016-06-29 23:55:46 +02:00
|
|
|
SmallString<256> FullPath;
|
|
|
|
|
|
|
|
if (!Relative)
|
|
|
|
FullPath.append(Opts.ShowOutputDirectory);
|
|
|
|
|
2016-06-28 18:12:24 +02:00
|
|
|
if (!InToplevel)
|
|
|
|
sys::path::append(FullPath, getCoverageDir());
|
2016-06-28 04:09:39 +02:00
|
|
|
|
2016-06-29 18:22:12 +02:00
|
|
|
SmallString<256> ParentPath = sys::path::parent_path(Path);
|
|
|
|
sys::path::remove_dots(ParentPath, /*remove_dot_dots=*/true);
|
|
|
|
sys::path::append(FullPath, sys::path::relative_path(ParentPath));
|
2016-06-28 04:09:39 +02:00
|
|
|
|
|
|
|
auto PathFilename = (sys::path::filename(Path) + "." + Extension).str();
|
|
|
|
sys::path::append(FullPath, PathFilename);
|
2016-08-30 09:01:37 +02:00
|
|
|
sys::path::native(FullPath);
|
2016-06-28 04:09:39 +02:00
|
|
|
|
2020-01-28 20:23:46 +01:00
|
|
|
return std::string(FullPath.str());
|
2016-06-28 04:09:39 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 18:12:24 +02:00
|
|
|
Expected<CoveragePrinter::OwnedStream>
|
|
|
|
CoveragePrinter::createOutputStream(StringRef Path, StringRef Extension,
|
2016-09-09 03:32:51 +02:00
|
|
|
bool InToplevel) const {
|
2016-06-28 18:12:12 +02:00
|
|
|
if (!Opts.hasOutputDirectory())
|
2016-06-28 04:09:39 +02:00
|
|
|
return OwnedStream(&outs());
|
|
|
|
|
2016-06-29 23:55:46 +02:00
|
|
|
std::string FullPath = getOutputPath(Path, Extension, InToplevel, false);
|
2016-06-28 18:12:24 +02:00
|
|
|
|
|
|
|
auto ParentDir = sys::path::parent_path(FullPath);
|
|
|
|
if (auto E = sys::fs::create_directories(ParentDir))
|
|
|
|
return errorCodeToError(E);
|
|
|
|
|
|
|
|
std::error_code E;
|
2018-06-07 21:58:58 +02:00
|
|
|
raw_ostream *RawStream =
|
|
|
|
new raw_fd_ostream(FullPath, E, sys::fs::FA_Read | sys::fs::FA_Write);
|
2016-06-28 18:12:24 +02:00
|
|
|
auto OS = CoveragePrinter::OwnedStream(RawStream);
|
|
|
|
if (E)
|
|
|
|
return errorCodeToError(E);
|
2020-02-10 16:06:45 +01:00
|
|
|
return std::move(OS);
|
2016-06-28 04:09:39 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 18:12:24 +02:00
|
|
|
std::unique_ptr<CoveragePrinter>
|
|
|
|
CoveragePrinter::create(const CoverageViewOptions &Opts) {
|
|
|
|
switch (Opts.Format) {
|
|
|
|
case CoverageViewOptions::OutputFormat::Text:
|
2019-08-15 17:54:37 +02:00
|
|
|
return std::make_unique<CoveragePrinterText>(Opts);
|
2016-07-06 23:44:05 +02:00
|
|
|
case CoverageViewOptions::OutputFormat::HTML:
|
2019-08-15 17:54:37 +02:00
|
|
|
return std::make_unique<CoveragePrinterHTML>(Opts);
|
2018-11-09 17:10:44 +01:00
|
|
|
case CoverageViewOptions::OutputFormat::Lcov:
|
|
|
|
// Unreachable because CodeCoverage.cpp should terminate with an error
|
|
|
|
// before we get here.
|
|
|
|
llvm_unreachable("Lcov format is not supported!");
|
2016-06-28 18:12:24 +02:00
|
|
|
}
|
2016-06-28 23:02:41 +02:00
|
|
|
llvm_unreachable("Unknown coverage output format!");
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 21:31:18 +02:00
|
|
|
unsigned SourceCoverageView::getFirstUncoveredLineNo() {
|
2017-10-19 01:58:27 +02:00
|
|
|
const auto MinSegIt = find_if(CoverageInfo, [](const CoverageSegment &S) {
|
|
|
|
return S.HasCount && S.Count == 0;
|
|
|
|
});
|
2017-09-19 01:37:27 +02:00
|
|
|
|
2016-09-06 21:31:18 +02:00
|
|
|
// There is no uncovered line, return zero.
|
2017-09-19 01:37:27 +02:00
|
|
|
if (MinSegIt == CoverageInfo.end())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (*MinSegIt).Line;
|
2016-09-06 21:31:18 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 18:12:24 +02:00
|
|
|
std::string SourceCoverageView::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 00:56:03 +02:00
|
|
|
}
|
|
|
|
|
2016-06-29 02:38:21 +02:00
|
|
|
bool SourceCoverageView::shouldRenderRegionMarkers(
|
2017-11-09 03:33:44 +01:00
|
|
|
const LineCoverageStats &LCS) const {
|
2017-09-08 20:44:46 +02:00
|
|
|
if (!getOptions().ShowRegionMarkers)
|
|
|
|
return false;
|
|
|
|
|
2017-11-09 03:33:44 +01:00
|
|
|
CoverageSegmentArray Segments = LCS.getLineSegments();
|
|
|
|
if (Segments.empty())
|
|
|
|
return false;
|
|
|
|
for (unsigned I = 0, E = Segments.size() - 1; I < E; ++I) {
|
|
|
|
const auto *CurSeg = Segments[I];
|
|
|
|
if (!CurSeg->IsRegionEntry || CurSeg->Count == LCS.getExecutionCount())
|
|
|
|
continue;
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-08 20:44:46 +02:00
|
|
|
return false;
|
2016-06-29 02:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SourceCoverageView::hasSubViews() const {
|
2020-12-28 18:20:48 +01:00
|
|
|
return !ExpansionSubViews.empty() || !InstantiationSubViews.empty() ||
|
|
|
|
!BranchSubViews.empty();
|
2016-06-29 02:38:21 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 04:58:30 +02:00
|
|
|
std::unique_ptr<SourceCoverageView>
|
|
|
|
SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
|
|
|
|
const CoverageViewOptions &Options,
|
2017-10-19 01:58:27 +02:00
|
|
|
CoverageData &&CoverageInfo) {
|
2016-06-28 18:12:15 +02:00
|
|
|
switch (Options.Format) {
|
2016-06-28 02:15:54 +02:00
|
|
|
case CoverageViewOptions::OutputFormat::Text:
|
2019-08-15 17:54:37 +02:00
|
|
|
return std::make_unique<SourceCoverageViewText>(
|
2016-09-08 02:56:48 +02:00
|
|
|
SourceName, File, Options, std::move(CoverageInfo));
|
2016-07-06 23:44:05 +02:00
|
|
|
case CoverageViewOptions::OutputFormat::HTML:
|
2019-08-15 17:54:37 +02:00
|
|
|
return std::make_unique<SourceCoverageViewHTML>(
|
2016-09-08 02:56:48 +02:00
|
|
|
SourceName, File, Options, std::move(CoverageInfo));
|
2018-11-09 17:10:44 +01:00
|
|
|
case CoverageViewOptions::OutputFormat::Lcov:
|
|
|
|
// Unreachable because CodeCoverage.cpp should terminate with an error
|
|
|
|
// before we get here.
|
|
|
|
llvm_unreachable("Lcov format is not supported!");
|
2016-06-28 02:15:54 +02:00
|
|
|
}
|
2016-06-28 14:55:35 +02:00
|
|
|
llvm_unreachable("Unknown coverage output format!");
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
|
|
|
|
2016-09-08 02:56:48 +02:00
|
|
|
std::string SourceCoverageView::getSourceName() const {
|
|
|
|
SmallString<128> SourceText(SourceName);
|
2016-09-06 23:41:38 +02:00
|
|
|
sys::path::remove_dots(SourceText, /*remove_dot_dots=*/true);
|
|
|
|
sys::path::native(SourceText);
|
2020-01-28 20:23:46 +01:00
|
|
|
return std::string(SourceText.str());
|
2016-09-08 02:56:48 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 18:12:24 +02:00
|
|
|
void SourceCoverageView::addExpansion(
|
2017-10-19 01:58:27 +02:00
|
|
|
const CounterMappingRegion &Region,
|
2016-06-28 18:12:24 +02:00
|
|
|
std::unique_ptr<SourceCoverageView> View) {
|
|
|
|
ExpansionSubViews.emplace_back(Region, std::move(View));
|
|
|
|
}
|
|
|
|
|
2020-12-28 18:20:48 +01:00
|
|
|
void SourceCoverageView::addBranch(unsigned Line,
|
|
|
|
ArrayRef<CountedRegion> Regions,
|
|
|
|
std::unique_ptr<SourceCoverageView> View) {
|
|
|
|
BranchSubViews.emplace_back(Line, Regions, std::move(View));
|
|
|
|
}
|
|
|
|
|
2016-06-28 18:12:24 +02:00
|
|
|
void SourceCoverageView::addInstantiation(
|
|
|
|
StringRef FunctionName, unsigned Line,
|
|
|
|
std::unique_ptr<SourceCoverageView> View) {
|
|
|
|
InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
|
|
|
|
}
|
|
|
|
|
2016-06-25 04:58:30 +02:00
|
|
|
void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
|
2017-09-28 12:07:30 +02:00
|
|
|
bool ShowSourceName, bool ShowTitle,
|
|
|
|
unsigned ViewDepth) {
|
|
|
|
if (ShowTitle)
|
2016-09-15 06:45:59 +02:00
|
|
|
renderTitle(OS, "Coverage Report");
|
2014-08-23 00:56:03 +02:00
|
|
|
|
2016-06-29 02:38:21 +02:00
|
|
|
renderViewHeader(OS);
|
|
|
|
|
2016-08-24 16:27:23 +02:00
|
|
|
if (ShowSourceName)
|
2016-09-10 21:37:26 +02:00
|
|
|
renderSourceName(OS, WholeFile);
|
|
|
|
|
2016-09-15 08:49:13 +02:00
|
|
|
renderTableHeader(OS, (ViewDepth > 0) ? 0 : getFirstUncoveredLineNo(),
|
|
|
|
ViewDepth);
|
2016-08-24 16:27:23 +02:00
|
|
|
|
2020-12-28 18:20:48 +01:00
|
|
|
// We need the expansions, instantiations, and branches sorted so we can go
|
|
|
|
// through them while we iterate lines.
|
2019-04-22 17:53:43 +02:00
|
|
|
llvm::stable_sort(ExpansionSubViews);
|
|
|
|
llvm::stable_sort(InstantiationSubViews);
|
2020-12-28 18:20:48 +01:00
|
|
|
llvm::stable_sort(BranchSubViews);
|
2014-09-17 07:33:20 +02:00
|
|
|
auto NextESV = ExpansionSubViews.begin();
|
|
|
|
auto EndESV = ExpansionSubViews.end();
|
|
|
|
auto NextISV = InstantiationSubViews.begin();
|
|
|
|
auto EndISV = InstantiationSubViews.end();
|
2020-12-28 18:20:48 +01:00
|
|
|
auto NextBRV = BranchSubViews.begin();
|
|
|
|
auto EndBRV = BranchSubViews.end();
|
2014-09-17 07:33:20 +02:00
|
|
|
|
2014-09-17 20:23:47 +02:00
|
|
|
// Get the coverage information for the file.
|
2017-10-14 04:27:29 +02:00
|
|
|
auto StartSegment = CoverageInfo.begin();
|
2014-09-20 17:31:56 +02:00
|
|
|
auto EndSegment = CoverageInfo.end();
|
2017-10-14 04:27:29 +02:00
|
|
|
LineCoverageIterator LCI{CoverageInfo, 1};
|
|
|
|
LineCoverageIterator LCIEnd = LCI.getEnd();
|
2014-09-17 20:23:47 +02:00
|
|
|
|
2017-10-14 04:27:29 +02:00
|
|
|
unsigned FirstLine = StartSegment != EndSegment ? StartSegment->Line : 0;
|
|
|
|
for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof();
|
|
|
|
++LI, ++LCI) {
|
2014-09-17 20:23:47 +02:00
|
|
|
// If we aren't rendering the whole file, we need to filter out the prologue
|
|
|
|
// and epilogue.
|
|
|
|
if (!WholeFile) {
|
2017-10-14 04:27:29 +02:00
|
|
|
if (LCI == LCIEnd)
|
2014-09-17 20:23:47 +02:00
|
|
|
break;
|
2014-09-19 10:13:16 +02:00
|
|
|
else if (LI.line_number() < FirstLine)
|
2014-09-17 20:23:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-08-23 00:56:03 +02:00
|
|
|
|
2016-06-25 04:58:30 +02:00
|
|
|
renderLinePrefix(OS, ViewDepth);
|
2016-06-24 02:41:26 +02:00
|
|
|
if (getOptions().ShowLineNumbers)
|
2014-09-17 20:23:47 +02:00
|
|
|
renderLineNumberColumn(OS, LI.line_number());
|
2017-08-04 02:36:24 +02:00
|
|
|
|
2016-08-09 21:53:35 +02:00
|
|
|
if (getOptions().ShowLineStats)
|
2017-10-14 04:27:29 +02:00
|
|
|
renderLineCoverageColumn(OS, *LCI);
|
2014-08-23 00:56:03 +02:00
|
|
|
|
2014-09-17 20:23:47 +02:00
|
|
|
// If there are expansion subviews, we want to highlight the first one.
|
|
|
|
unsigned ExpansionColumn = 0;
|
|
|
|
if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
|
2016-06-24 02:41:26 +02:00
|
|
|
getOptions().Colors)
|
2014-09-17 20:23:47 +02:00
|
|
|
ExpansionColumn = NextESV->getStartCol();
|
2014-08-23 00:56:03 +02:00
|
|
|
|
|
|
|
// Display the source code for the current line.
|
2017-10-18 20:52:28 +02:00
|
|
|
renderLine(OS, {*LI, LI.line_number()}, *LCI, ExpansionColumn, ViewDepth);
|
2014-08-23 00:56:03 +02:00
|
|
|
|
|
|
|
// Show the region markers.
|
2017-11-09 03:33:44 +01:00
|
|
|
if (shouldRenderRegionMarkers(*LCI))
|
2017-10-18 20:52:28 +02:00
|
|
|
renderRegionMarkers(OS, *LCI, ViewDepth);
|
2014-08-23 00:56:03 +02:00
|
|
|
|
2020-12-28 18:20:48 +01:00
|
|
|
// Show the expansions, instantiations, and branches for this line.
|
2014-09-17 07:33:20 +02:00
|
|
|
bool RenderedSubView = false;
|
2014-09-17 20:23:47 +02:00
|
|
|
for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
|
|
|
|
++NextESV) {
|
2016-06-25 04:58:30 +02:00
|
|
|
renderViewDivider(OS, ViewDepth + 1);
|
2016-06-26 04:45:13 +02:00
|
|
|
|
|
|
|
// Re-render the current line and highlight the expansion range for
|
|
|
|
// this subview.
|
|
|
|
if (RenderedSubView) {
|
|
|
|
ExpansionColumn = NextESV->getStartCol();
|
2017-10-18 20:52:28 +02:00
|
|
|
renderExpansionSite(OS, {*LI, LI.line_number()}, *LCI, ExpansionColumn,
|
|
|
|
ViewDepth);
|
2016-06-26 04:45:13 +02:00
|
|
|
renderViewDivider(OS, ViewDepth + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderExpansionView(OS, *NextESV, ViewDepth + 1);
|
2014-09-17 07:33:20 +02:00
|
|
|
RenderedSubView = true;
|
|
|
|
}
|
2014-09-17 20:23:47 +02:00
|
|
|
for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
|
2016-06-26 04:45:13 +02:00
|
|
|
renderViewDivider(OS, ViewDepth + 1);
|
2016-06-25 04:58:30 +02:00
|
|
|
renderInstantiationView(OS, *NextISV, ViewDepth + 1);
|
2014-09-17 07:33:20 +02:00
|
|
|
RenderedSubView = true;
|
|
|
|
}
|
2020-12-28 18:20:48 +01:00
|
|
|
for (; NextBRV != EndBRV && NextBRV->Line == LI.line_number(); ++NextBRV) {
|
|
|
|
renderViewDivider(OS, ViewDepth + 1);
|
|
|
|
renderBranchView(OS, *NextBRV, ViewDepth + 1);
|
|
|
|
RenderedSubView = true;
|
|
|
|
}
|
2016-06-25 04:58:30 +02:00
|
|
|
if (RenderedSubView)
|
|
|
|
renderViewDivider(OS, ViewDepth + 1);
|
2016-06-29 02:38:21 +02:00
|
|
|
renderLineSuffix(OS, ViewDepth);
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|
2016-06-29 02:38:21 +02:00
|
|
|
|
|
|
|
renderViewFooter(OS);
|
2014-08-23 00:56:03 +02:00
|
|
|
}
|