1 //===- CoverageFilters.h - Function coverage mapping filters --------------===//
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
7 //===----------------------------------------------------------------------===//
9 // These classes provide filtering for function coverage mapping records.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_COV_COVERAGEFILTERS_H
14 #define LLVM_COV_COVERAGEFILTERS_H
16 #include "CoverageSummaryInfo.h"
17 #include "llvm/ProfileData/Coverage/CoverageMapping.h"
18 #include "llvm/Support/SpecialCaseList.h"
24 /// Matches specific functions that pass the requirement of this filter.
25 class CoverageFilter
{
27 virtual ~CoverageFilter() {}
29 /// Return true if the function passes the requirements of this filter.
30 virtual bool matches(const coverage::CoverageMapping
&CM
,
31 const coverage::FunctionRecord
&Function
) const {
35 /// Return true if the filename passes the requirements of this filter.
36 virtual bool matchesFilename(StringRef Filename
) const {
41 /// Matches functions that contain a specific string in their name.
42 class NameCoverageFilter
: public CoverageFilter
{
46 NameCoverageFilter(StringRef Name
) : Name(Name
) {}
48 bool matches(const coverage::CoverageMapping
&CM
,
49 const coverage::FunctionRecord
&Function
) const override
;
52 /// Matches functions whose name matches a certain regular expression.
53 class NameRegexCoverageFilter
: public CoverageFilter
{
57 NameRegexCoverageFilter(StringRef Regex
) : Regex(Regex
) {}
59 bool matches(const coverage::CoverageMapping
&CM
,
60 const coverage::FunctionRecord
&Function
) const override
;
62 bool matchesFilename(StringRef Filename
) const override
;
65 /// Matches functions whose name appears in a SpecialCaseList in the
66 /// whitelist_fun section.
67 class NameWhitelistCoverageFilter
: public CoverageFilter
{
68 const SpecialCaseList
&Whitelist
;
71 NameWhitelistCoverageFilter(const SpecialCaseList
&Whitelist
)
72 : Whitelist(Whitelist
) {}
74 bool matches(const coverage::CoverageMapping
&CM
,
75 const coverage::FunctionRecord
&Function
) const override
;
78 /// Matches numbers that pass a certain threshold.
79 template <typename T
> class StatisticThresholdFilter
{
81 enum Operation
{ LessThan
, GreaterThan
};
87 StatisticThresholdFilter(Operation Op
, T Threshold
)
88 : Op(Op
), Threshold(Threshold
) {}
90 /// Return true if the given number is less than
91 /// or greater than the certain threshold.
92 bool PassesThreshold(T Value
) const {
95 return Value
< Threshold
;
97 return Value
> Threshold
;
103 /// Matches functions whose region coverage percentage
104 /// is above/below a certain percentage.
105 class RegionCoverageFilter
: public CoverageFilter
,
106 public StatisticThresholdFilter
<double> {
108 RegionCoverageFilter(Operation Op
, double Threshold
)
109 : StatisticThresholdFilter(Op
, Threshold
) {}
111 bool matches(const coverage::CoverageMapping
&CM
,
112 const coverage::FunctionRecord
&Function
) const override
;
115 /// Matches functions whose line coverage percentage
116 /// is above/below a certain percentage.
117 class LineCoverageFilter
: public CoverageFilter
,
118 public StatisticThresholdFilter
<double> {
120 LineCoverageFilter(Operation Op
, double Threshold
)
121 : StatisticThresholdFilter(Op
, Threshold
) {}
123 bool matches(const coverage::CoverageMapping
&CM
,
124 const coverage::FunctionRecord
&Function
) const override
;
127 /// A collection of filters.
128 /// Matches functions that match any filters contained
129 /// in an instance of this class.
130 class CoverageFilters
: public CoverageFilter
{
132 std::vector
<std::unique_ptr
<CoverageFilter
>> Filters
;
135 /// Append a filter to this collection.
136 void push_back(std::unique_ptr
<CoverageFilter
> Filter
);
138 bool empty() const { return Filters
.empty(); }
140 bool matches(const coverage::CoverageMapping
&CM
,
141 const coverage::FunctionRecord
&Function
) const override
;
143 bool matchesFilename(StringRef Filename
) const override
;
146 /// A collection of filters.
147 /// Matches functions that match all of the filters contained
148 /// in an instance of this class.
149 class CoverageFiltersMatchAll
: public CoverageFilters
{
151 bool matches(const coverage::CoverageMapping
&CM
,
152 const coverage::FunctionRecord
&Function
) const override
;
157 #endif // LLVM_COV_COVERAGEFILTERS_H