[Lldb-commits] [lldb] f5371eb - [Damangle] convert dlangDemangle to use std::string_view

Nick Desaulniers via lldb-commits lldb-commits at lists.llvm.org
Fri Jun 2 15:19:55 PDT 2023


Author: Nick Desaulniers
Date: 2023-06-02T15:19:41-07:00
New Revision: f5371eb3d3aed06ba84a69533586a60243ad2f24

URL: https://github.com/llvm/llvm-project/commit/f5371eb3d3aed06ba84a69533586a60243ad2f24
DIFF: https://github.com/llvm/llvm-project/commit/f5371eb3d3aed06ba84a69533586a60243ad2f24.diff

LOG: [Damangle] convert dlangDemangle 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 D language
demangler, so convert it.

I have a more aggressive refactoring of the entire D language demangler
to use std::string_view more extensively, but the interface with
llvm::nonMicrosoftDemangle is the more interesting one.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D151003

Added: 
    

Modified: 
    lldb/source/Core/Mangled.cpp
    llvm/include/llvm/Demangle/Demangle.h
    llvm/lib/Demangle/DLangDemangle.cpp
    llvm/lib/Demangle/Demangle.cpp
    llvm/tools/llvm-dlang-demangle-fuzzer/llvm-dlang-demangle-fuzzer.cpp
    llvm/unittests/Demangle/DLangDemangleTest.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Core/Mangled.cpp b/lldb/source/Core/Mangled.cpp
index bf9014daf8a26..3294b246ae743 100644
--- a/lldb/source/Core/Mangled.cpp
+++ b/lldb/source/Core/Mangled.cpp
@@ -164,7 +164,7 @@ static char *GetRustV0DemangledStr(std::string_view M) {
   return demangled_cstr;
 }
 
-static char *GetDLangDemangledStr(const char *M) {
+static char *GetDLangDemangledStr(std::string_view M) {
   char *demangled_cstr = llvm::dlangDemangle(M);
 
   if (Log *log = GetLog(LLDBLog::Demangle)) {
@@ -263,7 +263,7 @@ ConstString Mangled::GetDemangledName() const {
         demangled_name = GetRustV0DemangledStr(m_mangled);
         break;
       case eManglingSchemeD:
-        demangled_name = GetDLangDemangledStr(mangled_name);
+        demangled_name = GetDLangDemangledStr(m_mangled);
         break;
       case eManglingSchemeNone:
         llvm_unreachable("eManglingSchemeNone was handled already");

diff  --git a/llvm/include/llvm/Demangle/Demangle.h b/llvm/include/llvm/Demangle/Demangle.h
index 9ba96c093eaca..d9b830c660047 100644
--- a/llvm/include/llvm/Demangle/Demangle.h
+++ b/llvm/include/llvm/Demangle/Demangle.h
@@ -58,7 +58,7 @@ char *microsoftDemangle(const char *mangled_name, size_t *n_read, int *status,
 char *rustDemangle(std::string_view MangledName);
 
 // Demangles a D mangled symbol.
-char *dlangDemangle(const char *MangledName);
+char *dlangDemangle(std::string_view MangledName);
 
 /// Attempt to demangle a string using 
diff erent demangling schemes.
 /// The function uses heuristics to determine which demangling scheme to use.

diff  --git a/llvm/lib/Demangle/DLangDemangle.cpp b/llvm/lib/Demangle/DLangDemangle.cpp
index ad583b86946fd..8b94d40354b43 100644
--- a/llvm/lib/Demangle/DLangDemangle.cpp
+++ b/llvm/lib/Demangle/DLangDemangle.cpp
@@ -14,6 +14,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Demangle/Demangle.h"
+#include "llvm/Demangle/StringViewExtras.h"
 #include "llvm/Demangle/Utility.h"
 
 #include <cctype>
@@ -22,6 +23,7 @@
 
 using namespace llvm;
 using llvm::itanium_demangle::OutputBuffer;
+using llvm::itanium_demangle::starts_with;
 
 namespace {
 
@@ -541,20 +543,20 @@ const char *Demangler::parseMangle(OutputBuffer *Demangled) {
   return parseMangle(Demangled, this->Str);
 }
 
-char *llvm::dlangDemangle(const char *MangledName) {
-  if (MangledName == nullptr || strncmp(MangledName, "_D", 2) != 0)
+char *llvm::dlangDemangle(std::string_view MangledName) {
+  if (MangledName.empty() || !starts_with(MangledName, "_D"))
     return nullptr;
 
   OutputBuffer Demangled;
-  if (strcmp(MangledName, "_Dmain") == 0) {
+  if (MangledName == "_Dmain") {
     Demangled << "D main";
   } else {
 
-    Demangler D = Demangler(MangledName);
-    MangledName = D.parseMangle(&Demangled);
+    Demangler D(MangledName.data());
+    const char *M = D.parseMangle(&Demangled);
 
     // Check that the entire symbol was successfully demangled.
-    if (MangledName == nullptr || *MangledName != '\0') {
+    if (M == nullptr || *M != '\0') {
       std::free(Demangled.getBuffer());
       return nullptr;
     }

diff  --git a/llvm/lib/Demangle/Demangle.cpp b/llvm/lib/Demangle/Demangle.cpp
index 4627ca822f676..3dd4f31268f4a 100644
--- a/llvm/lib/Demangle/Demangle.cpp
+++ b/llvm/lib/Demangle/Demangle.cpp
@@ -53,7 +53,7 @@ bool llvm::nonMicrosoftDemangle(std::string_view MangledName,
   else if (isRustEncoding(MangledName.data()))
     Demangled = rustDemangle(MangledName);
   else if (isDLangEncoding(MangledName.data()))
-    Demangled = dlangDemangle(MangledName.data());
+    Demangled = dlangDemangle(MangledName);
 
   if (!Demangled)
     return false;

diff  --git a/llvm/tools/llvm-dlang-demangle-fuzzer/llvm-dlang-demangle-fuzzer.cpp b/llvm/tools/llvm-dlang-demangle-fuzzer/llvm-dlang-demangle-fuzzer.cpp
index 16175af9f233d..ac4bd752fe0bb 100644
--- a/llvm/tools/llvm-dlang-demangle-fuzzer/llvm-dlang-demangle-fuzzer.cpp
+++ b/llvm/tools/llvm-dlang-demangle-fuzzer/llvm-dlang-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::dlangDemangle(NullTerminatedString.c_str());
+  char *Demangled = llvm::dlangDemangle(NullTerminatedString);
   std::free(Demangled);
   return 0;
 }

diff  --git a/llvm/unittests/Demangle/DLangDemangleTest.cpp b/llvm/unittests/Demangle/DLangDemangleTest.cpp
index dd0c699d5c186..1322f530e5963 100644
--- a/llvm/unittests/Demangle/DLangDemangleTest.cpp
+++ b/llvm/unittests/Demangle/DLangDemangleTest.cpp
@@ -11,10 +11,11 @@
 #include "gtest/gtest.h"
 
 #include <cstdlib>
+#include <string_view>
 #include <utility>
 
 struct DLangDemangleTestFixture
-    : public testing::TestWithParam<std::pair<const char *, const char *>> {
+    : public testing::TestWithParam<std::pair<std::string_view, const char *>> {
   char *Demangled;
 
   void SetUp() override { Demangled = llvm::dlangDemangle(GetParam().first); }
@@ -29,9 +30,8 @@ TEST_P(DLangDemangleTestFixture, DLangDemangleTest) {
 INSTANTIATE_TEST_SUITE_P(
     DLangDemangleTest, DLangDemangleTestFixture,
     testing::Values(
-        std::make_pair("_Dmain", "D main"), std::make_pair(nullptr, nullptr),
-        std::make_pair("_Z", nullptr), std::make_pair("_DDD", nullptr),
-        std::make_pair("_D88", nullptr),
+        std::make_pair("_Dmain", "D main"), std::make_pair("_Z", nullptr),
+        std::make_pair("_DDD", nullptr), std::make_pair("_D88", nullptr),
         std::make_pair("_D8demangleZ", "demangle"),
         std::make_pair("_D8demangle4testZ", "demangle.test"),
         std::make_pair("_D8demangle4test5test2Z", "demangle.test.test2"),


        


More information about the lldb-commits mailing list