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 
matches(const coverage::CoverageMapping &,const coverage::FunctionRecord & Function) const20 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 
matches(const coverage::CoverageMapping &,const coverage::FunctionRecord & Function) const27 bool NameRegexCoverageFilter::matches(
28     const coverage::CoverageMapping &,
29     const coverage::FunctionRecord &Function) const {
30   return llvm::Regex(Regex).match(Function.Name);
31 }
32 
matchesFilename(StringRef Filename) const33 bool NameRegexCoverageFilter::matchesFilename(StringRef Filename) const {
34   return llvm::Regex(Regex).match(Filename);
35 }
36 
matches(const coverage::CoverageMapping &,const coverage::FunctionRecord & Function) const37 bool NameAllowlistCoverageFilter::matches(
38     const coverage::CoverageMapping &,
39     const coverage::FunctionRecord &Function) const {
40   return Allowlist.inSection("llvmcov", "allowlist_fun", Function.Name);
41 }
42 
matches(const coverage::CoverageMapping & CM,const coverage::FunctionRecord & Function) const43 bool RegionCoverageFilter::matches(
44     const coverage::CoverageMapping &CM,
45     const coverage::FunctionRecord &Function) const {
46   return PassesThreshold(FunctionCoverageSummary::get(CM, Function)
47                              .RegionCoverage.getPercentCovered());
48 }
49 
matches(const coverage::CoverageMapping & CM,const coverage::FunctionRecord & Function) const50 bool LineCoverageFilter::matches(
51     const coverage::CoverageMapping &CM,
52     const coverage::FunctionRecord &Function) const {
53   return PassesThreshold(FunctionCoverageSummary::get(CM, Function)
54                              .LineCoverage.getPercentCovered());
55 }
56 
push_back(std::unique_ptr<CoverageFilter> Filter)57 void CoverageFilters::push_back(std::unique_ptr<CoverageFilter> Filter) {
58   Filters.push_back(std::move(Filter));
59 }
60 
matches(const coverage::CoverageMapping & CM,const coverage::FunctionRecord & Function) const61 bool CoverageFilters::matches(const coverage::CoverageMapping &CM,
62                               const coverage::FunctionRecord &Function) const {
63   for (const auto &Filter : Filters) {
64     if (Filter->matches(CM, Function))
65       return true;
66   }
67   return false;
68 }
69 
matchesFilename(StringRef Filename) const70 bool CoverageFilters::matchesFilename(StringRef Filename) const {
71   for (const auto &Filter : Filters) {
72     if (Filter->matchesFilename(Filename))
73       return true;
74   }
75   return false;
76 }
77 
matches(const coverage::CoverageMapping & CM,const coverage::FunctionRecord & Function) const78 bool CoverageFiltersMatchAll::matches(
79     const coverage::CoverageMapping &CM,
80     const coverage::FunctionRecord &Function) const {
81   for (const auto &Filter : Filters) {
82     if (!Filter->matches(CM, Function))
83       return false;
84   }
85   return true;
86 }
87