File: autodebugger_c.py

package info (click to toggle)
emscripten 2.0.12~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,440 kB
  • sloc: ansic: 510,324; cpp: 384,763; javascript: 84,341; python: 51,362; sh: 50,019; pascal: 4,159; makefile: 3,409; asm: 2,150; lisp: 1,869; ruby: 488; cs: 142
file content (43 lines) | stat: -rw-r--r-- 1,371 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Copyright 2017 The Emscripten Authors.  All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License.  Both these licenses can be
# found in the LICENSE file.

'''
Processes a C source file, adding debugging information.

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

from __future__ import print_function
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(r'^  [ ]*([\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.')