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