GNUnet  0.20.0
find_typedefs.py
Go to the documentation of this file.
1 # XXX (F841): local variable 'li' is assigned to but never used
2 
3 import os
4 import re
5 import sys
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[
17  right_paren:].find('(') >= 0:
18  fname = line[:right_paren]
19  fname = fname.lstrip(' ').lstrip('*').lstrip(' ').rstrip(' ')
20  if len(fname) > 0:
21  if debug:
22  print("from {0}:{1}".format(file, num))
23  print("-T {0}".format(fname))
24 
25 
26 def get_td_from_simple_type(line, file, num):
27  line = line.rstrip(' ').rstrip('\t').rstrip(' ').rstrip('\t')
28  right_space = line.rfind(' ')
29  right_tab = line.rfind('\t')
30  sep = right_tab if right_tab > right_space else right_space
31  sep += 1
32  tname = line[sep:]
33  tname = tname.lstrip('*')
34  if len(tname) > 0:
35  if debug:
36  print("from {0}:{1}".format(file, num))
37  print("-T {0}".format(tname))
38 
39 
40 def find_typedefs(file):
41  with open(file, 'rb') as f:
42  td = False
43  td_struct = False
44  td_level = 0
45  td_line = []
46  data = f.read()
47  for i, l in enumerate(data.splitlines(False)):
48  # Don't try to be too smart: only count lines that begin with 'typedef '
49  l = l.rstrip(' ').rstrip('\t')
50  if len(l) == 0:
51  continue
52  if not td:
53  if l[:8] != 'typedef ':
54  continue
55  else:
56  td = True
57  if l[8:].lstrip(' ').lstrip('\t')[:6] == 'struct':
58  td_struct = True
59  if td_struct:
60  leftcbrace = l.find('{')
61  if leftcbrace >= 0:
62  if td_level == 0:
63  td_line.append(l[:leftcbrace])
64  l = l[leftcbrace + 1:]
65  td_level += 1
66  rightcbrace = l.rfind('}')
67  if rightcbrace >= 0:
68  td_level -= 1
69  if td_level == 0:
70  td_line.append(l[rightcbrace + 1:])
71  else:
72  td_line.append(l)
73  if len(l) > 0 and l[-1] == ';' and (not td_struct or td_level == 0):
74  td_line = ' '.join(td_line)
75  td_line = td_line[:-1]
76  if len(td_line) > 0:
77  if td_line[-1] == ')':
78  get_td_from_function_signature(td_line, file, i)
79  else:
80  get_td_from_simple_type(td_line, file, i)
81  td_line = []
82  td = False
83  td_struct = False
84  td_level = 0
85 
86 
87 def scan_dir(d):
88  for dirpath, dirs, files in os.walk(d):
89  for f in files:
90  if re.match(r'(?!lt_).+\.(c|cc|h)$', f):
91  file = os.path.join(dirpath, f)
92  find_typedefs(file)
93 
94 
95 if __name__ == '__main__':
96  if len(sys.argv[1:]) == 0:
97  arg = os.getcwd()
98  else:
99  arg = sys.argv[1]
100  scan_dir(arg)
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
def get_td_from_simple_type(line, file, num)
def get_td_from_function_signature(line, file, num)
def find_typedefs(file)