aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
diff options
context:
space:
mode:
authorYuhao Gu <yhgu2000@outlook.com>2023-08-24 13:12:04 +0800
committerYuhao Gu <yhgu2000@outlook.com>2023-08-24 13:46:12 +0800
commitbea39c5443612b638aa1cc56d36e3b1fd06f6e96 (patch)
tree63bdf728f2bd4c184f7bbf3b9239e53945815829 /llvm/tools/llvm-cov/SourceCoverageViewText.cpp
parent7a41af86041bd757b7f380d7f645403d4e1725ca (diff)
downloadllvm-bea39c5443612b638aa1cc56d36e3b1fd06f6e96.zip
llvm-bea39c5443612b638aa1cc56d36e3b1fd06f6e96.tar.gz
llvm-bea39c5443612b638aa1cc56d36e3b1fd06f6e96.tar.bz2
[llvm-cov] Support directory layout in coverage reports
This is a GSoC 2023 project ([discourse link](https://discourse.llvm.org/t/coverage-support-a-hierarchical-directory-structure-in-generated-coverage-html-reports/68239)). llvm-cov currently generates a single top-level index HTML file, which causes rendering scalability issues in large projects. This patch adds support for hierarchical directory structure into the HTML reports to solve scalability issues by introducing the following changes: - Added a new command line option `--show-directory-coverage` for `llvm-cov show`. It works both for `--format=html` and `--format=text`. - Two new classes: `CoveragePrinterHTMLDirectory` and `CoveragePrinterTextDirectory` was added to support the new option. - A tool class `DirectoryCoverageReport` was added to support the two classes above. - Updated the document. - Added a new regression test for `--show-directory-coverage`. Reviewed By: phosek, gulfem Differential Revision: https://reviews.llvm.org/D151283
Diffstat (limited to 'llvm/tools/llvm-cov/SourceCoverageViewText.cpp')
-rw-r--r--llvm/tools/llvm-cov/SourceCoverageViewText.cpp65
1 files changed, 65 insertions, 0 deletions
diff --git a/llvm/tools/llvm-cov/SourceCoverageViewText.cpp b/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
index 6e0db09..44694a0 100644
--- a/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
+++ b/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
@@ -14,7 +14,9 @@
#include "CoverageReport.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
+#include "llvm/Support/Path.h"
#include <optional>
using namespace llvm;
@@ -46,6 +48,69 @@ Error CoveragePrinterText::createIndexFile(
return Error::success();
}
+struct CoveragePrinterTextDirectory::Reporter : public DirectoryCoverageReport {
+ CoveragePrinterTextDirectory &Printer;
+
+ Reporter(CoveragePrinterTextDirectory &Printer,
+ const coverage::CoverageMapping &Coverage,
+ const CoverageFiltersMatchAll &Filters)
+ : DirectoryCoverageReport(Printer.Opts, Coverage, Filters),
+ Printer(Printer) {}
+
+ Error generateSubDirectoryReport(SubFileReports &&SubFiles,
+ SubDirReports &&SubDirs,
+ FileCoverageSummary &&SubTotals) override {
+ auto &LCPath = SubTotals.Name;
+ assert(Options.hasOutputDirectory() &&
+ "No output directory for index file");
+
+ SmallString<128> OSPath = LCPath;
+ sys::path::append(OSPath, "index");
+ auto OSOrErr = Printer.createOutputStream(OSPath, "txt",
+ /*InToplevel=*/false);
+ if (auto E = OSOrErr.takeError())
+ return E;
+ auto OS = std::move(OSOrErr.get());
+ raw_ostream &OSRef = *OS.get();
+
+ std::vector<FileCoverageSummary> Reports;
+ for (auto &&SubDir : SubDirs)
+ Reports.push_back(std::move(SubDir.second.first));
+ for (auto &&SubFile : SubFiles)
+ Reports.push_back(std::move(SubFile.second));
+
+ CoverageReport Report(Options, Coverage);
+ Report.renderFileReports(OSRef, Reports, SubTotals, Filters.empty());
+
+ Options.colored_ostream(OSRef, raw_ostream::CYAN)
+ << "\n"
+ << Options.getLLVMVersionString();
+
+ return Error::success();
+ }
+};
+
+Error CoveragePrinterTextDirectory::createIndexFile(
+ ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage,
+ const CoverageFiltersMatchAll &Filters) {
+ if (SourceFiles.size() <= 1)
+ return CoveragePrinterText::createIndexFile(SourceFiles, Coverage, Filters);
+
+ Reporter Report(*this, Coverage, Filters);
+ auto TotalsOrErr = Report.prepareDirectoryReports(SourceFiles);
+ if (auto E = TotalsOrErr.takeError())
+ return E;
+ auto &LCPath = TotalsOrErr->Name;
+
+ auto TopIndexFilePath =
+ getOutputPath("index", "txt", /*InToplevel=*/true, /*Relative=*/false);
+ auto LCPIndexFilePath =
+ getOutputPath((LCPath + "index").str(), "txt", /*InToplevel=*/false,
+ /*Relative=*/false);
+ return errorCodeToError(
+ sys::fs::copy_file(LCPIndexFilePath, TopIndexFilePath));
+}
+
namespace {
static const unsigned LineCoverageColumnWidth = 7;