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