xref: /llvm-project/llvm/tools/llvm-cov/CoverageFilters.cpp (revision 998b220e97b443a6dd80a2dbe12bbe6702d7042b)
1 //===- CoverageFilters.cpp - 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 
14 #include "CoverageFilters.h"
15 #include "CoverageSummaryInfo.h"
16 #include "llvm/Support/Regex.h"
17 
18 using namespace llvm;
19 
20 bool NameCoverageFilter::matches(const coverage::CoverageMapping &,
21                                  const coverage::FunctionRecord &Function) {
22   StringRef FuncName = Function.Name;
23   return FuncName.find(Name) != StringRef::npos;
24 }
25 
26 bool NameRegexCoverageFilter::matches(
27     const coverage::CoverageMapping &,
28     const coverage::FunctionRecord &Function) {
29   return llvm::Regex(Regex).match(Function.Name);
30 }
31 
32 bool NameWhitelistCoverageFilter::matches(
33     const coverage::CoverageMapping &,
34     const coverage::FunctionRecord &Function) {
35   return Whitelist.inSection("llvmcov", "whitelist_fun", Function.Name);
36 }
37 
38 bool RegionCoverageFilter::matches(const coverage::CoverageMapping &CM,
39                                    const coverage::FunctionRecord &Function) {
40   return PassesThreshold(FunctionCoverageSummary::get(CM, Function)
41                              .RegionCoverage.getPercentCovered());
42 }
43 
44 bool LineCoverageFilter::matches(const coverage::CoverageMapping &CM,
45                                  const coverage::FunctionRecord &Function) {
46   return PassesThreshold(FunctionCoverageSummary::get(CM, Function)
47                              .LineCoverage.getPercentCovered());
48 }
49 
50 void CoverageFilters::push_back(std::unique_ptr<CoverageFilter> Filter) {
51   Filters.push_back(std::move(Filter));
52 }
53 
54 bool CoverageFilters::matches(const coverage::CoverageMapping &CM,
55                               const coverage::FunctionRecord &Function) {
56   for (const auto &Filter : Filters) {
57     if (Filter->matches(CM, Function))
58       return true;
59   }
60   return false;
61 }
62 
63 bool CoverageFiltersMatchAll::matches(
64     const coverage::CoverageMapping &CM,
65     const coverage::FunctionRecord &Function) {
66   for (const auto &Filter : Filters) {
67     if (!Filter->matches(CM, Function))
68       return false;
69   }
70   return true;
71 }
72