1 //===- CoverageFilters.h - Function coverage mapping filters --------------===//
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 classes provide filtering for function coverage mapping records.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_COV_COVERAGEFILTERS_H
15 #define LLVM_COV_COVERAGEFILTERS_H
16 
17 #include "CoverageSummaryInfo.h"
18 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
19 #include "llvm/Support/SpecialCaseList.h"
20 #include <memory>
21 #include <vector>
22 
23 namespace llvm {
24 
25 /// Matches specific functions that pass the requirement of this filter.
26 class CoverageFilter {
27 public:
~CoverageFilter()28   virtual ~CoverageFilter() {}
29 
30   /// Return true if the function passes the requirements of this filter.
matches(const coverage::CoverageMapping & CM,const coverage::FunctionRecord & Function)31   virtual bool matches(const coverage::CoverageMapping &CM,
32                        const coverage::FunctionRecord &Function) const {
33     return true;
34   }
35 
36   /// Return true if the filename passes the requirements of this filter.
matchesFilename(StringRef Filename)37   virtual bool matchesFilename(StringRef Filename) const {
38     return true;
39   }
40 };
41 
42 /// Matches functions that contain a specific string in their name.
43 class NameCoverageFilter : public CoverageFilter {
44   StringRef Name;
45 
46 public:
NameCoverageFilter(StringRef Name)47   NameCoverageFilter(StringRef Name) : Name(Name) {}
48 
49   bool matches(const coverage::CoverageMapping &CM,
50                const coverage::FunctionRecord &Function) const override;
51 };
52 
53 /// Matches functions whose name matches a certain regular expression.
54 class NameRegexCoverageFilter : public CoverageFilter {
55   StringRef Regex;
56 
57 public:
NameRegexCoverageFilter(StringRef Regex)58   NameRegexCoverageFilter(StringRef Regex) : Regex(Regex) {}
59 
60   bool matches(const coverage::CoverageMapping &CM,
61                const coverage::FunctionRecord &Function) const override;
62 
63   bool matchesFilename(StringRef Filename) const override;
64 };
65 
66 /// Matches functions whose name appears in a SpecialCaseList in the
67 /// whitelist_fun section.
68 class NameWhitelistCoverageFilter : public CoverageFilter {
69   const SpecialCaseList &Whitelist;
70 
71 public:
NameWhitelistCoverageFilter(const SpecialCaseList & Whitelist)72   NameWhitelistCoverageFilter(const SpecialCaseList &Whitelist)
73       : Whitelist(Whitelist) {}
74 
75   bool matches(const coverage::CoverageMapping &CM,
76                const coverage::FunctionRecord &Function) const override;
77 };
78 
79 /// Matches numbers that pass a certain threshold.
80 template <typename T> class StatisticThresholdFilter {
81 public:
82   enum Operation { LessThan, GreaterThan };
83 
84 protected:
85   Operation Op;
86   T Threshold;
87 
StatisticThresholdFilter(Operation Op,T Threshold)88   StatisticThresholdFilter(Operation Op, T Threshold)
89       : Op(Op), Threshold(Threshold) {}
90 
91   /// Return true if the given number is less than
92   /// or greater than the certain threshold.
PassesThreshold(T Value)93   bool PassesThreshold(T Value) const {
94     switch (Op) {
95     case LessThan:
96       return Value < Threshold;
97     case GreaterThan:
98       return Value > Threshold;
99     }
100     return false;
101   }
102 };
103 
104 /// Matches functions whose region coverage percentage
105 /// is above/below a certain percentage.
106 class RegionCoverageFilter : public CoverageFilter,
107                              public StatisticThresholdFilter<double> {
108 public:
RegionCoverageFilter(Operation Op,double Threshold)109   RegionCoverageFilter(Operation Op, double Threshold)
110       : StatisticThresholdFilter(Op, Threshold) {}
111 
112   bool matches(const coverage::CoverageMapping &CM,
113                const coverage::FunctionRecord &Function) const override;
114 };
115 
116 /// Matches functions whose line coverage percentage
117 /// is above/below a certain percentage.
118 class LineCoverageFilter : public CoverageFilter,
119                            public StatisticThresholdFilter<double> {
120 public:
LineCoverageFilter(Operation Op,double Threshold)121   LineCoverageFilter(Operation Op, double Threshold)
122       : StatisticThresholdFilter(Op, Threshold) {}
123 
124   bool matches(const coverage::CoverageMapping &CM,
125                const coverage::FunctionRecord &Function) const override;
126 };
127 
128 /// A collection of filters.
129 /// Matches functions that match any filters contained
130 /// in an instance of this class.
131 class CoverageFilters : public CoverageFilter {
132 protected:
133   std::vector<std::unique_ptr<CoverageFilter>> Filters;
134 
135 public:
136   /// Append a filter to this collection.
137   void push_back(std::unique_ptr<CoverageFilter> Filter);
138 
empty()139   bool empty() const { return Filters.empty(); }
140 
141   bool matches(const coverage::CoverageMapping &CM,
142                const coverage::FunctionRecord &Function) const override;
143 
144   bool matchesFilename(StringRef Filename) const override;
145 };
146 
147 /// A collection of filters.
148 /// Matches functions that match all of the filters contained
149 /// in an instance of this class.
150 class CoverageFiltersMatchAll : public CoverageFilters {
151 public:
152   bool matches(const coverage::CoverageMapping &CM,
153                const coverage::FunctionRecord &Function) const override;
154 };
155 
156 } // namespace llvm
157 
158 #endif // LLVM_COV_COVERAGEFILTERS_H
159