1 //===- 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 
18 #include "llvm/ProfileData/CoverageMapping.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 namespace llvm {
22 
23 /// \brief Provides information about region coverage for a function/file.
24 struct 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 
RegionCoverageInfoRegionCoverageInfo34   RegionCoverageInfo() : Covered(0), NotCovered(0), NumRegions(0) {}
35 
RegionCoverageInfoRegionCoverageInfo36   RegionCoverageInfo(size_t Covered, size_t NumRegions)
37       : Covered(Covered), NotCovered(NumRegions - Covered),
38         NumRegions(NumRegions) {}
39 
40   RegionCoverageInfo &operator+=(const RegionCoverageInfo &RHS) {
41     Covered += RHS.Covered;
42     NotCovered += RHS.NotCovered;
43     NumRegions += RHS.NumRegions;
44     return *this;
45   }
46 
isFullyCoveredRegionCoverageInfo47   bool isFullyCovered() const { return Covered == NumRegions; }
48 
getPercentCoveredRegionCoverageInfo49   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.
55 struct 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 
LineCoverageInfoLineCoverageInfo68   LineCoverageInfo()
69       : Covered(0), NotCovered(0), NonCodeLines(0), NumLines(0) {}
70 
LineCoverageInfoLineCoverageInfo71   LineCoverageInfo(size_t Covered, size_t NumNonCodeLines, size_t NumLines)
72       : Covered(Covered), NotCovered(NumLines - NumNonCodeLines - Covered),
73         NonCodeLines(NumNonCodeLines), NumLines(NumLines) {}
74 
75   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 
isFullyCoveredLineCoverageInfo83   bool isFullyCovered() const { return Covered == (NumLines - NonCodeLines); }
84 
getPercentCoveredLineCoverageInfo85   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.
91 struct FunctionCoverageInfo {
92   /// \brief The number of functions that were executed.
93   size_t Executed;
94 
95   /// \brief The total number of functions in this file.
96   size_t NumFunctions;
97 
FunctionCoverageInfoFunctionCoverageInfo98   FunctionCoverageInfo() : Executed(0), NumFunctions(0) {}
99 
FunctionCoverageInfoFunctionCoverageInfo100   FunctionCoverageInfo(size_t Executed, size_t NumFunctions)
101       : Executed(Executed), NumFunctions(NumFunctions) {}
102 
addFunctionFunctionCoverageInfo103   void addFunction(bool Covered) {
104     if (Covered)
105       ++Executed;
106     ++NumFunctions;
107   }
108 
isFullyCoveredFunctionCoverageInfo109   bool isFullyCovered() const { return Executed == NumFunctions; }
110 
getPercentCoveredFunctionCoverageInfo111   double getPercentCovered() const {
112     return double(Executed) / double(NumFunctions) * 100.0;
113   }
114 };
115 
116 /// \brief A summary of function's code coverage.
117 struct FunctionCoverageSummary {
118   StringRef Name;
119   uint64_t ExecutionCount;
120   RegionCoverageInfo RegionCoverage;
121   LineCoverageInfo LineCoverage;
122 
FunctionCoverageSummaryFunctionCoverageSummary123   FunctionCoverageSummary(StringRef Name) : Name(Name), ExecutionCount(0) {}
124 
FunctionCoverageSummaryFunctionCoverageSummary125   FunctionCoverageSummary(StringRef Name, uint64_t ExecutionCount,
126                           const RegionCoverageInfo &RegionCoverage,
127                           const LineCoverageInfo &LineCoverage)
128       : Name(Name), ExecutionCount(ExecutionCount),
129         RegionCoverage(RegionCoverage), LineCoverage(LineCoverage) {
130   }
131 
132   /// \brief Compute the code coverage summary for the given function coverage
133   /// mapping record.
134   static FunctionCoverageSummary
135   get(const coverage::FunctionRecord &Function);
136 };
137 
138 /// \brief A summary of file's code coverage.
139 struct FileCoverageSummary {
140   StringRef Name;
141   RegionCoverageInfo RegionCoverage;
142   LineCoverageInfo LineCoverage;
143   FunctionCoverageInfo FunctionCoverage;
144 
FileCoverageSummaryFileCoverageSummary145   FileCoverageSummary(StringRef Name) : Name(Name) {}
146 
addFunctionFileCoverageSummary147   void addFunction(const FunctionCoverageSummary &Function) {
148     RegionCoverage += Function.RegionCoverage;
149     LineCoverage += Function.LineCoverage;
150     FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
151   }
152 };
153 
154 } // namespace llvm
155 
156 #endif // LLVM_COV_COVERAGESUMMARYINFO_H
157