diff --git a/llvm/include/llvm/Demangle/Demangle.h b/llvm/include/llvm/Demangle/Demangle.h --- a/llvm/include/llvm/Demangle/Demangle.h +++ b/llvm/include/llvm/Demangle/Demangle.h @@ -11,6 +11,7 @@ #include #include +#include namespace llvm { /// This is a llvm local version of __cxa_demangle. Other than the name and @@ -62,7 +63,7 @@ /// \param MangledName - reference to string to demangle. /// \returns - the demangled string, or a copy of the input string if no /// demangling occurred. -std::string demangle(const std::string &MangledName); +std::string demangle(const std::string_view MangledName); bool nonMicrosoftDemangle(const char *MangledName, std::string &Result); diff --git a/llvm/lib/Demangle/Demangle.cpp b/llvm/lib/Demangle/Demangle.cpp --- a/llvm/lib/Demangle/Demangle.cpp +++ b/llvm/lib/Demangle/Demangle.cpp @@ -11,24 +11,14 @@ //===----------------------------------------------------------------------===// #include "llvm/Demangle/Demangle.h" +#include "llvm/Demangle/StringViewExtras.h" #include -#include -static bool isItaniumEncoding(const char *S) { - // Itanium encoding requires 1 or 3 leading underscores, followed by 'Z'. - return std::strncmp(S, "_Z", 2) == 0 || std::strncmp(S, "___Z", 4) == 0; -} - -static bool isRustEncoding(const char *S) { return S[0] == '_' && S[1] == 'R'; } - -static bool isDLangEncoding(const std::string &MangledName) { - return MangledName.size() >= 2 && MangledName[0] == '_' && - MangledName[1] == 'D'; -} +using llvm::itanium_demangle::starts_with; -std::string llvm::demangle(const std::string &MangledName) { +std::string llvm::demangle(const std::string_view MangledName) { std::string Result; - const char *S = MangledName.c_str(); + const char *S = MangledName.data(); if (nonMicrosoftDemangle(S, Result)) return Result; @@ -39,12 +29,22 @@ if (char *Demangled = microsoftDemangle(S, nullptr, nullptr)) { Result = Demangled; std::free(Demangled); - return Result; + } else { + Result = MangledName; } + return Result; +} - return MangledName; +namespace { +bool isItaniumEncoding(std::string_view S) { + return starts_with(S, "_Z") || starts_with(S, "___Z"); } +bool isRustEncoding(std::string_view S) { return starts_with(S, "_R"); } + +bool isDLangEncoding(std::string_view S) { return starts_with(S, "_D"); } +} // namespace + bool llvm::nonMicrosoftDemangle(const char *MangledName, std::string &Result) { char *Demangled = nullptr; if (isItaniumEncoding(MangledName))