blob: c393b00d32a43ee157b5d6e2bb7f296931a6de2f [file] [log] [blame]
Alex Lorenze82d89c2014-08-22 22:56:03 +00001//===- CoverageSummaryInfo.h - 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#ifndef LLVM_COV_COVERAGESUMMARYINFO_H
16#define LLVM_COV_COVERAGESUMMARYINFO_H
17
Justin Bogner15562d02014-09-12 06:52:44 +000018#include "llvm/ProfileData/CoverageMapping.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000019#include "llvm/Support/raw_ostream.h"
20
21namespace llvm {
22
23/// \brief Provides information about region coverage for a function/file.
24struct RegionCoverageInfo {
25 /// \brief The number of regions that were executed at least once.
26 size_t Covered;
27
28 /// \brief The number of regions that weren't executed.
29 size_t NotCovered;
30
31 /// \brief The total number of regions in a function/file.
32 size_t NumRegions;
33
Justin Bognerf91bc6c2015-02-14 02:01:24 +000034 RegionCoverageInfo() : Covered(0), NotCovered(0), NumRegions(0) {}
35
Alex Lorenze82d89c2014-08-22 22:56:03 +000036 RegionCoverageInfo(size_t Covered, size_t NumRegions)
37 : Covered(Covered), NotCovered(NumRegions - Covered),
38 NumRegions(NumRegions) {}
39
Justin Bognerf91bc6c2015-02-14 02:01:24 +000040 RegionCoverageInfo &operator+=(const RegionCoverageInfo &RHS) {
41 Covered += RHS.Covered;
42 NotCovered += RHS.NotCovered;
43 NumRegions += RHS.NumRegions;
44 return *this;
45 }
46
Alex Lorenze82d89c2014-08-22 22:56:03 +000047 bool isFullyCovered() const { return Covered == NumRegions; }
48
49 double getPercentCovered() const {
50 return double(Covered) / double(NumRegions) * 100.0;
51 }
52};
53
54/// \brief Provides information about line coverage for a function/file.
55struct LineCoverageInfo {
56 /// \brief The number of lines that were executed at least once.
57 size_t Covered;
58
59 /// \brief The number of lines that weren't executed.
60 size_t NotCovered;
61
62 /// \brief The number of lines that aren't code.
63 size_t NonCodeLines;
64
65 /// \brief The total number of lines in a function/file.
66 size_t NumLines;
67
Justin Bognerf91bc6c2015-02-14 02:01:24 +000068 LineCoverageInfo()
69 : Covered(0), NotCovered(0), NonCodeLines(0), NumLines(0) {}
70
Alex Lorenze82d89c2014-08-22 22:56:03 +000071 LineCoverageInfo(size_t Covered, size_t NumNonCodeLines, size_t NumLines)
72 : Covered(Covered), NotCovered(NumLines - NumNonCodeLines - Covered),
73 NonCodeLines(NumNonCodeLines), NumLines(NumLines) {}
74
Justin Bognerf91bc6c2015-02-14 02:01:24 +000075 LineCoverageInfo &operator+=(const LineCoverageInfo &RHS) {
76 Covered += RHS.Covered;
77 NotCovered += RHS.NotCovered;
78 NonCodeLines += RHS.NonCodeLines;
79 NumLines += RHS.NumLines;
80 return *this;
81 }
82
Alex Lorenze82d89c2014-08-22 22:56:03 +000083 bool isFullyCovered() const { return Covered == (NumLines - NonCodeLines); }
84
85 double getPercentCovered() const {
86 return double(Covered) / double(NumLines - NonCodeLines) * 100.0;
87 }
88};
89
90/// \brief Provides information about function coverage for a file.
91struct FunctionCoverageInfo {
Alex Lorenzcb1702d2014-09-30 12:45:13 +000092 /// \brief The number of functions that were executed.
93 size_t Executed;
Alex Lorenze82d89c2014-08-22 22:56:03 +000094
95 /// \brief The total number of functions in this file.
96 size_t NumFunctions;
97
Justin Bognerf91bc6c2015-02-14 02:01:24 +000098 FunctionCoverageInfo() : Executed(0), NumFunctions(0) {}
99
Alex Lorenzcb1702d2014-09-30 12:45:13 +0000100 FunctionCoverageInfo(size_t Executed, size_t NumFunctions)
101 : Executed(Executed), NumFunctions(NumFunctions) {}
Alex Lorenze82d89c2014-08-22 22:56:03 +0000102
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000103 void addFunction(bool Covered) {
104 if (Covered)
105 ++Executed;
106 ++NumFunctions;
107 }
108
Alex Lorenzcb1702d2014-09-30 12:45:13 +0000109 bool isFullyCovered() const { return Executed == NumFunctions; }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000110
111 double getPercentCovered() const {
Alex Lorenzcb1702d2014-09-30 12:45:13 +0000112 return double(Executed) / double(NumFunctions) * 100.0;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000113 }
114};
115
116/// \brief A summary of function's code coverage.
117struct FunctionCoverageSummary {
118 StringRef Name;
Alex Lorenzcb1702d2014-09-30 12:45:13 +0000119 uint64_t ExecutionCount;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000120 RegionCoverageInfo RegionCoverage;
121 LineCoverageInfo LineCoverage;
122
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000123 FunctionCoverageSummary(StringRef Name) : Name(Name), ExecutionCount(0) {}
124
Alex Lorenzcb1702d2014-09-30 12:45:13 +0000125 FunctionCoverageSummary(StringRef Name, uint64_t ExecutionCount,
Alex Lorenze82d89c2014-08-22 22:56:03 +0000126 const RegionCoverageInfo &RegionCoverage,
127 const LineCoverageInfo &LineCoverage)
Alex Lorenzcb1702d2014-09-30 12:45:13 +0000128 : Name(Name), ExecutionCount(ExecutionCount),
129 RegionCoverage(RegionCoverage), LineCoverage(LineCoverage) {
Alex Lorenze82d89c2014-08-22 22:56:03 +0000130 }
131
132 /// \brief Compute the code coverage summary for the given function coverage
133 /// mapping record.
Justin Bogner15562d02014-09-12 06:52:44 +0000134 static FunctionCoverageSummary
Justin Bogner953e2402014-09-20 15:31:56 +0000135 get(const coverage::FunctionRecord &Function);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000136};
137
138/// \brief A summary of file's code coverage.
139struct FileCoverageSummary {
140 StringRef Name;
141 RegionCoverageInfo RegionCoverage;
142 LineCoverageInfo LineCoverage;
143 FunctionCoverageInfo FunctionCoverage;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000144
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000145 FileCoverageSummary(StringRef Name) : Name(Name) {}
Alex Lorenze82d89c2014-08-22 22:56:03 +0000146
Justin Bognerf91bc6c2015-02-14 02:01:24 +0000147 void addFunction(const FunctionCoverageSummary &Function) {
148 RegionCoverage += Function.RegionCoverage;
149 LineCoverage += Function.LineCoverage;
150 FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
151 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000152};
153
154} // namespace llvm
155
156#endif // LLVM_COV_COVERAGESUMMARYINFO_H