fold in the next changes entry
[tor.git] / contrib / redox.py
blobe9914dab189fd269d0a4eabb3881c6ddae958cb3
1 #!/usr/bin/python
3 # Copyright (c) 2008-2013, The Tor Project, Inc.
4 # See LICENSE for licensing information.
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.
18 # So, here's your workflow:
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.
32 # Which files should we ignore warning from? Mostly, these are external
33 # files that we've snarfed in from somebody else, whose C we do no intend
34 # to document for them.
35 SKIP_FILES = [ "OpenBSD_malloc_Linux.c",
36 "eventdns.c",
37 "eventdns.h",
38 "strlcat.c",
39 "strlcpy.c",
40 "sha256.c",
41 "sha256.h",
42 "aes.c",
43 "aes.h" ]
45 # What names of things never need javadoc
46 SKIP_NAME_PATTERNS = [ r'^.*_c_id$',
47 r'^.*_H_ID$' ]
49 # Which types of things should get DOCDOC comments added if they are
50 # missing documentation? Recognized types are in KINDS below.
51 ADD_DOCDOCS_TO_TYPES = [ 'function', 'type', 'typedef' ]
52 ADD_DOCDOCS_TO_TYPES += [ 'variable', ]
54 # ====================
55 # The rest of this should not need hacking.
57 import re
58 import sys
60 KINDS = [ "type", "field", "typedef", "define", "function", "variable",
61 "enumeration" ]
63 NODOC_LINE_RE = re.compile(r'^([^:]+):(\d+): (\w+): (.*) is not documented\.$')
65 THING_RE = re.compile(r'^Member ([a-zA-Z0-9_]+).*\((typedef|define|function|variable|enumeration)\) of (file|class) ')
67 SKIP_NAMES = [re.compile(s) for s in SKIP_NAME_PATTERNS]
69 def parsething(thing):
70 """I figure out what 'foobar baz in quux quum is not documented' means,
71 and return: the name of the foobar, and the kind of the foobar.
72 """
73 if thing.startswith("Compound "):
74 tp, name = "type", thing.split()[1]
75 else:
76 m = THING_RE.match(thing)
77 if not m:
78 print thing, "???? Format didn't match."
79 return None, None
80 else:
81 name, tp, parent = m.groups()
82 if parent == 'class':
83 if tp == 'variable' or tp == 'function':
84 tp = 'field'
86 return name, tp
88 def read():
89 """I snarf doxygen stderr from stdin, and parse all the "foo has no
90 documentation messages. I return a map from filename to lists
91 of tuples of (alleged line number, name of thing, kind of thing)
92 """
93 errs = {}
94 for line in sys.stdin:
95 m = NODOC_LINE_RE.match(line)
96 if m:
97 file, line, tp, thing = m.groups()
98 assert tp.lower() == 'warning'
99 name, kind = parsething(thing)
100 errs.setdefault(file, []).append((int(line), name, kind))
102 return errs
104 def findline(lines, lineno, ident):
105 """Given a list of all the lines in the file (adjusted so 1-indexing works),
106 a line number that ident is alledgedly on, and ident, I figure out
107 the line where ident was really declared."""
108 for lineno in xrange(lineno, 0, -1):
109 if ident in lines[lineno]:
110 return lineno
112 return None
114 FUNC_PAT = re.compile(r"^[A-Za-z0-9_]+\(")
116 def hascomment(lines, lineno, kind):
117 """I return true if it looks like there's already a good comment about
118 the thing on lineno of lines of type kind. """
119 if "*/" in lines[lineno-1]:
120 return True
121 if kind == 'function' and FUNC_PAT.match(lines[lineno]):
122 if "*/" in lines[lineno-2]:
123 return True
124 return False
126 def hasdocdoc(lines, lineno, kind):
127 """I return true if it looks like there's already a docdoc comment about
128 the thing on lineno of lines of type kind."""
129 if "DOCDOC" in lines[lineno] or "DOCDOC" in lines[lineno-1]:
130 return True
131 if kind == 'function' and FUNC_PAT.match(lines[lineno]):
132 if "DOCDOC" in lines[lineno-2]:
133 return True
134 return False
136 def checkf(fn, errs):
137 """I go through the output of read() for a single file, and build a list
138 of tuples of things that want DOCDOC comments. Each tuple has:
139 the line number where the comment goes; the kind of thing; its name.
141 for skip in SKIP_FILES:
142 if fn.endswith(skip):
143 print "Skipping",fn
144 return
146 comments = []
147 lines = [ None ]
148 try:
149 lines.extend( open(fn, 'r').readlines() )
150 except IOError:
151 return
153 for line, name, kind in errs:
154 if any(pat.match(name) for pat in SKIP_NAMES):
155 continue
157 if kind not in ADD_DOCDOCS_TO_TYPES:
158 continue
160 ln = findline(lines, line, name)
161 if ln == None:
162 print "Couldn't find the definition of %s allegedly on %s of %s"%(
163 name, line, fn)
164 else:
165 if hasdocdoc(lines, line, kind):
166 # print "Has a DOCDOC"
167 # print fn, line, name, kind
168 # print "\t",lines[line-2],
169 # print "\t",lines[line-1],
170 # print "\t",lines[line],
171 # print "-------"
172 pass
173 else:
174 if kind == 'function' and FUNC_PAT.match(lines[ln]):
175 ln = ln - 1
177 comments.append((ln, kind, name))
179 return comments
181 def applyComments(fn, entries):
182 """I apply lots of comments to the file in fn, making a new .newdoc file.
184 N = 0
186 lines = [ None ]
187 try:
188 lines.extend( open(fn, 'r').readlines() )
189 except IOError:
190 return
192 # Process the comments in reverse order by line number, so that
193 # the line numbers for the ones we haven't added yet remain valid
194 # until we add them. Standard trick.
195 entries.sort()
196 entries.reverse()
198 for ln, kind, name in entries:
200 lines.insert(ln, "/* DOCDOC %s */\n"%name)
201 N += 1
203 outf = open(fn+".newdoc", 'w')
204 for line in lines[1:]:
205 outf.write(line)
206 outf.close()
208 print "Added %s DOCDOCs to %s" %(N, fn)
210 e = read()
212 for fn, errs in e.iteritems():
213 comments = checkf(fn, errs)
214 if comments:
215 applyComments(fn, comments)