CoverageFilters.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #ifndef LLVM_COV_COVERAGEFILTERS_H
  14. #define LLVM_COV_COVERAGEFILTERS_H
  15. #include "llvm/ProfileData/CoverageMapping.h"
  16. #include <memory>
  17. #include <vector>
  18. namespace llvm {
  19. /// \brief Matches specific functions that pass the requirement of this filter.
  20. class CoverageFilter {
  21. public:
  22. virtual ~CoverageFilter() {}
  23. /// \brief Return true if the function passes the requirements of this filter.
  24. virtual bool matches(const coverage::FunctionRecord &Function) {
  25. return true;
  26. }
  27. };
  28. /// \brief Matches functions that contain a specific string in their name.
  29. class NameCoverageFilter : public CoverageFilter {
  30. StringRef Name;
  31. public:
  32. NameCoverageFilter(StringRef Name) : Name(Name) {}
  33. bool matches(const coverage::FunctionRecord &Function) override;
  34. };
  35. /// \brief Matches functions whose name matches a certain regular expression.
  36. class NameRegexCoverageFilter : public CoverageFilter {
  37. StringRef Regex;
  38. public:
  39. NameRegexCoverageFilter(StringRef Regex) : Regex(Regex) {}
  40. bool matches(const coverage::FunctionRecord &Function) override;
  41. };
  42. /// \brief Matches numbers that pass a certain threshold.
  43. template <typename T> class StatisticThresholdFilter {
  44. public:
  45. enum Operation { LessThan, GreaterThan };
  46. protected:
  47. Operation Op;
  48. T Threshold;
  49. StatisticThresholdFilter(Operation Op, T Threshold)
  50. : Op(Op), Threshold(Threshold) {}
  51. /// \brief Return true if the given number is less than
  52. /// or greater than the certain threshold.
  53. bool PassesThreshold(T Value) const {
  54. switch (Op) {
  55. case LessThan:
  56. return Value < Threshold;
  57. case GreaterThan:
  58. return Value > Threshold;
  59. }
  60. return false;
  61. }
  62. };
  63. /// \brief Matches functions whose region coverage percentage
  64. /// is above/below a certain percentage.
  65. class RegionCoverageFilter : public CoverageFilter,
  66. public StatisticThresholdFilter<double> {
  67. public:
  68. RegionCoverageFilter(Operation Op, double Threshold)
  69. : StatisticThresholdFilter(Op, Threshold) {}
  70. bool matches(const coverage::FunctionRecord &Function) override;
  71. };
  72. /// \brief Matches functions whose line coverage percentage
  73. /// is above/below a certain percentage.
  74. class LineCoverageFilter : public CoverageFilter,
  75. public StatisticThresholdFilter<double> {
  76. public:
  77. LineCoverageFilter(Operation Op, double Threshold)
  78. : StatisticThresholdFilter(Op, Threshold) {}
  79. bool matches(const coverage::FunctionRecord &Function) override;
  80. };
  81. /// \brief A collection of filters.
  82. /// Matches functions that match any filters contained
  83. /// in an instance of this class.
  84. class CoverageFilters : public CoverageFilter {
  85. protected:
  86. std::vector<std::unique_ptr<CoverageFilter>> Filters;
  87. public:
  88. /// \brief Append a filter to this collection.
  89. void push_back(std::unique_ptr<CoverageFilter> Filter);
  90. bool empty() const { return Filters.empty(); }
  91. bool matches(const coverage::FunctionRecord &Function) override;
  92. };
  93. /// \brief A collection of filters.
  94. /// Matches functions that match all of the filters contained
  95. /// in an instance of this class.
  96. class CoverageFiltersMatchAll : public CoverageFilters {
  97. public:
  98. bool matches(const coverage::FunctionRecord &Function) override;
  99. };
  100. } // namespace llvm
  101. #endif // LLVM_COV_COVERAGEFILTERS_H