CoverageFilters.h revision 277323
1218822Sdim//===- CoverageFilters.h - Function coverage mapping filters --------------===// 2107492Sobrien// 3218822Sdim// The LLVM Compiler Infrastructure 4218822Sdim// 5218822Sdim// This file is distributed under the University of Illinois Open Source 6218822Sdim// License. See LICENSE.TXT for details. 7107492Sobrien// 8218822Sdim//===----------------------------------------------------------------------===// 9107492Sobrien// 10218822Sdim// These classes provide filtering for function coverage mapping records. 11218822Sdim// 12218822Sdim//===----------------------------------------------------------------------===// 13218822Sdim 14218822Sdim#ifndef LLVM_COV_COVERAGEFILTERS_H 15218822Sdim#define LLVM_COV_COVERAGEFILTERS_H 16107492Sobrien 17218822Sdim#include "llvm/ProfileData/CoverageMapping.h" 18107492Sobrien#include <memory> 19218822Sdim#include <vector> 20218822Sdim 21107492Sobriennamespace llvm { 22218822Sdim 23107492Sobrien/// \brief Matches specific functions that pass the requirement of this filter. 24218822Sdimclass CoverageFilter { 25218822Sdimpublic: 26218822Sdim virtual ~CoverageFilter() {} 27107492Sobrien 28218822Sdim /// \brief Return true if the function passes the requirements of this filter. 29107492Sobrien virtual bool matches(const coverage::FunctionRecord &Function) { 30218822Sdim return true; 31218822Sdim } 32218822Sdim}; 33218822Sdim 34218822Sdim/// \brief Matches functions that contain a specific string in their name. 35218822Sdimclass NameCoverageFilter : public CoverageFilter { 36218822Sdim StringRef Name; 37218822Sdim 38218822Sdimpublic: 39218822Sdim NameCoverageFilter(StringRef Name) : Name(Name) {} 40218822Sdim 41218822Sdim bool matches(const coverage::FunctionRecord &Function) override; 42218822Sdim}; 43218822Sdim 44218822Sdim/// \brief Matches functions whose name matches a certain regular expression. 45218822Sdimclass NameRegexCoverageFilter : public CoverageFilter { 46218822Sdim StringRef Regex; 47218822Sdim 48218822Sdimpublic: 49218822Sdim NameRegexCoverageFilter(StringRef Regex) : Regex(Regex) {} 50218822Sdim 51104834Sobrien bool matches(const coverage::FunctionRecord &Function) override; 52218822Sdim}; 53218822Sdim 54218822Sdim/// \brief Matches numbers that pass a certain threshold. 55218822Sdimtemplate <typename T> class StatisticThresholdFilter { 56218822Sdimpublic: 57218822Sdim enum Operation { LessThan, GreaterThan }; 58218822Sdim 59218822Sdimprotected: 60218822Sdim Operation Op; 61218822Sdim T Threshold; 62218822Sdim 63218822Sdim StatisticThresholdFilter(Operation Op, T Threshold) 64218822Sdim : Op(Op), Threshold(Threshold) {} 65218822Sdim 66218822Sdim /// \brief Return true if the given number is less than 67218822Sdim /// or greater than the certain threshold. 68218822Sdim bool PassesThreshold(T Value) const { 69218822Sdim switch (Op) { 70218822Sdim case LessThan: 71218822Sdim return Value < Threshold; 72218822Sdim case GreaterThan: 73218822Sdim return Value > Threshold; 74218822Sdim } 75218822Sdim return false; 76218822Sdim } 77218822Sdim}; 78218822Sdim 79218822Sdim/// \brief Matches functions whose region coverage percentage 80218822Sdim/// is above/below a certain percentage. 81218822Sdimclass RegionCoverageFilter : public CoverageFilter, 82218822Sdim public StatisticThresholdFilter<double> { 83218822Sdimpublic: 84218822Sdim RegionCoverageFilter(Operation Op, double Threshold) 85218822Sdim : StatisticThresholdFilter(Op, Threshold) {} 86218822Sdim 87218822Sdim bool matches(const coverage::FunctionRecord &Function) override; 88218822Sdim}; 89218822Sdim 90218822Sdim/// \brief Matches functions whose line coverage percentage 91218822Sdim/// is above/below a certain percentage. 92218822Sdimclass LineCoverageFilter : public CoverageFilter, 93218822Sdim public StatisticThresholdFilter<double> { 94218822Sdimpublic: 95218822Sdim LineCoverageFilter(Operation Op, double Threshold) 96218822Sdim : StatisticThresholdFilter(Op, Threshold) {} 97218822Sdim 98218822Sdim bool matches(const coverage::FunctionRecord &Function) override; 99218822Sdim}; 100218822Sdim 101218822Sdim/// \brief A collection of filters. 102218822Sdim/// Matches functions that match any filters contained 103218822Sdim/// in an instance of this class. 104218822Sdimclass CoverageFilters : public CoverageFilter { 105104834Sobrienprotected: 106218822Sdim std::vector<std::unique_ptr<CoverageFilter>> Filters; 107104834Sobrien 108218822Sdimpublic: 109218822Sdim /// \brief Append a filter to this collection. 110218822Sdim void push_back(std::unique_ptr<CoverageFilter> Filter); 111218822Sdim 112104834Sobrien bool empty() const { return Filters.empty(); } 113218822Sdim 114104834Sobrien bool matches(const coverage::FunctionRecord &Function) override; 115218822Sdim}; 116104834Sobrien 117218822Sdim/// \brief A collection of filters. 118104834Sobrien/// Matches functions that match all of the filters contained 119218822Sdim/// in an instance of this class. 120218822Sdimclass CoverageFiltersMatchAll : public CoverageFilters { 121218822Sdimpublic: 122218822Sdim bool matches(const coverage::FunctionRecord &Function) override; 123218822Sdim}; 124218822Sdim 125218822Sdim} // namespace llvm 126218822Sdim 127218822Sdim#endif // LLVM_COV_COVERAGEFILTERS_H 128218822Sdim