[Lldb-commits] [lldb] fa52788 - [lldb] Improve RichManglingContext ergonomics (NFC)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Fri Feb 4 10:17:45 PST 2022


Author: Jonas Devlieghere
Date: 2022-02-04T10:17:39-08:00
New Revision: fa52788b7a6da1eab9fec0f2db5f74a8db555196

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

LOG: [lldb] Improve RichManglingContext ergonomics (NFC)

Have the different ::Parse.* methods return the demangled string
directly instead of having to go through ::GetBufferRef.

Differential revision: https://reviews.llvm.org/D118953

Added: 
    

Modified: 
    lldb/include/lldb/Core/RichManglingContext.h
    lldb/source/Core/Mangled.cpp
    lldb/source/Core/RichManglingContext.cpp
    lldb/source/Symbol/Symtab.cpp
    lldb/unittests/Core/RichManglingContextTest.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Core/RichManglingContext.h b/lldb/include/lldb/Core/RichManglingContext.h
index a6b7af8d8d7ec..ecd11e93f044d 100644
--- a/lldb/include/lldb/Core/RichManglingContext.h
+++ b/lldb/include/lldb/Core/RichManglingContext.h
@@ -43,25 +43,15 @@ class RichManglingContext {
   bool IsCtorOrDtor() const;
 
   /// Get the base name of a function. This doesn't include trailing template
-  /// arguments, ie "a::b<int>" gives "b". The result will overwrite the
-  /// internal buffer. It can be obtained via GetBufferRef().
-  void ParseFunctionBaseName();
+  /// arguments, ie "a::b<int>" gives "b".
+  llvm::StringRef ParseFunctionBaseName();
 
   /// Get the context name for a function. For "a::b::c", this function returns
-  /// "a::b". The result will overwrite the internal buffer. It can be obtained
-  /// via GetBufferRef().
-  void ParseFunctionDeclContextName();
-
-  /// Get the entire demangled name. The result will overwrite the internal
-  /// buffer. It can be obtained via GetBufferRef().
-  void ParseFullName();
-
-  /// Obtain a StringRef to the internal buffer that holds the result of the
-  /// most recent ParseXy() operation. The next ParseXy() call invalidates it.
-  llvm::StringRef GetBufferRef() const {
-    assert(m_provider != None && "Initialize a provider first");
-    return m_buffer;
-  }
+  /// "a::b".
+  llvm::StringRef ParseFunctionDeclContextName();
+
+  /// Get the entire demangled name.
+  llvm::StringRef ParseFullName();
 
 private:
   enum InfoProvider { None, ItaniumPartialDemangler, PluginCxxLanguage };
@@ -69,9 +59,6 @@ class RichManglingContext {
   /// Selects the rich mangling info provider.
   InfoProvider m_provider = None;
 
-  /// Reference to the buffer used for results of ParseXy() operations.
-  llvm::StringRef m_buffer;
-
   /// Members for ItaniumPartialDemangler
   llvm::ItaniumPartialDemangler m_ipd;
   /// Note: m_ipd_buf is a raw pointer due to being resized by realloc via
@@ -93,7 +80,7 @@ class RichManglingContext {
   void ResetProvider(InfoProvider new_provider);
 
   /// Uniform handling of string buffers for ItaniumPartialDemangler.
-  void processIPDStrResult(char *ipd_res, size_t res_len);
+  llvm::StringRef processIPDStrResult(char *ipd_res, size_t res_len);
 
   /// Cast the given parser to the given type. Ideally we would have a type
   /// trait to deduce \a ParserT from a given InfoProvider, but unfortunately we

diff  --git a/lldb/source/Core/Mangled.cpp b/lldb/source/Core/Mangled.cpp
index 60c1548dd6360..f3c0de40c4ef4 100644
--- a/lldb/source/Core/Mangled.cpp
+++ b/lldb/source/Core/Mangled.cpp
@@ -217,8 +217,7 @@ bool Mangled::DemangleWithRichManglingInfo(
     if (context.FromItaniumName(m_mangled)) {
       // If we got an info, we have a name. Copy to string pool and connect the
       // counterparts to accelerate later access in GetDemangledName().
-      context.ParseFullName();
-      m_demangled.SetStringWithMangledCounterpart(context.GetBufferRef(),
+      m_demangled.SetStringWithMangledCounterpart(context.ParseFullName(),
                                                   m_mangled);
       return true;
     } else {

diff  --git a/lldb/source/Core/RichManglingContext.cpp b/lldb/source/Core/RichManglingContext.cpp
index 852e9564b50e6..f1e81be417b0e 100644
--- a/lldb/source/Core/RichManglingContext.cpp
+++ b/lldb/source/Core/RichManglingContext.cpp
@@ -80,15 +80,15 @@ bool RichManglingContext::IsCtorOrDtor() const {
   llvm_unreachable("Fully covered switch above!");
 }
 
-void RichManglingContext::processIPDStrResult(char *ipd_res, size_t res_size) {
+llvm::StringRef RichManglingContext::processIPDStrResult(char *ipd_res,
+                                                         size_t res_size) {
   // Error case: Clear the buffer.
   if (LLVM_UNLIKELY(ipd_res == nullptr)) {
     assert(res_size == m_ipd_buf_size &&
            "Failed IPD queries keep the original size in the N parameter");
 
     m_ipd_buf[0] = '\0';
-    m_buffer = llvm::StringRef(m_ipd_buf, 0);
-    return;
+    return llvm::StringRef(m_ipd_buf, 0);
   }
 
   // IPD's res_size includes null terminator.
@@ -106,60 +106,54 @@ void RichManglingContext::processIPDStrResult(char *ipd_res, size_t res_size) {
   }
 
   // 99% case: Just remember the string length.
-  m_buffer = llvm::StringRef(m_ipd_buf, res_size - 1);
+  return llvm::StringRef(m_ipd_buf, res_size - 1);
 }
 
-void RichManglingContext::ParseFunctionBaseName() {
+llvm::StringRef RichManglingContext::ParseFunctionBaseName() {
   assert(m_provider != None && "Initialize a provider first");
   switch (m_provider) {
   case ItaniumPartialDemangler: {
     auto n = m_ipd_buf_size;
     auto buf = m_ipd.getFunctionBaseName(m_ipd_buf, &n);
-    processIPDStrResult(buf, n);
-    return;
+    return processIPDStrResult(buf, n);
   }
   case PluginCxxLanguage:
-    m_buffer =
-        get<CPlusPlusLanguage::MethodName>(m_cxx_method_parser)->GetBasename();
-    return;
+    return get<CPlusPlusLanguage::MethodName>(m_cxx_method_parser)
+        ->GetBasename();
   case None:
-    return;
+    return {};
   }
 }
 
-void RichManglingContext::ParseFunctionDeclContextName() {
+llvm::StringRef RichManglingContext::ParseFunctionDeclContextName() {
   assert(m_provider != None && "Initialize a provider first");
   switch (m_provider) {
   case ItaniumPartialDemangler: {
     auto n = m_ipd_buf_size;
     auto buf = m_ipd.getFunctionDeclContextName(m_ipd_buf, &n);
-    processIPDStrResult(buf, n);
-    return;
+    return processIPDStrResult(buf, n);
   }
   case PluginCxxLanguage:
-    m_buffer =
-        get<CPlusPlusLanguage::MethodName>(m_cxx_method_parser)->GetContext();
-    return;
+    return get<CPlusPlusLanguage::MethodName>(m_cxx_method_parser)
+        ->GetContext();
   case None:
-    return;
+    return {};
   }
 }
 
-void RichManglingContext::ParseFullName() {
+llvm::StringRef RichManglingContext::ParseFullName() {
   assert(m_provider != None && "Initialize a provider first");
   switch (m_provider) {
   case ItaniumPartialDemangler: {
     auto n = m_ipd_buf_size;
     auto buf = m_ipd.finishDemangle(m_ipd_buf, &n);
-    processIPDStrResult(buf, n);
-    return;
+    return processIPDStrResult(buf, n);
   }
   case PluginCxxLanguage:
-    m_buffer = get<CPlusPlusLanguage::MethodName>(m_cxx_method_parser)
-                   ->GetFullName()
-                   .GetStringRef();
-    return;
+    return get<CPlusPlusLanguage::MethodName>(m_cxx_method_parser)
+        ->GetFullName()
+        .GetStringRef();
   case None:
-    return;
+    return {};
   }
 }

diff  --git a/lldb/source/Symbol/Symtab.cpp b/lldb/source/Symbol/Symtab.cpp
index 97dc31bc97660..765e7bc9520fd 100644
--- a/lldb/source/Symbol/Symtab.cpp
+++ b/lldb/source/Symbol/Symtab.cpp
@@ -383,16 +383,13 @@ void Symtab::RegisterMangledNameEntry(
     std::vector<std::pair<NameToIndexMap::Entry, const char *>> &backlog,
     RichManglingContext &rmc) {
   // Only register functions that have a base name.
-  rmc.ParseFunctionBaseName();
-  llvm::StringRef base_name = rmc.GetBufferRef();
+  llvm::StringRef base_name = rmc.ParseFunctionBaseName();
   if (base_name.empty())
     return;
 
   // The base name will be our entry's name.
   NameToIndexMap::Entry entry(ConstString(base_name), value);
-
-  rmc.ParseFunctionDeclContextName();
-  llvm::StringRef decl_context = rmc.GetBufferRef();
+  llvm::StringRef decl_context = rmc.ParseFunctionDeclContextName();
 
   // Register functions with no context.
   if (decl_context.empty()) {

diff  --git a/lldb/unittests/Core/RichManglingContextTest.cpp b/lldb/unittests/Core/RichManglingContextTest.cpp
index 89e9ef03650f9..65a5503c61a30 100644
--- a/lldb/unittests/Core/RichManglingContextTest.cpp
+++ b/lldb/unittests/Core/RichManglingContextTest.cpp
@@ -18,18 +18,12 @@ using namespace lldb_private;
 TEST(RichManglingContextTest, Basic) {
   RichManglingContext RMC;
   ConstString mangled("_ZN3foo3barEv");
-  EXPECT_TRUE(RMC.FromItaniumName(mangled));
 
+  EXPECT_TRUE(RMC.FromItaniumName(mangled));
   EXPECT_FALSE(RMC.IsCtorOrDtor());
-
-  RMC.ParseFunctionDeclContextName();
-  EXPECT_EQ("foo", RMC.GetBufferRef());
-
-  RMC.ParseFunctionBaseName();
-  EXPECT_EQ("bar", RMC.GetBufferRef());
-
-  RMC.ParseFullName();
-  EXPECT_EQ("foo::bar()", RMC.GetBufferRef());
+  EXPECT_EQ("foo", RMC.ParseFunctionDeclContextName());
+  EXPECT_EQ("bar", RMC.ParseFunctionBaseName());
+  EXPECT_EQ("foo::bar()", RMC.ParseFullName());
 }
 
 TEST(RichManglingContextTest, FromCxxMethodName) {
@@ -41,19 +35,12 @@ TEST(RichManglingContextTest, FromCxxMethodName) {
   ConstString demangled("foo::bar()");
   EXPECT_TRUE(CxxMethodRMC.FromCxxMethodName(demangled));
 
-  EXPECT_TRUE(ItaniumRMC.IsCtorOrDtor() == CxxMethodRMC.IsCtorOrDtor());
-
-  ItaniumRMC.ParseFunctionDeclContextName();
-  CxxMethodRMC.ParseFunctionDeclContextName();
-  EXPECT_TRUE(ItaniumRMC.GetBufferRef() == CxxMethodRMC.GetBufferRef());
-
-  ItaniumRMC.ParseFunctionBaseName();
-  CxxMethodRMC.ParseFunctionBaseName();
-  EXPECT_TRUE(ItaniumRMC.GetBufferRef() == CxxMethodRMC.GetBufferRef());
-
-  ItaniumRMC.ParseFullName();
-  CxxMethodRMC.ParseFullName();
-  EXPECT_TRUE(ItaniumRMC.GetBufferRef() == CxxMethodRMC.GetBufferRef());
+  EXPECT_EQ(ItaniumRMC.IsCtorOrDtor(), CxxMethodRMC.IsCtorOrDtor());
+  EXPECT_EQ(ItaniumRMC.ParseFunctionDeclContextName(),
+            CxxMethodRMC.ParseFunctionDeclContextName());
+  EXPECT_EQ(ItaniumRMC.ParseFunctionBaseName(),
+            CxxMethodRMC.ParseFunctionBaseName());
+  EXPECT_EQ(ItaniumRMC.ParseFullName(), CxxMethodRMC.ParseFullName());
 
   // Construct with a random name.
   {
@@ -68,8 +55,7 @@ TEST(RichManglingContextTest, FromCxxMethodName) {
         ConstString("void * operator new(unsigned __int64)")));
 
     // We expect its context is empty.
-    CxxMethodRMC.ParseFunctionDeclContextName();
-    EXPECT_TRUE(CxxMethodRMC.GetBufferRef().empty());
+    EXPECT_TRUE(CxxMethodRMC.ParseFunctionDeclContextName().empty());
   }
 }
 
@@ -79,16 +65,13 @@ TEST(RichManglingContextTest, SwitchProvider) {
   llvm::StringRef demangled = "foo::bar()";
 
   EXPECT_TRUE(RMC.FromItaniumName(ConstString(mangled)));
-  RMC.ParseFullName();
-  EXPECT_EQ("foo::bar()", RMC.GetBufferRef());
+  EXPECT_EQ("foo::bar()", RMC.ParseFullName());
 
   EXPECT_TRUE(RMC.FromCxxMethodName(ConstString(demangled)));
-  RMC.ParseFullName();
-  EXPECT_EQ("foo::bar()", RMC.GetBufferRef());
+  EXPECT_EQ("foo::bar()", RMC.ParseFullName());
 
   EXPECT_TRUE(RMC.FromItaniumName(ConstString(mangled)));
-  RMC.ParseFullName();
-  EXPECT_EQ("foo::bar()", RMC.GetBufferRef());
+  EXPECT_EQ("foo::bar()", RMC.ParseFullName());
 }
 
 TEST(RichManglingContextTest, IPDRealloc) {
@@ -116,13 +99,11 @@ TEST(RichManglingContextTest, IPDRealloc) {
 
   // Demangle the short one.
   EXPECT_TRUE(RMC.FromItaniumName(ConstString(ShortMangled)));
-  RMC.ParseFullName();
-  const char *ShortDemangled = RMC.GetBufferRef().data();
+  const char *ShortDemangled = RMC.ParseFullName().data();
 
   // Demangle the long one.
   EXPECT_TRUE(RMC.FromItaniumName(ConstString(LongMangled)));
-  RMC.ParseFullName();
-  const char *LongDemangled = RMC.GetBufferRef().data();
+  const char *LongDemangled = RMC.ParseFullName().data();
 
   // Make sure a new buffer was allocated or the default buffer was extended.
   bool AllocatedNewBuffer = (ShortDemangled != LongDemangled);


        


More information about the lldb-commits mailing list