type_name.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "platform.h"
  2. #include "demangle_impl.h"
  3. #ifdef __GNUC__
  4. #include <cxxabi.h>
  5. #endif
  6. #include "type_name.h"
  7. namespace {
  8. #if defined(_LIBCPP_VERSION)
  9. // libc++ is nested under std::__y1
  10. constexpr std::string_view STD_ABI_PREFIX = "std::__y1::";
  11. #elif defined(_linux_)
  12. // libstdc++ is nested under std::__cxx11
  13. // FIXME: is there any way to test if we are building against libstdc++?
  14. constexpr std::string_view STD_ABI_PREFIX = "std::__cxx11::";
  15. #else
  16. // There is no need to cutoff ABI prefix on Windows
  17. #endif
  18. constexpr std::string_view STD_PREFIX = "std::";
  19. } // anonymous namespace
  20. const char* NPrivate::TCppDemangler::Demangle(const char* name) {
  21. #ifndef __GNUC__
  22. return name;
  23. #else
  24. int status;
  25. TmpBuf_.Reset(__cxxabiv1::__cxa_demangle(name, nullptr, nullptr, &status));
  26. if (!TmpBuf_) {
  27. return name;
  28. }
  29. return TmpBuf_.Get();
  30. #endif
  31. }
  32. TString CppDemangle(const TString& name) {
  33. return NPrivate::TCppDemangler().Demangle(name.data());
  34. }
  35. TString TypeName(const std::type_info& typeInfo) {
  36. TString demangled = CppDemangle(typeInfo.name()); // NOLINT(arcadia-typeid-name-restriction)
  37. #if defined(_linux_) || defined(_darwin_)
  38. SubstGlobal(demangled, STD_ABI_PREFIX, STD_PREFIX);
  39. #endif
  40. return demangled;
  41. }
  42. TString TypeName(const std::type_index& typeIndex) {
  43. TString demangled = CppDemangle(typeIndex.name());
  44. #if defined(_linux_) || defined(_darwin_)
  45. SubstGlobal(demangled, STD_ABI_PREFIX, STD_PREFIX);
  46. #endif
  47. return demangled;
  48. }