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 |
Justin Bogner | 953e240 | 2014-09-20 15:31:56 +0000 | [diff] [blame] | 21 | FunctionCoverageSummary::get(const coverage::FunctionRecord &Function) { |
Ying Yi | 78db64e | 2016-07-22 09:20:21 +0000 | [diff] [blame] | 22 | // Compute the region coverage. |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 23 | size_t NumCodeRegions = 0, CoveredRegions = 0; |
Justin Bogner | 8e8aa3f | 2014-09-09 05:32:14 +0000 | [diff] [blame] | 24 | for (auto &CR : Function.CountedRegions) { |
| 25 | if (CR.Kind != CounterMappingRegion::CodeRegion) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 26 | continue; |
| 27 | ++NumCodeRegions; |
Justin Bogner | 8e8aa3f | 2014-09-09 05:32:14 +0000 | [diff] [blame] | 28 | if (CR.ExecutionCount != 0) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 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; |
Justin Bogner | 8e8aa3f | 2014-09-09 05:32:14 +0000 | [diff] [blame] | 40 | for (auto &CR : Function.CountedRegions) { |
| 41 | if (CR.FileID != FileID) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 42 | continue; |
Justin Bogner | 8e8aa3f | 2014-09-09 05:32:14 +0000 | [diff] [blame] | 43 | LineStart = std::min(LineStart, CR.LineStart); |
| 44 | LineEnd = std::max(LineEnd, CR.LineEnd); |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 45 | } |
| 46 | unsigned LineCount = LineEnd - LineStart + 1; |
| 47 | |
| 48 | // Get counters |
| 49 | llvm::SmallVector<uint64_t, 16> ExecutionCounts; |
| 50 | ExecutionCounts.resize(LineCount, 0); |
Justin Bogner | 8e8aa3f | 2014-09-09 05:32:14 +0000 | [diff] [blame] | 51 | for (auto &CR : Function.CountedRegions) { |
| 52 | if (CR.FileID != FileID) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 53 | continue; |
| 54 | // Ignore the lines that were skipped by the preprocessor. |
Justin Bogner | 8e8aa3f | 2014-09-09 05:32:14 +0000 | [diff] [blame] | 55 | auto ExecutionCount = CR.ExecutionCount; |
| 56 | if (CR.Kind == CounterMappingRegion::SkippedRegion) { |
| 57 | LineCount -= CR.LineEnd - CR.LineStart + 1; |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 58 | ExecutionCount = 1; |
| 59 | } |
Justin Bogner | 8e8aa3f | 2014-09-09 05:32:14 +0000 | [diff] [blame] | 60 | for (unsigned I = CR.LineStart; I <= CR.LineEnd; ++I) |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 61 | ExecutionCounts[I - LineStart] = ExecutionCount; |
| 62 | } |
| 63 | CoveredLines += LineCount - std::count(ExecutionCounts.begin(), |
| 64 | ExecutionCounts.end(), 0); |
| 65 | NumLines += LineCount; |
| 66 | } |
| 67 | return FunctionCoverageSummary( |
Alex Lorenz | cb1702d | 2014-09-30 12:45:13 +0000 | [diff] [blame] | 68 | Function.Name, Function.ExecutionCount, |
| 69 | RegionCoverageInfo(CoveredRegions, NumCodeRegions), |
Alex Lorenz | e82d89c | 2014-08-22 22:56:03 +0000 | [diff] [blame] | 70 | LineCoverageInfo(CoveredLines, 0, NumLines)); |
| 71 | } |