redox.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. r'^.*_H_ID$' ]
  44. # Which types of things should get DOCDOC comments added if they are
  45. # missing documentation? Recognized types are in KINDS below.
  46. #ADD_DOCDOCS_TO_TYPES = [ 'function', 'type', 'typedef' ]
  47. ADD_DOCDOCS_TO_TYPES = [ 'variable' ]
  48. # ====================
  49. # The rest of this should not need hacking.
  50. import re
  51. import sys
  52. KINDS = [ "type", "field", "typedef", "define", "function", "variable" ]
  53. NODOC_LINE_RE = re.compile(r'^([^:]+):(\d+): (\w+): (.*) is not documented\.$')
  54. THING_RE = re.compile(r'^Member ([a-zA-Z0-9_]+).*\((typedef|define|function|variable)\) of (file|class) ')
  55. SKIP_NAMES = [re.compile(s) for s in SKIP_NAME_PATTERNS]
  56. def parsething(thing):
  57. """I figure out what 'foobar baz in quux quum is not documented' means,
  58. and return: the name of the foobar, and the kind of the foobar.
  59. """
  60. if thing.startswith("Compound "):
  61. tp, name = "type", thing.split()[1]
  62. else:
  63. m = THING_RE.match(thing)
  64. if not m:
  65. print thing
  66. return None, None
  67. else:
  68. name, tp, parent = m.groups()
  69. if parent == 'class':
  70. if tp == 'variable' or tp == 'function':
  71. tp = 'field'
  72. return name, tp
  73. def read():
  74. """I snarf doxygen stderr from stdin, and parse all the "foo has no
  75. documentation messages. I return a map from filename to lists
  76. of tuples of (alleged line number, name of thing, kind of thing)
  77. """
  78. errs = {}
  79. for line in sys.stdin:
  80. m = NODOC_LINE_RE.match(line)
  81. if m:
  82. file, line, tp, thing = m.groups()
  83. assert tp == 'Warning'
  84. name, kind = parsething(thing)
  85. errs.setdefault(file, []).append((int(line), name, kind))
  86. return errs
  87. def findline(lines, lineno, ident):
  88. """Given a list of all the lines in the file (adjusted so 1-indexing works),
  89. a line number that ident is alledgedly on, and ident, I figure out
  90. the line where ident was really declared."""
  91. for lineno in xrange(lineno, 0, -1):
  92. if ident in lines[lineno]:
  93. return lineno
  94. return None
  95. FUNC_PAT = re.compile(r"^[A-Za-z0-9_]+\(")
  96. def hascomment(lines, lineno, kind):
  97. """I return true if it looks like there's already a good comment about
  98. the thing on lineno of lines of type kind. """
  99. if "*/" in lines[lineno-1]:
  100. return True
  101. if kind == 'function' and FUNC_PAT.match(lines[lineno]):
  102. if "*/" in lines[lineno-2]:
  103. return True
  104. return False
  105. def hasdocdoc(lines, lineno, kind):
  106. """I return true if it looks like there's already a docdoc comment about
  107. the thing on lineno of lines of type kind."""
  108. if "DOCDOC" in lines[lineno] or "DOCDOC" in lines[lineno-1]:
  109. return True
  110. if kind == 'function' and FUNC_PAT.match(lines[lineno]):
  111. if "DOCDOC" in lines[lineno-2]:
  112. return True
  113. return False
  114. def checkf(fn, errs):
  115. """I go through the output of read() for a single file, and build a list
  116. of tuples of things that want DOCDOC comments. Each tuple has:
  117. the line number where the comment goes; the kind of thing; its name.
  118. """
  119. for skip in SKIP_FILES:
  120. if fn.endswith(skip):
  121. print "Skipping",fn
  122. return
  123. comments = []
  124. lines = [ None ]
  125. try:
  126. lines.extend( open(fn, 'r').readlines() )
  127. except IOError:
  128. return
  129. for line, name, kind in errs:
  130. if any(pat.match(name) for pat in SKIP_NAMES):
  131. continue
  132. if kind not in ADD_DOCDOCS_TO_TYPES:
  133. continue
  134. ln = findline(lines, line, name)
  135. if ln == None:
  136. print "Couldn't find the definition of %s allegedly on %s of %s"%(
  137. name, line, fn)
  138. else:
  139. if hasdocdoc(lines, line, kind):
  140. # print "Has a DOCDOC"
  141. # print fn, line, name, kind
  142. # print "\t",lines[line-2],
  143. # print "\t",lines[line-1],
  144. # print "\t",lines[line],
  145. # print "-------"
  146. pass
  147. else:
  148. if kind == 'function' and FUNC_PAT.match(lines[ln]):
  149. ln = ln - 1
  150. comments.append((ln, kind, name))
  151. return comments
  152. def applyComments(fn, entries):
  153. """I apply lots of comments to the file in fn, making a new .newdoc file.
  154. """
  155. N = 0
  156. lines = [ None ]
  157. try:
  158. lines.extend( open(fn, 'r').readlines() )
  159. except IOError:
  160. return
  161. # Process the comments in reverse order by line number, so that
  162. # the line numbers for the ones we haven't added yet remain valid
  163. # until we add them. Standard trick.
  164. entries.sort()
  165. entries.reverse()
  166. for ln, kind, name in entries:
  167. lines.insert(ln, "/* DOCDOC %s */\n"%name)
  168. N += 1
  169. outf = open(fn+".newdoc", 'w')
  170. for line in lines[1:]:
  171. outf.write(line)
  172. outf.close()
  173. print "Added %s DOCDOCs to %s" %(N, fn)
  174. e = read()
  175. for fn, errs in e.iteritems():
  176. comments = checkf(fn, errs)
  177. if comments:
  178. applyComments(fn, comments)