e543005c6bc81d0809e69b7a59d55288f69ed59b
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 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_COV_COVERAGEFILTERS_H
15 #define LLVM_COV_COVERAGEFILTERS_H
17 #include "llvm/ProfileData/CoverageMapping.h"
18 #include <vector>
19 #include <memory>
21 namespace llvm {
23 /// \brief Matches specific functions that pass the requirement of this filter.
24 class CoverageFilter {
25 public:
26 virtual ~CoverageFilter() {}
28 /// \brief Return true if the function passes the requirements of this filter.
29 virtual bool matches(const coverage::FunctionRecord &Function) {
30 return true;
31 }
32 };
34 /// \brief Matches functions that contain a specific string in their name.
35 class NameCoverageFilter : public CoverageFilter {
36 StringRef Name;
38 public:
39 NameCoverageFilter(StringRef Name) : Name(Name) {}
41 bool matches(const coverage::FunctionRecord &Function) override;
42 };
44 /// \brief Matches functions whose name matches a certain regular expression.
45 class NameRegexCoverageFilter : public CoverageFilter {
46 StringRef Regex;
48 public:
49 NameRegexCoverageFilter(StringRef Regex) : Regex(Regex) {}
51 bool matches(const coverage::FunctionRecord &Function) override;
52 };
54 /// \brief Matches numbers that pass a certain threshold.
55 template <typename T> class StatisticThresholdFilter {
56 public:
57 enum Operation { LessThan, GreaterThan };
59 protected:
60 Operation Op;
61 T Threshold;
63 StatisticThresholdFilter(Operation Op, T Threshold)
64 : Op(Op), Threshold(Threshold) {}
66 /// \brief Return true if the given number is less than
67 /// or greater than the certain threshold.
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 };
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:
84 RegionCoverageFilter(Operation Op, double Threshold)
85 : StatisticThresholdFilter(Op, Threshold) {}
87 bool matches(const coverage::FunctionRecord &Function) override;
88 };
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:
95 LineCoverageFilter(Operation Op, double Threshold)
96 : StatisticThresholdFilter(Op, Threshold) {}
98 bool matches(const coverage::FunctionRecord &Function) override;
99 };
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;
108 public:
109 /// \brief Append a filter to this collection.
110 void push_back(std::unique_ptr<CoverageFilter> Filter);
112 bool empty() const { return Filters.empty(); }
114 bool matches(const coverage::FunctionRecord &Function) override;
115 };
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 };
125 } // namespace llvm
127 #endif // LLVM_COV_COVERAGEFILTERS_H