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