Why do the two ways of registering pretty printers differ?

43 Views Asked by At

I have this code:

struct number
{
    int n;
};

int main()
{
    number n{5};
    const number & ref = n;
}

I want to pretty print the number struct. I will show two pretty printers I have made:

printer1:

class Printer:
    def __init__(self, val):
        self.val = val

    def to_string(self):
        return str(self.val["n"])

def matcher(val):
    lookup_tag = val.type.tag
    if lookup_tag is None:
        return None

    if lookup_tag == "number":
        return Printer(val)

gdb.pretty_printers.append(matcher)

printer2:

class Printer:
    def __init__(self, val):
        self.val = val

    def to_string(self):
        return str(self.val["n"])

def build_pretty_printer():
    pp = gdb.printing.RegexpCollectionPrettyPrinter("num")
    pp.add_printer("num", "^number$", Printer)
    return pp

gdb.printing.register_pretty_printer(None, build_pretty_printer())

The first printer uses the "old" way of registering pretty printers, the second uses the "new" way with RegexpCollectionPrettyPrinter.

When I print ref using the first pretty printer, GDB prints this:

$1 = (const number &) @0x7fffffffe0b8: 5

The second pretty printer prints this:

$1 = 5

The pretty printer itself (Printer) is identical in the two pretty printers. Only the way of registering it has changed. Why is the result different then? Why does the first pretty printer print (const number &) @0x7fffffffe0b8? How can I get rid of it?

0

There are 0 best solutions below