demangle.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef SUPPORT_DEMANGLE_H
  10. #define SUPPORT_DEMANGLE_H
  11. #include "test_macros.h"
  12. #include <string>
  13. #include <cstdlib>
  14. #if !defined(TEST_HAS_NO_DEMANGLE)
  15. # if defined(__GNUC__) || defined(__clang__)
  16. # if __has_include("cxxabi.h") && !defined(_LIBCPP_ABI_MICROSOFT)
  17. # include "cxxabi.h"
  18. # else
  19. # define TEST_HAS_NO_DEMANGLE
  20. # endif
  21. # else
  22. # define TEST_HAS_NO_DEMANGLE
  23. # endif
  24. #endif
  25. #if defined(TEST_HAS_NO_DEMANGLE)
  26. inline std::string demangle(const char* mangled_name) {
  27. return mangled_name;
  28. }
  29. #else
  30. template <size_t N> struct Printer;
  31. inline std::string demangle(const char* mangled_name) {
  32. int status = 0;
  33. char* out = __cxxabiv1::__cxa_demangle(mangled_name, nullptr, nullptr, &status);
  34. if (out != nullptr) {
  35. std::string res(out);
  36. std::free(out);
  37. return res;
  38. }
  39. return mangled_name;
  40. }
  41. #endif
  42. #endif // SUPPORT_DEMANGLE_H