Skip to content

Commit 25af88d

Browse files
authored
Unrolled build for #145218
Rollup merge of #145218 - nilptr:nilptr/feat/lldb-enum-pretty-printer, r=Mark-Simulacrum [Debuginfo] improve enum value formatting in LLDB for better readability > TL;DR: When debugging with CodeLLDB, I noticed enum values were often hard to read because LLDB lists every possible variant, resulting in a verbose and cluttered view, even though only one variant is actually valid. Interestingly, raw enum types display nicely. After some investigation, I found that `&enum` values get classified as `Other`, so it falls back to `DefaultSyntheticProvider`, which causes this verbose output. ## What does this PR do? This PR contains 2 commits: 1. change the enum value formatting from showing 2 separate fields (`value` for attached data and `$discr$` for the discriminator) to a concise `<readable variant name>: <attached data>` format 2. dereference pointer types in `classify_rust_type` so that it can return more accurate type for reference type ## Self-test proof Before: <img width="1706" height="799" alt="before" src="https://github.com/user-attachments/assets/b66c7e22-990a-4da5-9036-34e3f9f62367" /> After: <img width="1541" height="678" alt="after" src="https://github.com/user-attachments/assets/36db32e2-f822-4883-8f17-cb8067e509f6" />
2 parents 78b89eb + 6be749b commit 25af88d

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

src/etc/lldb_lookup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ def is_hashbrown_hashmap(hash_map: lldb.SBValue) -> bool:
1010

1111

1212
def classify_rust_type(type: lldb.SBType) -> str:
13+
if type.IsPointerType():
14+
type = type.GetPointeeType()
15+
1316
type_class = type.GetTypeClass()
1417
if type_class == lldb.eTypeClassStruct:
1518
return classify_struct(type.name, type.fields)

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

@@ -410,6 +411,16 @@ def get_type_name(self):
410411
return "&str"
411412

412413

414+
def _getVariantName(variant) -> str:
415+
"""
416+
Since the enum variant's type name is in the form `TheEnumName::TheVariantName$Variant`,
417+
we can extract `TheVariantName` from it for display purpose.
418+
"""
419+
s = variant.GetType().GetName()
420+
match = re.search(r"::([^:]+)\$Variant$", s)
421+
return match.group(1) if match else ""
422+
423+
413424
class ClangEncodedEnumProvider:
414425
"""Pretty-printer for 'clang-encoded' enums support implemented in LLDB"""
415426

@@ -424,37 +435,25 @@ def has_children(self) -> bool:
424435
return True
425436

426437
def num_children(self) -> int:
427-
if self.is_default:
428-
return 1
429-
return 2
438+
return 1
430439

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
440+
def get_child_index(self, _name: str) -> int:
436441
return -1
437442

438443
def get_child_at_index(self, index: int) -> SBValue:
439444
if index == 0:
440-
return self.variant.GetChildMemberWithName(
445+
value = self.variant.GetChildMemberWithName(
441446
ClangEncodedEnumProvider.VALUE_MEMBER_NAME
442447
)
443-
if index == 1:
444-
return self.variant.GetChildMemberWithName(
445-
ClangEncodedEnumProvider.DISCRIMINANT_MEMBER_NAME
448+
return value.CreateChildAtOffset(
449+
_getVariantName(self.variant), 0, 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)