Skip to content

[Debuginfo] improve enum value formatting in LLDB for better readability #145218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/etc/lldb_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def is_hashbrown_hashmap(hash_map: lldb.SBValue) -> bool:


def classify_rust_type(type: lldb.SBType) -> str:
if type.IsPointerType():
type = type.GetPointeeType()

type_class = type.GetTypeClass()
if type_class == lldb.eTypeClassStruct:
return classify_struct(type.name, type.fields)
Expand Down
35 changes: 17 additions & 18 deletions src/etc/lldb_providers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations
import re
import sys
from typing import List, TYPE_CHECKING

Expand Down Expand Up @@ -410,6 +411,16 @@ def get_type_name(self):
return "&str"


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


class ClangEncodedEnumProvider:
"""Pretty-printer for 'clang-encoded' enums support implemented in LLDB"""

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

def num_children(self) -> int:
if self.is_default:
return 1
return 2
return 1

def get_child_index(self, name: str) -> int:
if name == ClangEncodedEnumProvider.VALUE_MEMBER_NAME:
return 0
if name == ClangEncodedEnumProvider.DISCRIMINANT_MEMBER_NAME:
return 1
def get_child_index(self, _name: str) -> int:
return -1

def get_child_at_index(self, index: int) -> SBValue:
if index == 0:
return self.variant.GetChildMemberWithName(
value = self.variant.GetChildMemberWithName(
ClangEncodedEnumProvider.VALUE_MEMBER_NAME
)
if index == 1:
return self.variant.GetChildMemberWithName(
ClangEncodedEnumProvider.DISCRIMINANT_MEMBER_NAME
return value.CreateChildAtOffset(
_getVariantName(self.variant), 0, value.GetType()
)
return None

def update(self):
all_variants = self.valobj.GetChildAtIndex(0)
index = self._getCurrentVariantIndex(all_variants)
self.variant = all_variants.GetChildAtIndex(index)
self.is_default = (
self.variant.GetIndexOfChildWithName(
ClangEncodedEnumProvider.DISCRIMINANT_MEMBER_NAME
)
== -1
)

def _getCurrentVariantIndex(self, all_variants: SBValue) -> int:
default_index = 0
Expand Down
6 changes: 3 additions & 3 deletions tests/debuginfo/borrowed-enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
// lldb-command:run

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

#![allow(unused_variables)]

Expand Down
Loading