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 "llvm/ProfileData/CoverageMapping.h"
18 #include <memory>
19 #include <vector>
20 
21 namespace llvm {
22 
23 /// \brief Matches specific functions that pass the requirement of this filter.
24 class CoverageFilter {
25 public:
~CoverageFilter()26   virtual ~CoverageFilter() {}
27 
28   /// \brief Return true if the function passes the requirements of this filter.
matches(const coverage::FunctionRecord & Function)29   virtual bool matches(const coverage::FunctionRecord &Function) {
30     return true;
31   }
32 };
33 
34 /// \brief Matches functions that contain a specific string in their name.
35 class NameCoverageFilter : public CoverageFilter {
36   StringRef Name;
37 
38 public:
NameCoverageFilter(StringRef Name)39   NameCoverageFilter(StringRef Name) : Name(Name) {}
40 
41   bool matches(const coverage::FunctionRecord &Function) override;
42 };
43 
44 /// \brief Matches functions whose name matches a certain regular expression.
45 class NameRegexCoverageFilter : public CoverageFilter {
46   StringRef Regex;
47 
48 public:
NameRegexCoverageFilter(StringRef Regex)49   NameRegexCoverageFilter(StringRef Regex) : Regex(Regex) {}
50 
51   bool matches(const coverage::FunctionRecord &Function) override;
52 };
53 
54 /// \brief Matches numbers that pass a certain threshold.
55 template <typename T> class StatisticThresholdFilter {
56 public:
57   enum Operation { LessThan, GreaterThan };
58 
59 protected:
60   Operation Op;
61   T Threshold;
62 
StatisticThresholdFilter(Operation Op,T Threshold)63   StatisticThresholdFilter(Operation Op, T Threshold)
64       : Op(Op), Threshold(Threshold) {}
65 
66   /// \brief Return true if the given number is less than
67   /// or greater than the certain threshold.
PassesThreshold(T Value)68   bool PassesThreshold(T Value) const {
69     switch (Op) {
70     case LessThan:
71       return Value < Threshold;
72     case GreaterThan:
73       return Value > Threshold;
74     }
75     return false;
76   }
77 };
78 
79 /// \brief Matches functions whose region coverage percentage
80 /// is above/below a certain percentage.
81 class RegionCoverageFilter : public CoverageFilter,
82                              public StatisticThresholdFilter<double> {
83 public:
RegionCoverageFilter(Operation Op,double Threshold)84   RegionCoverageFilter(Operation Op, double Threshold)
85       : StatisticThresholdFilter(Op, Threshold) {}
86 
87   bool matches(const coverage::FunctionRecord &Function) override;
88 };
89 
90 /// \brief Matches functions whose line coverage percentage
91 /// is above/below a certain percentage.
92 class LineCoverageFilter : public CoverageFilter,
93                            public StatisticThresholdFilter<double> {
94 public:
LineCoverageFilter(Operation Op,double Threshold)95   LineCoverageFilter(Operation Op, double Threshold)
96       : StatisticThresholdFilter(Op, Threshold) {}
97 
98   bool matches(const coverage::FunctionRecord &Function) override;
99 };
100 
101 /// \brief A collection of filters.
102 /// Matches functions that match any filters contained
103 /// in an instance of this class.
104 class CoverageFilters : public CoverageFilter {
105 protected:
106   std::vector<std::unique_ptr<CoverageFilter>> Filters;
107 
108 public:
109   /// \brief Append a filter to this collection.
110   void push_back(std::unique_ptr<CoverageFilter> Filter);
111 
empty()112   bool empty() const { return Filters.empty(); }
113 
114   bool matches(const coverage::FunctionRecord &Function) override;
115 };
116 
117 /// \brief A collection of filters.
118 /// Matches functions that match all of the filters contained
119 /// in an instance of this class.
120 class CoverageFiltersMatchAll : public CoverageFilters {
121 public:
122   bool matches(const coverage::FunctionRecord &Function) override;
123 };
124 
125 } // namespace llvm
126 
127 #endif // LLVM_COV_COVERAGEFILTERS_H
128