This is the mail archive of the archer@sourceware.org mailing list for the Archer 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]

[PATCH 3/3] [python] Create pretty-printer lookup_function classes.


These prevent repeated code and are extensible.

gdb/ChangeLog

2009-31-12  Matt McCormick  <matt@mmmccormick.com>

        * Makefile.in: Install gdb/pretty/lookup_function.py.
        * python/lib/gdb/pretty/lookup_function.py)
        (RELookupFunction, RELookupFunctionTag): New lookup_function classes.

gdb/testsuite/ChangeLog

2009-31-12  Matt McCormick  <matt@mmmccormick.com>

        * gdb.python/py-prettyprint.py (lookup_function):
        Use RELookupFunctionTag to create the lookup_function.
---

 gdb/Makefile.in                              |    2 -
 gdb/python/lib/gdb/pretty/lookup_function.py |   75 ++++++++++++++++++++++++++
 gdb/testsuite/gdb.python/py-prettyprint.py   |   35 +-----------
 3 files changed, 79 insertions(+), 33 deletions(-)
 create mode 100644 gdb/python/lib/gdb/pretty/lookup_function.py

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index e9055b1..d1510c2 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -2075,7 +2075,7 @@ PY_FILES = gdb/FrameIterator.py gdb/FrameWrapper.py gdb/command/alias.py \
     gdb/command/ignore_errors.py gdb/command/save_breakpoints.py \
     gdb/function/caller_is.py gdb/function/in_scope.py \
     gdb/function/__init__.py gdb/backtrace.py gdb/__init__.py \
-    gdb/pretty/__init__.py
+    gdb/pretty/__init__.py gdb/pretty/lookup_function.py
 
 # Install the Python library.  Python library files go under
 # $(pythondir).
diff --git a/gdb/python/lib/gdb/pretty/lookup_function.py b/gdb/python/lib/gdb/pretty/lookup_function.py
new file mode 100644
index 0000000..f6699bd
--- /dev/null
+++ b/gdb/python/lib/gdb/pretty/lookup_function.py
@@ -0,0 +1,75 @@
+# Pretty-printer lookup_function classes.
+
+# Copyright (C) 2009 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import gdb
+
+class RELookupFunction (object):
+    """Returns a pretty-printer based on some type filtering followed by a search
+    in a dictionary of key:value of typename_regex:pretty-printer.  If it cannot
+    find a pretty-printer to print the given type, returns None.
+    
+    Arguments
+    ---------
+    pretty_printers_dict: dict
+        typename_regex : pretty-pretter-function
+    """
+
+    def __init__ (self, pretty_printers_dict, type_attribute=''):
+        self.pretty_printers_dict = pretty_printers_dict
+
+    def _canonical_type (self, gdb_type):
+# If it points to a reference, get the reference.
+        if gdb_type.code == gdb.TYPE_CODE_REF:
+            gdb_type = gdb_type.target ()
+
+# Get the unqualified type, stripped of typedefs.
+        gdb_type = gdb_type.unqualified ().strip_typedefs ()
+
+        return gdb_type
+
+    def _filter_types (self, gdb_type):
+        """Filters the given type and returns a name to pass to the
+        pretty_printers_dict or None."""
+        gdb_type = self._canonical_type (gdb_type)
+
+        return gdb_type
+
+    def __call__ (self, val):
+        "Returns a pretty-printer that can print val or None."
+        typename = self._filter_types (val.type)
+        if typename == None:
+            return None
+
+        for regex in self.pretty_printers_dict:
+            if regex.match (str (typename)):
+                return self.pretty_printers_dict[regex] (val)
+
+        return None
+        
+class RELookupFunctionTag (RELookupFunction):
+    """Similar to RELookupFunction, but when trying to match to type.tag.
+    Useful for ``struct``, ``union``, or ``enum`` in C or C++."""
+
+    def __init__ (self, pretty_printers_dict):
+        super (RELookupFunctionTag, self).__init__ (pretty_printers_dict)
+
+    def _filter_types (self, gdb_type):
+        """Filters the given type and returns a name to pass to the
+        pretty_printers_dict or None."""
+        gdb_type = self._canonical_type (gdb_type)
+
+        return gdb_type.tag
diff --git a/gdb/testsuite/gdb.python/py-prettyprint.py b/gdb/testsuite/gdb.python/py-prettyprint.py
index 9a0d107..84682a8 100644
--- a/gdb/testsuite/gdb.python/py-prettyprint.py
+++ b/gdb/testsuite/gdb.python/py-prettyprint.py
@@ -18,6 +18,8 @@
 
 import re
 
+from gdb.pretty.lookup_function import RELookupFunctionTag
+
 # Test returning a Value from a printer.
 class string_print:
     def __init__(self, val):
@@ -137,37 +139,6 @@ class pp_outer:
         yield 's', self.val['s']
         yield 'x', self.val['x']
 
-def lookup_function (val):
-    "Look-up and return a pretty-printer that can print val."
-
-    # Get the type.
-    type = val.type
-
-    # If it points to a reference, get the reference.
-    if type.code == gdb.TYPE_CODE_REF:
-        type = type.target ()
-
-    # Get the unqualified type, stripped of typedefs.
-    type = type.unqualified ().strip_typedefs ()
-
-    # Get the type name.    
-    typename = type.tag
-
-    if typename == None:
-        return None
-
-    # Iterate over local dictionary of types to determine
-    # if a printer is registered for that type.  Return an
-    # instantiation of the printer if found.
-    for function in pretty_printers_dict:
-        if function.match (typename):
-            return pretty_printers_dict[function] (val)
-        
-    # Cannot find a pretty printer.  Return None.
-
-    return None
-
-
 def register_pretty_printers ():
     pretty_printers_dict[re.compile ('^struct s$')]   = pp_s
     pretty_printers_dict[re.compile ('^s$')]   = pp_s
@@ -203,6 +174,6 @@ def register_pretty_printers ():
     pretty_printers_dict[re.compile ('^outerstruct$')]  = pp_outer
 
 pretty_printers_dict = {}
-
 register_pretty_printers ()
+lookup_function = RELookupFunctionTag (pretty_printers_dict)
 gdb.pretty_printers.append (lookup_function)


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