1//===- CoverageFilters.h - Function coverage mapping filters --------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// These classes provide filtering for function coverage mapping records.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_COV_COVERAGEFILTERS_H
14#define LLVM_COV_COVERAGEFILTERS_H
15
16#include "llvm/ADT/StringRef.h"
17#include <memory>
18#include <vector>
19
20namespace llvm {
21class SpecialCaseList;
22
23namespace coverage {
24class CoverageMapping;
25struct FunctionRecord;
26} // namespace coverage
27
28/// Matches specific functions that pass the requirement of this filter.
29class CoverageFilter {
30public:
31 virtual ~CoverageFilter() = default;
32
33 /// Return true if the function passes the requirements of this filter.
34 virtual bool matches(const coverage::CoverageMapping &CM,
35 const coverage::FunctionRecord &Function) const {
36 return true;
37 }
38
39 /// Return true if the filename passes the requirements of this filter.
40 virtual bool matchesFilename(StringRef Filename) const {
41 return true;
42 }
43};
44
45/// Matches functions that contain a specific string in their name.
46class NameCoverageFilter : public CoverageFilter {
47 StringRef Name;
48
49public:
50 NameCoverageFilter(StringRef Name) : Name(Name) {}
51
52 bool matches(const coverage::CoverageMapping &CM,
53 const coverage::FunctionRecord &Function) const override;
54};
55
56/// Matches functions whose name matches a certain regular expression.
57class NameRegexCoverageFilter : public CoverageFilter {
58public:
59 enum class FilterType {
60 Include,
61 Exclude,
62 };
63
64private:
65 StringRef Regex;
66 FilterType Type;
67
68public:
69 NameRegexCoverageFilter(StringRef Regex,
70 FilterType Type = FilterType::Exclude)
71 : Regex(Regex), Type(Type) {}
72
73 bool matches(const coverage::CoverageMapping &CM,
74 const coverage::FunctionRecord &Function) const override;
75
76 bool matchesFilename(StringRef Filename) const override;
77};
78
79/// Matches functions whose name appears in a SpecialCaseList in the
80/// allowlist_fun section.
81class NameAllowlistCoverageFilter : public CoverageFilter {
82 const SpecialCaseList &Allowlist;
83
84public:
85 NameAllowlistCoverageFilter(const SpecialCaseList &Allowlist)
86 : Allowlist(Allowlist) {}
87
88 bool matches(const coverage::CoverageMapping &CM,
89 const coverage::FunctionRecord &Function) const override;
90};
91
92/// Matches numbers that pass a certain threshold.
93template <typename T> class StatisticThresholdFilter {
94public:
95 enum Operation { LessThan, GreaterThan };
96
97protected:
98 Operation Op;
99 T Threshold;
100
101 StatisticThresholdFilter(Operation Op, T Threshold)
102 : Op(Op), Threshold(Threshold) {}
103
104 /// Return true if the given number is less than
105 /// or greater than the certain threshold.
106 bool PassesThreshold(T Value) const {
107 switch (Op) {
108 case LessThan:
109 return Value < Threshold;
110 case GreaterThan:
111 return Value > Threshold;
112 }
113 return false;
114 }
115};
116
117/// Matches functions whose region coverage percentage
118/// is above/below a certain percentage.
119class RegionCoverageFilter : public CoverageFilter,
120 public StatisticThresholdFilter<double> {
121public:
122 RegionCoverageFilter(Operation Op, double Threshold)
123 : StatisticThresholdFilter(Op, Threshold) {}
124
125 bool matches(const coverage::CoverageMapping &CM,
126 const coverage::FunctionRecord &Function) const override;
127};
128
129/// Matches functions whose line coverage percentage
130/// is above/below a certain percentage.
131class LineCoverageFilter : public CoverageFilter,
132 public StatisticThresholdFilter<double> {
133public:
134 LineCoverageFilter(Operation Op, double Threshold)
135 : StatisticThresholdFilter(Op, Threshold) {}
136
137 bool matches(const coverage::CoverageMapping &CM,
138 const coverage::FunctionRecord &Function) const override;
139};
140
141/// A collection of filters.
142/// Matches functions that match any filters contained
143/// in an instance of this class.
144class CoverageFilters : public CoverageFilter {
145protected:
146 std::vector<std::unique_ptr<CoverageFilter>> Filters;
147
148public:
149 /// Append a filter to this collection.
150 void push_back(std::unique_ptr<CoverageFilter> Filter);
151
152 bool empty() const { return Filters.empty(); }
153
154 bool matches(const coverage::CoverageMapping &CM,
155 const coverage::FunctionRecord &Function) const override;
156
157 bool matchesFilename(StringRef Filename) const override;
158};
159
160/// A collection of filters.
161/// Matches functions that match all of the filters contained
162/// in an instance of this class.
163class CoverageFiltersMatchAll : public CoverageFilters {
164public:
165 bool matches(const coverage::CoverageMapping &CM,
166 const coverage::FunctionRecord &Function) const override;
167};
168
169} // namespace llvm
170
171#endif // LLVM_COV_COVERAGEFILTERS_H
172