-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
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
@llvm/pr-subscribers-lldb Author: Michael Buch (Michael137) ChangesThis patch refactors Also performed small cleanups where I could:
Full diff: https://github.com/llvm/llvm-project/pull/130622.diff 1 Files Affected:
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;
|
There was a problem hiding this 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 :)
: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 |
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:
eManglingSchemeNone
inside the switch instead of a separate if-block