blob: ffdffbd4b7b4b383ed50725aa9d4c46334db6453 [file] [log] [blame]
Alex Lorenze82d89c2014-08-22 22:56:03 +00001//===- 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
17using namespace llvm;
18using namespace coverage;
19
20FunctionCoverageSummary
Justin Bogner953e2402014-09-20 15:31:56 +000021FunctionCoverageSummary::get(const coverage::FunctionRecord &Function) {
Ying Yi78db64e2016-07-22 09:20:21 +000022 // Compute the region coverage.
Alex Lorenze82d89c2014-08-22 22:56:03 +000023 size_t NumCodeRegions = 0, CoveredRegions = 0;
Justin Bogner8e8aa3f2014-09-09 05:32:14 +000024 for (auto &CR : Function.CountedRegions) {
25 if (CR.Kind != CounterMappingRegion::CodeRegion)
Alex Lorenze82d89c2014-08-22 22:56:03 +000026 continue;
27 ++NumCodeRegions;
Justin Bogner8e8aa3f2014-09-09 05:32:14 +000028 if (CR.ExecutionCount != 0)
Alex Lorenze82d89c2014-08-22 22:56:03 +000029 ++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 Bogner8e8aa3f2014-09-09 05:32:14 +000040 for (auto &CR : Function.CountedRegions) {
41 if (CR.FileID != FileID)
Alex Lorenze82d89c2014-08-22 22:56:03 +000042 continue;
Justin Bogner8e8aa3f2014-09-09 05:32:14 +000043 LineStart = std::min(LineStart, CR.LineStart);
44 LineEnd = std::max(LineEnd, CR.LineEnd);
Alex Lorenze82d89c2014-08-22 22:56:03 +000045 }
46 unsigned LineCount = LineEnd - LineStart + 1;
47
48 // Get counters
49 llvm::SmallVector<uint64_t, 16> ExecutionCounts;
50 ExecutionCounts.resize(LineCount, 0);
Justin Bogner8e8aa3f2014-09-09 05:32:14 +000051 for (auto &CR : Function.CountedRegions) {
52 if (CR.FileID != FileID)
Alex Lorenze82d89c2014-08-22 22:56:03 +000053 continue;
54 // Ignore the lines that were skipped by the preprocessor.
Justin Bogner8e8aa3f2014-09-09 05:32:14 +000055 auto ExecutionCount = CR.ExecutionCount;
56 if (CR.Kind == CounterMappingRegion::SkippedRegion) {
57 LineCount -= CR.LineEnd - CR.LineStart + 1;
Alex Lorenze82d89c2014-08-22 22:56:03 +000058 ExecutionCount = 1;
59 }
Justin Bogner8e8aa3f2014-09-09 05:32:14 +000060 for (unsigned I = CR.LineStart; I <= CR.LineEnd; ++I)
Alex Lorenze82d89c2014-08-22 22:56:03 +000061 ExecutionCounts[I - LineStart] = ExecutionCount;
62 }
63 CoveredLines += LineCount - std::count(ExecutionCounts.begin(),
64 ExecutionCounts.end(), 0);
65 NumLines += LineCount;
66 }
67 return FunctionCoverageSummary(
Alex Lorenzcb1702d2014-09-30 12:45:13 +000068 Function.Name, Function.ExecutionCount,
69 RegionCoverageInfo(CoveredRegions, NumCodeRegions),
Alex Lorenze82d89c2014-08-22 22:56:03 +000070 LineCoverageInfo(CoveredLines, 0, NumLines));
71}