aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-cov/CoverageFilters.h
blob: 5345b0c87cc27c264593f03bc5652f4febf371b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//===- CoverageFilters.h - Function coverage mapping filters --------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// These classes provide filtering for function coverage mapping records.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_COV_COVERAGEFILTERS_H
#define LLVM_COV_COVERAGEFILTERS_H

#include "llvm/ADT/StringRef.h"
#include <memory>
#include <vector>

namespace llvm {
class SpecialCaseList;

namespace coverage {
class CoverageMapping;
struct FunctionRecord;
} // namespace coverage

/// Matches specific functions that pass the requirement of this filter.
class CoverageFilter {
public:
  virtual ~CoverageFilter() {}

  /// Return true if the function passes the requirements of this filter.
  virtual bool matches(const coverage::CoverageMapping &CM,
                       const coverage::FunctionRecord &Function) const {
    return true;
  }

  /// Return true if the filename passes the requirements of this filter.
  virtual bool matchesFilename(StringRef Filename) const {
    return true;
  }
};

/// Matches functions that contain a specific string in their name.
class NameCoverageFilter : public CoverageFilter {
  StringRef Name;

public:
  NameCoverageFilter(StringRef Name) : Name(Name) {}

  bool matches(const coverage::CoverageMapping &CM,
               const coverage::FunctionRecord &Function) const override;
};

/// Matches functions whose name matches a certain regular expression.
class NameRegexCoverageFilter : public CoverageFilter {
  StringRef Regex;

public:
  NameRegexCoverageFilter(StringRef Regex) : Regex(Regex) {}

  bool matches(const coverage::CoverageMapping &CM,
               const coverage::FunctionRecord &Function) const override;

  bool matchesFilename(StringRef Filename) const override;
};

/// Matches functions whose name appears in a SpecialCaseList in the
/// allowlist_fun section.
class NameAllowlistCoverageFilter : public CoverageFilter {
  const SpecialCaseList &Allowlist;

public:
  NameAllowlistCoverageFilter(const SpecialCaseList &Allowlist)
      : Allowlist(Allowlist) {}

  bool matches(const coverage::CoverageMapping &CM,
               const coverage::FunctionRecord &Function) const override;
};

/// Matches numbers that pass a certain threshold.
template <typename T> class StatisticThresholdFilter {
public:
  enum Operation { LessThan, GreaterThan };

protected:
  Operation Op;
  T Threshold;

  StatisticThresholdFilter(Operation Op, T Threshold)
      : Op(Op), Threshold(Threshold) {}

  /// Return true if the given number is less than
  /// or greater than the certain threshold.
  bool PassesThreshold(T Value) const {
    switch (Op) {
    case LessThan:
      return Value < Threshold;
    case GreaterThan:
      return Value > Threshold;
    }
    return false;
  }
};

/// Matches functions whose region coverage percentage
/// is above/below a certain percentage.
class RegionCoverageFilter : public CoverageFilter,
                             public StatisticThresholdFilter<double> {
public:
  RegionCoverageFilter(Operation Op, double Threshold)
      : StatisticThresholdFilter(Op, Threshold) {}

  bool matches(const coverage::CoverageMapping &CM,
               const coverage::FunctionRecord &Function) const override;
};

/// Matches functions whose line coverage percentage
/// is above/below a certain percentage.
class LineCoverageFilter : public CoverageFilter,
                           public StatisticThresholdFilter<double> {
public:
  LineCoverageFilter(Operation Op, double Threshold)
      : StatisticThresholdFilter(Op, Threshold) {}

  bool matches(const coverage::CoverageMapping &CM,
               const coverage::FunctionRecord &Function) const override;
};

/// A collection of filters.
/// Matches functions that match any filters contained
/// in an instance of this class.
class CoverageFilters : public CoverageFilter {
protected:
  std::vector<std::unique_ptr<CoverageFilter>> Filters;

public:
  /// Append a filter to this collection.
  void push_back(std::unique_ptr<CoverageFilter> Filter);

  bool empty() const { return Filters.empty(); }

  bool matches(const coverage::CoverageMapping &CM,
               const coverage::FunctionRecord &Function) const override;

  bool matchesFilename(StringRef Filename) const override;
};

/// A collection of filters.
/// Matches functions that match all of the filters contained
/// in an instance of this class.
class CoverageFiltersMatchAll : public CoverageFilters {
public:
  bool matches(const coverage::CoverageMapping &CM,
               const coverage::FunctionRecord &Function) const override;
};

} // namespace llvm

#endif // LLVM_COV_COVERAGEFILTERS_H