3 # Copyright (c) 2008-2013, 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",
45 # What names of things never need javadoc
46 SKIP_NAME_PATTERNS
= [ r
'^.*_c_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.
60 KINDS
= [ "type", "field", "typedef", "define", "function", "variable",
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.
73 if thing
.startswith("Compound "):
74 tp
, name
= "type", thing
.split()[1]
76 m
= THING_RE
.match(thing
)
78 print thing
, "???? Format didn't match."
81 name
, tp
, parent
= m
.groups()
83 if tp
== 'variable' or tp
== 'function':
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)
94 for line
in sys
.stdin
:
95 m
= NODOC_LINE_RE
.match(line
)
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
))
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
]:
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]:
121 if kind
== 'function' and FUNC_PAT
.match(lines
[lineno
]):
122 if "*/" in lines
[lineno
-2]:
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]:
131 if kind
== 'function' and FUNC_PAT
.match(lines
[lineno
]):
132 if "DOCDOC" in lines
[lineno
-2]:
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
):
149 lines
.extend( open(fn
, 'r').readlines() )
153 for line
, name
, kind
in errs
:
154 if any(pat
.match(name
) for pat
in SKIP_NAMES
):
157 if kind
not in ADD_DOCDOCS_TO_TYPES
:
160 ln
= findline(lines
, line
, name
)
162 print "Couldn't find the definition of %s allegedly on %s of %s"%(
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],
174 if kind
== 'function' and FUNC_PAT
.match(lines
[ln
]):
177 comments
.append((ln
, kind
, name
))
181 def applyComments(fn
, entries
):
182 """I apply lots of comments to the file in fn, making a new .newdoc file.
188 lines
.extend( open(fn
, 'r').readlines() )
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.
198 for ln
, kind
, name
in entries
:
200 lines
.insert(ln
, "/* DOCDOC %s */\n"%name
)
203 outf
= open(fn
+".newdoc", 'w')
204 for line
in lines
[1:]:
208 print "Added %s DOCDOCs to %s" %(N
, fn
)
212 for fn
, errs
in e
.iteritems():
213 comments
= checkf(fn
, errs
)
215 applyComments(fn
, comments
)