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