[Lldb-commits] [lldb] [lldb] Improve identification of Dlang mangled names (PR #93881)

Dave Lee via lldb-commits lldb-commits at lists.llvm.org
Thu May 30 14:19:58 PDT 2024


https://github.com/kastiglione created https://github.com/llvm/llvm-project/pull/93881

None

>From 21a61a1a5ae68fc1e913f73c4311258675990f95 Mon Sep 17 00:00:00 2001
From: Dave Lee <davelee.com at gmail.com>
Date: Thu, 30 May 2024 13:29:41 -0700
Subject: [PATCH] [lldb] Improve identification of Dlang mangled names

---
 lldb/source/Core/Mangled.cpp                     | 11 +++++++++--
 lldb/test/API/lang/c/non-mangled/Makefile        |  4 ++++
 .../API/lang/c/non-mangled/TestCNonMangled.py    | 16 ++++++++++++++++
 lldb/test/API/lang/c/non-mangled/main.c          |  8 ++++++++
 4 files changed, 37 insertions(+), 2 deletions(-)
 create mode 100644 lldb/test/API/lang/c/non-mangled/Makefile
 create mode 100644 lldb/test/API/lang/c/non-mangled/TestCNonMangled.py
 create mode 100644 lldb/test/API/lang/c/non-mangled/main.c

diff --git a/lldb/source/Core/Mangled.cpp b/lldb/source/Core/Mangled.cpp
index 8efc4c639cca5..3142c81d12ed9 100644
--- a/lldb/source/Core/Mangled.cpp
+++ b/lldb/source/Core/Mangled.cpp
@@ -19,6 +19,7 @@
 #include "lldb/Utility/Stream.h"
 #include "lldb/lldb-enumerations.h"
 
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Demangle/Demangle.h"
 #include "llvm/Support/Compiler.h"
@@ -48,8 +49,14 @@ Mangled::ManglingScheme Mangled::GetManglingScheme(llvm::StringRef const name) {
   if (name.starts_with("_R"))
     return Mangled::eManglingSchemeRustV0;
 
-  if (name.starts_with("_D"))
-    return Mangled::eManglingSchemeD;
+  if (name.starts_with("_D")) {
+    // A dlang mangled name begins with `_D`, followed by a numeric length.
+    // See `SymbolName` and `LName` in
+    // https://dlang.org/spec/abi.html#name_mangling
+    llvm::StringRef buf = name.drop_front(2);
+    if (!buf.empty() && llvm::isDigit(buf.front()))
+      return Mangled::eManglingSchemeD;
+  }
 
   if (name.starts_with("_Z"))
     return Mangled::eManglingSchemeItanium;
diff --git a/lldb/test/API/lang/c/non-mangled/Makefile b/lldb/test/API/lang/c/non-mangled/Makefile
new file mode 100644
index 0000000000000..695335e068c0c
--- /dev/null
+++ b/lldb/test/API/lang/c/non-mangled/Makefile
@@ -0,0 +1,4 @@
+C_SOURCES := main.c
+CFLAGS_EXTRAS := -std=c99
+
+include Makefile.rules
diff --git a/lldb/test/API/lang/c/non-mangled/TestCNonMangled.py b/lldb/test/API/lang/c/non-mangled/TestCNonMangled.py
new file mode 100644
index 0000000000000..32bd778fa6eb6
--- /dev/null
+++ b/lldb/test/API/lang/c/non-mangled/TestCNonMangled.py
@@ -0,0 +1,16 @@
+import lldbsuite.test.lldbutil as lldbutil
+from lldbsuite.test.lldbtest import *
+
+
+class TestCase(TestBase):
+
+    def test_functions_having_dlang_mangling_prefix(self):
+        """
+        Ensure C functions with a '_D' prefix alone are not mistakenly treated
+        as a Dlang mangled name. A proper Dlang mangling will have digits
+        immediately following the '_D' prefix.
+        """
+        self.build()
+        _, _, thread, _ = lldbutil.run_to_name_breakpoint(self, "_Dfunction")
+        symbol = thread.frame[0].symbol
+        self.assertEqual(symbol.GetDisplayName(), "_Dfunction")
diff --git a/lldb/test/API/lang/c/non-mangled/main.c b/lldb/test/API/lang/c/non-mangled/main.c
new file mode 100644
index 0000000000000..ad9d86e5c25a8
--- /dev/null
+++ b/lldb/test/API/lang/c/non-mangled/main.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+void _Dfunction() {}
+
+int main() {
+  _Dfunction();
+  return 0;
+}



More information about the lldb-commits mailing list