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