gdb/testsuite: add regression test for per-objfile typeprinters
authorBruno Larsen <blarsen@redhat.com>
Thu, 23 Feb 2023 12:56:32 +0000 (13:56 +0100)
committerBruno Larsen <blarsen@redhat.com>
Mon, 13 Mar 2023 13:40:33 +0000 (14:40 +0100)
PR python/17136 reported an unhandled exception when using typeprinters
only valid on some objfiles, rather than being a global typeprinter. The
fix was accepted without a regression test, and we've been carrying one
out-of-tree for a while but I think it's worth upstreaming. The code
itself was developed by Jan Kratochvil.

Co-Authored-By: Jan Kratochvil <jkratochvil@azul.com>
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=17136
Reviewed-By: Andrew Burgess <aburgess@redhat.com>
Approved-By: Tom Tromey <tom@tromey.com>
gdb/testsuite/gdb.python/py-typeprint.cc
gdb/testsuite/gdb.python/py-typeprint.exp
gdb/testsuite/gdb.python/py-typeprint.py

index ccd2ed02db15bcd43bc47ceb17bba05135474423..3f274a73498e0ddfdd685f13612384a93f3e4281 100644 (file)
@@ -31,6 +31,12 @@ templ<basic_string> s;
 
 basic_string bs;
 
+class Other
+{
+};
+
+Other ovar;
+
 int main()
 {
   return 0;
index 40f7e53847f6985fe6d2536ea8bf2284c199ff81..778583b9eff2f4ad8ccdc747c97bac2267383144 100644 (file)
@@ -48,3 +48,7 @@ gdb_test_no_output "enable type-printer string"
 gdb_test "whatis bs" "string" "whatis with enabled printer"
 
 gdb_test "whatis s" "templ<string>"
+
+gdb_test "info type-printers" "Type printers for \[^\r\n\]*/py-typeprint:\r\n *other\r\n.*" \
+        "info type-printers for other"
+gdb_test "whatis ovar" "type = Another"
index b5ba243889bb0720dbe3b4b5546058b0afd1162c..aac42a1a99a69b96144269c5eb86c973a94a73a6 100644 (file)
@@ -15,8 +15,7 @@
 
 import gdb
 
-
-class Recognizer(object):
+class StringRecognizer(object):
     def __init__(self):
         self.enabled = True
 
@@ -32,7 +31,27 @@ class StringTypePrinter(object):
         self.enabled = True
 
     def instantiate(self):
-        return Recognizer()
+        return StringRecognizer()
 
 
 gdb.type_printers.append(StringTypePrinter())
+
+class OtherRecognizer(object):
+    def __init__(self):
+        self.enabled = True
+
+    def recognize(self, type_obj):
+        if type_obj.tag == 'Other':
+            return 'Another'
+        return None
+
+class OtherTypePrinter(object):
+    def __init__(self):
+        self.name = 'other'
+        self.enabled = True
+
+    def instantiate(self):
+        return OtherRecognizer()
+
+import gdb.types
+gdb.types.register_type_printer(gdb.objfiles()[0], OtherTypePrinter())