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