Skip to content

[lldb][Mangled] Use early-return style in GetDemangledName #130622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025

Conversation

Michael137
Copy link
Member

This patch refactors Mangled::GetDemangledName to use LLVM's preferred early-return style. I'm planning on introducing a way to force re-demangling of names in a future patch, and this stylisitc cleanup makes that easier to reason about.

Also performed small cleanups where I could:

  • we can handle eManglingSchemeNone inside the switch instead of a separate if-block
  • removed some redundant explicit StringRef<->C-string conversions

This patch refactors `Mangled::GetDemangledName` to use LLVM's preferred early-return style. I'm planning on introducing a way to force re-demangling of names in a future patch, and this stylisitc cleanup makes that easier to reason about.

Also performed small cleanups where I could:
* we can handle `eManglingSchemeNone` inside the switch instead of a separate if-block
* removed some redundant explicit StringRef<->C-string conversions
@Michael137 Michael137 requested a review from labath March 10, 2025 15:13
@Michael137 Michael137 changed the title [lldb][Mangled][NFCI] Use early-return style in GetDemangledName [lldb][Mangled] Use early-return style in GetDemangledName Mar 10, 2025
@llvmbot llvmbot added the lldb label Mar 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 10, 2025

@llvm/pr-subscribers-lldb

Author: Michael Buch (Michael137)

Changes

This patch refactors Mangled::GetDemangledName to use LLVM's preferred early-return style. I'm planning on introducing a way to force re-demangling of names in a future patch, and this stylisitc cleanup makes that easier to reason about.

Also performed small cleanups where I could:

  • we can handle eManglingSchemeNone inside the switch instead of a separate if-block
  • removed some redundant explicit StringRef<->C-string conversions

Full diff: https://github.com/llvm/llvm-project/pull/130622.diff

1 Files Affected:

  • (modified) lldb/source/Core/Mangled.cpp (+45-44)
diff --git a/lldb/source/Core/Mangled.cpp b/lldb/source/Core/Mangled.cpp
index 51c22495a16d7..ddaaedea04183 100644
--- a/lldb/source/Core/Mangled.cpp
+++ b/lldb/source/Core/Mangled.cpp
@@ -270,50 +270,51 @@ bool Mangled::GetRichManglingInfo(RichManglingContext &context,
 // name. The result is cached and will be kept until a new string value is
 // supplied to this object, or until the end of the object's lifetime.
 ConstString Mangled::GetDemangledName() const {
-  // Check to make sure we have a valid mangled name and that we haven't
-  // already decoded our mangled name.
-  if (m_mangled && m_demangled.IsNull()) {
-    // Don't bother running anything that isn't mangled
-    const char *mangled_name = m_mangled.GetCString();
-    ManglingScheme mangling_scheme =
-        GetManglingScheme(m_mangled.GetStringRef());
-    if (mangling_scheme != eManglingSchemeNone &&
-        !m_mangled.GetMangledCounterpart(m_demangled)) {
-      // We didn't already mangle this name, demangle it and if all goes well
-      // add it to our map.
-      char *demangled_name = nullptr;
-      switch (mangling_scheme) {
-      case eManglingSchemeMSVC:
-        demangled_name = GetMSVCDemangledStr(mangled_name);
-        break;
-      case eManglingSchemeItanium: {
-        demangled_name = GetItaniumDemangledStr(mangled_name);
-        break;
-      }
-      case eManglingSchemeRustV0:
-        demangled_name = GetRustV0DemangledStr(m_mangled);
-        break;
-      case eManglingSchemeD:
-        demangled_name = GetDLangDemangledStr(m_mangled);
-        break;
-      case eManglingSchemeSwift:
-        // Demangling a swift name requires the swift compiler. This is
-        // explicitly unsupported on llvm.org.
-        break;
-      case eManglingSchemeNone:
-        llvm_unreachable("eManglingSchemeNone was handled already");
-      }
-      if (demangled_name) {
-        m_demangled.SetStringWithMangledCounterpart(
-            llvm::StringRef(demangled_name), m_mangled);
-        free(demangled_name);
-      }
-    }
-    if (m_demangled.IsNull()) {
-      // Set the demangled string to the empty string to indicate we tried to
-      // parse it once and failed.
-      m_demangled.SetCString("");
-    }
+  if (!m_mangled)
+    return m_demangled;
+
+  // Re-use previously demangled names.
+  if (!m_demangled.IsNull())
+    return m_demangled;
+
+  if (m_mangled.GetMangledCounterpart(m_demangled) && !m_demangled.IsNull())
+    return m_demangled;
+
+  // We didn't already mangle this name, demangle it and if all goes well
+  // add it to our map.
+  char *demangled_name = nullptr;
+  switch (GetManglingScheme(m_mangled.GetStringRef())) {
+  case eManglingSchemeMSVC:
+    demangled_name = GetMSVCDemangledStr(m_mangled);
+    break;
+  case eManglingSchemeItanium: {
+    demangled_name = GetItaniumDemangledStr(m_mangled.GetCString());
+    break;
+  }
+  case eManglingSchemeRustV0:
+    demangled_name = GetRustV0DemangledStr(m_mangled);
+    break;
+  case eManglingSchemeD:
+    demangled_name = GetDLangDemangledStr(m_mangled);
+    break;
+  case eManglingSchemeSwift:
+    // Demangling a swift name requires the swift compiler. This is
+    // explicitly unsupported on llvm.org.
+    break;
+  case eManglingSchemeNone:
+    // Don't bother demangling anything that isn't mangled.
+    break;
+  }
+
+  if (demangled_name) {
+    m_demangled.SetStringWithMangledCounterpart(demangled_name, m_mangled);
+    free(demangled_name);
+  }
+
+  if (m_demangled.IsNull()) {
+    // Set the demangled string to the empty string to indicate we tried to
+    // parse it once and failed.
+    m_demangled.SetCString("");
   }
 
   return m_demangled;

Copy link
Collaborator

@labath labath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, although the "re-demangling" part sounds scary :)

@Michael137
Copy link
Member Author

This looks good, although the "re-demangling" part sounds scary :)

:D

I'm trying to provide a way for the demangler to tell LLDB about individual bits of a demangled name (like where the basename starts/ends for example), which we can use in the frame name formatting. Turns out using debug-info for frame names is trickier than expected because we create function templates AST nodes in a way that doesn't work with the Clang type-printer. I'll make a post on the LLVM formus once I have something working to discuss this a bit more

@Michael137 Michael137 merged commit 7ecb0d0 into llvm:main Mar 11, 2025
12 checks passed
@Michael137 Michael137 deleted the lldb/getdemangledname-nfc branch March 11, 2025 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants