redox.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/python
  2. #
  3. # Copyright (c) 2008 The Tor Project, Inc.
  4. # See LICENSE for licensing information.
  5. #
  6. # Hi!
  7. # I'm redox.py, the Tor redocumentation tool!
  8. # I am a horrible hack!
  9. # I read the output of doxygen from stderr, and add missing DOCDOC comments
  10. # to tell you where documentation should go!
  11. # To use me, edit the stuff below...
  12. # ...and run 'make doxygen 2>doxygen.stderr' ...
  13. # ...and run ./contrib/redox.py < doxygen.stderr !
  14. # I'll make a bunch of new files by adding missing DOCDOC comments to your
  15. # source. Those files will have names like ./src/common/util.c.newdoc.
  16. # You will want to look over the changes by hand before checking them in.
  17. #
  18. # So, here's your workflow:
  19. #
  20. # 0. Make sure you're running a bourne shell for the redirects below.
  21. # 1. make doxygen 1>doxygen.stdout 2>doxygen.stderr.
  22. # 2. grep Warning doxygen.stderr | grep -v 'is not documented' | less
  23. # [This will tell you about all the bogus doxygen output you have]
  24. # 3. python ./contrib/redox.py <doxygen.stderr
  25. # [This will make lots of .newdoc files with DOCDOC comments for
  26. # whatever was missing documentation.]
  27. # 4. Look over those .newdoc files, and see which docdoc comments you
  28. # want to merge into the main file. If it's all good, just run
  29. # "mv fname.c.newdoc fname.c". Otherwise, you'll need to merge
  30. # the parts you like by hand.
  31. # Which files should we ignore warning from? Mostly, these are external
  32. # files that we've snarfed in from somebody else, whose C we do no intend
  33. # to document for them.
  34. SKIP_FILES = [ "OpenBSD_malloc_Linux.c",
  35. "eventdns.c",
  36. "eventdns.h",
  37. "strlcat.c",
  38. "strlcpy.c",
  39. "aes.c",
  40. "aes.h" ]
  41. # What names of things never need javadoc
  42. SKIP_NAME_PATTERNS = [ r'^.*_c_id$' ]
  43. # Which types of things should get DOCDOC comments added if they are
  44. # missing documentation? Recognized types are in KINDS below.
  45. ADD_DOCDOCS_TO_TYPES = [ 'function', 'type', 'typedef' ]
  46. # ADD_DOCDOCS_TO_TYPES += [ 'variable', 'define' ]
  47. # ====================
  48. # The rest of this should not need hacking.
  49. import re
  50. import sys
  51. KINDS = [ "type", "field", "typedef", "define", "function", "variable" ]
  52. NODOC_LINE_RE = re.compile(r'^([^:]+):(\d+): (\w+): (.*) is not documented\.$')
  53. THING_RE = re.compile(r'^Member ([a-zA-Z0-9_]+).*\((typedef|define|function|variable)\) of (file|class) ')
  54. SKIP_NAMES = [re.compile(s) for s in SKIP_NAME_PATTERNS]
  55. def parsething(thing):
  56. """I figure out what 'foobar baz in quux quum is not documented' means,
  57. and return: the name of the foobar, and the kind of the foobar.
  58. """
  59. if thing.startswith("Compound "):
  60. tp, name = "type", thing.split()[1]
  61. else:
  62. m = THING_RE.match(thing)
  63. if not m:
  64. print thing
  65. return None, None
  66. else:
  67. name, tp, parent = m.groups()
  68. if parent == 'class':
  69. if tp == 'variable' or tp == 'function':
  70. tp = 'field'
  71. return name, tp
  72. def read():
  73. """I snarf doxygen stderr from stdin, and parse all the "foo has no
  74. documentation messages. I return a map from filename to lists
  75. of tuples of (alleged line number, name of thing, kind of thing)
  76. """
  77. errs = {}
  78. for line in sys.stdin:
  79. m = NODOC_LINE_RE.match(line)
  80. if m:
  81. file, line, tp, thing = m.groups()
  82. assert tp == 'Warning'
  83. name, kind = parsething(thing)
  84. errs.setdefault(file, []).append((int(line), name, kind))
  85. return errs
  86. def findline(lines, lineno, ident):
  87. """Given a list of all the lines in the file (adjusted so 1-indexing works),
  88. a line number that ident is alledgedly on, and ident, I figure out
  89. the line where ident was really declared."""
  90. for lineno in xrange(lineno, 0, -1):
  91. if ident in lines[lineno]:
  92. return lineno
  93. return None
  94. FUNC_PAT = re.compile(r"^[A-Za-z0-9_]+\(")
  95. def hascomment(lines, lineno, kind):
  96. """I return true if it looks like there's already a good comment about
  97. the thing on lineno of lines of type kind. """
  98. if "*/" in lines[lineno-1]:
  99. return True
  100. if kind == 'function' and FUNC_PAT.match(lines[lineno]):
  101. if "*/" in lines[lineno-2]:
  102. return True
  103. return False
  104. def hasdocdoc(lines, lineno, kind):
  105. """I return true if it looks like there's already a docdoc comment about
  106. the thing on lineno of lines of type kind."""
  107. if "DOCDOC" in lines[lineno] or "DOCDOC" in lines[lineno-1]:
  108. return True
  109. if kind == 'function' and FUNC_PAT.match(lines[lineno]):
  110. if "DOCDOC" in lines[lineno-2]:
  111. return True
  112. return False
  113. def checkf(fn, errs):
  114. """I go through the output of read() for a single file, and build a list
  115. of tuples of things that want DOCDOC comments. Each tuple has:
  116. the line number where the comment goes; the kind of thing; its name.
  117. """
  118. for skip in SKIP_FILES:
  119. if fn.endswith(skip):
  120. print "Skipping",fn
  121. return
  122. comments = []
  123. lines = [ None ]
  124. try:
  125. lines.extend( open(fn, 'r').readlines() )
  126. except IOError:
  127. return
  128. for line, name, kind in errs:
  129. if any(pat.match(name) for pat in SKIP_NAMES):
  130. continue
  131. if kind not in ADD_DOCDOCS_TO_TYPES:
  132. continue
  133. ln = findline(lines, line, name)
  134. if ln == None:
  135. print "Couldn't find the definition of %s allegedly on %s of %s"%(
  136. name, line, fn)
  137. else:
  138. if hasdocdoc(lines, line, kind):
  139. # print "Has a DOCDOC"
  140. # print fn, line, name, kind
  141. # print "\t",lines[line-2],
  142. # print "\t",lines[line-1],
  143. # print "\t",lines[line],
  144. # print "-------"
  145. pass
  146. else:
  147. if kind == 'function' and FUNC_PAT.match(lines[ln]):
  148. ln = ln - 1
  149. comments.append((ln, kind, name))
  150. return comments
  151. def applyComments(fn, entries):
  152. """I apply lots of comments to the file in fn, making a new .newdoc file.
  153. """
  154. N = 0
  155. lines = [ None ]
  156. try:
  157. lines.extend( open(fn, 'r').readlines() )
  158. except IOError:
  159. return
  160. # Process the comments in reverse order by line number, so that
  161. # the line numbers for the ones we haven't added yet remain valid
  162. # until we add them. Standard trick.
  163. entries.sort()
  164. entries.reverse()
  165. for ln, kind, name in entries:
  166. lines.insert(ln, "/* DOCDOC %s */\n"%name)
  167. N += 1
  168. outf = open(fn+".newdoc", 'w')
  169. for line in lines[1:]:
  170. outf.write(line)
  171. outf.close()
  172. print "Added %s DOCDOCs to %s" %(N, fn)
  173. e = read()
  174. for fn, errs in e.iteritems():
  175. comments = checkf(fn, errs)
  176. if comments:
  177. applyComments(fn, comments)