[OptTable] Fix typo VALUE => VALUES (NFCI) (#121523)
[llvm-project.git] / lldb / examples / synthetic / recognizer_function / example.py
bloba0d80f0f7c5aa6239aba53a8bbddbdfc695029fd
1 # Formatters for classes that derive from Message.
3 # Usage:
4 # command script import ./example.py
5 # type summary add --expand --recognizer-function --python-function example.message_summary example.is_message_type
6 # type synth add --recognizer-function --python-class example.MessageChildProvider example.is_message_type
8 import sys
11 def is_message_type(t, internal_dict):
12 for base in t.get_bases_array():
13 if base.GetName() == "Message":
14 return True
15 return False
18 def message_summary(value, internal_dict):
19 # Could have used a summary string as well. All the work is done by the child
20 # provider.
21 return "Message"
24 class MessageChildProvider:
25 def __init__(self, value, internal_dict):
26 self.value = value
27 self.synthetic_children = self._analyze_children(value)
29 def has_children(self):
30 return self.num_children() > 0
32 def num_children(self):
33 return len(self.synthetic_children)
35 def get_child_index(self, name):
36 for index, child in enumerate(self.synthetic_children):
37 if child.GetName() == name:
38 return index
39 return None
41 def get_child_at_index(self, index):
42 return self.synthetic_children[index]
44 def _rename_sbvalue(self, value):
45 # We want to display the field with its original name without a trailing
46 # underscore. So we create a new SBValue with the same type and address but
47 # a different name.
48 name = value.GetName()
49 assert name.endswith("_")
50 new_name = name[:-1]
51 return value.CreateValueFromAddress(
52 new_name, value.GetLoadAddress(), value.GetType()
55 def _analyze_children(self, value):
56 result = []
57 for i in range(value.GetNumChildren()):
58 child = value.GetChildAtIndex(i)
59 child_name = child.GetName()
60 if child_name.startswith("_"):
61 continue # Internal field, skip
62 # Normal field. Check presence bit.
63 presence_bit = value.GetChildMemberWithName("_has_" + child_name)
64 if presence_bit.GetValueAsUnsigned() != 0:
65 result.append(self._rename_sbvalue(child))
66 return result