1 //===- CoverageFilters.cpp - Function coverage mapping filters ------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // These classes provide filtering for function coverage mapping records.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CoverageFilters.h"
14 #include "CoverageSummaryInfo.h"
15 #include "llvm/Support/Regex.h"
16 #include "llvm/Support/SpecialCaseList.h"
17 
18 using namespace llvm;
19 
20 bool NameCoverageFilter::matches(
21     const coverage::CoverageMapping &,
22     const coverage::FunctionRecord &Function) const {
23   StringRef FuncName = Function.Name;
24   return FuncName.contains(Name);
25 }
26 
27 bool NameRegexCoverageFilter::matches(
28     const coverage::CoverageMapping &,
29     const coverage::FunctionRecord &Function) const {
30   return llvm::Regex(Regex).match(Function.Name);
31 }
32 
33 bool NameRegexCoverageFilter::matchesFilename(StringRef Filename) const {
34   return llvm::Regex(Regex).match(Filename);
35 }
36 
37 bool NameAllowlistCoverageFilter::matches(
38     const coverage::CoverageMapping &,
39     const coverage::FunctionRecord &Function) const {
40   return Allowlist.inSection("llvmcov", "allowlist_fun", Function.Name);
41 }
42 
43 // TODO: remove this when -name-whitelist option is removed.
44 bool NameWhitelistCoverageFilter::matches(
45     const coverage::CoverageMapping &,
46     const coverage::FunctionRecord &Function) const {
47   return Whitelist.inSection("llvmcov", "whitelist_fun", Function.Name);
48 }
49 
50 bool RegionCoverageFilter::matches(
51     const coverage::CoverageMapping &CM,
52     const coverage::FunctionRecord &Function) const {
53   return PassesThreshold(FunctionCoverageSummary::get(CM, Function)
54                              .RegionCoverage.getPercentCovered());
55 }
56 
57 bool LineCoverageFilter::matches(
58     const coverage::CoverageMapping &CM,
59     const coverage::FunctionRecord &Function) const {
60   return PassesThreshold(FunctionCoverageSummary::get(CM, Function)
61                              .LineCoverage.getPercentCovered());
62 }
63 
64 void CoverageFilters::push_back(std::unique_ptr<CoverageFilter> Filter) {
65   Filters.push_back(std::move(Filter));
66 }
67 
68 bool CoverageFilters::matches(const coverage::CoverageMapping &CM,
69                               const coverage::FunctionRecord &Function) const {
70   for (const auto &Filter : Filters) {
71     if (Filter->matches(CM, Function))
72       return true;
73   }
74   return false;
75 }
76 
77 bool CoverageFilters::matchesFilename(StringRef Filename) const {
78   for (const auto &Filter : Filters) {
79     if (Filter->matchesFilename(Filename))
80       return true;
81   }
82   return false;
83 }
84 
85 bool CoverageFiltersMatchAll::matches(
86     const coverage::CoverageMapping &CM,
87     const coverage::FunctionRecord &Function) const {
88   for (const auto &Filter : Filters) {
89     if (!Filter->matches(CM, Function))
90       return false;
91   }
92   return true;
93 }
94