Skip to content

Commit 2d4e5c4

Browse files
authored
[LLDB][NativePDB] Use undecorated name for types if UniqueName isn't mangled (#152114)
Languages other than C/C++ don't necessarily emit mangled names in the `UniqueName` field of type records. Rust specifically emits a unique ID that doesn't contain the name. For example, `(i32, i32)` is emitted as ```llvm !266 = !DICompositeType( tag: DW_TAG_structure_type, name: "tuple$<i32,i32>", file: !9, size: 64, align: 32, elements: !267, templateParams: !17, identifier: "19122721b0632fe96c0dd37477674472" ) ``` which results in ``` 0x1091 | LF_STRUCTURE [size = 72, hash = 0x1AC67] `tuple$<i32,i32>` unique name: `19122721b0632fe96c0dd37477674472` vtable: <no type>, base list: <no type>, field list: 0x1090 options: has unique name, sizeof 8 ``` In C++ with Clang and MSVC, a structure similar to this would result in ``` 0x136F | LF_STRUCTURE [size = 44, hash = 0x30BE2] `MyTuple` unique name: `.?AUMyTuple@@` vtable: <no type>, base list: <no type>, field list: 0x136E options: has unique name, sizeof 8 ``` With this PR, if a `UniqueName` is encountered that couldn't be parsed, it will fall back to using the undecorated (→ do the same as if the unique name is empty/unavailable). I'm not sure how to test this. Maybe compiling the LLVM IR that rustc emits? Fixes #152051.
1 parent 7b8dea2 commit 2d4e5c4

File tree

4 files changed

+90
-9
lines changed

4 files changed

+90
-9
lines changed

lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ PdbAstBuilder::CreateDeclInfoForType(const TagRecord &record, TypeIndex ti) {
178178
std::string_view sv(record.UniqueName.begin(), record.UniqueName.size());
179179
llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv);
180180
if (demangler.Error)
181-
return {m_clang.GetTranslationUnitDecl(), std::string(record.UniqueName)};
181+
return CreateDeclInfoForUndecoratedName(record.Name);
182182

183183
llvm::ms_demangle::IdentifierNode *idn =
184184
ttn->QualifiedName->getUnqualifiedIdentifier();

lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -622,18 +622,14 @@ lldb::TypeSP SymbolFileNativePDB::CreateSimpleType(TypeIndex ti,
622622
}
623623

624624
static std::string GetUnqualifiedTypeName(const TagRecord &record) {
625-
if (!record.hasUniqueName()) {
626-
MSVCUndecoratedNameParser parser(record.Name);
627-
llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers();
628-
629-
return std::string(specs.back().GetBaseName());
630-
}
625+
if (!record.hasUniqueName())
626+
return std::string(MSVCUndecoratedNameParser::DropScope(record.Name));
631627

632628
llvm::ms_demangle::Demangler demangler;
633629
std::string_view sv(record.UniqueName.begin(), record.UniqueName.size());
634630
llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv);
635631
if (demangler.Error)
636-
return std::string(record.Name);
632+
return std::string(MSVCUndecoratedNameParser::DropScope(record.Name));
637633

638634
llvm::ms_demangle::IdentifierNode *idn =
639635
ttn->QualifiedName->getUnqualifiedIdentifier();
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
; REQUIRES: system-windows
2+
; RUN: %build --compiler=clang-cl --nodefaultlib -o %t.exe -- %s
3+
; RUN: lldb-test symbols %t.exe | FileCheck %s
4+
5+
; Output from
6+
; rustc lib.rs -C debuginfo=2 --emit=llvm-ir --crate-type=rlib
7+
; (using rlib to avoid including panic handlers in IR)
8+
;
9+
; lib.rs:
10+
;
11+
; #![no_std]
12+
; mod my_module {
13+
; #[repr(C)]
14+
; pub struct MyStruct {
15+
; pub a: i32,
16+
; pub b: i32,
17+
; }
18+
; }
19+
; #[unsafe(no_mangle)]
20+
; extern "C" fn mainCRTStartup() -> my_module::MyStruct {
21+
; my_module::MyStruct { a: 3, b: 4 }
22+
; }
23+
; #[unsafe(no_mangle)]
24+
; extern "C" fn main() {}
25+
26+
; =======================================================================
27+
; ModuleID = 'lib.b43fc69277defcf4-cgu.0'
28+
source_filename = "lib.b43fc69277defcf4-cgu.0"
29+
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
30+
target triple = "x86_64-pc-windows-msvc"
31+
32+
; Function Attrs: nounwind uwtable
33+
define i64 @mainCRTStartup() unnamed_addr #0 !dbg !6 {
34+
start:
35+
%_0 = alloca [8 x i8], align 4
36+
store i32 3, ptr %_0, align 4, !dbg !20
37+
%0 = getelementptr inbounds i8, ptr %_0, i64 4, !dbg !20
38+
store i32 4, ptr %0, align 4, !dbg !20
39+
%1 = load i64, ptr %_0, align 4, !dbg !21
40+
ret i64 %1, !dbg !21
41+
}
42+
43+
; Function Attrs: nounwind uwtable
44+
define void @main() unnamed_addr #0 !dbg !22 {
45+
start:
46+
ret void, !dbg !25
47+
}
48+
49+
attributes #0 = { nounwind uwtable "target-cpu"="x86-64" "target-features"="+cx16,+sse3,+sahf" }
50+
51+
!llvm.module.flags = !{!0, !1, !2}
52+
!llvm.ident = !{!3}
53+
!llvm.dbg.cu = !{!4}
54+
55+
!0 = !{i32 8, !"PIC Level", i32 2}
56+
!1 = !{i32 2, !"CodeView", i32 1}
57+
!2 = !{i32 2, !"Debug Info Version", i32 3}
58+
!3 = !{!"rustc version 1.88.0 (6b00bc388 2025-06-23)"}
59+
!4 = distinct !DICompileUnit(language: DW_LANG_Rust, file: !5, producer: "clang LLVM (rustc version 1.88.0 (6b00bc388 2025-06-23))", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false)
60+
!5 = !DIFile(filename: "src/lib.rs\\@\\lib.b43fc69277defcf4-cgu.0", directory: "F:\\Dev\\testing")
61+
!6 = distinct !DISubprogram(name: "mainCRTStartup", scope: !8, file: !7, line: 12, type: !9, scopeLine: 12, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !4, templateParams: !19)
62+
!7 = !DIFile(filename: "src/lib.rs", directory: "F:\\Dev\\testing", checksumkind: CSK_SHA256, checksum: "586087c038922a8fa0183c4b20c075445761d545e02d06af80cd5a62dcadb3ec")
63+
!8 = !DINamespace(name: "lib", scope: null)
64+
!9 = !DISubroutineType(types: !10)
65+
!10 = !{!11}
66+
!11 = !DICompositeType(tag: DW_TAG_structure_type, name: "MyStruct", scope: !13, file: !12, size: 64, align: 32, flags: DIFlagPublic, elements: !14, templateParams: !19, identifier: "99a3f33b03974e4eaf7224f807a544bf")
67+
!12 = !DIFile(filename: "<unknown>", directory: "")
68+
!13 = !DINamespace(name: "my_module", scope: !8)
69+
!14 = !{!15, !18}
70+
!15 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !11, file: !12, baseType: !16, size: 32, align: 32, flags: DIFlagPublic)
71+
!16 = !DIDerivedType(tag: DW_TAG_typedef, name: "i32", file: !12, baseType: !17)
72+
!17 = !DIBasicType(name: "__int32", size: 32, encoding: DW_ATE_signed)
73+
!18 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !11, file: !12, baseType: !16, size: 32, align: 32, offset: 32, flags: DIFlagPublic)
74+
!19 = !{}
75+
!20 = !DILocation(line: 13, scope: !6)
76+
!21 = !DILocation(line: 14, scope: !6)
77+
!22 = distinct !DISubprogram(name: "main", scope: !8, file: !7, line: 17, type: !23, scopeLine: 17, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !4, templateParams: !19)
78+
!23 = !DISubroutineType(types: !24)
79+
!24 = !{null}
80+
!25 = !DILocation(line: 17, scope: !22)
81+
82+
; CHECK: Type{{.*}} , name = "MyStruct", size = 8, compiler_type = {{.*}} struct MyStruct {
83+
; CHECK-NEXT: int a;
84+
; CHECK-NEXT: int b;
85+
; CHECK-NEXT: }

lldb/test/Shell/lit.cfg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
# suffixes: A list of file extensions to treat as test files. This is overriden
2727
# by individual lit.local.cfg files in the test subdirectories.
28-
config.suffixes = [".test", ".cpp", ".s", ".m"]
28+
config.suffixes = [".test", ".cpp", ".s", ".m", ".ll"]
2929

3030
# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
3131
# subdirectories contain auxiliary inputs for various tests in their parent

0 commit comments

Comments
 (0)