Skip to content

Commit 8c99a9e

Browse files
author
git apple-llvm automerger
committed
Merge commit 'd1d4af214fde' from swift/release/6.0 into stable/20230725
2 parents 9f29590 + d1d4af2 commit 8c99a9e

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

lldb/source/Core/Mangled.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "lldb/Utility/Stream.h"
2020
#include "lldb/lldb-enumerations.h"
2121

22+
#include "llvm/ADT/StringExtras.h"
2223
#include "llvm/ADT/StringRef.h"
2324
#include "llvm/Demangle/Demangle.h"
2425
#include "llvm/Support/Compiler.h"
@@ -57,8 +58,15 @@ Mangled::ManglingScheme Mangled::GetManglingScheme(llvm::StringRef const name) {
5758
if (name.startswith("_R"))
5859
return Mangled::eManglingSchemeRustV0;
5960

60-
if (name.startswith("_D"))
61-
return Mangled::eManglingSchemeD;
61+
if (name.startswith("_D")) {
62+
// A dlang mangled name begins with `_D`, followed by a numeric length. One
63+
// known exception is the symbol `_Dmain`.
64+
// See `SymbolName` and `LName` in
65+
// https://dlang.org/spec/abi.html#name_mangling
66+
llvm::StringRef buf = name.drop_front(2);
67+
if (!buf.empty() && (llvm::isDigit(buf.front()) || name == "_Dmain"))
68+
return Mangled::eManglingSchemeD;
69+
}
6270

6371
if (name.startswith("_Z"))
6472
return Mangled::eManglingSchemeItanium;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
C_SOURCES := main.c
2+
CFLAGS_EXTRAS := -std=c99
3+
4+
include Makefile.rules
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import lldbsuite.test.lldbutil as lldbutil
2+
from lldbsuite.test.lldbtest import *
3+
4+
5+
class TestCase(TestBase):
6+
def test_functions_having_dlang_mangling_prefix(self):
7+
"""
8+
Ensure C functions with a '_D' prefix alone are not mistakenly treated
9+
as a Dlang mangled name. A proper Dlang mangling will have digits
10+
immediately following the '_D' prefix.
11+
"""
12+
self.build()
13+
_, _, thread, _ = lldbutil.run_to_name_breakpoint(self, "_Dfunction")
14+
symbol = thread.frame[0].symbol
15+
self.assertEqual(symbol.GetDisplayName(), "_Dfunction")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdio.h>
2+
3+
void _Dfunction() {}
4+
5+
int main() {
6+
_Dfunction();
7+
return 0;
8+
}

lldb/unittests/Core/MangledTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ TEST(MangledTest, ResultForValidDLangName) {
8181
EXPECT_STREQ(expected_result.GetCString(), the_demangled.GetCString());
8282
}
8383

84-
TEST(MangledTest, EmptyForInvalidDLangName) {
84+
TEST(MangledTest, SameForInvalidDLangPrefixedName) {
8585
ConstString mangled_name("_DDD");
8686
Mangled the_mangled(mangled_name);
8787
ConstString the_demangled = the_mangled.GetDemangledName();
8888

89-
EXPECT_STREQ("", the_demangled.GetCString());
89+
EXPECT_STREQ("_DDD", the_demangled.GetCString());
9090
}
9191

9292
TEST(MangledTest, BoolConversionOperator) {

0 commit comments

Comments
 (0)