Skip to content

[lldb] Add synthetic formatter for llvm::Expected #118758

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

Merged
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
46 changes: 46 additions & 0 deletions llvm/utils/lldbDataFormatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def __lldb_init_module(debugger, internal_dict):
'-x "^llvm::DenseMap<.+>$"'
)

debugger.HandleCommand(
"type synthetic add -w llvm "
f"-l {__name__}.ExpectedSynthetic "
'-x "^llvm::Expected<.+>$"'
)


# Pretty printer for llvm::SmallVector/llvm::SmallVectorImpl
class SmallVectorSynthProvider:
Expand Down Expand Up @@ -432,3 +438,43 @@ def update(self):
for indexes in key_buckets.values():
if len(indexes) == 1:
self.child_buckets.append(indexes[0])


class ExpectedSynthetic:
# The llvm::Expected<T> value.
expected: lldb.SBValue
# The stored success value or error value.
stored_value: lldb.SBValue

def __init__(self, valobj: lldb.SBValue, _) -> None:
self.expected = valobj

def update(self) -> None:
has_error = self.expected.GetChildMemberWithName("HasError").unsigned
if not has_error:
name = "value"
member = "TStorage"
else:
name = "error"
member = "ErrorStorage"
# Anonymous union.
union = self.expected.child[0]
storage = union.GetChildMemberWithName(member)
stored_type = storage.type.template_args[0]
self.stored_value = storage.Cast(stored_type).Clone(name)

def num_children(self) -> int:
return 1

def get_child_index(self, name: str) -> int:
if name == self.stored_value.name:
return 0
# Allow dereferencing for values, not errors.
if name == "$$dereference$$" and self.stored_value.name == "value":
return 0
return -1

def get_child_at_index(self, idx: int) -> lldb.SBValue:
if idx == 0:
return self.stored_value
return lldb.SBValue()
Loading