aboutsummaryrefslogtreecommitdiff
path: root/contrib/scripts/find_typedefs.py
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/scripts/find_typedefs.py')
-rw-r--r--contrib/scripts/find_typedefs.py102
1 files changed, 102 insertions, 0 deletions
diff --git a/contrib/scripts/find_typedefs.py b/contrib/scripts/find_typedefs.py
new file mode 100644
index 000000000..b344cee2b
--- /dev/null
+++ b/contrib/scripts/find_typedefs.py
@@ -0,0 +1,102 @@
1# XXX (F841): local variable 'li' is assigned to but never used
2
3from __future__ import print_function
4from __future__ import unicode_literals
5import os
6import re
7import sys
8
9
10debug = False
11
12
13def get_td_from_function_signature(line, file, num):
14 left_paren = line.find('(')
15 if left_paren > 0:
16 left_paren += 1
17 li = line[left_paren:]
18 right_paren = line.find(')')
19 if right_paren > 0 and right_paren > left_paren and line[right_paren:].find('(') >= 0:
20 fname = line[:right_paren]
21 fname = fname.lstrip(' ').lstrip('*').lstrip(' ').rstrip(' ')
22 if len(fname) > 0:
23 if debug:
24 print("from {0}:{1}".format(file, num))
25 print("-T {0}".format(fname))
26
27
28def get_td_from_simple_type(line, file, num):
29 line = line.rstrip(' ').rstrip('\t').rstrip(' ').rstrip('\t')
30 right_space = line.rfind(' ')
31 right_tab = line.rfind('\t')
32 sep = right_tab if right_tab > right_space else right_space
33 sep += 1
34 tname = line[sep:]
35 tname = tname.lstrip('*')
36 if len(tname) > 0:
37 if debug:
38 print("from {0}:{1}".format(file, num))
39 print("-T {0}".format(tname))
40
41
42def find_typedefs(file):
43 with open(file, 'rb') as f:
44 td = False
45 td_struct = False
46 td_level = 0
47 td_line = []
48 data = f.read()
49 for i, l in enumerate(data.splitlines(False)):
50 # Don't try to be too smart: only count lines that begin with 'typedef '
51 l = l.rstrip(' ').rstrip('\t')
52 if len(l) == 0:
53 continue
54 if not td:
55 if l[:8] != 'typedef ':
56 continue
57 else:
58 td = True
59 if l[8:].lstrip(' ').lstrip('\t')[:6] == 'struct':
60 td_struct = True
61 if td_struct:
62 leftcbrace = l.find('{')
63 if leftcbrace >= 0:
64 if td_level == 0:
65 td_line.append(l[:leftcbrace])
66 l = l[leftcbrace + 1:]
67 td_level += 1
68 rightcbrace = l.rfind('}')
69 if rightcbrace >= 0:
70 td_level -= 1
71 if td_level == 0:
72 td_line.append(l[rightcbrace + 1:])
73 else:
74 td_line.append(l)
75 if len(l) > 0 and l[-1] == ';' and(not td_struct or td_level == 0):
76 td_line = ' '.join(td_line)
77 td_line = td_line[:-1]
78 if len(td_line) > 0:
79 if td_line[-1] == ')':
80 get_td_from_function_signature(td_line, file, i)
81 else:
82 get_td_from_simple_type(td_line, file, i)
83 td_line = []
84 td = False
85 td_struct = False
86 td_level = 0
87
88
89def scan_dir(d):
90 for dirpath, dirs, files in os.walk(d):
91 for f in files:
92 if re.match(r'(?!lt_).+\.(c|cc|h)$', f):
93 file = os.path.join(dirpath, f)
94 find_typedefs(file)
95
96
97if __name__ == '__main__':
98 if len(sys.argv[1:]) == 0:
99 arg = os.getcwd()
100 else:
101 arg = sys.argv[1]
102 scan_dir(arg)