[llvm] 398f3b3 - [lldb] Add synthetic formatter for llvm::Expected (#118758)

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 5 19:31:23 PST 2024


Author: Dave Lee
Date: 2024-12-05T19:31:20-08:00
New Revision: 398f3b368af9bced530e65c02bb38136d7f69caf

URL: https://github.com/llvm/llvm-project/commit/398f3b368af9bced530e65c02bb38136d7f69caf
DIFF: https://github.com/llvm/llvm-project/commit/398f3b368af9bced530e65c02bb38136d7f69caf.diff

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

Adds a synthetic formatter for `llvm::Expected<T>`. The synthetic value
will have a single child, either `value` or `error`.

Added: 
    

Modified: 
    llvm/utils/lldbDataFormatters.py

Removed: 
    


################################################################################
diff  --git a/llvm/utils/lldbDataFormatters.py b/llvm/utils/lldbDataFormatters.py
index 4a34ad2a87c331..09143d2d81cbb7 100644
--- a/llvm/utils/lldbDataFormatters.py
+++ b/llvm/utils/lldbDataFormatters.py
@@ -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:
@@ -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()


        


More information about the llvm-commits mailing list