Demangle.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //===-- Demangle.cpp - Common demangling functions ------------------------===//
  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. /// \file This file contains definitions of common demangling functions.
  10. ///
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Demangle/Demangle.h"
  13. #include <cstdlib>
  14. #include <cstring>
  15. static bool isItaniumEncoding(const char *S) {
  16. // Itanium encoding requires 1 or 3 leading underscores, followed by 'Z'.
  17. return std::strncmp(S, "_Z", 2) == 0 || std::strncmp(S, "___Z", 4) == 0;
  18. }
  19. static bool isRustEncoding(const char *S) { return S[0] == '_' && S[1] == 'R'; }
  20. static bool isDLangEncoding(const std::string &MangledName) {
  21. return MangledName.size() >= 2 && MangledName[0] == '_' &&
  22. MangledName[1] == 'D';
  23. }
  24. std::string llvm::demangle(const std::string &MangledName) {
  25. std::string Result;
  26. const char *S = MangledName.c_str();
  27. if (nonMicrosoftDemangle(S, Result))
  28. return Result;
  29. if (S[0] == '_' && nonMicrosoftDemangle(S + 1, Result))
  30. return Result;
  31. if (char *Demangled =
  32. microsoftDemangle(S, nullptr, nullptr, nullptr, nullptr)) {
  33. Result = Demangled;
  34. std::free(Demangled);
  35. return Result;
  36. }
  37. return MangledName;
  38. }
  39. bool llvm::nonMicrosoftDemangle(const char *MangledName, std::string &Result) {
  40. char *Demangled = nullptr;
  41. if (isItaniumEncoding(MangledName))
  42. Demangled = itaniumDemangle(MangledName, nullptr, nullptr, nullptr);
  43. else if (isRustEncoding(MangledName))
  44. Demangled = rustDemangle(MangledName);
  45. else if (isDLangEncoding(MangledName))
  46. Demangled = dlangDemangle(MangledName);
  47. if (!Demangled)
  48. return false;
  49. Result = Demangled;
  50. std::free(Demangled);
  51. return true;
  52. }