1277323Sdim//===- CoverageFilters.h - Function coverage mapping filters --------------===//
2277323Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6277323Sdim//
7277323Sdim//===----------------------------------------------------------------------===//
8277323Sdim//
9277323Sdim// These classes provide filtering for function coverage mapping records.
10277323Sdim//
11277323Sdim//===----------------------------------------------------------------------===//
12277323Sdim
13277323Sdim#ifndef LLVM_COV_COVERAGEFILTERS_H
14277323Sdim#define LLVM_COV_COVERAGEFILTERS_H
15277323Sdim
16327952Sdim#include "CoverageSummaryInfo.h"
17309124Sdim#include "llvm/ProfileData/Coverage/CoverageMapping.h"
18327952Sdim#include "llvm/Support/SpecialCaseList.h"
19277323Sdim#include <memory>
20277323Sdim#include <vector>
21277323Sdim
22277323Sdimnamespace llvm {
23277323Sdim
24341825Sdim/// Matches specific functions that pass the requirement of this filter.
25277323Sdimclass CoverageFilter {
26277323Sdimpublic:
27277323Sdim  virtual ~CoverageFilter() {}
28277323Sdim
29341825Sdim  /// Return true if the function passes the requirements of this filter.
30327952Sdim  virtual bool matches(const coverage::CoverageMapping &CM,
31327952Sdim                       const coverage::FunctionRecord &Function) const {
32277323Sdim    return true;
33277323Sdim  }
34341825Sdim
35341825Sdim  /// Return true if the filename passes the requirements of this filter.
36341825Sdim  virtual bool matchesFilename(StringRef Filename) const {
37341825Sdim    return true;
38341825Sdim  }
39277323Sdim};
40277323Sdim
41341825Sdim/// Matches functions that contain a specific string in their name.
42277323Sdimclass NameCoverageFilter : public CoverageFilter {
43277323Sdim  StringRef Name;
44277323Sdim
45277323Sdimpublic:
46277323Sdim  NameCoverageFilter(StringRef Name) : Name(Name) {}
47277323Sdim
48327952Sdim  bool matches(const coverage::CoverageMapping &CM,
49327952Sdim               const coverage::FunctionRecord &Function) const override;
50277323Sdim};
51277323Sdim
52341825Sdim/// Matches functions whose name matches a certain regular expression.
53277323Sdimclass NameRegexCoverageFilter : public CoverageFilter {
54277323Sdim  StringRef Regex;
55277323Sdim
56277323Sdimpublic:
57277323Sdim  NameRegexCoverageFilter(StringRef Regex) : Regex(Regex) {}
58277323Sdim
59327952Sdim  bool matches(const coverage::CoverageMapping &CM,
60327952Sdim               const coverage::FunctionRecord &Function) const override;
61341825Sdim
62341825Sdim  bool matchesFilename(StringRef Filename) const override;
63277323Sdim};
64277323Sdim
65341825Sdim/// Matches functions whose name appears in a SpecialCaseList in the
66327952Sdim/// whitelist_fun section.
67327952Sdimclass NameWhitelistCoverageFilter : public CoverageFilter {
68327952Sdim  const SpecialCaseList &Whitelist;
69327952Sdim
70327952Sdimpublic:
71327952Sdim  NameWhitelistCoverageFilter(const SpecialCaseList &Whitelist)
72327952Sdim      : Whitelist(Whitelist) {}
73327952Sdim
74327952Sdim  bool matches(const coverage::CoverageMapping &CM,
75327952Sdim               const coverage::FunctionRecord &Function) const override;
76327952Sdim};
77327952Sdim
78341825Sdim/// Matches numbers that pass a certain threshold.
79277323Sdimtemplate <typename T> class StatisticThresholdFilter {
80277323Sdimpublic:
81277323Sdim  enum Operation { LessThan, GreaterThan };
82277323Sdim
83277323Sdimprotected:
84277323Sdim  Operation Op;
85277323Sdim  T Threshold;
86277323Sdim
87277323Sdim  StatisticThresholdFilter(Operation Op, T Threshold)
88277323Sdim      : Op(Op), Threshold(Threshold) {}
89277323Sdim
90341825Sdim  /// Return true if the given number is less than
91277323Sdim  /// or greater than the certain threshold.
92277323Sdim  bool PassesThreshold(T Value) const {
93277323Sdim    switch (Op) {
94277323Sdim    case LessThan:
95277323Sdim      return Value < Threshold;
96277323Sdim    case GreaterThan:
97277323Sdim      return Value > Threshold;
98277323Sdim    }
99277323Sdim    return false;
100277323Sdim  }
101277323Sdim};
102277323Sdim
103341825Sdim/// Matches functions whose region coverage percentage
104277323Sdim/// is above/below a certain percentage.
105277323Sdimclass RegionCoverageFilter : public CoverageFilter,
106277323Sdim                             public StatisticThresholdFilter<double> {
107277323Sdimpublic:
108277323Sdim  RegionCoverageFilter(Operation Op, double Threshold)
109277323Sdim      : StatisticThresholdFilter(Op, Threshold) {}
110277323Sdim
111327952Sdim  bool matches(const coverage::CoverageMapping &CM,
112327952Sdim               const coverage::FunctionRecord &Function) const override;
113277323Sdim};
114277323Sdim
115341825Sdim/// Matches functions whose line coverage percentage
116277323Sdim/// is above/below a certain percentage.
117277323Sdimclass LineCoverageFilter : public CoverageFilter,
118277323Sdim                           public StatisticThresholdFilter<double> {
119277323Sdimpublic:
120277323Sdim  LineCoverageFilter(Operation Op, double Threshold)
121277323Sdim      : StatisticThresholdFilter(Op, Threshold) {}
122277323Sdim
123327952Sdim  bool matches(const coverage::CoverageMapping &CM,
124327952Sdim               const coverage::FunctionRecord &Function) const override;
125277323Sdim};
126277323Sdim
127341825Sdim/// A collection of filters.
128277323Sdim/// Matches functions that match any filters contained
129277323Sdim/// in an instance of this class.
130277323Sdimclass CoverageFilters : public CoverageFilter {
131277323Sdimprotected:
132277323Sdim  std::vector<std::unique_ptr<CoverageFilter>> Filters;
133277323Sdim
134277323Sdimpublic:
135341825Sdim  /// Append a filter to this collection.
136277323Sdim  void push_back(std::unique_ptr<CoverageFilter> Filter);
137277323Sdim
138277323Sdim  bool empty() const { return Filters.empty(); }
139277323Sdim
140327952Sdim  bool matches(const coverage::CoverageMapping &CM,
141327952Sdim               const coverage::FunctionRecord &Function) const override;
142341825Sdim
143341825Sdim  bool matchesFilename(StringRef Filename) const override;
144277323Sdim};
145277323Sdim
146341825Sdim/// A collection of filters.
147277323Sdim/// Matches functions that match all of the filters contained
148277323Sdim/// in an instance of this class.
149277323Sdimclass CoverageFiltersMatchAll : public CoverageFilters {
150277323Sdimpublic:
151327952Sdim  bool matches(const coverage::CoverageMapping &CM,
152327952Sdim               const coverage::FunctionRecord &Function) const override;
153277323Sdim};
154277323Sdim
155277323Sdim} // namespace llvm
156277323Sdim
157277323Sdim#endif // LLVM_COV_COVERAGEFILTERS_H
158