Skip to content

Commit 12d967c

Browse files
[Damangle] convert rustDemangle to use std::string_view
I was doing this API conversion to use std::string_view top-down in D149104, but this exposed issues in individual demanglers that needed to get fixed first. There's no issue with the conversion for the Rust demangler, so convert it first. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D149784
1 parent 61e1c3d commit 12d967c

File tree

6 files changed

+16
-13
lines changed

6 files changed

+16
-13
lines changed

lldb/include/lldb/Utility/ConstString.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/Support/FormatVariadic.h"
1515

1616
#include <cstddef>
17+
#include <string_view>
1718

1819
namespace lldb_private {
1920
class Stream;
@@ -182,6 +183,8 @@ class ConstString {
182183

183184
// Implicitly convert \class ConstString instances to \class StringRef.
184185
operator llvm::StringRef() const { return GetStringRef(); }
186+
// Implicitly convert \class ConstString instances to \calss std::string_view.
187+
operator std::string_view() const { return std::string_view(m_string, GetLength()); }
185188

186189
/// Get the string value as a C string.
187190
///

lldb/source/Core/Mangled.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <mutex>
2727
#include <string>
28+
#include <string_view>
2829
#include <utility>
2930

3031
#include <cstdlib>
@@ -150,7 +151,7 @@ static char *GetItaniumDemangledStr(const char *M) {
150151
return demangled_cstr;
151152
}
152153

153-
static char *GetRustV0DemangledStr(const char *M) {
154+
static char *GetRustV0DemangledStr(std::string_view M) {
154155
char *demangled_cstr = llvm::rustDemangle(M);
155156

156157
if (Log *log = GetLog(LLDBLog::Demangle)) {
@@ -259,7 +260,7 @@ ConstString Mangled::GetDemangledName() const {
259260
break;
260261
}
261262
case eManglingSchemeRustV0:
262-
demangled_name = GetRustV0DemangledStr(mangled_name);
263+
demangled_name = GetRustV0DemangledStr(m_mangled);
263264
break;
264265
case eManglingSchemeD:
265266
demangled_name = GetDLangDemangledStr(mangled_name);

llvm/include/llvm/Demangle/Demangle.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <cstddef>
1313
#include <string>
14+
#include <string_view>
1415

1516
namespace llvm {
1617
/// This is a llvm local version of __cxa_demangle. Other than the name and
@@ -54,7 +55,7 @@ char *microsoftDemangle(const char *mangled_name, size_t *n_read, int *status,
5455
MSDemangleFlags Flags = MSDF_None);
5556

5657
// Demangles a Rust v0 mangled symbol.
57-
char *rustDemangle(const char *MangledName);
58+
char *rustDemangle(std::string_view MangledName);
5859

5960
// Demangles a D mangled symbol.
6061
char *dlangDemangle(const char *MangledName);

llvm/lib/Demangle/Demangle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bool llvm::nonMicrosoftDemangle(std::string_view MangledName,
5151
if (isItaniumEncoding(MangledName.data()))
5252
Demangled = itaniumDemangle(MangledName);
5353
else if (isRustEncoding(MangledName.data()))
54-
Demangled = rustDemangle(MangledName.data());
54+
Demangled = rustDemangle(MangledName);
5555
else if (isDLangEncoding(MangledName.data()))
5656
Demangled = dlangDemangle(MangledName.data());
5757

llvm/lib/Demangle/RustDemangle.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
#include <cstdint>
2121
#include <cstring>
2222
#include <limits>
23+
#include <string_view>
2324

2425
using namespace llvm;
2526

2627
using llvm::itanium_demangle::OutputBuffer;
2728
using llvm::itanium_demangle::ScopedOverride;
29+
using llvm::itanium_demangle::starts_with;
2830

2931
namespace {
3032

@@ -146,17 +148,13 @@ class Demangler {
146148

147149
} // namespace
148150

149-
char *llvm::rustDemangle(const char *MangledName) {
150-
if (MangledName == nullptr)
151-
return nullptr;
152-
151+
char *llvm::rustDemangle(std::string_view MangledName) {
153152
// Return early if mangled name doesn't look like a Rust symbol.
154-
std::string_view Mangled(MangledName);
155-
if (!llvm::itanium_demangle::starts_with(Mangled, "_R"))
153+
if (MangledName.empty() || !starts_with(MangledName, "_R"))
156154
return nullptr;
157155

158156
Demangler D;
159-
if (!D.demangle(Mangled)) {
157+
if (!D.demangle(MangledName)) {
160158
std::free(D.Output.getBuffer());
161159
return nullptr;
162160
}
@@ -196,7 +194,7 @@ bool Demangler::demangle(std::string_view Mangled) {
196194
RecursionLevel = 0;
197195
BoundLifetimes = 0;
198196

199-
if (!llvm::itanium_demangle::starts_with(Mangled, "_R")) {
197+
if (!starts_with(Mangled, "_R")) {
200198
Error = true;
201199
return false;
202200
}

llvm/tools/llvm-rust-demangle-fuzzer/llvm-rust-demangle-fuzzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
1515
std::string NullTerminatedString((const char *)Data, Size);
16-
char *Demangled = llvm::rustDemangle(NullTerminatedString.c_str());
16+
char *Demangled = llvm::rustDemangle(NullTerminatedString);
1717
std::free(Demangled);
1818
return 0;
1919
}

0 commit comments

Comments
 (0)