From: Vedant Kumar Date: Fri, 9 Sep 2016 01:32:49 +0000 (+0000) Subject: [llvm-cov] Add an API to prepare file reports (NFC) X-Git-Tag: llvmorg-4.0.0-rc1~10262 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=627887b65078e57eaa8a40cb8db047ea56758045;p=platform%2Fupstream%2Fllvm.git [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 @@ unsigned getLongestCommonPrefixLen(ArrayRef 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 Files, } } -void CoverageReport::renderFileReports(raw_ostream &OS) { +std::vector +CoverageReport::prepareFileReports(FileCoverageSummary &Totals, + ArrayRef Files) const { + std::vector 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 UniqueSourceFiles = Coverage.getUniqueSourceFiles(); + renderFileReports(OS, UniqueSourceFiles); +} + +void CoverageReport::renderFileReports(raw_ostream &OS, + ArrayRef 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 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); diff --git a/llvm/tools/llvm-cov/CoverageReport.h b/llvm/tools/llvm-cov/CoverageReport.h index 8d6a20e..7a06954 100644 --- a/llvm/tools/llvm-cov/CoverageReport.h +++ b/llvm/tools/llvm-cov/CoverageReport.h @@ -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 Files, raw_ostream &OS); - void renderFileReports(raw_ostream &OS); + /// Prepare file reports for the files specified in \p Files. + std::vector + prepareFileReports(FileCoverageSummary &Totals, + ArrayRef 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 Files) const; }; } // end namespace llvm