[llvm-cov] Add an API to prepare file reports (NFC)

It would be nice to prepare file reports (using the CoverageReport API)
without actually rendering them to the console. I plan on using this to
flesh out the 'index' files in the coverage views.

llvm-svn: 281009
diff --git a/llvm/tools/llvm-cov/CoverageReport.cpp b/llvm/tools/llvm-cov/CoverageReport.cpp
index 648ddf8..695e50f 100644
--- a/llvm/tools/llvm-cov/CoverageReport.cpp
+++ b/llvm/tools/llvm-cov/CoverageReport.cpp
@@ -133,7 +133,8 @@
 
 namespace llvm {
 
-void CoverageReport::render(const FileCoverageSummary &File, raw_ostream &OS) {
+void CoverageReport::render(const FileCoverageSummary &File,
+                            raw_ostream &OS) const {
   auto FileCoverageColor =
       determineCoveragePercentageColor(File.RegionCoverage);
   auto FuncCoverageColor =
@@ -169,7 +170,7 @@
 }
 
 void CoverageReport::render(const FunctionCoverageSummary &Function,
-                            raw_ostream &OS) {
+                            raw_ostream &OS) const {
   auto FuncCoverageColor =
       determineCoveragePercentageColor(Function.RegionCoverage);
   auto LineCoverageColor =
@@ -234,7 +235,34 @@
   }
 }
 
-void CoverageReport::renderFileReports(raw_ostream &OS) {
+std::vector<FileCoverageSummary>
+CoverageReport::prepareFileReports(FileCoverageSummary &Totals,
+                                   ArrayRef<StringRef> Files) const {
+  std::vector<FileCoverageSummary> FileReports;
+  unsigned LCP = 0;
+  if (Files.size() > 1)
+    LCP = getLongestCommonPrefixLen(Files);
+
+  for (StringRef Filename : Files) {
+    FileCoverageSummary Summary(Filename.drop_front(LCP));
+    for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
+      FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
+      Summary.addFunction(Function);
+      Totals.addFunction(Function);
+    }
+    FileReports.push_back(Summary);
+  }
+
+  return FileReports;
+}
+
+void CoverageReport::renderFileReports(raw_ostream &OS) const {
+  std::vector<StringRef> UniqueSourceFiles = Coverage.getUniqueSourceFiles();
+  renderFileReports(OS, UniqueSourceFiles);
+}
+
+void CoverageReport::renderFileReports(raw_ostream &OS,
+                                       ArrayRef<StringRef> Files) const {
   adjustColumnWidths(Coverage);
   OS << column("Filename", FileReportColumns[0])
      << column("Regions", FileReportColumns[1], Column::RightAlignment)
@@ -249,21 +277,11 @@
   renderDivider(FileReportColumns, OS);
   OS << "\n";
 
-  std::vector<StringRef> UniqueSourceFiles = Coverage.getUniqueSourceFiles();
-  unsigned LCP = 0;
-  if (UniqueSourceFiles.size() > 1)
-    LCP = getLongestCommonPrefixLen(UniqueSourceFiles);
-
   FileCoverageSummary Totals("TOTAL");
-  for (StringRef Filename : UniqueSourceFiles) {
-    FileCoverageSummary Summary(Filename.drop_front(LCP));
-    for (const auto &F : Coverage.getCoveredFunctions(Filename)) {
-      FunctionCoverageSummary Function = FunctionCoverageSummary::get(F);
-      Summary.addFunction(Function);
-      Totals.addFunction(Function);
-    }
-    render(Summary, OS);
-  }
+  auto FileReports = prepareFileReports(Totals, Files);
+  for (const FileCoverageSummary &FCS : FileReports)
+    render(FCS, OS);
+
   renderDivider(FileReportColumns, OS);
   OS << "\n";
   render(Totals, OS);