1# Copyright (C) 2010-2023 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16# This file is part of the GDB testsuite.  It tests python pretty
17# printer registration.
18
19import re
20import gdb.types
21import gdb.printing
22
23
24def lookup_function_lookup_test(val):
25    class PrintFunctionLookup(object):
26        def __init__(self, val):
27            self.val = val
28
29        def to_string(self):
30            return "x=<" + str(self.val["x"]) + "> y=<" + str(self.val["y"]) + ">"
31
32    typename = gdb.types.get_basic_type(val.type).tag
33    # Note: typename could be None.
34    if typename == "function_lookup_test":
35        return PrintFunctionLookup(val)
36    return None
37
38
39class pp_s1(object):
40    def __init__(self, val):
41        self.val = val
42
43    def to_string(self):
44        a = self.val["a"]
45        b = self.val["b"]
46        return "s1 a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">"
47
48
49class pp_s2(object):
50    def __init__(self, val):
51        self.val = val
52
53    def to_string(self):
54        a = self.val["a"]
55        b = self.val["b"]
56        return "s2 a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">"
57
58
59def build_pretty_printer1():
60    pp = gdb.printing.RegexpCollectionPrettyPrinter("pp-test")
61
62    pp.add_printer("struct s", "^struct s$", pp_s1)
63    pp.add_printer("s", "^s$", pp_s1)
64
65    return pp
66
67
68def build_pretty_printer2():
69    # This intentionally has the same name as build_pretty_printer1.
70    # It is used to test the "replace" functionality of
71    # register_pretty_printer.
72    pp = gdb.printing.RegexpCollectionPrettyPrinter("pp-test")
73
74    pp.add_printer("struct s", "^struct s$", pp_s2)
75    pp.add_printer("s", "^s$", pp_s2)
76
77    return pp
78
79
80# Note: Registering the printers is done in the .exp file.
81