clang  16.0.0git
ProfileList.cpp
Go to the documentation of this file.
1 //===--- ProfileList.h - ProfileList filter ---------------------*- C++ -*-===//
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 // User-provided filters include/exclude profile instrumentation in certain
10 // functions or files.
11 //
12 //===----------------------------------------------------------------------===//
13 
17 #include "llvm/Support/SpecialCaseList.h"
18 
19 #include "llvm/Support/raw_ostream.h"
20 
21 using namespace clang;
22 
23 namespace clang {
24 
25 class ProfileSpecialCaseList : public llvm::SpecialCaseList {
26 public:
27  static std::unique_ptr<ProfileSpecialCaseList>
28  create(const std::vector<std::string> &Paths, llvm::vfs::FileSystem &VFS,
30 
31  static std::unique_ptr<ProfileSpecialCaseList>
32  createOrDie(const std::vector<std::string> &Paths,
33  llvm::vfs::FileSystem &VFS);
34 
35  bool isEmpty() const { return Sections.empty(); }
36 
37  bool hasPrefix(StringRef Prefix) const {
38  for (auto &SectionIter : Sections)
39  if (SectionIter.Entries.count(Prefix) > 0)
40  return true;
41  return false;
42  }
43 };
44 
45 std::unique_ptr<ProfileSpecialCaseList>
46 ProfileSpecialCaseList::create(const std::vector<std::string> &Paths,
47  llvm::vfs::FileSystem &VFS,
48  std::string &Error) {
49  auto PSCL = std::make_unique<ProfileSpecialCaseList>();
50  if (PSCL->createInternal(Paths, VFS, Error))
51  return PSCL;
52  return nullptr;
53 }
54 
55 std::unique_ptr<ProfileSpecialCaseList>
56 ProfileSpecialCaseList::createOrDie(const std::vector<std::string> &Paths,
57  llvm::vfs::FileSystem &VFS) {
59  if (auto PSCL = create(Paths, VFS, Error))
60  return PSCL;
61  llvm::report_fatal_error(llvm::Twine(Error));
62 }
63 
64 }
65 
66 ProfileList::ProfileList(ArrayRef<std::string> Paths, SourceManager &SM)
67  : SCL(ProfileSpecialCaseList::createOrDie(
68  Paths, SM.getFileManager().getVirtualFileSystem())),
69  Empty(SCL->isEmpty()), SM(SM) {}
70 
71 ProfileList::~ProfileList() = default;
72 
74  switch (Kind) {
76  return "";
78  return "clang";
80  return "llvm";
82  return "csllvm";
83  }
84  llvm_unreachable("Unhandled CodeGenOptions::ProfileInstrKind enum");
85 }
86 
89  StringRef Section = getSectionName(Kind);
90  // Check for "default:<type>"
91  if (SCL->inSection(Section, "default", "allow"))
92  return Allow;
93  if (SCL->inSection(Section, "default", "skip"))
94  return Skip;
95  if (SCL->inSection(Section, "default", "forbid"))
96  return Forbid;
97  // If any cases use "fun" or "src", set the default to FORBID.
98  if (SCL->hasPrefix("fun") || SCL->hasPrefix("src"))
99  return Forbid;
100  return Allow;
101 }
102 
104 ProfileList::inSection(StringRef Section, StringRef Prefix,
105  StringRef Query) const {
106  if (SCL->inSection(Section, Prefix, Query, "allow"))
107  return Allow;
108  if (SCL->inSection(Section, Prefix, Query, "skip"))
109  return Skip;
110  if (SCL->inSection(Section, Prefix, Query, "forbid"))
111  return Forbid;
112  if (SCL->inSection(Section, Prefix, Query))
113  return Allow;
114  return None;
115 }
116 
118 ProfileList::isFunctionExcluded(StringRef FunctionName,
120  StringRef Section = getSectionName(Kind);
121  // Check for "function:<regex>=<case>"
122  if (auto V = inSection(Section, "function", FunctionName))
123  return V;
124  if (SCL->inSection(Section, "!fun", FunctionName))
125  return Forbid;
126  if (SCL->inSection(Section, "fun", FunctionName))
127  return Allow;
128  return None;
129 }
130 
134  return isFileExcluded(SM.getFilename(SM.getFileLoc(Loc)), Kind);
135 }
136 
138 ProfileList::isFileExcluded(StringRef FileName,
140  StringRef Section = getSectionName(Kind);
141  // Check for "source:<regex>=<case>"
142  if (auto V = inSection(Section, "source", FileName))
143  return V;
144  if (SCL->inSection(Section, "!src", FileName))
145  return Forbid;
146  if (SCL->inSection(Section, "src", FileName))
147  return Allow;
148  return None;
149 }
clang::ProfileList::isLocationExcluded
llvm::Optional< ExclusionType > isLocationExcluded(SourceLocation Loc, CodeGenOptions::ProfileInstrKind Kind) const
Definition: ProfileList.cpp:132
clang::ProfileList::Skip
@ Skip
Profiling is skipped using the skipprofile attribute.
Definition: ProfileList.h:35
clang::SourceManager::getFilename
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
Definition: SourceManager.cpp:1021
Error
llvm::Error Error
Definition: ByteCodeEmitter.cpp:20
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
ProfileList.h
clang::SourceManager::getFileLoc
SourceLocation getFileLoc(SourceLocation Loc) const
Given Loc, if it is a macro location return the expansion location or the spelling location,...
Definition: SourceManager.h:1175
clang::ProfileList::ExclusionType
ExclusionType
Represents if an how something should be excluded from profiling.
Definition: ProfileList.h:31
llvm::Optional
Definition: LLVM.h:40
SourceManager.h
clang::CodeGenOptions::ProfileInstrKind
ProfileInstrKind
Definition: CodeGenOptions.h:85
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:636
V
#define V(N, I)
Definition: ASTContext.h:3235
clang::CodeGenOptions::ProfileNone
@ ProfileNone
Definition: CodeGenOptions.h:86
clang::ProfileSpecialCaseList
Definition: ProfileList.cpp:25
clang::CodeGenOptions::ProfileClangInstr
@ ProfileClangInstr
Definition: CodeGenOptions.h:87
clang::ProfileList::isFileExcluded
llvm::Optional< ExclusionType > isFileExcluded(StringRef FileName, CodeGenOptions::ProfileInstrKind Kind) const
Definition: ProfileList.cpp:138
clang::ProfileSpecialCaseList::createOrDie
static std::unique_ptr< ProfileSpecialCaseList > createOrDie(const std::vector< std::string > &Paths, llvm::vfs::FileSystem &VFS)
Definition: ProfileList.cpp:56
clang::ProfileSpecialCaseList::isEmpty
bool isEmpty() const
Definition: ProfileList.cpp:35
clang::ProfileList::~ProfileList
~ProfileList()
clang::ProfileSpecialCaseList::hasPrefix
bool hasPrefix(StringRef Prefix) const
Definition: ProfileList.cpp:37
clang::ProfileList::getDefault
ExclusionType getDefault(CodeGenOptions::ProfileInstrKind Kind) const
Definition: ProfileList.cpp:88
llvm::ArrayRef< std::string >
clang::ObjCPropertyAttribute::Kind
Kind
Definition: DeclObjCCommon.h:22
clang
Definition: CalledOnceCheck.h:17
clang::ProfileList::isFunctionExcluded
llvm::Optional< ExclusionType > isFunctionExcluded(StringRef FunctionName, CodeGenOptions::ProfileInstrKind Kind) const
Definition: ProfileList.cpp:118
clang::CodeGenOptions::ProfileIRInstr
@ ProfileIRInstr
Definition: CodeGenOptions.h:89
getSectionName
static StringRef getSectionName(CodeGenOptions::ProfileInstrKind Kind)
Definition: ProfileList.cpp:73
clang::ProfileList::Forbid
@ Forbid
Profiling is forbidden using the noprofile attribute.
Definition: ProfileList.h:37
clang::ProfileList::Allow
@ Allow
Profiling is allowed.
Definition: ProfileList.h:33
FileManager.h
clang::ProfileSpecialCaseList::create
static std::unique_ptr< ProfileSpecialCaseList > create(const std::vector< std::string > &Paths, llvm::vfs::FileSystem &VFS, std::string &Error)
Definition: ProfileList.cpp:46
SM
#define SM(sm)
Definition: Cuda.cpp:79
clang::CodeGenOptions::ProfileCSIRInstr
@ ProfileCSIRInstr
Definition: CodeGenOptions.h:90