lldb-webkit: Pretty-print OptionSet
authordbates@webkit.org <dbates@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 27 Aug 2018 16:59:32 +0000 (16:59 +0000)
committerdbates@webkit.org <dbates@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 27 Aug 2018 16:59:32 +0000 (16:59 +0000)
https://bugs.webkit.org/show_bug.cgi?id=188936

Reviewed by Simon Fraser.

Add LLDB formatters to pretty-print an OptionSet.

* lldb/lldbWebKitTester/main.cpp:
(testSummaryProviders):
* lldb/lldb_webkit.py:
(__lldb_init_module):
(__lldb_init_module.lldb_webkit):
(WTFOptionSet_SummaryProvider):
(WTFOptionSetProvider):
(WTFOptionSetProvider.__init__):
(WTFOptionSetProvider.has_children):
(WTFOptionSetProvider.num_children):
(WTFOptionSetProvider.get_child_index):
(WTFOptionSetProvider.get_child_at_index):
(WTFOptionSetProvider.update):
* lldb/lldb_webkit_unittest.py:
(TestSummaryProviders.serial_test_WTFHashSet_tablesize_and_size):
(TestSummaryProviders):
(TestSummaryProviders.serial_test_WTFOptionSet_SummaryProvider_empty):
(TestSummaryProviders.serial_test_WTFOptionSet_SummaryProvider_simple):
(TestSummaryProviders.serial_test_WTFOptionSetProvider_empty):
(TestSummaryProviders.serial_test_WTFOptionSetProvider_simple):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@235376 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/ChangeLog
Tools/lldb/lldbWebKitTester/main.cpp
Tools/lldb/lldb_webkit.py
Tools/lldb/lldb_webkit_unittest.py

index d5bd385..bde6783 100644 (file)
@@ -1,3 +1,33 @@
+2018-08-27  Daniel Bates  <dabates@apple.com>
+
+        lldb-webkit: Pretty-print OptionSet
+        https://bugs.webkit.org/show_bug.cgi?id=188936
+
+        Reviewed by Simon Fraser.
+
+        Add LLDB formatters to pretty-print an OptionSet.
+
+        * lldb/lldbWebKitTester/main.cpp:
+        (testSummaryProviders):
+        * lldb/lldb_webkit.py:
+        (__lldb_init_module):
+        (__lldb_init_module.lldb_webkit):
+        (WTFOptionSet_SummaryProvider):
+        (WTFOptionSetProvider):
+        (WTFOptionSetProvider.__init__):
+        (WTFOptionSetProvider.has_children):
+        (WTFOptionSetProvider.num_children):
+        (WTFOptionSetProvider.get_child_index):
+        (WTFOptionSetProvider.get_child_at_index):
+        (WTFOptionSetProvider.update):
+        * lldb/lldb_webkit_unittest.py:
+        (TestSummaryProviders.serial_test_WTFHashSet_tablesize_and_size):
+        (TestSummaryProviders):
+        (TestSummaryProviders.serial_test_WTFOptionSet_SummaryProvider_empty):
+        (TestSummaryProviders.serial_test_WTFOptionSet_SummaryProvider_simple):
+        (TestSummaryProviders.serial_test_WTFOptionSetProvider_empty):
+        (TestSummaryProviders.serial_test_WTFOptionSetProvider_simple):
+
 2018-08-27  Wenson Hsieh  <wenson_hsieh@apple.com>
 
         [Attachment Support] [WK2] Images copied from Mail message view paste with the wrong file name in compose
index 2ce8d61..9f8af52 100644 (file)
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <wtf/HashMap.h>
 #include <wtf/HashSet.h>
+#include <wtf/OptionSet.h>
 #include <wtf/Vector.h>
 #include <wtf/text/StringBuilder.h>
 #include <wtf/text/WTFString.h>
@@ -44,6 +45,13 @@ static String utf16String(const char16_t (&string)[length])
     return builder.toString();
 }
 
+enum class ExampleFlags {
+    A = 1 << 0,
+    B = 1 << 1,
+    C = 1 << 2,
+    D = 1 << 3,
+};
+
 static void testSummaryProviders()
 {
     String aNullString { "" };
@@ -73,6 +81,9 @@ static void testSummaryProviders()
     HashMap<unsigned, Vector<int>> hashMapOfVectors;
     hashMapOfVectors.add(1, Vector<int>({2, 3}));
 
+    OptionSet<ExampleFlags> exampleFlagsEmpty;
+    OptionSet<ExampleFlags> exampleFlagsSimple { ExampleFlags::A, ExampleFlags::D, ExampleFlags::C };
+
     breakForTestingSummaryProviders();
 }
 
index aabae4d..83dced4 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2012-2017 Apple Inc. All rights reserved.
+# Copyright (C) 2012-2018 Apple Inc. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
@@ -44,6 +44,7 @@ def __lldb_init_module(debugger, dict):
     debugger.HandleCommand('type summary add --expand -F lldb_webkit.WTFHashMap_SummaryProvider -x "^WTF::HashMap<.+>$"')
     debugger.HandleCommand('type summary add --expand -F lldb_webkit.WTFHashSet_SummaryProvider -x "^WTF::HashSet<.+>$"')
     debugger.HandleCommand('type summary add --expand -F lldb_webkit.WTFMediaTime_SummaryProvider WTF::MediaTime')
+    debugger.HandleCommand('type summary add --expand -F lldb_webkit.WTFOptionSet_SummaryProvider -x "^WTF::OptionSet<.+>$"')
 
     debugger.HandleCommand('type summary add -F lldb_webkit.WebCoreURL_SummaryProvider WebCore::URL')
     debugger.HandleCommand('type summary add -F lldb_webkit.WebCoreColor_SummaryProvider WebCore::Color')
@@ -64,6 +65,7 @@ def __lldb_init_module(debugger, dict):
     # synthetic types (see <https://lldb.llvm.org/varformats.html>)
     debugger.HandleCommand('type synthetic add -x "^WTF::Vector<.+>$" --python-class lldb_webkit.WTFVectorProvider')
     debugger.HandleCommand('type synthetic add -x "^WTF::HashTable<.+>$" --python-class lldb_webkit.WTFHashTableProvider')
+    debugger.HandleCommand('type synthetic add -x "^WTF::OptionSet<.+>$" --python-class lldb_webkit.WTFOptionSetProvider')
 
 
 def WTFString_SummaryProvider(valobj, dict):
@@ -107,6 +109,11 @@ def WTFHashSet_SummaryProvider(valobj, dict):
     return "{ tableSize = %d, keyCount = %d }" % (provider.tableSize(), provider.keyCount())
 
 
+def WTFOptionSet_SummaryProvider(valobj, dict):
+    provider = WTFOptionSetProvider(valobj, dict)
+    return "{ size = %d }" % provider.size
+
+
 def WTFMediaTime_SummaryProvider(valobj, dict):
     provider = WTFMediaTimeProvider(valobj, dict)
     if provider.isInvalid():
@@ -574,6 +581,53 @@ class WebCoreURLProvider:
         return WTFStringProvider(self.valobj.GetChildMemberWithName('m_string'), dict).to_string()
 
 
+class WTFOptionSetProvider:
+    def __init__(self, valobj, internal_dict):
+        self.valobj = valobj
+        self.update()
+
+    def has_children(self):
+        return bool(self._elements)
+
+    #  Metadata is stored at indices greater than or equal to the number of elements in the set.
+    def num_children(self):
+        return len(self._elements) + 1
+
+    def get_child_index(self, name):
+        if name == 'm_storage':
+            return self.num_children()
+        try:
+            return int(name.lstrip('[').rstrip(']'))
+        except:
+            return None
+
+    def get_child_at_index(self, index):
+        if index < 0 or not self.valobj.IsValid():
+            return None
+        if index == self.num_children():
+            return self.storage
+        if index < len(self._elements):
+            (name, value) = self._elements[index]
+            return self.valobj.CreateValueFromExpression(name, str(value))
+        return None
+
+    def update(self):
+        self.storage = self.valobj.GetChildMemberWithName('m_storage')
+
+        template_argument_sbType = self.valobj.GetType().GetTemplateArgumentType(0)
+        enumerator_value_to_name_map = {sbTypeEnumMember.GetValueAsUnsigned(): sbTypeEnumMember.GetName() for sbTypeEnumMember in template_argument_sbType.get_enum_members_array()}
+
+        # Iterate from least significant bit to most significant bit.
+        elements = []
+        bitmask = self.storage.GetValueAsUnsigned(0)
+        while bitmask > 0:
+            current = bitmask & -bitmask  # Isolate the rightmost set bit.
+            elements.append((enumerator_value_to_name_map[current], current))  # e.g. ('Spelling', 4)
+            bitmask = bitmask & (bitmask - 1)  # Turn off the rightmost set bit.
+        self._elements = elements
+        self.size = len(elements)
+
+
 class WTFVectorProvider:
     def __init__(self, valobj, internal_dict):
         self.valobj = valobj
index 865c97a..32eee24 100755 (executable)
@@ -167,3 +167,32 @@ class TestSummaryProviders(unittest.TestCase):
         variable = self._sbFrame.FindVariable('hashSetOfInts')
         summary = lldb_webkit.WTFHashSet_SummaryProvider(variable, {})
         self.assertEqual(summary, "{ tableSize = 8, keyCount = 1 }")
+
+    # MARK: WTFOptionSet_SummaryProvider test cases
+
+    def serial_test_WTFOptionSet_SummaryProvider_empty(self):
+        variable = self._sbFrame.FindVariable('exampleFlagsEmpty')
+        summary = lldb_webkit.WTFOptionSet_SummaryProvider(variable, {})
+        self.assertEqual(summary, "{ size = 0 }")
+
+    def serial_test_WTFOptionSet_SummaryProvider_simple(self):
+        variable = self._sbFrame.FindVariable('exampleFlagsSimple')
+        summary = lldb_webkit.WTFOptionSet_SummaryProvider(variable, {})
+        self.assertEqual(summary, "{ size = 3 }")
+
+    # MARK: WTFOptionSetProvider test cases
+
+    def serial_test_WTFOptionSetProvider_empty(self):
+        variable = self._sbFrame.FindVariable('exampleFlagsEmpty')
+        provider = lldb_webkit.WTFOptionSetProvider(variable, {})
+        self.assertEqual(provider.get_child_at_index(0), None)
+
+    def serial_test_WTFOptionSetProvider_simple(self):
+        variable = self._sbFrame.FindVariable('exampleFlagsSimple')
+        provider = lldb_webkit.WTFOptionSetProvider(variable, {})
+        self.assertEqual(provider.get_child_at_index(0).GetName(), 'A')
+        self.assertEqual(provider.get_child_at_index(0).GetValue(), '1')
+        self.assertEqual(provider.get_child_at_index(1).GetName(), 'C')
+        self.assertEqual(provider.get_child_at_index(1).GetValue(), '4')
+        self.assertEqual(provider.get_child_at_index(2).GetName(), 'D')
+        self.assertEqual(provider.get_child_at_index(2).GetValue(), '8')