Skip to content

Commit 3758519

Browse files
committed
feat(lldb debug info): improve enum value formatting in lldb
1 parent 915a766 commit 3758519

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

src/etc/lldb_providers.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import annotations
2+
import re
23
import sys
34
from typing import List, TYPE_CHECKING
45

@@ -409,6 +410,14 @@ def get_type_name(self):
409410
else:
410411
return "&str"
411412

413+
def _getVariantName(variant) -> str:
414+
"""
415+
Since the enum variant's type name is in the form `TheEnumName::TheVariantName$Variant`,
416+
we can extract `TheVariantName` from it for display purpose.
417+
"""
418+
s = variant.GetType().GetName()
419+
match = re.search(r'::([^:]+)\$Variant$', s)
420+
return match.group(1) if match else ""
412421

413422
class ClangEncodedEnumProvider:
414423
"""Pretty-printer for 'clang-encoded' enums support implemented in LLDB"""
@@ -424,37 +433,27 @@ def has_children(self) -> bool:
424433
return True
425434

426435
def num_children(self) -> int:
427-
if self.is_default:
428-
return 1
429-
return 2
436+
return 1
430437

431-
def get_child_index(self, name: str) -> int:
432-
if name == ClangEncodedEnumProvider.VALUE_MEMBER_NAME:
433-
return 0
434-
if name == ClangEncodedEnumProvider.DISCRIMINANT_MEMBER_NAME:
435-
return 1
438+
def get_child_index(self, _name: str) -> int:
436439
return -1
437440

438441
def get_child_at_index(self, index: int) -> SBValue:
439442
if index == 0:
440-
return self.variant.GetChildMemberWithName(
443+
value = self.variant.GetChildMemberWithName(
441444
ClangEncodedEnumProvider.VALUE_MEMBER_NAME
442445
)
443-
if index == 1:
444-
return self.variant.GetChildMemberWithName(
445-
ClangEncodedEnumProvider.DISCRIMINANT_MEMBER_NAME
446+
return value.CreateChildAtOffset(
447+
_getVariantName(self.variant),
448+
0,
449+
value.GetType()
446450
)
451+
return None
447452

448453
def update(self):
449454
all_variants = self.valobj.GetChildAtIndex(0)
450455
index = self._getCurrentVariantIndex(all_variants)
451456
self.variant = all_variants.GetChildAtIndex(index)
452-
self.is_default = (
453-
self.variant.GetIndexOfChildWithName(
454-
ClangEncodedEnumProvider.DISCRIMINANT_MEMBER_NAME
455-
)
456-
== -1
457-
)
458457

459458
def _getCurrentVariantIndex(self, all_variants: SBValue) -> int:
460459
default_index = 0

tests/debuginfo/borrowed-enum.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
// lldb-command:run
2323

2424
// lldb-command:v *the_a_ref
25-
// lldb-check:(borrowed_enum::ABC) *the_a_ref = { value = { x = 0 y = 8970181431921507452 } $discr$ = 0 }
25+
// lldb-check:(borrowed_enum::ABC) *the_a_ref = { TheA = { x = 0 y = 8970181431921507452 } }
2626
// lldb-command:v *the_b_ref
27-
// lldb-check:(borrowed_enum::ABC) *the_b_ref = { value = { 0 = 0 1 = 286331153 2 = 286331153 } $discr$ = 1 }
27+
// lldb-check:(borrowed_enum::ABC) *the_b_ref = { TheB = { 0 = 0 1 = 286331153 2 = 286331153 } }
2828
// lldb-command:v *univariant_ref
29-
// lldb-check:(borrowed_enum::Univariant) *univariant_ref = { value = { 0 = 4820353753753434 } }
29+
// lldb-check:(borrowed_enum::Univariant) *univariant_ref = { TheOnlyCase = { 0 = 4820353753753434 } }
3030

3131
#![allow(unused_variables)]
3232

0 commit comments

Comments
 (0)