3 # Copyright (c) 2008 The Tor Project, Inc.
4 # See LICENSE for licensing information.
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",
43 # What names of things never need javadoc
44 SKIP_NAME_PATTERNS
= [ r
'^.*_c_id$',
47 # Which types of things should get DOCDOC comments added if they are
48 # missing documentation? Recognized types are in KINDS below.
49 #ADD_DOCDOCS_TO_TYPES = [ 'function', 'type', 'typedef' ]
50 ADD_DOCDOCS_TO_TYPES
= [ 'variable' ]
52 # ====================
53 # The rest of this should not need hacking.
58 KINDS
= [ "type", "field", "typedef", "define", "function", "variable" ]
60 NODOC_LINE_RE
= re
.compile(r
'^([^:]+):(\d+): (\w+): (.*) is not documented\.$')
62 THING_RE
= re
.compile(r
'^Member ([a-zA-Z0-9_]+).*\((typedef|define|function|variable)\) of (file|class) ')
64 SKIP_NAMES
= [re
.compile(s
) for s
in SKIP_NAME_PATTERNS
]
66 def parsething(thing
):
67 """I figure out what 'foobar baz in quux quum is not documented' means,
68 and return: the name of the foobar, and the kind of the foobar.
70 if thing
.startswith("Compound "):
71 tp
, name
= "type", thing
.split()[1]
73 m
= THING_RE
.match(thing
)
78 name
, tp
, parent
= m
.groups()
80 if tp
== 'variable' or tp
== 'function':
86 """I snarf doxygen stderr from stdin, and parse all the "foo has no
87 documentation messages. I return a map from filename to lists
88 of tuples of (alleged line number, name of thing, kind of thing)
91 for line
in sys
.stdin
:
92 m
= NODOC_LINE_RE
.match(line
)
94 file, line
, tp
, thing
= m
.groups()
95 assert tp
== 'Warning'
96 name
, kind
= parsething(thing
)
97 errs
.setdefault(file, []).append((int(line
), name
, kind
))
101 def findline(lines
, lineno
, ident
):
102 """Given a list of all the lines in the file (adjusted so 1-indexing works),
103 a line number that ident is alledgedly on, and ident, I figure out
104 the line where ident was really declared."""
105 for lineno
in xrange(lineno
, 0, -1):
106 if ident
in lines
[lineno
]:
111 FUNC_PAT
= re
.compile(r
"^[A-Za-z0-9_]+\(")
113 def hascomment(lines
, lineno
, kind
):
114 """I return true if it looks like there's already a good comment about
115 the thing on lineno of lines of type kind. """
116 if "*/" in lines
[lineno
-1]:
118 if kind
== 'function' and FUNC_PAT
.match(lines
[lineno
]):
119 if "*/" in lines
[lineno
-2]:
123 def hasdocdoc(lines
, lineno
, kind
):
124 """I return true if it looks like there's already a docdoc comment about
125 the thing on lineno of lines of type kind."""
126 if "DOCDOC" in lines
[lineno
] or "DOCDOC" in lines
[lineno
-1]:
128 if kind
== 'function' and FUNC_PAT
.match(lines
[lineno
]):
129 if "DOCDOC" in lines
[lineno
-2]:
133 def checkf(fn
, errs
):
134 """I go through the output of read() for a single file, and build a list
135 of tuples of things that want DOCDOC comments. Each tuple has:
136 the line number where the comment goes; the kind of thing; its name.
138 for skip
in SKIP_FILES
:
139 if fn
.endswith(skip
):
146 lines
.extend( open(fn
, 'r').readlines() )
150 for line
, name
, kind
in errs
:
151 if any(pat
.match(name
) for pat
in SKIP_NAMES
):
154 if kind
not in ADD_DOCDOCS_TO_TYPES
:
157 ln
= findline(lines
, line
, name
)
159 print "Couldn't find the definition of %s allegedly on %s of %s"%(
162 if hasdocdoc(lines
, line
, kind
):
163 # print "Has a DOCDOC"
164 # print fn, line, name, kind
165 # print "\t",lines[line-2],
166 # print "\t",lines[line-1],
167 # print "\t",lines[line],
171 if kind
== 'function' and FUNC_PAT
.match(lines
[ln
]):
174 comments
.append((ln
, kind
, name
))
178 def applyComments(fn
, entries
):
179 """I apply lots of comments to the file in fn, making a new .newdoc file.
185 lines
.extend( open(fn
, 'r').readlines() )
189 # Process the comments in reverse order by line number, so that
190 # the line numbers for the ones we haven't added yet remain valid
191 # until we add them. Standard trick.
195 for ln
, kind
, name
in entries
:
197 lines
.insert(ln
, "/* DOCDOC %s */\n"%name
)
200 outf
= open(fn
+".newdoc", 'w')
201 for line
in lines
[1:]:
205 print "Added %s DOCDOCs to %s" %(N
, fn
)
209 for fn
, errs
in e
.iteritems():
210 comments
= checkf(fn
, errs
)
212 applyComments(fn
, comments
)