CheckComments.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import sys
  2. import re
  3. import os
  4. ok = 100.0
  5. if sys.argv[1] == '-l':
  6. ok = float(sys.argv[2])
  7. sys.argv = [sys.argv[0]] + sys.argv[3:]
  8. debug = False
  9. if sys.argv[1] == '-v':
  10. debug = True
  11. sys.argv = [sys.argv[0]] + sys.argv[2:]
  12. errors = 0
  13. for path in sys.argv[1:]:
  14. other_depth = 0
  15. brace_depth = 0
  16. code_count = 0
  17. comment_count = 0
  18. long_comment = None
  19. long_comments = {}
  20. kind = "function"
  21. function_name = "<unknown>"
  22. ctor_dtor = False
  23. linenum = 0
  24. fd = open(path, 'r')
  25. for line in fd.readlines():
  26. linenum += 1
  27. match = re.search('/\*\*(.*)', line)
  28. if match:
  29. long_comment = re.sub('\s+', '', match.group(1))
  30. continue
  31. elif long_comment:
  32. match = re.search('(.*)\*/', line)
  33. if match:
  34. long_comment += re.sub('\s+', '', match.group(1))
  35. comment_count = len(long_comment)
  36. long_comment = None
  37. else:
  38. long_comment += re.sub('\s+', '', line[:-1])
  39. continue
  40. if brace_depth == 0:
  41. match = re.search('(namespace|enum|class|struct|union)', line)
  42. if match:
  43. kind = match.group(1)
  44. if debug: print "kind =", kind
  45. elif kind == "function":
  46. match = re.search('(\S+)\(', line)
  47. if match:
  48. function_name = match.group(1)
  49. long_comments[function_name] = comment_count
  50. comment_count = 0
  51. if debug: print "name found %s" % function_name
  52. if re.search('{', line) and not re.search('@{', line):
  53. if kind == "function":
  54. brace_depth += 1
  55. if debug: print "brace_depth =", brace_depth
  56. else:
  57. other_depth += 1
  58. kind = "function"
  59. if debug: print "other_depth =", other_depth
  60. if re.search('}', line) and not re.search('@}', line):
  61. if brace_depth > 0:
  62. brace_depth -= 1
  63. if debug: print "brace_depth =", brace_depth
  64. if brace_depth == 0:
  65. if debug: print "function done"
  66. if function_name in long_comments:
  67. comment_count += long_comments[function_name]
  68. if code_count == 0:
  69. percent = ok
  70. print "%7s %4d/%4d %s:%d: %s" % \
  71. ("empty", comment_count, code_count,
  72. os.path.basename(path), linenum,
  73. function_name)
  74. errors += 1
  75. else:
  76. percent = 100.0 * (float(comment_count) /
  77. float(code_count))
  78. if percent < ok and not ctor_dtor:
  79. print "%6.0f%% %4d/%4d %s:%d: %s" % \
  80. (percent, comment_count, code_count,
  81. os.path.basename(path), linenum,
  82. function_name)
  83. errors += 1
  84. code_count = 0
  85. comment_count = 0
  86. kind = "function"
  87. function_name = "<unknown>"
  88. ctor_dtor = False
  89. else:
  90. other_depth -= 1
  91. if debug: print "other_depth =", other_depth
  92. if brace_depth > 0:
  93. if re.search("TRACE_[CD]TOR", line):
  94. ctor_dtor = True
  95. line = re.sub('\s+', '', line[:-1])
  96. match = re.search('//(.*)', line)
  97. if match:
  98. comment = match.group(1)
  99. line = re.sub('//.*', '', line)
  100. else:
  101. comment = None
  102. if line:
  103. code_count += len(line)
  104. if comment:
  105. comment_count += len(comment)
  106. sys.exit(errors)