Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame^] | 1 | //===- CoverageSummaryInfo.cpp - Coverage summary for function/file -------===// |
| 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 | // These structures are used to represent code coverage metrics |
| 11 | // for functions/files. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "CoverageSummaryInfo.h" |
| 16 | |
| 17 | using namespace llvm; |
| 18 | using namespace coverage; |
| 19 | |
| 20 | FunctionCoverageSummary |
| 21 | FunctionCoverageSummary::get(const FunctionCoverageMapping &Function) { |
| 22 | // Compute the region coverage |
| 23 | size_t NumCodeRegions = 0, CoveredRegions = 0; |
| 24 | for (auto &Region : Function.MappingRegions) { |
| 25 | if (Region.Kind != CounterMappingRegion::CodeRegion) |
| 26 | continue; |
| 27 | ++NumCodeRegions; |
| 28 | if (Region.ExecutionCount != 0) |
| 29 | ++CoveredRegions; |
| 30 | } |
| 31 | |
| 32 | // Compute the line coverage |
| 33 | size_t NumLines = 0, CoveredLines = 0; |
| 34 | for (unsigned FileID = 0, E = Function.Filenames.size(); FileID < E; |
| 35 | ++FileID) { |
| 36 | // Find the line start and end of the function's source code |
| 37 | // in that particular file |
| 38 | unsigned LineStart = std::numeric_limits<unsigned>::max(); |
| 39 | unsigned LineEnd = 0; |
| 40 | for (auto &Region : Function.MappingRegions) { |
| 41 | if (Region.FileID != FileID) |
| 42 | continue; |
| 43 | LineStart = std::min(LineStart, Region.LineStart); |
| 44 | LineEnd = std::max(LineEnd, Region.LineEnd); |
| 45 | } |
| 46 | unsigned LineCount = LineEnd - LineStart + 1; |
| 47 | |
| 48 | // Get counters |
| 49 | llvm::SmallVector<uint64_t, 16> ExecutionCounts; |
| 50 | ExecutionCounts.resize(LineCount, 0); |
| 51 | for (auto &Region : Function.MappingRegions) { |
| 52 | if (Region.FileID != FileID) |
| 53 | continue; |
| 54 | // Ignore the lines that were skipped by the preprocessor. |
| 55 | auto ExecutionCount = Region.ExecutionCount; |
| 56 | if (Region.Kind == MappingRegion::SkippedRegion) { |
| 57 | LineCount -= Region.LineEnd - Region.LineStart + 1; |
| 58 | ExecutionCount = 1; |
| 59 | } |
| 60 | for (unsigned I = Region.LineStart; I <= Region.LineEnd; ++I) |
| 61 | ExecutionCounts[I - LineStart] = ExecutionCount; |
| 62 | } |
| 63 | CoveredLines += LineCount - std::count(ExecutionCounts.begin(), |
| 64 | ExecutionCounts.end(), 0); |
| 65 | NumLines += LineCount; |
| 66 | } |
| 67 | return FunctionCoverageSummary( |
| 68 | Function.PrettyName, RegionCoverageInfo(CoveredRegions, NumCodeRegions), |
| 69 | LineCoverageInfo(CoveredLines, 0, NumLines)); |
| 70 | } |
| 71 | |
| 72 | FileCoverageSummary |
| 73 | FileCoverageSummary::get(StringRef Name, |
| 74 | ArrayRef<FunctionCoverageSummary> FunctionSummaries) { |
| 75 | size_t NumRegions = 0, CoveredRegions = 0; |
| 76 | size_t NumLines = 0, NonCodeLines = 0, CoveredLines = 0; |
| 77 | size_t NumFunctionsCovered = 0; |
| 78 | for (const auto &Func : FunctionSummaries) { |
| 79 | CoveredRegions += Func.RegionCoverage.Covered; |
| 80 | NumRegions += Func.RegionCoverage.NumRegions; |
| 81 | |
| 82 | CoveredLines += Func.LineCoverage.Covered; |
| 83 | NonCodeLines += Func.LineCoverage.NonCodeLines; |
| 84 | NumLines += Func.LineCoverage.NumLines; |
| 85 | |
| 86 | if (Func.RegionCoverage.isFullyCovered()) |
| 87 | ++NumFunctionsCovered; |
| 88 | } |
| 89 | |
| 90 | return FileCoverageSummary( |
| 91 | Name, RegionCoverageInfo(CoveredRegions, NumRegions), |
| 92 | LineCoverageInfo(CoveredLines, NonCodeLines, NumLines), |
| 93 | FunctionCoverageInfo(NumFunctionsCovered, FunctionSummaries.size()), |
| 94 | FunctionSummaries); |
| 95 | } |