1 1.1 joerg //===--- ProfileList.h - ProfileList filter ---------------------*- C++ -*-===// 2 1.1 joerg // 3 1.1 joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 1.1 joerg // See https://llvm.org/LICENSE.txt for license information. 5 1.1 joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 1.1 joerg // 7 1.1 joerg //===----------------------------------------------------------------------===// 8 1.1 joerg // 9 1.1 joerg // User-provided filters include/exclude profile instrumentation in certain 10 1.1 joerg // functions or files. 11 1.1 joerg // 12 1.1 joerg //===----------------------------------------------------------------------===// 13 1.1 joerg 14 1.1 joerg #include "clang/Basic/ProfileList.h" 15 1.1 joerg #include "clang/Basic/FileManager.h" 16 1.1 joerg #include "clang/Basic/SourceManager.h" 17 1.1 joerg #include "llvm/Support/SpecialCaseList.h" 18 1.1 joerg 19 1.1 joerg #include "llvm/Support/raw_ostream.h" 20 1.1 joerg 21 1.1 joerg using namespace clang; 22 1.1 joerg 23 1.1 joerg namespace clang { 24 1.1 joerg 25 1.1 joerg class ProfileSpecialCaseList : public llvm::SpecialCaseList { 26 1.1 joerg public: 27 1.1 joerg static std::unique_ptr<ProfileSpecialCaseList> 28 1.1 joerg create(const std::vector<std::string> &Paths, llvm::vfs::FileSystem &VFS, 29 1.1 joerg std::string &Error); 30 1.1 joerg 31 1.1 joerg static std::unique_ptr<ProfileSpecialCaseList> 32 1.1 joerg createOrDie(const std::vector<std::string> &Paths, 33 1.1 joerg llvm::vfs::FileSystem &VFS); 34 1.1 joerg 35 1.1 joerg bool isEmpty() const { return Sections.empty(); } 36 1.1 joerg 37 1.1 joerg bool hasPrefix(StringRef Prefix) const { 38 1.1 joerg for (auto &SectionIter : Sections) 39 1.1 joerg if (SectionIter.Entries.count(Prefix) > 0) 40 1.1 joerg return true; 41 1.1 joerg return false; 42 1.1 joerg } 43 1.1 joerg }; 44 1.1 joerg 45 1.1 joerg std::unique_ptr<ProfileSpecialCaseList> 46 1.1 joerg ProfileSpecialCaseList::create(const std::vector<std::string> &Paths, 47 1.1 joerg llvm::vfs::FileSystem &VFS, 48 1.1 joerg std::string &Error) { 49 1.1 joerg auto PSCL = std::make_unique<ProfileSpecialCaseList>(); 50 1.1 joerg if (PSCL->createInternal(Paths, VFS, Error)) 51 1.1 joerg return PSCL; 52 1.1 joerg return nullptr; 53 1.1 joerg } 54 1.1 joerg 55 1.1 joerg std::unique_ptr<ProfileSpecialCaseList> 56 1.1 joerg ProfileSpecialCaseList::createOrDie(const std::vector<std::string> &Paths, 57 1.1 joerg llvm::vfs::FileSystem &VFS) { 58 1.1 joerg std::string Error; 59 1.1 joerg if (auto PSCL = create(Paths, VFS, Error)) 60 1.1 joerg return PSCL; 61 1.1 joerg llvm::report_fatal_error(Error); 62 1.1 joerg } 63 1.1 joerg 64 1.1 joerg } 65 1.1 joerg 66 1.1 joerg ProfileList::ProfileList(ArrayRef<std::string> Paths, SourceManager &SM) 67 1.1 joerg : SCL(ProfileSpecialCaseList::createOrDie( 68 1.1 joerg Paths, SM.getFileManager().getVirtualFileSystem())), 69 1.1 joerg Empty(SCL->isEmpty()), 70 1.1 joerg Default(SCL->hasPrefix("fun") || SCL->hasPrefix("src")), SM(SM) {} 71 1.1 joerg 72 1.1 joerg ProfileList::~ProfileList() = default; 73 1.1 joerg 74 1.1 joerg static StringRef getSectionName(CodeGenOptions::ProfileInstrKind Kind) { 75 1.1 joerg switch (Kind) { 76 1.1 joerg case CodeGenOptions::ProfileNone: 77 1.1 joerg return ""; 78 1.1 joerg case CodeGenOptions::ProfileClangInstr: 79 1.1 joerg return "clang"; 80 1.1 joerg case CodeGenOptions::ProfileIRInstr: 81 1.1 joerg return "llvm"; 82 1.1 joerg case CodeGenOptions::ProfileCSIRInstr: 83 1.1 joerg return "csllvm"; 84 1.1 joerg } 85 1.1 joerg llvm_unreachable("Unhandled CodeGenOptions::ProfileInstrKind enum"); 86 1.1 joerg } 87 1.1 joerg 88 1.1 joerg llvm::Optional<bool> 89 1.1 joerg ProfileList::isFunctionExcluded(StringRef FunctionName, 90 1.1 joerg CodeGenOptions::ProfileInstrKind Kind) const { 91 1.1 joerg StringRef Section = getSectionName(Kind); 92 1.1 joerg if (SCL->inSection(Section, "!fun", FunctionName)) 93 1.1 joerg return true; 94 1.1 joerg if (SCL->inSection(Section, "fun", FunctionName)) 95 1.1 joerg return false; 96 1.1 joerg return None; 97 1.1 joerg } 98 1.1 joerg 99 1.1 joerg llvm::Optional<bool> 100 1.1 joerg ProfileList::isLocationExcluded(SourceLocation Loc, 101 1.1 joerg CodeGenOptions::ProfileInstrKind Kind) const { 102 1.1 joerg return isFileExcluded(SM.getFilename(SM.getFileLoc(Loc)), Kind); 103 1.1 joerg } 104 1.1 joerg 105 1.1 joerg llvm::Optional<bool> 106 1.1 joerg ProfileList::isFileExcluded(StringRef FileName, 107 1.1 joerg CodeGenOptions::ProfileInstrKind Kind) const { 108 1.1 joerg StringRef Section = getSectionName(Kind); 109 1.1 joerg if (SCL->inSection(Section, "!src", FileName)) 110 1.1 joerg return true; 111 1.1 joerg if (SCL->inSection(Section, "src", FileName)) 112 1.1 joerg return false; 113 1.1 joerg return None; 114 1.1 joerg } 115