1e82d89ccSAlex Lorenz //===- CoverageFilters.cpp - Function coverage mapping filters ------------===//
2e82d89ccSAlex Lorenz //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e82d89ccSAlex Lorenz //
7e82d89ccSAlex Lorenz //===----------------------------------------------------------------------===//
8e82d89ccSAlex Lorenz //
9e82d89ccSAlex Lorenz // These classes provide filtering for function coverage mapping records.
10e82d89ccSAlex Lorenz //
11e82d89ccSAlex Lorenz //===----------------------------------------------------------------------===//
12e82d89ccSAlex Lorenz 
13e82d89ccSAlex Lorenz #include "CoverageFilters.h"
14e82d89ccSAlex Lorenz #include "CoverageSummaryInfo.h"
15e82d89ccSAlex Lorenz #include "llvm/Support/Regex.h"
166022efb0SSimon Pilgrim #include "llvm/Support/SpecialCaseList.h"
17e82d89ccSAlex Lorenz 
18e82d89ccSAlex Lorenz using namespace llvm;
19e82d89ccSAlex Lorenz 
matches(const coverage::CoverageMapping &,const coverage::FunctionRecord & Function) const2025ea19eaSSean Eveson bool NameCoverageFilter::matches(
2125ea19eaSSean Eveson     const coverage::CoverageMapping &,
2225ea19eaSSean Eveson     const coverage::FunctionRecord &Function) const {
2384ae925fSJustin Bogner   StringRef FuncName = Function.Name;
244e3eebc6SKazu Hirata   return FuncName.contains(Name);
25e82d89ccSAlex Lorenz }
26e82d89ccSAlex Lorenz 
matches(const coverage::CoverageMapping &,const coverage::FunctionRecord & Function) const27b7fdaf2cSVedant Kumar bool NameRegexCoverageFilter::matches(
28b7fdaf2cSVedant Kumar     const coverage::CoverageMapping &,
2925ea19eaSSean Eveson     const coverage::FunctionRecord &Function) const {
3084ae925fSJustin Bogner   return llvm::Regex(Regex).match(Function.Name);
31e82d89ccSAlex Lorenz }
32e82d89ccSAlex Lorenz 
matchesFilename(StringRef Filename) const334220f891SMax Moroz bool NameRegexCoverageFilter::matchesFilename(StringRef Filename) const {
344220f891SMax Moroz   return llvm::Regex(Regex).match(Filename);
354220f891SMax Moroz }
364220f891SMax Moroz 
matches(const coverage::CoverageMapping &,const coverage::FunctionRecord & Function) const37*e714394aSZarko Todorovski bool NameAllowlistCoverageFilter::matches(
38*e714394aSZarko Todorovski     const coverage::CoverageMapping &,
39*e714394aSZarko Todorovski     const coverage::FunctionRecord &Function) const {
40*e714394aSZarko Todorovski   return Allowlist.inSection("llvmcov", "allowlist_fun", Function.Name);
41*e714394aSZarko Todorovski }
42*e714394aSZarko Todorovski 
43*e714394aSZarko Todorovski // TODO: remove this when -name-whitelist option is removed.
matches(const coverage::CoverageMapping &,const coverage::FunctionRecord & Function) const44e15300ecSSean Eveson bool NameWhitelistCoverageFilter::matches(
45b7fdaf2cSVedant Kumar     const coverage::CoverageMapping &,
4625ea19eaSSean Eveson     const coverage::FunctionRecord &Function) const {
47998b220eSVlad Tsyrklevich   return Whitelist.inSection("llvmcov", "whitelist_fun", Function.Name);
48e15300ecSSean Eveson }
49e15300ecSSean Eveson 
matches(const coverage::CoverageMapping & CM,const coverage::FunctionRecord & Function) const5025ea19eaSSean Eveson bool RegionCoverageFilter::matches(
5125ea19eaSSean Eveson     const coverage::CoverageMapping &CM,
5225ea19eaSSean Eveson     const coverage::FunctionRecord &Function) const {
53b7fdaf2cSVedant Kumar   return PassesThreshold(FunctionCoverageSummary::get(CM, Function)
54e82d89ccSAlex Lorenz                              .RegionCoverage.getPercentCovered());
55e82d89ccSAlex Lorenz }
56e82d89ccSAlex Lorenz 
matches(const coverage::CoverageMapping & CM,const coverage::FunctionRecord & Function) const5725ea19eaSSean Eveson bool LineCoverageFilter::matches(
5825ea19eaSSean Eveson     const coverage::CoverageMapping &CM,
5925ea19eaSSean Eveson     const coverage::FunctionRecord &Function) const {
60b7fdaf2cSVedant Kumar   return PassesThreshold(FunctionCoverageSummary::get(CM, Function)
61b7fdaf2cSVedant Kumar                              .LineCoverage.getPercentCovered());
62e82d89ccSAlex Lorenz }
63e82d89ccSAlex Lorenz 
push_back(std::unique_ptr<CoverageFilter> Filter)64e82d89ccSAlex Lorenz void CoverageFilters::push_back(std::unique_ptr<CoverageFilter> Filter) {
65e82d89ccSAlex Lorenz   Filters.push_back(std::move(Filter));
66e82d89ccSAlex Lorenz }
67e82d89ccSAlex Lorenz 
matches(const coverage::CoverageMapping & CM,const coverage::FunctionRecord & Function) const68b7fdaf2cSVedant Kumar bool CoverageFilters::matches(const coverage::CoverageMapping &CM,
6925ea19eaSSean Eveson                               const coverage::FunctionRecord &Function) const {
70e82d89ccSAlex Lorenz   for (const auto &Filter : Filters) {
71b7fdaf2cSVedant Kumar     if (Filter->matches(CM, Function))
72e82d89ccSAlex Lorenz       return true;
73e82d89ccSAlex Lorenz   }
74e82d89ccSAlex Lorenz   return false;
75e82d89ccSAlex Lorenz }
76e82d89ccSAlex Lorenz 
matchesFilename(StringRef Filename) const774220f891SMax Moroz bool CoverageFilters::matchesFilename(StringRef Filename) const {
784220f891SMax Moroz   for (const auto &Filter : Filters) {
794220f891SMax Moroz     if (Filter->matchesFilename(Filename))
804220f891SMax Moroz       return true;
814220f891SMax Moroz   }
824220f891SMax Moroz   return false;
834220f891SMax Moroz }
844220f891SMax Moroz 
matches(const coverage::CoverageMapping & CM,const coverage::FunctionRecord & Function) const85b7fdaf2cSVedant Kumar bool CoverageFiltersMatchAll::matches(
86b7fdaf2cSVedant Kumar     const coverage::CoverageMapping &CM,
8725ea19eaSSean Eveson     const coverage::FunctionRecord &Function) const {
88e82d89ccSAlex Lorenz   for (const auto &Filter : Filters) {
89b7fdaf2cSVedant Kumar     if (!Filter->matches(CM, Function))
90e82d89ccSAlex Lorenz       return false;
91e82d89ccSAlex Lorenz   }
92e82d89ccSAlex Lorenz   return true;
93e82d89ccSAlex Lorenz }
94