[llvm-cov] Create an index of reports in -output-dir mode

This index lists the reports available in the 'coverage' sub-directory.
This will help navigate coverage output from large projects.

This commit factors the file creation code out of SourceCoverageView and
into CoveragePrinter.

llvm-svn: 274029
diff --git a/llvm/tools/llvm-cov/CodeCoverage.cpp b/llvm/tools/llvm-cov/CodeCoverage.cpp
index 240acff..f2f9716 100644
--- a/llvm/tools/llvm-cov/CodeCoverage.cpp
+++ b/llvm/tools/llvm-cov/CodeCoverage.cpp
@@ -448,6 +448,8 @@
   if (!Coverage)
     return 1;
 
+  auto Printer = CoveragePrinter::create(ViewOpts);
+
   if (!Filters.empty()) {
     // Show functions
     for (const auto &Function : Coverage->getCoveredFunctions()) {
@@ -462,15 +464,14 @@
         continue;
       }
 
-      auto OSOrErr =
-          mainView->createOutputFile("functions", /*InToplevel=*/true);
+      auto OSOrErr = Printer->createViewFile("functions", /*InToplevel=*/true);
       if (Error E = OSOrErr.takeError()) {
         error(toString(std::move(E)));
         return 1;
       }
       auto OS = std::move(OSOrErr.get());
       mainView->print(*OS.get(), /*WholeFile=*/false, /*ShowSourceName=*/true);
-      mainView->closeOutputFile(std::move(OS));
+      Printer->closeViewFile(std::move(OS));
     }
     return 0;
   }
@@ -483,6 +484,14 @@
     for (StringRef Filename : Coverage->getUniqueSourceFiles())
       SourceFiles.push_back(Filename);
 
+  // Create an index out of the source files.
+  if (ViewOpts.hasOutputDirectory()) {
+    if (Error E = Printer->createIndexFile(SourceFiles)) {
+      error(toString(std::move(E)));
+      return 1;
+    }
+  }
+
   for (const auto &SourceFile : SourceFiles) {
     auto mainView = createSourceFileView(SourceFile, *Coverage);
     if (!mainView) {
@@ -492,7 +501,7 @@
       continue;
     }
 
-    auto OSOrErr = mainView->createOutputFile(SourceFile, /*InToplevel=*/false);
+    auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false);
     if (Error E = OSOrErr.takeError()) {
       error(toString(std::move(E)));
       return 1;
@@ -500,7 +509,7 @@
     auto OS = std::move(OSOrErr.get());
     mainView->print(*OS.get(), /*Wholefile=*/true,
                     /*ShowSourceName=*/ShowFilenames);
-    mainView->closeOutputFile(std::move(OS));
+    Printer->closeViewFile(std::move(OS));
   }
 
   return 0;