SymbolRemappingReader.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SymbolRemappingReader.h - Read symbol remapping file -----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains definitions needed for reading and applying symbol
  15. // remapping files.
  16. //
  17. // Support is provided only for the Itanium C++ name mangling scheme for now.
  18. //
  19. // NOTE: If you are making changes to this file format, please remember
  20. // to document them in the Clang documentation at
  21. // tools/clang/docs/UsersManual.rst.
  22. //
  23. // File format
  24. // -----------
  25. //
  26. // The symbol remappings are written as an ASCII text file. Blank lines and
  27. // lines starting with a # are ignored. All other lines specify a kind of
  28. // mangled name fragment, along with two fragments of that kind that should
  29. // be treated as equivalent, separated by spaces.
  30. //
  31. // See http://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling for a
  32. // description of the Itanium name mangling scheme.
  33. //
  34. // The accepted fragment kinds are:
  35. //
  36. // * name A <name>, such as 6foobar or St3__1
  37. // * type A <type>, such as Ss or N4llvm9StringRefE
  38. // * encoding An <encoding> (a complete mangling without the leading _Z)
  39. //
  40. // For example:
  41. //
  42. // # Ignore int / long differences to treat symbols from 32-bit and 64-bit
  43. // # builds with differing size_t / ptrdiff_t / intptr_t as equivalent.
  44. // type i l
  45. // type j m
  46. //
  47. // # Ignore differences between libc++ and libstdc++, and between libstdc++'s
  48. // # C++98 and C++11 ABIs.
  49. // name 3std St3__1
  50. // name 3std St7__cxx11
  51. //
  52. // # Remap a function overload to a specialization of a template (including
  53. // # any local symbols declared within it).
  54. // encoding N2NS1fEi N2NS1fIiEEvT_
  55. //
  56. // # Substitutions must be remapped separately from namespace 'std' for now.
  57. // name Sa NSt3__19allocatorE
  58. // name Sb NSt3__112basic_stringE
  59. // type Ss NSt3__112basic_stringIcSt11char_traitsIcESaE
  60. // # ...
  61. //
  62. //===----------------------------------------------------------------------===//
  63. #ifndef LLVM_SUPPORT_SYMBOLREMAPPINGREADER_H
  64. #define LLVM_SUPPORT_SYMBOLREMAPPINGREADER_H
  65. #include "llvm/ADT/StringRef.h"
  66. #include "llvm/Support/Error.h"
  67. #include "llvm/Support/ItaniumManglingCanonicalizer.h"
  68. namespace llvm {
  69. class MemoryBuffer;
  70. class SymbolRemappingParseError : public ErrorInfo<SymbolRemappingParseError> {
  71. public:
  72. SymbolRemappingParseError(StringRef File, int64_t Line, const Twine &Message)
  73. : File(File), Line(Line), Message(Message.str()) {}
  74. void log(llvm::raw_ostream &OS) const override {
  75. OS << File << ':' << Line << ": " << Message;
  76. }
  77. std::error_code convertToErrorCode() const override {
  78. return llvm::inconvertibleErrorCode();
  79. }
  80. StringRef getFileName() const { return File; }
  81. int64_t getLineNum() const { return Line; }
  82. StringRef getMessage() const { return Message; }
  83. static char ID;
  84. private:
  85. std::string File;
  86. int64_t Line;
  87. std::string Message;
  88. };
  89. /// Reader for symbol remapping files.
  90. ///
  91. /// Remaps the symbol names in profile data to match those in the program
  92. /// according to a set of rules specified in a given file.
  93. class SymbolRemappingReader {
  94. public:
  95. /// Read remappings from the given buffer, which must live as long as
  96. /// the remapper.
  97. Error read(MemoryBuffer &B);
  98. /// A Key represents an equivalence class of symbol names.
  99. using Key = uintptr_t;
  100. /// Construct a key for the given symbol, or return an existing one if an
  101. /// equivalent name has already been inserted. The symbol name must live
  102. /// as long as the remapper.
  103. ///
  104. /// The result will be Key() if the name cannot be remapped (typically
  105. /// because it is not a valid mangled name).
  106. Key insert(StringRef FunctionName) {
  107. return Canonicalizer.canonicalize(FunctionName);
  108. }
  109. /// Map the given symbol name into the key for the corresponding equivalence
  110. /// class.
  111. ///
  112. /// The result will typically be Key() if no equivalent symbol has been
  113. /// inserted, but this is not guaranteed: a Key different from all keys ever
  114. /// returned by \c insert may be returned instead.
  115. Key lookup(StringRef FunctionName) {
  116. return Canonicalizer.lookup(FunctionName);
  117. }
  118. private:
  119. ItaniumManglingCanonicalizer Canonicalizer;
  120. };
  121. } // end namespace llvm
  122. #endif // LLVM_SUPPORT_SYMBOLREMAPPINGREADER_H
  123. #ifdef __GNUC__
  124. #pragma GCC diagnostic pop
  125. #endif