find_typedefs.py 3.2 KB

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