[Lldb-commits] [lldb] 4ba5da8 - Improve optional formatter

Walter Erquinigo via lldb-commits lldb-commits at lists.llvm.org
Tue Nov 23 13:58:38 PST 2021


Author: Walter Erquinigo
Date: 2021-11-23T13:52:17-08:00
New Revision: 4ba5da8e3d38f38ed7845c22baadd8908729c520

URL: https://github.com/llvm/llvm-project/commit/4ba5da8e3d38f38ed7845c22baadd8908729c520
DIFF: https://github.com/llvm/llvm-project/commit/4ba5da8e3d38f38ed7845c22baadd8908729c520.diff

LOG: Improve optional formatter

As suggested by @labath in https://reviews.llvm.org/D114403, we should
make the formatter more resilient to corrupted data. The Libcxx version
explicitly checks for engaged = 1, so we can do that as well for safety.

Differential Revision: https://reviews.llvm.org/D114450

Added: 
    

Modified: 
    lldb/examples/synthetic/gnu_libstdcpp.py
    lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
    lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/optional/TestDataFormatterGenericOptional.py

Removed: 
    


################################################################################
diff  --git a/lldb/examples/synthetic/gnu_libstdcpp.py b/lldb/examples/synthetic/gnu_libstdcpp.py
index 9f1ba29ae126..efeb4fe29546 100644
--- a/lldb/examples/synthetic/gnu_libstdcpp.py
+++ b/lldb/examples/synthetic/gnu_libstdcpp.py
@@ -1,5 +1,4 @@
 from __future__ import division
-import re
 import lldb.formatters.Logger
 
 # C++ STL formatters for LLDB
@@ -22,14 +21,14 @@ def update(self):
         try:
             self.payload = self.valobj.GetChildMemberWithName('_M_payload')
             self.value = self.payload.GetChildMemberWithName('_M_payload')
-            self.count = self.payload.GetChildMemberWithName('_M_engaged').GetValueAsUnsigned(0)
+            self.has_value = self.payload.GetChildMemberWithName('_M_engaged').GetValueAsUnsigned(0) != 0
         except:
-            self.count = 0
+            self.has_value = False
         return False
 
 
     def num_children(self):
-        return self.count
+        return 1 if self.has_value else 0
 
     def get_child_index(self, name):
         return 0

diff  --git a/lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp b/lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
index c0c819632851..c1b40ba65e7d 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
+++ b/lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
@@ -45,7 +45,7 @@ bool OptionalFrontEnd::Update() {
   // __engaged_ is a bool flag and is true if the optional contains a value.
   // Converting it to unsigned gives us a size of 1 if it contains a value
   // and 0 if not.
-  m_has_value = engaged_sp->GetValueAsUnsigned(0) == 1;
+  m_has_value = engaged_sp->GetValueAsUnsigned(0) != 0;
 
   return false;
 }

diff  --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/optional/TestDataFormatterGenericOptional.py b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/optional/TestDataFormatterGenericOptional.py
index 557875132a65..b95abf224ff9 100644
--- a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/optional/TestDataFormatterGenericOptional.py
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/optional/TestDataFormatterGenericOptional.py
@@ -12,6 +12,15 @@ class GenericOptionalDataFormatterTestCase(TestBase):
 
     def do_test_with_run_command(self, stdlib_type):
         """Test that that file and class static variables display correctly."""
+        # This is the function to remove the custom formats in order to have a
+        # clean slate for the next test case.
+        def cleanup():
+            self.runCmd('type format clear', check=False)
+            self.runCmd('type summary clear', check=False)
+            self.runCmd('type filter clear', check=False)
+            self.runCmd('type synth clear', check=False)
+        self.addTearDownHook(cleanup)
+
         self.build(dictionary={stdlib_type: "1"})
         self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
 


        


More information about the lldb-commits mailing list