This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Committed: gdbhooks.py: reorganization to support regex-matching and typedefs


I've committed the following to trunk as r205085, as a mixture of
enabling work towards gdb being able to print vec<>, and to fix issues
where the string-matching logic got confused by typedefs (done in a
rather crude way, by supporting multiple strings in the match, but it
works).

commit 0ac97497793e6cb742fd553744ff214379501ff8
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Tue Nov 19 22:10:27 2013 -0500

    gdbhooks.py: Support aliases of the types being printed
    
    gcc/
    	* gdbhooks.py (GdbSubprinter.__init__): Drop str_type_ field.
    	(GdbSubprinter.handles_type): New.
    	(GdbSubprinterTypeList): New subclass of GdbSubprinter.
    	(GdbSubprinterRegex): New subclass of GdbSubprinter.
    	(GdbPrettyPrinters.add_printer): Remove in favor of...
    	(GdbPrettyPrinters.add_printer_for_types): ...this new method
    	and...
    	(GdbPrettyPrinters.add_printer_for_regex): ...this other new method.
    	(GdbPrettyPrinters.__call__): Update search for subprinter
    	to use handles_type method.
    	(build_pretty_printer): Update registration of subprinters to
    	use the new API above, supporting multiple spelling of each type,
    	and allowing for future regex-based subprinters.

diff --git a/gcc/gdbhooks.py b/gcc/gdbhooks.py
index 8ddaf3d..3afa796 100644
--- a/gcc/gdbhooks.py
+++ b/gcc/gdbhooks.py
@@ -355,23 +355,50 @@ class PassPrinter:
 #   * location_t
 
 class GdbSubprinter(gdb.printing.SubPrettyPrinter):
-    def __init__(self, name, str_type_, class_):
+    def __init__(self, name, class_):
         super(GdbSubprinter, self).__init__(name)
-        self.str_type_ = str_type_
         self.class_ = class_
 
+    def handles_type(self, str_type):
+        raise NotImplementedError
+
+class GdbSubprinterTypeList(GdbSubprinter):
+    """
+    A GdbSubprinter that handles a specific set of types
+    """
+    def __init__(self, str_types, name, class_):
+        super(GdbSubprinterTypeList, self).__init__(name, class_)
+        self.str_types = frozenset(str_types)
+
+    def handles_type(self, str_type):
+        return str_type in self.str_types
+
+class GdbSubprinterRegex(GdbSubprinter):
+    """
+    A GdbSubprinter that handles types that match a regex
+    """
+    def __init__(self, regex, name, class_):
+        super(GdbSubprinterRegex, self).__init__(name, class_)
+        self.regex = re.compile(regex)
+
+    def handles_type(self, str_type):
+        return self.regex.match(str_type)
+
 class GdbPrettyPrinters(gdb.printing.PrettyPrinter):
     def __init__(self, name):
         super(GdbPrettyPrinters, self).__init__(name, [])
 
-    def add_printer(self, name, exp, class_):
-        self.subprinters.append(GdbSubprinter(name, exp, class_))
+    def add_printer_for_types(self, name, class_, types):
+        self.subprinters.append(GdbSubprinterTypeList(name, class_, types))
+
+    def add_printer_for_regex(self, name, class_, regex):
+        self.subprinters.append(GdbSubprinterRegex(name, class_, regex))
 
     def __call__(self, gdbval):
         type_ = gdbval.type.unqualified()
-        str_type_ = str(type_)
+        str_type = str(type_)
         for printer in self.subprinters:
-            if printer.enabled and str_type_ == printer.str_type_:
+            if printer.enabled and printer.handles_type(str_type):
                 return printer.class_(gdbval)
 
         # Couldn't find a pretty printer (or it was disabled):
@@ -380,13 +407,22 @@ class GdbPrettyPrinters(gdb.printing.PrettyPrinter):
 
 def build_pretty_printer():
     pp = GdbPrettyPrinters('gcc')
-    pp.add_printer('tree', 'tree', TreePrinter)
-    pp.add_printer('cgraph_node', 'cgraph_node *', CGraphNodePrinter)
-    pp.add_printer('gimple', 'gimple', GimplePrinter)
-    pp.add_printer('basic_block', 'basic_block', BasicBlockPrinter)
-    pp.add_printer('edge', 'edge', CfgEdgePrinter)
-    pp.add_printer('rtx_def', 'rtx_def *', RtxPrinter)
-    pp.add_printer('opt_pass', 'opt_pass *', PassPrinter)
+    pp.add_printer_for_types(['tree'],
+                             'tree', TreePrinter)
+    pp.add_printer_for_types(['cgraph_node *'],
+                             'cgraph_node', CGraphNodePrinter)
+    pp.add_printer_for_types(['gimple', 'gimple_statement_base *'],
+                             'gimple',
+                             GimplePrinter)
+    pp.add_printer_for_types(['basic_block', 'basic_block_def *'],
+                             'basic_block',
+                             BasicBlockPrinter)
+    pp.add_printer_for_types(['edge', 'edge_def *'],
+                             'edge',
+                             CfgEdgePrinter)
+    pp.add_printer_for_types(['rtx_def *'], 'rtx_def', RtxPrinter)
+    pp.add_printer_for_types(['opt_pass *'], 'opt_pass', PassPrinter)
+
     return pp
 
 gdb.printing.register_pretty_printer(

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]