aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2023-12-18 20:30:12 +0000
committerDimitry Andric <dim@FreeBSD.org>2024-04-06 20:11:55 +0000
commit5f757f3ff9144b609b3c433dfd370cc6bdc191ad (patch)
tree1b4e980b866cd26a00af34c0a653eb640bd09caf /contrib/llvm-project/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
parent3e1c8a35f741a5d114d0ba670b15191355711fe9 (diff)
parent312c0ed19cc5276a17bacf2120097bec4515b0f1 (diff)
Diffstat (limited to 'contrib/llvm-project/llvm/tools/llvm-cov/SourceCoverageViewText.cpp')
-rw-r--r--contrib/llvm-project/llvm/tools/llvm-cov/SourceCoverageViewText.cpp120
1 files changed, 119 insertions, 1 deletions
diff --git a/contrib/llvm-project/llvm/tools/llvm-cov/SourceCoverageViewText.cpp b/contrib/llvm-project/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
index 40d4359e375c..73b7ffe16a96 100644
--- a/contrib/llvm-project/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
+++ b/contrib/llvm-project/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;
@@ -270,6 +337,57 @@ void SourceCoverageViewText::renderBranchView(raw_ostream &OS, BranchView &BRV,
}
}
+void SourceCoverageViewText::renderMCDCView(raw_ostream &OS, MCDCView &MRV,
+ unsigned ViewDepth) {
+ for (auto &Record : MRV.Records) {
+ renderLinePrefix(OS, ViewDepth);
+ OS << "---> MC/DC Decision Region (";
+ // Display Line + Column information.
+ const CounterMappingRegion &DecisionRegion = Record.getDecisionRegion();
+ OS << DecisionRegion.LineStart << ":";
+ OS << DecisionRegion.ColumnStart << ") to (";
+ OS << DecisionRegion.LineEnd << ":";
+ OS << DecisionRegion.ColumnEnd << ")\n";
+ renderLinePrefix(OS, ViewDepth);
+ OS << "\n";
+
+ // Display MC/DC Information.
+ renderLinePrefix(OS, ViewDepth);
+ OS << " Number of Conditions: " << Record.getNumConditions() << "\n";
+ for (unsigned i = 0; i < Record.getNumConditions(); i++) {
+ renderLinePrefix(OS, ViewDepth);
+ OS << " " << Record.getConditionHeaderString(i);
+ }
+ renderLinePrefix(OS, ViewDepth);
+ OS << "\n";
+ renderLinePrefix(OS, ViewDepth);
+ OS << " Executed MC/DC Test Vectors:\n";
+ renderLinePrefix(OS, ViewDepth);
+ OS << "\n";
+ renderLinePrefix(OS, ViewDepth);
+ OS << " ";
+ OS << Record.getTestVectorHeaderString();
+ for (unsigned i = 0; i < Record.getNumTestVectors(); i++) {
+ renderLinePrefix(OS, ViewDepth);
+ OS << Record.getTestVectorString(i);
+ }
+ renderLinePrefix(OS, ViewDepth);
+ OS << "\n";
+ for (unsigned i = 0; i < Record.getNumConditions(); i++) {
+ renderLinePrefix(OS, ViewDepth);
+ OS << Record.getConditionCoverageString(i);
+ }
+ renderLinePrefix(OS, ViewDepth);
+ OS << " MC/DC Coverage for Decision: ";
+ colored_ostream(OS, raw_ostream::RED,
+ getOptions().Colors && Record.getPercentCovered() < 100.0,
+ /*Bold=*/false, /*BG=*/true)
+ << format("%0.2f", Record.getPercentCovered()) << "%\n";
+ renderLinePrefix(OS, ViewDepth);
+ OS << "\n";
+ }
+}
+
void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS,
InstantiationView &ISV,
unsigned ViewDepth) {