xref: /llvm-project/lldb/test/API/python_api/formatters/synth.py (revision 82af55983d75d4a821b76ee926b19725ec7fa889)
199451b44SJordan Rupprechtimport lldb
299451b44SJordan Rupprecht
399451b44SJordan Rupprecht
499451b44SJordan Rupprechtclass jasSynthProvider:
599451b44SJordan Rupprecht    def __init__(self, valobj, dict):
699451b44SJordan Rupprecht        self.valobj = valobj
799451b44SJordan Rupprecht
899451b44SJordan Rupprecht    def num_children(self):
999451b44SJordan Rupprecht        return 2
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprecht    def get_child_at_index(self, index):
1299451b44SJordan Rupprecht        child = None
1399451b44SJordan Rupprecht        if index == 0:
142238dcc3SJonas Devlieghere            child = self.valobj.GetChildMemberWithName("A")
1599451b44SJordan Rupprecht        if index == 1:
162238dcc3SJonas Devlieghere            child = self.valobj.CreateValueFromExpression("X", "(int)1")
1799451b44SJordan Rupprecht        return child
1899451b44SJordan Rupprecht
1999451b44SJordan Rupprecht    def get_child_index(self, name):
202238dcc3SJonas Devlieghere        if name == "A":
2199451b44SJordan Rupprecht            return 0
222238dcc3SJonas Devlieghere        if name == "X":
2399451b44SJordan Rupprecht            return 1
2499451b44SJordan Rupprecht        return None
2599451b44SJordan Rupprecht
2699451b44SJordan Rupprecht
2799451b44SJordan Rupprechtdef ccc_summary(sbvalue, internal_dict):
2899451b44SJordan Rupprecht    sbvalue = sbvalue.GetNonSyntheticValue()
2999451b44SJordan Rupprecht    # This tests that the SBValue.GetNonSyntheticValue() actually returns a
3099451b44SJordan Rupprecht    # non-synthetic value. If it does not, then sbvalue.GetChildMemberWithName("a")
3199451b44SJordan Rupprecht    # in the following statement will call the 'get_child_index' method of the
32*82af5598SVincent Belliard    # synthetic child provider CCCSynthProvider below (which return the "b" field").
332238dcc3SJonas Devlieghere    return "CCC object with leading value " + str(sbvalue.GetChildMemberWithName("a"))
3499451b44SJordan Rupprecht
3599451b44SJordan Rupprecht
36*82af5598SVincent Belliarddef ccc_synthetic(sbvalue, internal_dict):
37*82af5598SVincent Belliard    sbvalue = sbvalue.GetSyntheticValue()
38*82af5598SVincent Belliard    # This tests that the SBValue.GetSyntheticValue() actually returns a
39*82af5598SVincent Belliard    # synthetic value. If it does, then sbvalue.GetChildMemberWithName("a")
40*82af5598SVincent Belliard    # in the following statement will call the 'get_child_index' method of the
41*82af5598SVincent Belliard    # synthetic child provider CCCSynthProvider below (which return the "b" field").
42*82af5598SVincent Belliard    return "CCC object with leading synthetic value " + str(
43*82af5598SVincent Belliard        sbvalue.GetChildMemberWithName("a")
44*82af5598SVincent Belliard    )
45*82af5598SVincent Belliard
46*82af5598SVincent Belliard
47*82af5598SVincent Belliarddef bar_int_synthetic(sbvalue, internal_dict):
48*82af5598SVincent Belliard    sbvalue = sbvalue.GetSyntheticValue()
49*82af5598SVincent Belliard    # This tests that the SBValue.GetSyntheticValue() actually returns no
50*82af5598SVincent Belliard    # value when the value has no synthetic representation.
51*82af5598SVincent Belliard    return "bar_int synthetic: " + str(sbvalue)
52*82af5598SVincent Belliard
53*82af5598SVincent Belliard
5499451b44SJordan Rupprechtclass CCCSynthProvider(object):
5599451b44SJordan Rupprecht    def __init__(self, sbvalue, internal_dict):
5699451b44SJordan Rupprecht        self._sbvalue = sbvalue
5799451b44SJordan Rupprecht
5899451b44SJordan Rupprecht    def num_children(self):
5999451b44SJordan Rupprecht        return 3
6099451b44SJordan Rupprecht
6199451b44SJordan Rupprecht    def get_child_index(self, name):
62*82af5598SVincent Belliard        if name == "a":
63*82af5598SVincent Belliard            # Return b for test.
64*82af5598SVincent Belliard            return 1
6599451b44SJordan Rupprecht        raise RuntimeError("I don't want to be called!")
6699451b44SJordan Rupprecht
6799451b44SJordan Rupprecht    def get_child_at_index(self, index):
6899451b44SJordan Rupprecht        if index == 0:
6999451b44SJordan Rupprecht            return self._sbvalue.GetChildMemberWithName("a")
7099451b44SJordan Rupprecht        if index == 1:
7199451b44SJordan Rupprecht            return self._sbvalue.GetChildMemberWithName("b")
7299451b44SJordan Rupprecht        if index == 2:
7399451b44SJordan Rupprecht            return self._sbvalue.GetChildMemberWithName("c")
7499451b44SJordan Rupprecht
7599451b44SJordan Rupprecht
7699451b44SJordan Rupprechtdef empty1_summary(sbvalue, internal_dict):
7799451b44SJordan Rupprecht    return "I am an empty Empty1"
7899451b44SJordan Rupprecht
7999451b44SJordan Rupprecht
8099451b44SJordan Rupprechtclass Empty1SynthProvider(object):
8199451b44SJordan Rupprecht    def __init__(self, sbvalue, internal_dict):
8299451b44SJordan Rupprecht        self._sbvalue = sbvalue
8399451b44SJordan Rupprecht
8499451b44SJordan Rupprecht    def num_children(self):
8599451b44SJordan Rupprecht        return 0
8699451b44SJordan Rupprecht
8799451b44SJordan Rupprecht    def get_child_at_index(self, index):
8899451b44SJordan Rupprecht        return None
8999451b44SJordan Rupprecht
9099451b44SJordan Rupprecht
9199451b44SJordan Rupprechtdef empty2_summary(sbvalue, internal_dict):
9299451b44SJordan Rupprecht    return "I am an empty Empty2"
9399451b44SJordan Rupprecht
9499451b44SJordan Rupprecht
9599451b44SJordan Rupprechtclass Empty2SynthProvider(object):
9699451b44SJordan Rupprecht    def __init__(self, sbvalue, internal_dict):
9799451b44SJordan Rupprecht        self._sbvalue = sbvalue
9899451b44SJordan Rupprecht
9999451b44SJordan Rupprecht    def num_children(self):
10099451b44SJordan Rupprecht        return 0
10199451b44SJordan Rupprecht
10299451b44SJordan Rupprecht    def get_child_at_index(self, index):
10399451b44SJordan Rupprecht        return None
10499451b44SJordan Rupprecht
10599451b44SJordan Rupprecht
10699451b44SJordan Rupprechtdef __lldb_init_module(debugger, dict):
10799451b44SJordan Rupprecht    debugger.CreateCategory("JASSynth").AddTypeSynthetic(
10899451b44SJordan Rupprecht        lldb.SBTypeNameSpecifier("JustAStruct"),
1092238dcc3SJonas Devlieghere        lldb.SBTypeSynthetic.CreateWithClassName("synth.jasSynthProvider"),
1102238dcc3SJonas Devlieghere    )
11199451b44SJordan Rupprecht    cat = debugger.CreateCategory("CCCSynth")
11299451b44SJordan Rupprecht    cat.AddTypeSynthetic(
11399451b44SJordan Rupprecht        lldb.SBTypeNameSpecifier("CCC"),
1142238dcc3SJonas Devlieghere        lldb.SBTypeSynthetic.CreateWithClassName(
1152238dcc3SJonas Devlieghere            "synth.CCCSynthProvider", lldb.eTypeOptionCascade
1162238dcc3SJonas Devlieghere        ),
1172238dcc3SJonas Devlieghere    )
11899451b44SJordan Rupprecht    cat.AddTypeSummary(
11999451b44SJordan Rupprecht        lldb.SBTypeNameSpecifier("CCC"),
1202238dcc3SJonas Devlieghere        lldb.SBTypeSummary.CreateWithFunctionName(
1212238dcc3SJonas Devlieghere            "synth.ccc_summary", lldb.eTypeOptionCascade
1222238dcc3SJonas Devlieghere        ),
1232238dcc3SJonas Devlieghere    )
12499451b44SJordan Rupprecht    cat.AddTypeSynthetic(
12599451b44SJordan Rupprecht        lldb.SBTypeNameSpecifier("Empty1"),
1262238dcc3SJonas Devlieghere        lldb.SBTypeSynthetic.CreateWithClassName("synth.Empty1SynthProvider"),
1272238dcc3SJonas Devlieghere    )
12899451b44SJordan Rupprecht    cat.AddTypeSummary(
12999451b44SJordan Rupprecht        lldb.SBTypeNameSpecifier("Empty1"),
1302238dcc3SJonas Devlieghere        lldb.SBTypeSummary.CreateWithFunctionName("synth.empty1_summary"),
1312238dcc3SJonas Devlieghere    )
13299451b44SJordan Rupprecht    cat.AddTypeSynthetic(
13399451b44SJordan Rupprecht        lldb.SBTypeNameSpecifier("Empty2"),
1342238dcc3SJonas Devlieghere        lldb.SBTypeSynthetic.CreateWithClassName("synth.Empty2SynthProvider"),
1352238dcc3SJonas Devlieghere    )
13699451b44SJordan Rupprecht    cat.AddTypeSummary(
13799451b44SJordan Rupprecht        lldb.SBTypeNameSpecifier("Empty2"),
13899451b44SJordan Rupprecht        lldb.SBTypeSummary.CreateWithFunctionName(
1392238dcc3SJonas Devlieghere            "synth.empty2_summary", lldb.eTypeOptionHideEmptyAggregates
1402238dcc3SJonas Devlieghere        ),
1412238dcc3SJonas Devlieghere    )
142*82af5598SVincent Belliard    cat2 = debugger.CreateCategory("CCCSynth2")
143*82af5598SVincent Belliard    cat2.AddTypeSynthetic(
144*82af5598SVincent Belliard        lldb.SBTypeNameSpecifier("CCC"),
145*82af5598SVincent Belliard        lldb.SBTypeSynthetic.CreateWithClassName(
146*82af5598SVincent Belliard            "synth.CCCSynthProvider", lldb.eTypeOptionCascade
147*82af5598SVincent Belliard        ),
148*82af5598SVincent Belliard    )
149*82af5598SVincent Belliard    cat2.AddTypeSummary(
150*82af5598SVincent Belliard        lldb.SBTypeNameSpecifier("CCC"),
151*82af5598SVincent Belliard        lldb.SBTypeSummary.CreateWithFunctionName(
152*82af5598SVincent Belliard            "synth.ccc_synthetic", lldb.eTypeOptionCascade
153*82af5598SVincent Belliard        ),
154*82af5598SVincent Belliard    )
155*82af5598SVincent Belliard    cat3 = debugger.CreateCategory("BarIntSynth")
156*82af5598SVincent Belliard    cat3.AddTypeSummary(
157*82af5598SVincent Belliard        lldb.SBTypeNameSpecifier("int"),
158*82af5598SVincent Belliard        lldb.SBTypeSummary.CreateWithFunctionName(
159*82af5598SVincent Belliard            "synth.bar_int_synthetic", lldb.eTypeOptionCascade
160*82af5598SVincent Belliard        ),
161*82af5598SVincent Belliard    )
162