diff --git a/llvm/docs/CommandGuide/llvm-cov.rst b/llvm/docs/CommandGuide/llvm-cov.rst --- a/llvm/docs/CommandGuide/llvm-cov.rst +++ b/llvm/docs/CommandGuide/llvm-cov.rst @@ -249,6 +249,12 @@ line, but show the individual regions if there are multiple on the line. Defaults to false. +.. option:: -show-directory-coverage + + Generate index files in each level of the directory to show the coverage. + Defaults to false and a single index file will be generated in the output + directory (which can be very large and impossible to read for large projects). + .. option:: -use-color Enable or disable color output. By default this is autodetected. diff --git a/llvm/test/tools/llvm-cov/Inputs/directory_coverage/a0/a1/a2.cc b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/a0/a1/a2.cc new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/a0/a1/a2.cc @@ -0,0 +1,7 @@ +#include "../../header.h" + +template<> +bool equal(int a, int b) +{ + return a == b; +} diff --git a/llvm/test/tools/llvm-cov/Inputs/directory_coverage/b/b1.h b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/b/b1.h new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/b/b1.h @@ -0,0 +1,3 @@ +int add(int a, int b); + +int sub(int a, int b); diff --git a/llvm/test/tools/llvm-cov/Inputs/directory_coverage/b/b1_1.cc b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/b/b1_1.cc new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/b/b1_1.cc @@ -0,0 +1,5 @@ +#include "b1.h" + +int add(int a, int b) { + return a + b; +} diff --git a/llvm/test/tools/llvm-cov/Inputs/directory_coverage/b/b1_2.cc b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/b/b1_2.cc new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/b/b1_2.cc @@ -0,0 +1,5 @@ +#include "b1.h" + +int sub(int a, int b) { + return a - b; +} diff --git a/llvm/test/tools/llvm-cov/Inputs/directory_coverage/c0/c1/c2.h b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/c0/c1/c2.h new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/c0/c1/c2.h @@ -0,0 +1,9 @@ +int mul(int a, int b); + +int div(int a, int b); + +#ifdef DEF +int div(int a, int b) { + return a / b; +} +#endif diff --git a/llvm/test/tools/llvm-cov/Inputs/directory_coverage/c0/c1/c2_1.cc b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/c0/c1/c2_1.cc new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/c0/c1/c2_1.cc @@ -0,0 +1,3 @@ +int mul(int a, int b) { + return a * b; +} diff --git a/llvm/test/tools/llvm-cov/Inputs/directory_coverage/c0/c1/c2_2.cc b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/c0/c1/c2_2.cc new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/c0/c1/c2_2.cc @@ -0,0 +1,2 @@ +#define DEF +#include "c2.h" diff --git a/llvm/test/tools/llvm-cov/Inputs/directory_coverage/header.h b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/header.h new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/header.h @@ -0,0 +1,5 @@ +#include "b/b1.h" +#include "c0/c1/c2.h" + +template +bool equal(T a, T b); diff --git a/llvm/test/tools/llvm-cov/Inputs/directory_coverage/main.cc b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/main.cc new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-cov/Inputs/directory_coverage/main.cc @@ -0,0 +1,9 @@ +#include "header.h" + +int main() { + int a = 3; + int b = 4; + int c = c = add(a, b); + int d = mul(a, b); + return equal(a, sub(c, b)) - equal(a, div(d, b)); +} diff --git a/llvm/test/tools/llvm-cov/directory_coverage.test b/llvm/test/tools/llvm-cov/directory_coverage.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-cov/directory_coverage.test @@ -0,0 +1,37 @@ +// RUN: mkdir -p %t && clang -fprofile-instr-generate=%t/default.profraw -fcoverage-mapping -o %t/main.out \ +// RUN: %S/Inputs/directory_coverage/main.cc \ +// RUN: %S/Inputs/directory_coverage/a0/a1/a2.cc \ +// RUN: %S/Inputs/directory_coverage/b/b1_1.cc \ +// RUN: %S/Inputs/directory_coverage/b/b1_2.cc \ +// RUN: %S/Inputs/directory_coverage/c0/c1/c2_1.cc \ +// RUN: %S/Inputs/directory_coverage/c0/c1/c2_2.cc +// RUN: %t/main.out +// RUN: llvm-profdata merge %t/default.profraw -o %t/profdata +// RUN: llvm-cov show %t/main.out --instr-profile %t/profdata --format=html --show-directory-coverage -o %t/report +// RUN: cat %t/report/index.html | FileCheck %s --check-prefix=ROOT --allow-empty +// RUN: cat %t/report/coverage/%:S/Inputs/directory_coverage/index.html | FileCheck %s --check-prefix=TOP +// RUN: cat %t/report/coverage/%:S/Inputs/directory_coverage/b/index.html | FileCheck %s --check-prefix=B +// RUN: cat %t/report/coverage/%:S/Inputs/directory_coverage/c0/c1/index.html | FileCheck %s --check-prefix=C1 + +// ROOT: coverage{{.*}}Inputs{{/|\\}}directory_coverage{{/|\\}}index.html + + +// TOP: a0{{/|\\}}a1{{/|\\}}a2.cc +// TOP: b{{/|\\}} +// TOP-NOT: b1_1.cc +// TOP-NOT: b1_2.cc +// TOP: c0{{/|\\}}c1{{/|\\}} +// TOP-NOT: c2_1.cc +// TOP-NOT: b2_2.cc +// TOP: main.cc + + +// B: href={{"|'}}..{{/|\\}}index.html{{"|'}} +// B: b1_1.cc +// B: b1_2.cc + + +// C1: href={{"|'}}..{{/|\\}}..{{/|\\}}index.html{{"|'}} +// C1: c0{{/|\\}}c1 +// C1: c2.h +// C1: c2_1.cc diff --git a/llvm/tools/llvm-cov/CodeCoverage.cpp b/llvm/tools/llvm-cov/CodeCoverage.cpp --- a/llvm/tools/llvm-cov/CodeCoverage.cpp +++ b/llvm/tools/llvm-cov/CodeCoverage.cpp @@ -971,6 +971,10 @@ cl::desc("Show function instantiations"), cl::init(true), cl::cat(ViewCategory)); + cl::opt ShowDirectoryCoverage("show-directory-coverage", cl::Optional, + cl::desc("Show directory coverage"), + cl::cat(ViewCategory)); + cl::opt ShowOutputDirectory( "output-dir", cl::init(""), cl::desc("Directory in which coverage information is written out")); @@ -1051,6 +1055,7 @@ ViewOpts.ShowBranchPercents = ShowBranches == CoverageViewOptions::BranchOutputType::Percent; ViewOpts.ShowFunctionInstantiations = ShowInstantiations; + ViewOpts.ShowDirectoryCoverage = ShowDirectoryCoverage; ViewOpts.ShowOutputDirectory = ShowOutputDirectory; ViewOpts.TabSize = TabSize; ViewOpts.ProjectTitle = ProjectTitle; diff --git a/llvm/tools/llvm-cov/CoverageViewOptions.h b/llvm/tools/llvm-cov/CoverageViewOptions.h --- a/llvm/tools/llvm-cov/CoverageViewOptions.h +++ b/llvm/tools/llvm-cov/CoverageViewOptions.h @@ -38,6 +38,7 @@ bool ShowBranchSummary; bool ShowRegionSummary; bool ShowInstantiationSummary; + bool ShowDirectoryCoverage; bool ExportSummaryOnly; bool SkipExpansions; bool SkipFunctions; diff --git a/llvm/tools/llvm-cov/SourceCoverageView.h b/llvm/tools/llvm-cov/SourceCoverageView.h --- a/llvm/tools/llvm-cov/SourceCoverageView.h +++ b/llvm/tools/llvm-cov/SourceCoverageView.h @@ -99,7 +99,7 @@ CoveragePrinter(const CoverageViewOptions &Opts) : Opts(Opts) {} /// Return `OutputDir/ToplevelDir/Path.Extension`. If \p InToplevel is - /// false, skip the ToplevelDir component. If \p Relative is false, skip the + /// true, skip the ToplevelDir component. If \p Relative is true, skip the /// OutputDir component. std::string getOutputPath(StringRef Path, StringRef Extension, bool InToplevel, bool Relative = true) const; diff --git a/llvm/tools/llvm-cov/SourceCoverageView.cpp b/llvm/tools/llvm-cov/SourceCoverageView.cpp --- a/llvm/tools/llvm-cov/SourceCoverageView.cpp +++ b/llvm/tools/llvm-cov/SourceCoverageView.cpp @@ -78,6 +78,8 @@ case CoverageViewOptions::OutputFormat::Text: return std::make_unique(Opts); case CoverageViewOptions::OutputFormat::HTML: + if (Opts.ShowDirectoryCoverage) + return std::make_unique(Opts); return std::make_unique(Opts); case CoverageViewOptions::OutputFormat::Lcov: // Unreachable because CodeCoverage.cpp should terminate with an error diff --git a/llvm/tools/llvm-cov/SourceCoverageViewHTML.h b/llvm/tools/llvm-cov/SourceCoverageViewHTML.h --- a/llvm/tools/llvm-cov/SourceCoverageViewHTML.h +++ b/llvm/tools/llvm-cov/SourceCoverageViewHTML.h @@ -19,6 +19,8 @@ using namespace coverage; +class ThreadPool; + struct FileCoverageSummary; /// A coverage printer for html output. @@ -36,6 +38,10 @@ CoveragePrinterHTML(const CoverageViewOptions &Opts) : CoveragePrinter(Opts) {} +protected: + Error emitStyleSheet(); + void emitBeforeTable(raw_ostream &OSRef, const std::string &Title); + private: void emitFileSummary(raw_ostream &OS, StringRef SF, const FileCoverageSummary &FCS, @@ -44,6 +50,39 @@ const FileCoverageSummary &FCS) const; }; +/// A coverage printer for html output, but generates index files in every +/// subdirectory to show a hierarchical view. +class CoveragePrinterHTMLDirectory : public CoveragePrinterHTML { +public: + using CoveragePrinterHTML::CoveragePrinterHTML; + + Error createIndexFile(ArrayRef SourceFiles, + const coverage::CoverageMapping &Coverage, + const CoverageFiltersMatchAll &Filters) override; + +private: + // Member variables for avoiding passing arguments repeatedly in recursive + // calls. + const coverage::CoverageMapping *CoverageNow; + const CoverageFiltersMatchAll *FiltersNow; + + ThreadPool *PoolNow; // For calling CoverageReport::prepareSingleFileReport + // asynchronously in createSubIndexFile. + std::vector LCPStack; // For calling createSubIndexFile + // recursively. [0] is root LCP length. + + // Filter out files in LCP directory, group others by subdirectory and call + // recursively on them. The size of \p Files must be greater than 0. The + // FileCoverageSummary of this directory will be added to \p Totals. + // Current LCP is LCPStack.back(). + Error createSubIndexFile(const ArrayRef &Files, + FileCoverageSummary *Totals); + + std::string buildTitleLinks(StringRef LCPath) const; + + std::string buildRelLinkToFile(StringRef RelPath, bool IsDir) const; +}; + /// A code coverage view which supports html-based rendering. class SourceCoverageViewHTML : public SourceCoverageView { void renderViewHeader(raw_ostream &OS) override; diff --git a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp --- a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp +++ b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp @@ -10,12 +10,15 @@ /// //===----------------------------------------------------------------------===// -#include "CoverageReport.h" #include "SourceCoverageViewHTML.h" +#include "CoverageReport.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/Format.h" #include "llvm/Support/Path.h" +#include "llvm/Support/ThreadPool.h" +#include +#include #include using namespace llvm; @@ -65,7 +68,7 @@ } const char *BeginHeader = - "" + "" "" ""; @@ -272,11 +275,96 @@ OS << EndHeader << ""; } +void emitTableRow(raw_ostream &OS, const CoverageViewOptions &Opts, + const std::string &FirstCol, const FileCoverageSummary &FCS, + bool IsTotals) { + SmallVector Columns; + + // Format a coverage triple and add the result to the list of columns. + auto AddCoverageTripleToColumn = + [&Columns, &Opts](unsigned Hit, unsigned Total, float Pctg) { + std::string S; + { + raw_string_ostream RSO{S}; + if (Total) + RSO << format("%*.2f", 7, Pctg) << "% "; + else + RSO << "- "; + RSO << '(' << Hit << '/' << Total << ')'; + } + const char *CellClass = "column-entry-yellow"; + if (Pctg >= Opts.HighCovWatermark) + CellClass = "column-entry-green"; + else if (Pctg < Opts.LowCovWatermark) + CellClass = "column-entry-red"; + Columns.emplace_back(tag("td", tag("pre", S), CellClass)); + }; + + Columns.emplace_back(tag("td", tag("pre", FirstCol))); + AddCoverageTripleToColumn(FCS.FunctionCoverage.getExecuted(), + FCS.FunctionCoverage.getNumFunctions(), + FCS.FunctionCoverage.getPercentCovered()); + if (Opts.ShowInstantiationSummary) + AddCoverageTripleToColumn(FCS.InstantiationCoverage.getExecuted(), + FCS.InstantiationCoverage.getNumFunctions(), + FCS.InstantiationCoverage.getPercentCovered()); + AddCoverageTripleToColumn(FCS.LineCoverage.getCovered(), + FCS.LineCoverage.getNumLines(), + FCS.LineCoverage.getPercentCovered()); + if (Opts.ShowRegionSummary) + AddCoverageTripleToColumn(FCS.RegionCoverage.getCovered(), + FCS.RegionCoverage.getNumRegions(), + FCS.RegionCoverage.getPercentCovered()); + if (Opts.ShowBranchSummary) + AddCoverageTripleToColumn(FCS.BranchCoverage.getCovered(), + FCS.BranchCoverage.getNumBranches(), + FCS.BranchCoverage.getPercentCovered()); + + if (IsTotals) + OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row-bold"); + else + OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row"); +} + void emitEpilog(raw_ostream &OS) { OS << "" << ""; } +/// Determine the length of the longest redundant prefix of the substrs starts +/// from \p LCP in \p Paths. \p Paths can't be empty. If there's only one path +/// in \p Paths, the length of the path is returned. Note this is differnet from +/// the behavior for the function of the same name in CoverageReport.cpp . +unsigned getRedundantPrefixLen(ArrayRef Paths, unsigned LCP = 0) { + assert(!Paths.empty() && "Paths must have at least one element"); + + auto Iter = Paths.begin(); + auto IterE = Paths.end(); + auto First = Iter->substr(LCP); + SmallVector CommonComponents{sys::path::begin(First), + sys::path::end(First)}; + unsigned Len = First.size() - LCP; + while (++Iter != IterE) { + auto Substr = Iter->substr(LCP); + auto I = sys::path::begin(Substr); + auto E = sys::path::end(Substr); + + unsigned Index = 0; + while (I != E) { + if (Index >= CommonComponents.size()) + break; + if (CommonComponents[Index] != *I) { + CommonComponents.resize(Index); + Len = I - sys::path::begin(Substr); + break; + } + ++Index, ++I; + } + } + + return Len; +} + } // anonymous namespace Expected @@ -330,33 +418,44 @@ return a(LinkTarget, LinkText); } +Error CoveragePrinterHTML::emitStyleSheet() { + auto CSSOrErr = createOutputStream("style", "css", /*InToplevel=*/true); + if (Error E = CSSOrErr.takeError()) + return E; + + OwnedStream CSS = std::move(CSSOrErr.get()); + CSS->operator<<(CSSForCoverage); + + return Error::success(); +} + +void CoveragePrinterHTML::emitBeforeTable(raw_ostream &OSRef, + const std::string &Title) { + // Emit some basic information about the coverage report. + if (Opts.hasProjectTitle()) + OSRef << tag(ProjectTitleTag, escape(Opts.ProjectTitle, Opts)); + OSRef << tag(ReportTitleTag, Title); + if (Opts.hasCreatedTime()) + OSRef << tag(CreatedTimeTag, escape(Opts.CreatedTimeStr, Opts)); + + // Emit a link to some documentation. + OSRef << tag("p", "Click " + + a("http://clang.llvm.org/docs/" + "SourceBasedCodeCoverage.html#interpreting-reports", + "here") + + " for information about interpreting this report."); + + // Emit a table containing links to reports for each file in the covmapping. + // Exclude files which don't contain any regions. + OSRef << BeginCenteredDiv << BeginTable; + emitColumnLabelsForIndex(OSRef, Opts); +} + /// Render a file coverage summary (\p FCS) in a table row. If \p IsTotals is /// false, link the summary to \p SF. void CoveragePrinterHTML::emitFileSummary(raw_ostream &OS, StringRef SF, const FileCoverageSummary &FCS, bool IsTotals) const { - SmallVector Columns; - - // Format a coverage triple and add the result to the list of columns. - auto AddCoverageTripleToColumn = - [&Columns, this](unsigned Hit, unsigned Total, float Pctg) { - std::string S; - { - raw_string_ostream RSO{S}; - if (Total) - RSO << format("%*.2f", 7, Pctg) << "% "; - else - RSO << "- "; - RSO << '(' << Hit << '/' << Total << ')'; - } - const char *CellClass = "column-entry-yellow"; - if (Pctg >= Opts.HighCovWatermark) - CellClass = "column-entry-green"; - else if (Pctg < Opts.LowCovWatermark) - CellClass = "column-entry-red"; - Columns.emplace_back(tag("td", tag("pre", S), CellClass)); - }; - // Simplify the display file path, and wrap it in a link if requested. std::string Filename; if (IsTotals) { @@ -365,43 +464,16 @@ Filename = buildLinkToFile(SF, FCS); } - Columns.emplace_back(tag("td", tag("pre", Filename))); - AddCoverageTripleToColumn(FCS.FunctionCoverage.getExecuted(), - FCS.FunctionCoverage.getNumFunctions(), - FCS.FunctionCoverage.getPercentCovered()); - if (Opts.ShowInstantiationSummary) - AddCoverageTripleToColumn(FCS.InstantiationCoverage.getExecuted(), - FCS.InstantiationCoverage.getNumFunctions(), - FCS.InstantiationCoverage.getPercentCovered()); - AddCoverageTripleToColumn(FCS.LineCoverage.getCovered(), - FCS.LineCoverage.getNumLines(), - FCS.LineCoverage.getPercentCovered()); - if (Opts.ShowRegionSummary) - AddCoverageTripleToColumn(FCS.RegionCoverage.getCovered(), - FCS.RegionCoverage.getNumRegions(), - FCS.RegionCoverage.getPercentCovered()); - if (Opts.ShowBranchSummary) - AddCoverageTripleToColumn(FCS.BranchCoverage.getCovered(), - FCS.BranchCoverage.getNumBranches(), - FCS.BranchCoverage.getPercentCovered()); - - if (IsTotals) - OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row-bold"); - else - OS << tag("tr", join(Columns.begin(), Columns.end(), ""), "light-row"); + emitTableRow(OS, Opts, Filename, FCS, IsTotals); } Error CoveragePrinterHTML::createIndexFile( ArrayRef SourceFiles, const CoverageMapping &Coverage, const CoverageFiltersMatchAll &Filters) { // Emit the default stylesheet. - auto CSSOrErr = createOutputStream("style", "css", /*InToplevel=*/true); - if (Error E = CSSOrErr.takeError()) + if (Error E = emitStyleSheet()) return E; - OwnedStream CSS = std::move(CSSOrErr.get()); - CSS->operator<<(CSSForCoverage); - // Emit a file index along with some coverage statistics. auto OSOrErr = createOutputStream("index", "html", /*InToplevel=*/true); if (Error E = OSOrErr.takeError()) @@ -412,24 +484,8 @@ assert(Opts.hasOutputDirectory() && "No output directory for index file"); emitPrelude(OSRef, Opts, getPathToStyle("")); - // Emit some basic information about the coverage report. - if (Opts.hasProjectTitle()) - OSRef << tag(ProjectTitleTag, escape(Opts.ProjectTitle, Opts)); - OSRef << tag(ReportTitleTag, "Coverage Report"); - if (Opts.hasCreatedTime()) - OSRef << tag(CreatedTimeTag, escape(Opts.CreatedTimeStr, Opts)); + emitBeforeTable(OSRef, "Coverage Report"); - // Emit a link to some documentation. - OSRef << tag("p", "Click " + - a("http://clang.llvm.org/docs/" - "SourceBasedCodeCoverage.html#interpreting-reports", - "here") + - " for information about interpreting this report."); - - // Emit a table containing links to reports for each file in the covmapping. - // Exclude files which don't contain any regions. - OSRef << BeginCenteredDiv << BeginTable; - emitColumnLabelsForIndex(OSRef, Opts); FileCoverageSummary Totals("TOTALS"); auto FileReports = CoverageReport::prepareFileReports( Coverage, Totals, SourceFiles, Opts, Filters); @@ -465,6 +521,232 @@ return Error::success(); } +Error CoveragePrinterHTMLDirectory::createIndexFile( + ArrayRef SourceFiles, const CoverageMapping &Coverage, + const CoverageFiltersMatchAll &Filters) { + // The createSubIndexFile function only works when SourceFiles is + // not empty. So we fallback to CoveragePrinterHTML when it is. + if (SourceFiles.empty()) + return CoveragePrinterHTML::createIndexFile(SourceFiles, Coverage, Filters); + + // Emit the default stylesheet. + if (Error E = emitStyleSheet()) + return E; + + // Emit index files in every subdirectory. + std::vector Files(SourceFiles.begin(), SourceFiles.end()); + ThreadPoolStrategy PoolS = hardware_concurrency(Opts.NumThreads); + if (Opts.NumThreads == 0) { + PoolS = heavyweight_hardware_concurrency(Files.size()); + PoolS.Limit = true; + } + ThreadPool Pool(PoolS); // The above initialization code was copied from + // CoverageReport::prepareFileReports. + + unsigned RootLCP; + if (SourceFiles.size() == 1) + RootLCP = sys::path::parent_path(SourceFiles.front()).size() + 1; + // We handle this case specially. If there's only one file in total, + // its parent directory is considered as the LCP. + else + RootLCP = getRedundantPrefixLen(Files); + auto LCPath = SourceFiles.front().substr(0, RootLCP); + + CoverageNow = &Coverage; + FiltersNow = &Filters; + PoolNow = &Pool; + LCPStack = {RootLCP}; + FileCoverageSummary RootTotal({}); // The root total is useless and ignored. + if (auto E = createSubIndexFile(Files, &RootTotal)) + return E; + + // Emit the top level index file. Top level index file is just a redirection + // to the index file in the LCP directory. + auto OSOrErr = createOutputStream("index", "html", /*InToplevel=*/true); + if (auto E = OSOrErr.takeError()) + return E; + auto OS = std::move(OSOrErr.get()); + auto LCPIndexFilePath = + getOutputPath(LCPath + "index", "html", /*InToplevel=*/false); + *OS.get() << R"( + + + + + + + )"; + + return Error::success(); +} + +Error CoveragePrinterHTMLDirectory::createSubIndexFile( + const ArrayRef &Files, FileCoverageSummary *Totals) { + assert(!Files.empty() && "Only works when Files is not empty"); + auto LCP = LCPStack.back(); + auto LCPath = Files.front().substr(0, LCP).str(); + assert(LCPath.back() == '/' && "LCPath should end with a slash"); + auto OSOrErr = + createOutputStream(LCPath + "index", "html", /*InToplevel=*/false); + if (auto E = OSOrErr.takeError()) + return E; + auto OS = std::move(OSOrErr.get()); + raw_ostream &OSRef = *OS.get(); + + assert(Opts.hasOutputDirectory() && "No output directory for index file"); + auto IndexHtmlPath = + getOutputPath(LCPath + "index", "html", /*InToplevel=*/false); + emitPrelude(OSRef, Opts, getPathToStyle(IndexHtmlPath)); + + auto NavLink = buildTitleLinks(LCPath); + emitBeforeTable(OSRef, "Coverage Report (" + NavLink + ")"); + + // Filter out files in current directory and group others by subdiretory. + // std::map is used to keep entries in order. + std::map CurrentFiles; + std::map> CurrentDirs; + for (auto &&File : Files) { + auto SubPath = File.substr(LCPath.size()); + + auto I = sys::path::begin(SubPath); + auto E = sys::path::end(SubPath); + assert(I != E && "Such case should have been filtered out in the caller"); + + auto Name = *I; + if (++I == E) { + auto Iter = CurrentFiles.emplace(Name, File).first; + PoolNow->async(&CoverageReport::prepareSingleFileReport, File, + CoverageNow, Opts, LCP, &Iter->second, FiltersNow); + } + + else + CurrentDirs[Name].push_back(File); + } + + // Emit subdirectories first to make them at the top of the table. + FileCoverageSummary CurrentTotals(LCPath); + for (auto &&Dir : CurrentDirs) { + if (Dir.second.size() == 1) { + // If there's only one file in that subdirectory, we don't bother to + // create index file for it. Instead, we show the file report directly at + // this level. + auto RelPath = Dir.second.front().substr(LCP); + FileCoverageSummary FCS(RelPath); + CoverageReport::prepareSingleFileReport(Dir.second[0], CoverageNow, Opts, + LCP, &FCS, FiltersNow); + emitTableRow(OSRef, Opts, buildRelLinkToFile(RelPath, /*IsDir=*/false), + FCS, /*IsTotals=*/false); + CurrentTotals += FCS; + } else { + auto SubDirLCP = getRedundantPrefixLen(Dir.second, LCP); + auto RelPath = Dir.second.front().substr(LCP, SubDirLCP); + FileCoverageSummary FCS(RelPath); + LCPStack.push_back(LCP + SubDirLCP); + if (auto E = createSubIndexFile(Dir.second, &FCS)) + return E; + emitTableRow(OSRef, Opts, buildRelLinkToFile(RelPath, /*IsDir=*/true), + FCS, /*IsTotals=*/false); + CurrentTotals += FCS; + } + } + + // Then emit the files in current directory. + PoolNow->wait(); // Makes files reporting overlap with subdir reporting. + std::vector EmptyFiles; + for (auto I = CurrentFiles.begin(), E = CurrentFiles.end(); I != E; ++I) { + if (I->second.FunctionCoverage.getNumFunctions()) + emitTableRow(OSRef, Opts, buildRelLinkToFile(I->first, /*IsDir=*/false), + I->second, /*IsTotals=*/false); + else + EmptyFiles.push_back(I); + CurrentTotals += I->second; + } + + // Emit the totals row. + emitTableRow(OSRef, Opts, "Totals", CurrentTotals, /*IsTotals=*/false); + OSRef << EndTable << EndCenteredDiv; + + // Emit links to files which don't contain any functions. These are normally + // not very useful, but could be relevant for code which abuses the + // preprocessor. + if (!EmptyFiles.empty() && FiltersNow->empty()) { + OSRef << tag("p", "Files which contain no functions. (These " + "files contain code pulled into other files " + "by the preprocessor.)\n"); + OSRef << BeginCenteredDiv << BeginTable; + for (auto I : EmptyFiles) { + auto Link = buildRelLinkToFile(I->first, /*IsDir=*/false); + OSRef << tag("tr", tag("td", tag("pre", Link)), "light-row") << '\n'; + } + OSRef << EndTable << EndCenteredDiv; + } + + // Emit epilog. + OSRef << tag("h5", escape(Opts.getLLVMVersionString(), Opts)); + emitEpilog(OSRef); + + *Totals += CurrentTotals; + LCPStack.pop_back(); + return Error::success(); +} + +std::string +CoveragePrinterHTMLDirectory::buildTitleLinks(StringRef LCPath) const { + SmallVector, 16> Components; + auto Iter = LCPStack.begin(), IterE = LCPStack.end(); + if (*Iter > 0) + Components.push_back({ + LCPath.substr(0, *Iter - 1).str(), // Remove trailing slash. + 0, + }); + for (auto Last = *Iter; ++Iter != IterE; Last = *Iter) { + auto SubPath = LCPath.substr(Last, *Iter - Last - 1); + auto Level = unsigned(SubPath.count(sys::path::get_separator())) + 1; + Components.back().second += Level; + Components.push_back({SubPath.str(), Level}); + } + + std::string S; + for (auto I = Components.begin(), E = Components.end();;) { + auto &Name = I->first; + if (++I == E) { + S += a("./index.html", Name); + S += sys::path::get_separator(); + break; + } + + std::string Link; + for (unsigned J = I->second; J > 0; --J) { + Link += ".."; + Link += sys::path::get_separator(); + } + Link += "index.html"; + S += a(Link, Name); + S += sys::path::get_separator(); + } + return S; +} + +std::string CoveragePrinterHTMLDirectory::buildRelLinkToFile(StringRef RelPath, + bool IsDir) const { + SmallString<128> LinkTextStr(RelPath); + sys::path::remove_dots(LinkTextStr, /*remove_dot_dot=*/true); + sys::path::native(LinkTextStr); + + std::string LinkTargetStr; + if (IsDir) { + LinkTextStr += sys::path::get_separator(); + LinkTargetStr = (RelPath + "index.html").str(); + } else { + LinkTargetStr = (RelPath + ".html").str(); + } + + auto LinkText = escape(LinkTextStr, Opts); + auto LinkTarget = escape(LinkTargetStr, Opts); + return a(LinkTarget, LinkText); +} + void SourceCoverageViewHTML::renderViewHeader(raw_ostream &OS) { OS << BeginCenteredDiv << BeginTable; } diff --git a/llvm/utils/prepare-code-coverage-artifact.py b/llvm/utils/prepare-code-coverage-artifact.py --- a/llvm/utils/prepare-code-coverage-artifact.py +++ b/llvm/utils/prepare-code-coverage-artifact.py @@ -67,6 +67,7 @@ "-o", report_dir, "-show-line-counts-or-regions", + "-show-directory-coverage", "-Xdemangler", "c++filt", "-Xdemangler",