aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2023-12-09 13:28:42 +0000
committerDimitry Andric <dim@FreeBSD.org>2023-12-09 13:28:42 +0000
commitb1c73532ee8997fe5dfbeb7d223027bdf99758a0 (patch)
tree7d6e51c294ab6719475d660217aa0c0ad0526292 /llvm/tools/llvm-cov/SourceCoverageViewText.cpp
parent7fa27ce4a07f19b07799a767fc29416f3b625afb (diff)
Diffstat (limited to 'llvm/tools/llvm-cov/SourceCoverageViewText.cpp')
-rw-r--r--llvm/tools/llvm-cov/SourceCoverageViewText.cpp69
1 files changed, 68 insertions, 1 deletions
diff --git a/llvm/tools/llvm-cov/SourceCoverageViewText.cpp b/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
index 40d4359e375c..44694a0426c8 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;
@@ -232,7 +297,9 @@ void SourceCoverageViewText::renderBranchView(raw_ostream &OS, BranchView &BRV,
for (const auto &R : BRV.Regions) {
double TruePercent = 0.0;
double FalsePercent = 0.0;
- unsigned Total = R.ExecutionCount + R.FalseExecutionCount;
+ // FIXME: It may overflow when the data is too large, but I have not
+ // encountered it in actual use, and not sure whether to use __uint128_t.
+ uint64_t Total = R.ExecutionCount + R.FalseExecutionCount;
if (!getOptions().ShowBranchCounts && Total != 0) {
TruePercent = ((double)(R.ExecutionCount) / (double)Total) * 100.0;