[llvm-cov] Add an API to prepare file reports (NFC)
authorVedant Kumar <vsk@apple.com>
Fri, 9 Sep 2016 01:32:49 +0000 (01:32 +0000)
committerVedant Kumar <vsk@apple.com>
Fri, 9 Sep 2016 01:32:49 +0000 (01:32 +0000)
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

llvm/tools/llvm-cov/CoverageReport.cpp
llvm/tools/llvm-cov/CoverageReport.h

index 648ddf8..695e50f 100644 (file)
@@ -133,7 +133,8 @@ unsigned getLongestCommonPrefixLen(ArrayRef<StringRef> Strings) {
 
 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 FileCoverageSummary &File, raw_ostream &OS) {
 }
 
 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::renderFunctionReports(ArrayRef<StringRef> Files,
   }
 }
 
-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 @@ void CoverageReport::renderFileReports(raw_ostream &OS) {
   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);
index 8d6a20e..7a06954 100644 (file)
@@ -24,8 +24,8 @@ class CoverageReport {
   const CoverageViewOptions &Options;
   const coverage::CoverageMapping &Coverage;
 
-  void render(const FileCoverageSummary &File, raw_ostream &OS);
-  void render(const FunctionCoverageSummary &Function, raw_ostream &OS);
+  void render(const FileCoverageSummary &File, raw_ostream &OS) const;
+  void render(const FunctionCoverageSummary &Function, raw_ostream &OS) const;
 
 public:
   CoverageReport(const CoverageViewOptions &Options,
@@ -34,7 +34,16 @@ public:
 
   void renderFunctionReports(ArrayRef<StringRef> Files, raw_ostream &OS);
 
-  void renderFileReports(raw_ostream &OS);
+  /// Prepare file reports for the files specified in \p Files.
+  std::vector<FileCoverageSummary>
+  prepareFileReports(FileCoverageSummary &Totals,
+                     ArrayRef<StringRef> Files) const;
+
+  /// Render file reports for every unique file in the coverage mapping.
+  void renderFileReports(raw_ostream &OS) const;
+
+  /// Render file reports for the files specified in \p Files.
+  void renderFileReports(raw_ostream &OS, ArrayRef<StringRef> Files) const;
 };
 
 } // end namespace llvm