xref: /llvm-project/lldb/test/API/python_api/formatters/synth.py (revision 82af55983d75d4a821b76ee926b19725ec7fa889)
1import lldb
2
3
4class jasSynthProvider:
5    def __init__(self, valobj, dict):
6        self.valobj = valobj
7
8    def num_children(self):
9        return 2
10
11    def get_child_at_index(self, index):
12        child = None
13        if index == 0:
14            child = self.valobj.GetChildMemberWithName("A")
15        if index == 1:
16            child = self.valobj.CreateValueFromExpression("X", "(int)1")
17        return child
18
19    def get_child_index(self, name):
20        if name == "A":
21            return 0
22        if name == "X":
23            return 1
24        return None
25
26
27def ccc_summary(sbvalue, internal_dict):
28    sbvalue = sbvalue.GetNonSyntheticValue()
29    # This tests that the SBValue.GetNonSyntheticValue() actually returns a
30    # non-synthetic value. If it does not, then sbvalue.GetChildMemberWithName("a")
31    # in the following statement will call the 'get_child_index' method of the
32    # synthetic child provider CCCSynthProvider below (which return the "b" field").
33    return "CCC object with leading value " + str(sbvalue.GetChildMemberWithName("a"))
34
35
36def ccc_synthetic(sbvalue, internal_dict):
37    sbvalue = sbvalue.GetSyntheticValue()
38    # This tests that the SBValue.GetSyntheticValue() actually returns a
39    # synthetic value. If it does, then sbvalue.GetChildMemberWithName("a")
40    # in the following statement will call the 'get_child_index' method of the
41    # synthetic child provider CCCSynthProvider below (which return the "b" field").
42    return "CCC object with leading synthetic value " + str(
43        sbvalue.GetChildMemberWithName("a")
44    )
45
46
47def bar_int_synthetic(sbvalue, internal_dict):
48    sbvalue = sbvalue.GetSyntheticValue()
49    # This tests that the SBValue.GetSyntheticValue() actually returns no
50    # value when the value has no synthetic representation.
51    return "bar_int synthetic: " + str(sbvalue)
52
53
54class CCCSynthProvider(object):
55    def __init__(self, sbvalue, internal_dict):
56        self._sbvalue = sbvalue
57
58    def num_children(self):
59        return 3
60
61    def get_child_index(self, name):
62        if name == "a":
63            # Return b for test.
64            return 1
65        raise RuntimeError("I don't want to be called!")
66
67    def get_child_at_index(self, index):
68        if index == 0:
69            return self._sbvalue.GetChildMemberWithName("a")
70        if index == 1:
71            return self._sbvalue.GetChildMemberWithName("b")
72        if index == 2:
73            return self._sbvalue.GetChildMemberWithName("c")
74
75
76def empty1_summary(sbvalue, internal_dict):
77    return "I am an empty Empty1"
78
79
80class Empty1SynthProvider(object):
81    def __init__(self, sbvalue, internal_dict):
82        self._sbvalue = sbvalue
83
84    def num_children(self):
85        return 0
86
87    def get_child_at_index(self, index):
88        return None
89
90
91def empty2_summary(sbvalue, internal_dict):
92    return "I am an empty Empty2"
93
94
95class Empty2SynthProvider(object):
96    def __init__(self, sbvalue, internal_dict):
97        self._sbvalue = sbvalue
98
99    def num_children(self):
100        return 0
101
102    def get_child_at_index(self, index):
103        return None
104
105
106def __lldb_init_module(debugger, dict):
107    debugger.CreateCategory("JASSynth").AddTypeSynthetic(
108        lldb.SBTypeNameSpecifier("JustAStruct"),
109        lldb.SBTypeSynthetic.CreateWithClassName("synth.jasSynthProvider"),
110    )
111    cat = debugger.CreateCategory("CCCSynth")
112    cat.AddTypeSynthetic(
113        lldb.SBTypeNameSpecifier("CCC"),
114        lldb.SBTypeSynthetic.CreateWithClassName(
115            "synth.CCCSynthProvider", lldb.eTypeOptionCascade
116        ),
117    )
118    cat.AddTypeSummary(
119        lldb.SBTypeNameSpecifier("CCC"),
120        lldb.SBTypeSummary.CreateWithFunctionName(
121            "synth.ccc_summary", lldb.eTypeOptionCascade
122        ),
123    )
124    cat.AddTypeSynthetic(
125        lldb.SBTypeNameSpecifier("Empty1"),
126        lldb.SBTypeSynthetic.CreateWithClassName("synth.Empty1SynthProvider"),
127    )
128    cat.AddTypeSummary(
129        lldb.SBTypeNameSpecifier("Empty1"),
130        lldb.SBTypeSummary.CreateWithFunctionName("synth.empty1_summary"),
131    )
132    cat.AddTypeSynthetic(
133        lldb.SBTypeNameSpecifier("Empty2"),
134        lldb.SBTypeSynthetic.CreateWithClassName("synth.Empty2SynthProvider"),
135    )
136    cat.AddTypeSummary(
137        lldb.SBTypeNameSpecifier("Empty2"),
138        lldb.SBTypeSummary.CreateWithFunctionName(
139            "synth.empty2_summary", lldb.eTypeOptionHideEmptyAggregates
140        ),
141    )
142    cat2 = debugger.CreateCategory("CCCSynth2")
143    cat2.AddTypeSynthetic(
144        lldb.SBTypeNameSpecifier("CCC"),
145        lldb.SBTypeSynthetic.CreateWithClassName(
146            "synth.CCCSynthProvider", lldb.eTypeOptionCascade
147        ),
148    )
149    cat2.AddTypeSummary(
150        lldb.SBTypeNameSpecifier("CCC"),
151        lldb.SBTypeSummary.CreateWithFunctionName(
152            "synth.ccc_synthetic", lldb.eTypeOptionCascade
153        ),
154    )
155    cat3 = debugger.CreateCategory("BarIntSynth")
156    cat3.AddTypeSummary(
157        lldb.SBTypeNameSpecifier("int"),
158        lldb.SBTypeSummary.CreateWithFunctionName(
159            "synth.bar_int_synthetic", lldb.eTypeOptionCascade
160        ),
161    )
162