ProfileList.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include "clang/Basic/ProfileList.h"
  14. #include "clang/Basic/FileManager.h"
  15. #include "clang/Basic/SourceManager.h"
  16. #include "llvm/Support/SpecialCaseList.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. using namespace clang;
  19. namespace clang {
  20. class ProfileSpecialCaseList : public llvm::SpecialCaseList {
  21. public:
  22. static std::unique_ptr<ProfileSpecialCaseList>
  23. create(const std::vector<std::string> &Paths, llvm::vfs::FileSystem &VFS,
  24. std::string &Error);
  25. static std::unique_ptr<ProfileSpecialCaseList>
  26. createOrDie(const std::vector<std::string> &Paths,
  27. llvm::vfs::FileSystem &VFS);
  28. bool isEmpty() const { return Sections.empty(); }
  29. bool hasPrefix(StringRef Prefix) const {
  30. for (auto &SectionIter : Sections)
  31. if (SectionIter.Entries.count(Prefix) > 0)
  32. return true;
  33. return false;
  34. }
  35. };
  36. std::unique_ptr<ProfileSpecialCaseList>
  37. ProfileSpecialCaseList::create(const std::vector<std::string> &Paths,
  38. llvm::vfs::FileSystem &VFS,
  39. std::string &Error) {
  40. auto PSCL = std::make_unique<ProfileSpecialCaseList>();
  41. if (PSCL->createInternal(Paths, VFS, Error))
  42. return PSCL;
  43. return nullptr;
  44. }
  45. std::unique_ptr<ProfileSpecialCaseList>
  46. ProfileSpecialCaseList::createOrDie(const std::vector<std::string> &Paths,
  47. llvm::vfs::FileSystem &VFS) {
  48. std::string Error;
  49. if (auto PSCL = create(Paths, VFS, Error))
  50. return PSCL;
  51. llvm::report_fatal_error(llvm::Twine(Error));
  52. }
  53. }
  54. ProfileList::ProfileList(ArrayRef<std::string> Paths, SourceManager &SM)
  55. : SCL(ProfileSpecialCaseList::createOrDie(
  56. Paths, SM.getFileManager().getVirtualFileSystem())),
  57. Empty(SCL->isEmpty()),
  58. Default(SCL->hasPrefix("fun") || SCL->hasPrefix("src")), SM(SM) {}
  59. ProfileList::~ProfileList() = default;
  60. static StringRef getSectionName(CodeGenOptions::ProfileInstrKind Kind) {
  61. switch (Kind) {
  62. case CodeGenOptions::ProfileNone:
  63. return "";
  64. case CodeGenOptions::ProfileClangInstr:
  65. return "clang";
  66. case CodeGenOptions::ProfileIRInstr:
  67. return "llvm";
  68. case CodeGenOptions::ProfileCSIRInstr:
  69. return "csllvm";
  70. }
  71. llvm_unreachable("Unhandled CodeGenOptions::ProfileInstrKind enum");
  72. }
  73. llvm::Optional<bool>
  74. ProfileList::isFunctionExcluded(StringRef FunctionName,
  75. CodeGenOptions::ProfileInstrKind Kind) const {
  76. StringRef Section = getSectionName(Kind);
  77. if (SCL->inSection(Section, "!fun", FunctionName))
  78. return true;
  79. if (SCL->inSection(Section, "fun", FunctionName))
  80. return false;
  81. return None;
  82. }
  83. llvm::Optional<bool>
  84. ProfileList::isLocationExcluded(SourceLocation Loc,
  85. CodeGenOptions::ProfileInstrKind Kind) const {
  86. return isFileExcluded(SM.getFilename(SM.getFileLoc(Loc)), Kind);
  87. }
  88. llvm::Optional<bool>
  89. ProfileList::isFileExcluded(StringRef FileName,
  90. CodeGenOptions::ProfileInstrKind Kind) const {
  91. StringRef Section = getSectionName(Kind);
  92. if (SCL->inSection(Section, "!src", FileName))
  93. return true;
  94. if (SCL->inSection(Section, "src", FileName))
  95. return false;
  96. return None;
  97. }