Cleaner code annotation
authorRobert Bradshaw <robertwb@math.washington.edu>
Sat, 17 Jan 2009 09:14:40 +0000 (01:14 -0800)
committerRobert Bradshaw <robertwb@math.washington.edu>
Sat, 17 Jan 2009 09:14:40 +0000 (01:14 -0800)
Cython/Compiler/Code.py
Cython/Compiler/Nodes.py

index abb712cfb4d44e809e0ad50c125572c1f6377fa4..9ecc46450a260a47ed928b8f703079dc23b06d1c 100644 (file)
@@ -510,6 +510,7 @@ class CCodeWriter(object):
         
         self.funcstate = None
         self.level = 0
+        self.call_level = 0
         self.bol = 1
         if create_from is None:
             # Root CCodeWriter
@@ -523,6 +524,7 @@ class CCodeWriter(object):
             if copy_formatting:
                 self.level = create_from.level
                 self.bol = create_from.bol
+                self.call_level = create_from.call_level
         if emit_linenums is None:
             self.emit_linenums = self.globalstate.emit_linenums
         else:
index 957843bcd77730f381c6c889bfa7142af67bda14..081afbbe32faa88a95f2fed9ec2d23d5e3aa2cc2 100644 (file)
@@ -76,13 +76,33 @@ from types import FunctionType
 def write_func_call(func):
     def f(*args, **kwds):
         if len(args) > 1 and isinstance(args[1], CCodeWriter):
+            # here we annotate the code with this function call
+            # but only if new code is generated
             node, code = args[:2]
-            code.putln('/* %s.%s %s */' % (node.__class__.__name__, func.__name__, node.pos[1:]))
-        return func(*args, **kwds)
+            marker = '                    /* %s -> %s.%s %s */' % (
+                    ' ' * code.call_level,
+                    node.__class__.__name__, 
+                    func.__name__, 
+                    node.pos[1:])
+            pristine = code.buffer.stream.tell()
+            code.putln(marker)
+            start = code.buffer.stream.tell()
+            code.call_level += 4
+            res = func(*args, **kwds)
+            code.call_level -= 4
+            if start == code.buffer.stream.tell():
+                code.buffer.stream.seek(pristine)
+            else:
+                marker = marker.replace('->', '<-')
+                code.putln(marker)
+            return res
+        else:
+            return func(*args, **kwds)
     return f
 
 class VerboseCodeWriter(type):
     # Set this as a metaclass to trace function calls in code.
+    # This slows down code generation and makes much larger files. 
     def __new__(cls, name, bases, attrs):
         attrs = dict(attrs)
         for mname, m in attrs.items():