2008-12-22 15:56:19 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
2013-01-16 07:54:56 +01:00
|
|
|
# Copyright (c) 2008-2013, The Tor Project, Inc.
|
2008-12-22 15:56:19 +01:00
|
|
|
# See LICENSE for licensing information.
|
|
|
|
#
|
|
|
|
# Hi!
|
|
|
|
# I'm redox.py, the Tor redocumentation tool!
|
|
|
|
# I am a horrible hack!
|
|
|
|
# I read the output of doxygen from stderr, and add missing DOCDOC comments
|
|
|
|
# to tell you where documentation should go!
|
|
|
|
# To use me, edit the stuff below...
|
|
|
|
# ...and run 'make doxygen 2>doxygen.stderr' ...
|
|
|
|
# ...and run ./contrib/redox.py < doxygen.stderr !
|
|
|
|
# I'll make a bunch of new files by adding missing DOCDOC comments to your
|
|
|
|
# source. Those files will have names like ./src/common/util.c.newdoc.
|
|
|
|
# You will want to look over the changes by hand before checking them in.
|
2008-12-22 19:36:22 +01:00
|
|
|
#
|
|
|
|
# So, here's your workflow:
|
|
|
|
#
|
|
|
|
# 0. Make sure you're running a bourne shell for the redirects below.
|
|
|
|
# 1. make doxygen 1>doxygen.stdout 2>doxygen.stderr.
|
|
|
|
# 2. grep Warning doxygen.stderr | grep -v 'is not documented' | less
|
|
|
|
# [This will tell you about all the bogus doxygen output you have]
|
|
|
|
# 3. python ./contrib/redox.py <doxygen.stderr
|
|
|
|
# [This will make lots of .newdoc files with DOCDOC comments for
|
|
|
|
# whatever was missing documentation.]
|
|
|
|
# 4. Look over those .newdoc files, and see which docdoc comments you
|
|
|
|
# want to merge into the main file. If it's all good, just run
|
|
|
|
# "mv fname.c.newdoc fname.c". Otherwise, you'll need to merge
|
|
|
|
# the parts you like by hand.
|
|
|
|
|
|
|
|
# Which files should we ignore warning from? Mostly, these are external
|
|
|
|
# files that we've snarfed in from somebody else, whose C we do no intend
|
|
|
|
# to document for them.
|
2008-12-22 15:56:19 +01:00
|
|
|
SKIP_FILES = [ "OpenBSD_malloc_Linux.c",
|
|
|
|
"eventdns.c",
|
|
|
|
"eventdns.h",
|
|
|
|
"strlcat.c",
|
|
|
|
"strlcpy.c",
|
2012-06-05 01:56:44 +02:00
|
|
|
"sha256.c",
|
|
|
|
"sha256.h",
|
2008-12-22 15:56:19 +01:00
|
|
|
"aes.c",
|
|
|
|
"aes.h" ]
|
|
|
|
|
2008-12-22 19:36:22 +01:00
|
|
|
# What names of things never need javadoc
|
2008-12-22 20:00:05 +01:00
|
|
|
SKIP_NAME_PATTERNS = [ r'^.*_c_id$',
|
|
|
|
r'^.*_H_ID$' ]
|
2008-12-22 15:56:19 +01:00
|
|
|
|
2008-12-22 19:36:22 +01:00
|
|
|
# Which types of things should get DOCDOC comments added if they are
|
|
|
|
# missing documentation? Recognized types are in KINDS below.
|
2012-06-05 01:56:44 +02:00
|
|
|
ADD_DOCDOCS_TO_TYPES = [ 'function', 'type', 'typedef' ]
|
|
|
|
ADD_DOCDOCS_TO_TYPES += [ 'variable', ]
|
2008-12-22 15:56:19 +01:00
|
|
|
|
|
|
|
# ====================
|
|
|
|
# The rest of this should not need hacking.
|
|
|
|
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
2012-06-05 01:56:44 +02:00
|
|
|
KINDS = [ "type", "field", "typedef", "define", "function", "variable",
|
|
|
|
"enumeration" ]
|
2008-12-22 15:56:19 +01:00
|
|
|
|
|
|
|
NODOC_LINE_RE = re.compile(r'^([^:]+):(\d+): (\w+): (.*) is not documented\.$')
|
|
|
|
|
2014-03-25 16:01:09 +01:00
|
|
|
THING_RE = re.compile(r'^Member ([a-zA-Z0-9_]+).*\((typedef|define|function|variable|enumeration|macro definition)\) of (file|class) ')
|
2008-12-22 15:56:19 +01:00
|
|
|
|
|
|
|
SKIP_NAMES = [re.compile(s) for s in SKIP_NAME_PATTERNS]
|
|
|
|
|
|
|
|
def parsething(thing):
|
2008-12-22 19:36:22 +01:00
|
|
|
"""I figure out what 'foobar baz in quux quum is not documented' means,
|
|
|
|
and return: the name of the foobar, and the kind of the foobar.
|
|
|
|
"""
|
2008-12-22 15:56:19 +01:00
|
|
|
if thing.startswith("Compound "):
|
|
|
|
tp, name = "type", thing.split()[1]
|
|
|
|
else:
|
|
|
|
m = THING_RE.match(thing)
|
|
|
|
if not m:
|
2012-06-05 01:56:44 +02:00
|
|
|
print thing, "???? Format didn't match."
|
2008-12-22 15:56:19 +01:00
|
|
|
return None, None
|
|
|
|
else:
|
|
|
|
name, tp, parent = m.groups()
|
|
|
|
if parent == 'class':
|
|
|
|
if tp == 'variable' or tp == 'function':
|
|
|
|
tp = 'field'
|
|
|
|
|
|
|
|
return name, tp
|
|
|
|
|
|
|
|
def read():
|
2008-12-22 19:36:22 +01:00
|
|
|
"""I snarf doxygen stderr from stdin, and parse all the "foo has no
|
|
|
|
documentation messages. I return a map from filename to lists
|
|
|
|
of tuples of (alleged line number, name of thing, kind of thing)
|
|
|
|
"""
|
2008-12-22 15:56:19 +01:00
|
|
|
errs = {}
|
|
|
|
for line in sys.stdin:
|
|
|
|
m = NODOC_LINE_RE.match(line)
|
|
|
|
if m:
|
|
|
|
file, line, tp, thing = m.groups()
|
2012-06-05 01:56:44 +02:00
|
|
|
assert tp.lower() == 'warning'
|
2008-12-22 15:56:19 +01:00
|
|
|
name, kind = parsething(thing)
|
|
|
|
errs.setdefault(file, []).append((int(line), name, kind))
|
|
|
|
|
|
|
|
return errs
|
|
|
|
|
|
|
|
def findline(lines, lineno, ident):
|
2008-12-22 19:36:22 +01:00
|
|
|
"""Given a list of all the lines in the file (adjusted so 1-indexing works),
|
|
|
|
a line number that ident is alledgedly on, and ident, I figure out
|
|
|
|
the line where ident was really declared."""
|
2014-03-25 16:01:09 +01:00
|
|
|
lno = lineno
|
2008-12-22 15:56:19 +01:00
|
|
|
for lineno in xrange(lineno, 0, -1):
|
2014-03-25 16:01:09 +01:00
|
|
|
try:
|
|
|
|
if ident in lines[lineno]:
|
|
|
|
return lineno
|
|
|
|
except IndexError:
|
|
|
|
continue
|
2008-12-22 15:56:19 +01:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
FUNC_PAT = re.compile(r"^[A-Za-z0-9_]+\(")
|
|
|
|
|
|
|
|
def hascomment(lines, lineno, kind):
|
2008-12-22 19:36:22 +01:00
|
|
|
"""I return true if it looks like there's already a good comment about
|
|
|
|
the thing on lineno of lines of type kind. """
|
2008-12-22 15:56:19 +01:00
|
|
|
if "*/" in lines[lineno-1]:
|
|
|
|
return True
|
|
|
|
if kind == 'function' and FUNC_PAT.match(lines[lineno]):
|
|
|
|
if "*/" in lines[lineno-2]:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def hasdocdoc(lines, lineno, kind):
|
2008-12-22 19:36:22 +01:00
|
|
|
"""I return true if it looks like there's already a docdoc comment about
|
|
|
|
the thing on lineno of lines of type kind."""
|
2014-03-25 16:01:09 +01:00
|
|
|
try:
|
|
|
|
if "DOCDOC" in lines[lineno]:
|
|
|
|
return True
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
if "DOCDOC" in lines[lineno-1]:
|
|
|
|
return True
|
|
|
|
except IndexError:
|
|
|
|
pass
|
2008-12-22 15:56:19 +01:00
|
|
|
if kind == 'function' and FUNC_PAT.match(lines[lineno]):
|
|
|
|
if "DOCDOC" in lines[lineno-2]:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2008-12-22 19:36:22 +01:00
|
|
|
def checkf(fn, errs):
|
|
|
|
"""I go through the output of read() for a single file, and build a list
|
|
|
|
of tuples of things that want DOCDOC comments. Each tuple has:
|
|
|
|
the line number where the comment goes; the kind of thing; its name.
|
|
|
|
"""
|
2008-12-22 15:56:19 +01:00
|
|
|
for skip in SKIP_FILES:
|
|
|
|
if fn.endswith(skip):
|
|
|
|
print "Skipping",fn
|
|
|
|
return
|
|
|
|
|
2008-12-22 19:36:22 +01:00
|
|
|
comments = []
|
2008-12-22 15:56:19 +01:00
|
|
|
lines = [ None ]
|
|
|
|
try:
|
|
|
|
lines.extend( open(fn, 'r').readlines() )
|
|
|
|
except IOError:
|
|
|
|
return
|
|
|
|
|
|
|
|
for line, name, kind in errs:
|
|
|
|
if any(pat.match(name) for pat in SKIP_NAMES):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if kind not in ADD_DOCDOCS_TO_TYPES:
|
|
|
|
continue
|
|
|
|
|
|
|
|
ln = findline(lines, line, name)
|
|
|
|
if ln == None:
|
|
|
|
print "Couldn't find the definition of %s allegedly on %s of %s"%(
|
|
|
|
name, line, fn)
|
|
|
|
else:
|
|
|
|
if hasdocdoc(lines, line, kind):
|
|
|
|
# print "Has a DOCDOC"
|
|
|
|
# print fn, line, name, kind
|
|
|
|
# print "\t",lines[line-2],
|
|
|
|
# print "\t",lines[line-1],
|
|
|
|
# print "\t",lines[line],
|
|
|
|
# print "-------"
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if kind == 'function' and FUNC_PAT.match(lines[ln]):
|
|
|
|
ln = ln - 1
|
|
|
|
|
2008-12-22 19:36:22 +01:00
|
|
|
comments.append((ln, kind, name))
|
2008-12-22 15:56:19 +01:00
|
|
|
|
2008-12-22 19:44:06 +01:00
|
|
|
return comments
|
|
|
|
|
2008-12-22 15:56:19 +01:00
|
|
|
def applyComments(fn, entries):
|
2008-12-22 19:36:22 +01:00
|
|
|
"""I apply lots of comments to the file in fn, making a new .newdoc file.
|
|
|
|
"""
|
2008-12-22 15:56:19 +01:00
|
|
|
N = 0
|
|
|
|
|
|
|
|
lines = [ None ]
|
|
|
|
try:
|
|
|
|
lines.extend( open(fn, 'r').readlines() )
|
|
|
|
except IOError:
|
|
|
|
return
|
|
|
|
|
2008-12-22 19:36:22 +01:00
|
|
|
# Process the comments in reverse order by line number, so that
|
|
|
|
# the line numbers for the ones we haven't added yet remain valid
|
|
|
|
# until we add them. Standard trick.
|
2008-12-22 15:56:19 +01:00
|
|
|
entries.sort()
|
|
|
|
entries.reverse()
|
|
|
|
|
|
|
|
for ln, kind, name in entries:
|
|
|
|
|
|
|
|
lines.insert(ln, "/* DOCDOC %s */\n"%name)
|
|
|
|
N += 1
|
|
|
|
|
|
|
|
outf = open(fn+".newdoc", 'w')
|
|
|
|
for line in lines[1:]:
|
|
|
|
outf.write(line)
|
|
|
|
outf.close()
|
|
|
|
|
|
|
|
print "Added %s DOCDOCs to %s" %(N, fn)
|
|
|
|
|
|
|
|
e = read()
|
|
|
|
|
|
|
|
for fn, errs in e.iteritems():
|
2014-03-25 16:01:09 +01:00
|
|
|
print `(fn, errs)`
|
2008-12-22 19:36:22 +01:00
|
|
|
comments = checkf(fn, errs)
|
|
|
|
if comments:
|
|
|
|
applyComments(fn, comments)
|