diff --git a/lldb/include/lldb/Utility/ConstString.h b/lldb/include/lldb/Utility/ConstString.h --- a/lldb/include/lldb/Utility/ConstString.h +++ b/lldb/include/lldb/Utility/ConstString.h @@ -14,6 +14,7 @@ #include "llvm/Support/FormatVariadic.h" #include +#include namespace lldb_private { class Stream; @@ -182,6 +183,8 @@ // Implicitly convert \class ConstString instances to \class StringRef. operator llvm::StringRef() const { return GetStringRef(); } + // Implicitly convert \class ConstString instances to \calss std::string_view. + operator std::string_view() const { return std::string_view(m_string, GetLength()); } /// Get the string value as a C string. /// diff --git a/lldb/source/Core/Mangled.cpp b/lldb/source/Core/Mangled.cpp --- a/lldb/source/Core/Mangled.cpp +++ b/lldb/source/Core/Mangled.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -150,7 +151,7 @@ return demangled_cstr; } -static char *GetRustV0DemangledStr(const char *M) { +static char *GetRustV0DemangledStr(std::string_view M) { char *demangled_cstr = llvm::rustDemangle(M); if (Log *log = GetLog(LLDBLog::Demangle)) { @@ -259,7 +260,7 @@ break; } case eManglingSchemeRustV0: - demangled_name = GetRustV0DemangledStr(mangled_name); + demangled_name = GetRustV0DemangledStr(m_mangled); break; case eManglingSchemeD: demangled_name = GetDLangDemangledStr(mangled_name); 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 @@ -54,7 +55,7 @@ MSDemangleFlags Flags = MSDF_None); // Demangles a Rust v0 mangled symbol. -char *rustDemangle(const char *MangledName); +char *rustDemangle(std::string_view MangledName); // Demangles a D mangled symbol. char *dlangDemangle(const char *MangledName); diff --git a/llvm/lib/Demangle/RustDemangle.cpp b/llvm/lib/Demangle/RustDemangle.cpp --- a/llvm/lib/Demangle/RustDemangle.cpp +++ b/llvm/lib/Demangle/RustDemangle.cpp @@ -20,11 +20,13 @@ #include #include #include +#include using namespace llvm; using llvm::itanium_demangle::OutputBuffer; using llvm::itanium_demangle::ScopedOverride; +using llvm::itanium_demangle::starts_with; namespace { @@ -146,17 +148,13 @@ } // namespace -char *llvm::rustDemangle(const char *MangledName) { - if (MangledName == nullptr) - return nullptr; - +char *llvm::rustDemangle(std::string_view MangledName) { // Return early if mangled name doesn't look like a Rust symbol. - std::string_view Mangled(MangledName); - if (!llvm::itanium_demangle::starts_with(Mangled, "_R")) + if (MangledName.empty() || !starts_with(MangledName, "_R")) return nullptr; Demangler D; - if (!D.demangle(Mangled)) { + if (!D.demangle(MangledName)) { std::free(D.Output.getBuffer()); return nullptr; } @@ -196,7 +194,7 @@ RecursionLevel = 0; BoundLifetimes = 0; - if (!llvm::itanium_demangle::starts_with(Mangled, "_R")) { + if (!starts_with(Mangled, "_R")) { Error = true; return false; } diff --git a/llvm/tools/llvm-rust-demangle-fuzzer/llvm-rust-demangle-fuzzer.cpp b/llvm/tools/llvm-rust-demangle-fuzzer/llvm-rust-demangle-fuzzer.cpp --- a/llvm/tools/llvm-rust-demangle-fuzzer/llvm-rust-demangle-fuzzer.cpp +++ b/llvm/tools/llvm-rust-demangle-fuzzer/llvm-rust-demangle-fuzzer.cpp @@ -13,7 +13,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { std::string NullTerminatedString((const char *)Data, Size); - char *Demangled = llvm::rustDemangle(NullTerminatedString.c_str()); + char *Demangled = llvm::rustDemangle(NullTerminatedString); std::free(Demangled); return 0; }