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
18using namespace llvm;
19
20bool NameCoverageFilter::matches(
21 const coverage::CoverageMapping &,
22 const coverage::FunctionRecord &Function) const {
23 StringRef FuncName = Function.Name;
24 return FuncName.contains(Other: Name);
25}
26
27bool NameRegexCoverageFilter::matches(
28 const coverage::CoverageMapping &,
29 const coverage::FunctionRecord &Function) const {
30 return llvm::Regex(Regex).match(String: Function.Name);
31}
32
33bool NameRegexCoverageFilter::matchesFilename(StringRef Filename) const {
34 bool regex_match = llvm::Regex(Regex).match(String: Filename);
35 return Type == FilterType::Exclude ? regex_match : !regex_match;
36}
37
38bool NameAllowlistCoverageFilter::matches(
39 const coverage::CoverageMapping &,
40 const coverage::FunctionRecord &Function) const {
41 return Allowlist.inSection(Section: "llvmcov", Prefix: "allowlist_fun", Query: Function.Name);
42}
43
44bool RegionCoverageFilter::matches(
45 const coverage::CoverageMapping &CM,
46 const coverage::FunctionRecord &Function) const {
47 return PassesThreshold(Value: FunctionCoverageSummary::get(CM, Function)
48 .RegionCoverage.getPercentCovered());
49}
50
51bool LineCoverageFilter::matches(
52 const coverage::CoverageMapping &CM,
53 const coverage::FunctionRecord &Function) const {
54 return PassesThreshold(Value: FunctionCoverageSummary::get(CM, Function)
55 .LineCoverage.getPercentCovered());
56}
57
58void CoverageFilters::push_back(std::unique_ptr<CoverageFilter> Filter) {
59 Filters.push_back(x: std::move(Filter));
60}
61
62bool CoverageFilters::matches(const coverage::CoverageMapping &CM,
63 const coverage::FunctionRecord &Function) const {
64 for (const auto &Filter : Filters) {
65 if (Filter->matches(CM, Function))
66 return true;
67 }
68 return false;
69}
70
71bool CoverageFilters::matchesFilename(StringRef Filename) const {
72 for (const auto &Filter : Filters) {
73 if (Filter->matchesFilename(Filename))
74 return true;
75 }
76 return false;
77}
78
79bool CoverageFiltersMatchAll::matches(
80 const coverage::CoverageMapping &CM,
81 const coverage::FunctionRecord &Function) const {
82 for (const auto &Filter : Filters) {
83 if (!Filter->matches(CM, Function))
84 return false;
85 }
86 return true;
87}
88