Revision 5c03805d684cb541ba5a40138457bb598642de20 authored by Alon Zakai on 14 March 2014, 22:09:59 UTC, committed by Alon Zakai on 14 March 2014, 23:14:00 UTC
1 parent e56b193
Raw File
autodebugger_c.py
'''
Processes a C source file, adding debugging information.

Similar to autodebugger.py, but runs on .c files. Will
overwrite the files it is given!
'''

import os, sys, re

filenames = sys.argv[1:]
for filename in filenames:
  print '..%s..' % filename

  f = open(filename, 'r')
  data = f.read()
  f.close()

  lines = data.split('\n')
  for i in range(len(lines)):
    m = re.match('^  [ ]*([\w\d \[\]]+) = +([^;]+);$', lines[i])
    if m and (' if ' not in lines[i-1] or '{' in lines[i-1]) and \
             (' if ' not in lines[i+1] or '{' in lines[i+1]) and \
             (' else' not in lines[i-1] or '{' in lines[i-1]) and \
             (' else' not in lines[i+1] or '{' in lines[i+1]) and \
             (' for' not in lines[i-1]) and \
             ('struct' not in lines[i]):
      raw = m.groups(1)[0].rstrip()
      var = raw.split(' ')[-1]
      if ' ' in raw and '[' in var: continue
      lines[i] += ''' printf("%s:%d:%s=%%d\\n", (int)%s);''' % (filename, i+1, var, var)

  f = open(filename, 'w')
  f.write('\n'.join(lines))
  f.close()

print 'Success.'

back to top