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 @@ -31,7 +31,7 @@ /// Returns a non-NULL pointer to a NUL-terminated C style string /// that should be explicitly freed, if successful. Otherwise, may return /// nullptr if mangled_name is not a valid mangling or is nullptr. -char *itaniumDemangle(const char *mangled_name); +char *itaniumDemangle(std::string_view mangled_name); enum MSDemangleFlags { MSDF_None = 0, @@ -66,7 +66,7 @@ /// demangling occurred. std::string demangle(const std::string &MangledName); -bool nonMicrosoftDemangle(const char *MangledName, std::string &Result); +bool nonMicrosoftDemangle(std::string_view MangledName, std::string &Result); /// "Partial" demangler. This supports demangling a string into an AST /// (typically an intermediate stage in itaniumDemangle) and querying certain diff --git a/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp b/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp --- a/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp +++ b/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp @@ -679,7 +679,7 @@ LLVMSymbolizer::DemangleName(const std::string &Name, const SymbolizableModule *DbiModuleDescriptor) { std::string Result; - if (nonMicrosoftDemangle(Name.c_str(), Result)) + if (nonMicrosoftDemangle(Name, Result)) return Result; if (!Name.empty() && Name.front() == '?') { @@ -700,7 +700,7 @@ std::string DemangledCName(demanglePE32ExternCFunc(Name)); // On i386 Windows, the C name mangling for different calling conventions // may also be applied on top of the Itanium or Rust name mangling. - if (nonMicrosoftDemangle(DemangledCName.c_str(), Result)) + if (nonMicrosoftDemangle(DemangledCName, Result)) return Result; return DemangledCName; } 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 @@ -30,10 +30,10 @@ std::string Result; const char *S = MangledName.c_str(); - if (nonMicrosoftDemangle(S, Result)) + if (nonMicrosoftDemangle(MangledName, Result)) return Result; - if (S[0] == '_' && nonMicrosoftDemangle(S + 1, Result)) + if (S[0] == '_' && nonMicrosoftDemangle(MangledName.substr(1), Result)) return Result; if (char *Demangled = microsoftDemangle(S, nullptr, nullptr)) { @@ -45,14 +45,15 @@ return MangledName; } -bool llvm::nonMicrosoftDemangle(const char *MangledName, std::string &Result) { +bool llvm::nonMicrosoftDemangle(std::string_view MangledName, + std::string &Result) { char *Demangled = nullptr; - if (isItaniumEncoding(MangledName)) + if (isItaniumEncoding(MangledName.data())) Demangled = itaniumDemangle(MangledName); - else if (isRustEncoding(MangledName)) - Demangled = rustDemangle(MangledName); - else if (isDLangEncoding(MangledName)) - Demangled = dlangDemangle(MangledName); + else if (isRustEncoding(MangledName.data())) + Demangled = rustDemangle(MangledName.data()); + else if (isDLangEncoding(MangledName.data())) + Demangled = dlangDemangle(MangledName.data()); if (!Demangled) return false; diff --git a/llvm/lib/Demangle/ItaniumDemangle.cpp b/llvm/lib/Demangle/ItaniumDemangle.cpp --- a/llvm/lib/Demangle/ItaniumDemangle.cpp +++ b/llvm/lib/Demangle/ItaniumDemangle.cpp @@ -365,11 +365,12 @@ using Demangler = itanium_demangle::ManglingParser; -char *llvm::itaniumDemangle(const char *MangledName) { - if (!MangledName) +char *llvm::itaniumDemangle(std::string_view MangledName) { + if (MangledName.empty()) return nullptr; - Demangler Parser(MangledName, MangledName + std::strlen(MangledName)); + Demangler Parser(MangledName.data(), + MangledName.data() + MangledName.length()); Node *AST = Parser.parse(); if (!AST) return nullptr; diff --git a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp --- a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp +++ b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp @@ -8,6 +8,7 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/Demangle/Demangle.h" +#include "llvm/Demangle/StringViewExtras.h" #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" @@ -71,10 +72,11 @@ } static std::string demangle(const std::string &Mangled) { - const char *DecoratedStr = Mangled.c_str(); + using llvm::itanium_demangle::starts_with; + std::string_view DecoratedStr = Mangled; if (StripUnderscore) if (DecoratedStr[0] == '_') - ++DecoratedStr; + DecoratedStr.remove_prefix(1); std::string Result; if (nonMicrosoftDemangle(DecoratedStr, Result)) @@ -86,9 +88,9 @@ if (Types) Undecorated = itaniumDemangle(DecoratedStr); - if (!Undecorated && strncmp(DecoratedStr, "__imp_", 6) == 0) { + if (!Undecorated && starts_with(DecoratedStr, "__imp_")) { Prefix = "import thunk for "; - Undecorated = itaniumDemangle(DecoratedStr + 6); + Undecorated = itaniumDemangle(DecoratedStr.substr(6)); } Result = Undecorated ? Prefix + Undecorated : Mangled; diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp --- a/llvm/tools/llvm-nm/llvm-nm.cpp +++ b/llvm/tools/llvm-nm/llvm-nm.cpp @@ -639,7 +639,7 @@ static std::optional demangle(StringRef Name) { std::string Demangled; - if (nonMicrosoftDemangle(Name.str().c_str(), Demangled)) + if (nonMicrosoftDemangle(Name, Demangled)) return Demangled; return std::nullopt; } diff --git a/llvm/tools/llvm-opt-report/OptReport.cpp b/llvm/tools/llvm-opt-report/OptReport.cpp --- a/llvm/tools/llvm-opt-report/OptReport.cpp +++ b/llvm/tools/llvm-opt-report/OptReport.cpp @@ -338,7 +338,7 @@ bool Printed = false; if (!NoDemangle) { - if (char *Demangled = itaniumDemangle(FuncName.c_str())) { + if (char *Demangled = itaniumDemangle(FuncName)) { OS << Demangled; Printed = true; std::free(Demangled);