[Lldb-commits] [PATCH] D114266: [formatters] Draft Optional PR

Danil Stefaniuc via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Fri Nov 19 10:39:16 PST 2021


danilashtefan created this revision.
danilashtefan requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114266

Files:
  lldb/examples/synthetic/gnu_libstdcpp.py
  lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp


Index: lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
===================================================================
--- lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
+++ lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
@@ -913,6 +913,11 @@
       SyntheticChildrenSP(new ScriptedSyntheticChildren(
           stl_deref_flags,
           "lldb.formatters.cpp.gnu_libstdcpp.StdMapLikeSynthProvider")));
+  cpp_category_sp->GetRegexTypeSyntheticsContainer()->Add(
+      RegularExpression("^std::optional<.+>(( )?&)?$"),
+      SyntheticChildrenSP(new ScriptedSyntheticChildren(
+          stl_synth_flags,
+          "lldb.formatters.cpp.gnu_libstdcpp.StdOptionalSynthProvider")));
   cpp_category_sp->GetRegexTypeSyntheticsContainer()->Add(
       RegularExpression("^std::multiset<.+> >(( )?&)?$"),
       SyntheticChildrenSP(new ScriptedSyntheticChildren(
Index: lldb/examples/synthetic/gnu_libstdcpp.py
===================================================================
--- lldb/examples/synthetic/gnu_libstdcpp.py
+++ lldb/examples/synthetic/gnu_libstdcpp.py
@@ -8,6 +8,46 @@
 # You are encouraged to look at the STL implementation for your platform
 # before relying on these formatters to do the right thing for your setup
 
+class StdOptionalSynthProvider:
+    def __init__(self, valobj, dict):
+        self.valobj = valobj
+
+    def update(self):
+        try:
+            self.payload = self.valobj.GetChildMemberWithName('_M_payload')
+            self.engaged = self.payload.GetChildMemberWithName('_M_engaged')
+            self.payload_payload= self.payload.GetChildMemberWithName('_M_payload')
+        except:
+            pass
+        return False
+
+    def has_value(self):
+        logger = lldb.formatters.Logger.Logger()
+        try:
+            return self.engaged.GetValueAsUnsigned(0) == 1
+        except:
+            logger >> "Error determining engaged value"
+            return False
+
+    def num_children(self):
+        logger = lldb.formatters.Logger.Logger()
+        try:
+            num_children = 1 if self.has_value() else 0
+            return num_children
+        except:
+            logger >> "Error determining number of children"
+
+    def get_child_index(self, name):
+        try:
+            return int(name.lstrip('[').rstrip(']'))
+        except:
+            return -1
+
+    def get_child_at_index(self, index):
+        self.payload_value = self.payload_payload.GetChildMemberWithName("_M_value").GetValue()
+        if (self.has_value() == False):
+            pass
+        return self.payload_value
 
 class AbstractListSynthProvider:
     def __init__(self, valobj, dict, has_prev):


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114266.388558.patch
Type: text/x-patch
Size: 2700 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20211119/cc759a0a/attachment.bin>


More information about the lldb-commits mailing list