Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 1 | //===- CoverageSummary.cpp - Code coverage summary ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This class implements data management and rendering for the code coverage |
| 11 | // summaries of all files and functions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "CoverageSummary.h" |
| 16 | #include "llvm/Support/FileSystem.h" |
| 17 | #include "llvm/Support/Format.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | unsigned CoverageSummary::getFileID(StringRef Filename) { |
| 22 | for (unsigned I = 0, E = Filenames.size(); I < E; ++I) { |
| 23 | if (sys::fs::equivalent(Filenames[I], Filename)) |
| 24 | return I; |
| 25 | } |
| 26 | Filenames.push_back(Filename); |
| 27 | return Filenames.size() - 1; |
| 28 | } |
| 29 | |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 30 | void |
| 31 | CoverageSummary::createSummaries(ArrayRef<coverage::FunctionRecord> Functions) { |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 32 | std::vector<std::pair<unsigned, size_t>> FunctionFileIDs; |
| 33 | |
| 34 | FunctionFileIDs.resize(Functions.size()); |
| 35 | for (size_t I = 0, E = Functions.size(); I < E; ++I) { |
| 36 | StringRef Filename = Functions[I].Filenames[0]; |
| 37 | FunctionFileIDs[I] = std::make_pair(getFileID(Filename), I); |
| 38 | } |
| 39 | |
| 40 | // Sort the function records by file ids |
| 41 | std::sort(FunctionFileIDs.begin(), FunctionFileIDs.end(), |
| 42 | [](const std::pair<unsigned, size_t> &lhs, |
| 43 | const std::pair<unsigned, size_t> &rhs) { |
| 44 | return lhs.first < rhs.first; |
| 45 | }); |
| 46 | |
| 47 | // Create function summaries in a sorted order (by file ids) |
| 48 | FunctionSummaries.reserve(Functions.size()); |
| 49 | for (size_t I = 0, E = Functions.size(); I < E; ++I) |
| 50 | FunctionSummaries.push_back( |
| 51 | FunctionCoverageSummary::get(Functions[FunctionFileIDs[I].second])); |
| 52 | |
| 53 | // Create file summaries |
| 54 | size_t CurrentSummary = 0; |
| 55 | for (unsigned FileID = 0; FileID < Filenames.size(); ++FileID) { |
| 56 | // Gather the relevant functions summaries |
| 57 | auto PrevSummary = CurrentSummary; |
| 58 | while (CurrentSummary < FunctionSummaries.size() && |
| 59 | FunctionFileIDs[CurrentSummary].first == FileID) |
| 60 | ++CurrentSummary; |
| 61 | ArrayRef<FunctionCoverageSummary> LocalSummaries( |
| 62 | FunctionSummaries.data() + PrevSummary, |
| 63 | FunctionSummaries.data() + CurrentSummary); |
| 64 | if (LocalSummaries.empty()) |
| 65 | continue; |
| 66 | |
| 67 | FileSummaries.push_back( |
| 68 | FileCoverageSummary::get(Filenames[FileID], LocalSummaries)); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | FileCoverageSummary CoverageSummary::getCombinedFileSummaries() { |
| 73 | size_t NumRegions = 0, CoveredRegions = 0; |
| 74 | size_t NumLines = 0, NonCodeLines = 0, CoveredLines = 0; |
Alex Lorenz | cb1702d | 2014-09-30 12:45:13 +0000 | [diff] [blame^] | 75 | size_t NumFunctionsExecuted = 0, NumFunctions = 0; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 76 | for (const auto &File : FileSummaries) { |
| 77 | NumRegions += File.RegionCoverage.NumRegions; |
| 78 | CoveredRegions += File.RegionCoverage.Covered; |
| 79 | |
| 80 | NumLines += File.LineCoverage.NumLines; |
| 81 | NonCodeLines += File.LineCoverage.NonCodeLines; |
| 82 | CoveredLines += File.LineCoverage.Covered; |
| 83 | |
Alex Lorenz | cb1702d | 2014-09-30 12:45:13 +0000 | [diff] [blame^] | 84 | NumFunctionsExecuted += File.FunctionCoverage.Executed; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 85 | NumFunctions += File.FunctionCoverage.NumFunctions; |
| 86 | } |
| 87 | return FileCoverageSummary( |
| 88 | "TOTAL", RegionCoverageInfo(CoveredRegions, NumRegions), |
| 89 | LineCoverageInfo(CoveredLines, NonCodeLines, NumLines), |
Alex Lorenz | cb1702d | 2014-09-30 12:45:13 +0000 | [diff] [blame^] | 90 | FunctionCoverageInfo(NumFunctionsExecuted, NumFunctions), |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 91 | None); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 92 | } |