Demangle.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- Demangle.h ---------------------------------------------*- 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. #ifndef LLVM_DEMANGLE_DEMANGLE_H
  14. #define LLVM_DEMANGLE_DEMANGLE_H
  15. #include <cstddef>
  16. #include <string>
  17. namespace llvm {
  18. /// This is a llvm local version of __cxa_demangle. Other than the name and
  19. /// being in the llvm namespace it is identical.
  20. ///
  21. /// The mangled_name is demangled into buf and returned. If the buffer is not
  22. /// large enough, realloc is used to expand it.
  23. ///
  24. /// The *status will be set to a value from the following enumeration
  25. enum : int {
  26. demangle_unknown_error = -4,
  27. demangle_invalid_args = -3,
  28. demangle_invalid_mangled_name = -2,
  29. demangle_memory_alloc_failure = -1,
  30. demangle_success = 0,
  31. };
  32. char *itaniumDemangle(const char *mangled_name, char *buf, size_t *n,
  33. int *status);
  34. enum MSDemangleFlags {
  35. MSDF_None = 0,
  36. MSDF_DumpBackrefs = 1 << 0,
  37. MSDF_NoAccessSpecifier = 1 << 1,
  38. MSDF_NoCallingConvention = 1 << 2,
  39. MSDF_NoReturnType = 1 << 3,
  40. MSDF_NoMemberType = 1 << 4,
  41. MSDF_NoVariableType = 1 << 5,
  42. };
  43. /// Demangles the Microsoft symbol pointed at by mangled_name and returns it.
  44. /// Returns a pointer to the start of a null-terminated demangled string on
  45. /// success, or nullptr on error.
  46. /// If n_read is non-null and demangling was successful, it receives how many
  47. /// bytes of the input string were consumed.
  48. /// buf can point to a *n_buf bytes large buffer where the demangled name is
  49. /// stored. If the buffer is too small, it is grown with realloc(). If buf is
  50. /// nullptr, then this malloc()s memory for the result.
  51. /// *n_buf stores the size of buf on input if buf is non-nullptr, and it
  52. /// receives the size of the demangled string on output if n_buf is not nullptr.
  53. /// status receives one of the demangle_ enum entries above if it's not nullptr.
  54. /// Flags controls various details of the demangled representation.
  55. char *microsoftDemangle(const char *mangled_name, size_t *n_read, char *buf,
  56. size_t *n_buf, int *status,
  57. MSDemangleFlags Flags = MSDF_None);
  58. // Demangles a Rust v0 mangled symbol.
  59. char *rustDemangle(const char *MangledName);
  60. // Demangles a D mangled symbol.
  61. char *dlangDemangle(const char *MangledName);
  62. /// Attempt to demangle a string using different demangling schemes.
  63. /// The function uses heuristics to determine which demangling scheme to use.
  64. /// \param MangledName - reference to string to demangle.
  65. /// \returns - the demangled string, or a copy of the input string if no
  66. /// demangling occurred.
  67. std::string demangle(const std::string &MangledName);
  68. bool nonMicrosoftDemangle(const char *MangledName, std::string &Result);
  69. /// "Partial" demangler. This supports demangling a string into an AST
  70. /// (typically an intermediate stage in itaniumDemangle) and querying certain
  71. /// properties or partially printing the demangled name.
  72. struct ItaniumPartialDemangler {
  73. ItaniumPartialDemangler();
  74. ItaniumPartialDemangler(ItaniumPartialDemangler &&Other);
  75. ItaniumPartialDemangler &operator=(ItaniumPartialDemangler &&Other);
  76. /// Demangle into an AST. Subsequent calls to the rest of the member functions
  77. /// implicitly operate on the AST this produces.
  78. /// \return true on error, false otherwise
  79. bool partialDemangle(const char *MangledName);
  80. /// Just print the entire mangled name into Buf. Buf and N behave like the
  81. /// second and third parameters to itaniumDemangle.
  82. char *finishDemangle(char *Buf, size_t *N) const;
  83. /// Get the base name of a function. This doesn't include trailing template
  84. /// arguments, ie for "a::b<int>" this function returns "b".
  85. char *getFunctionBaseName(char *Buf, size_t *N) const;
  86. /// Get the context name for a function. For "a::b::c", this function returns
  87. /// "a::b".
  88. char *getFunctionDeclContextName(char *Buf, size_t *N) const;
  89. /// Get the entire name of this function.
  90. char *getFunctionName(char *Buf, size_t *N) const;
  91. /// Get the parameters for this function.
  92. char *getFunctionParameters(char *Buf, size_t *N) const;
  93. char *getFunctionReturnType(char *Buf, size_t *N) const;
  94. /// If this function has any any cv or reference qualifiers. These imply that
  95. /// the function is a non-static member function.
  96. bool hasFunctionQualifiers() const;
  97. /// If this symbol describes a constructor or destructor.
  98. bool isCtorOrDtor() const;
  99. /// If this symbol describes a function.
  100. bool isFunction() const;
  101. /// If this symbol describes a variable.
  102. bool isData() const;
  103. /// If this symbol is a <special-name>. These are generally implicitly
  104. /// generated by the implementation, such as vtables and typeinfo names.
  105. bool isSpecialName() const;
  106. ~ItaniumPartialDemangler();
  107. private:
  108. void *RootNode;
  109. void *Context;
  110. };
  111. } // namespace llvm
  112. #endif
  113. #ifdef __GNUC__
  114. #pragma GCC diagnostic pop
  115. #endif