## Comment.
-class Comment:
+class Comment(object):
+
+ def __init__(self, lines, first_line, first_col, last_line, last_col, indent, func, \
+ append_empty=True):
- def __init__(self, lines, first_line, first_col, last_line, last_col, indent, func):
assert first_line > 0 and last_line >= first_line, 'Wrong line numbers'
self.lines = lines
self.first_line = first_line
self.last_col = last_col
self.indent = indent
self.func = func
+ self.append_empty = append_empty
def has_comment(self, line):
return line >= self.first_line and line <= self.last_line
def __str__(self):
- return "<Comment for %s: [%d,%d:%d,%d] %s>" % (self.func, self.first_line, self.first_col, self.last_line, self.last_col, self.lines)
+ return "<%s for %s: [%d,%d:%d,%d] %s>" % ( \
+ self.__class__.__name__, self.func,
+ self.first_line, self.first_col, self.last_line, self.last_col,
+ self.lines)
+
+
+## Prepend comment.
+class PrependComment(Comment):
+
+ def __init__(self, lines, first_line, first_col, last_line, last_col, indent, func, \
+ append_empty=False):
+ super(PrependComment, self).__init__( \
+ lines, first_line, first_col, last_line, last_col, indent, func, append_empty)
## A data member comment.
assert found, 'A line that should exist was not found in file' % cursor.location.file
- recomm = r'(//(!|\|\||->)|///?)(\[([0-9,]+)\])?<?\s*(.*?)\s*$'
+ recomm = r'(//(!|\|\||->)|///?)(\[(.+?)\])?<?\s*(.*?)\s*$'
recomm_prevline = r'^\s*///\s*(.*?)\s*$'
mcomm = re.search(recomm, raw)
#
# @param filename Name of the current file
# @param comments Array of comments: new ones will be appended there
-def comment_classdesc(filename, comments):
+# @param look_no_further_than_line Stop before reaching this line when looking for class comment
+def comment_classdesc(filename, comments, look_no_further_than_line):
recomm = r'^\s*///?(\s*.*?)\s*/*\s*$'
line_num = line_num + 1
+ if look_no_further_than_line is not None and line_num == look_no_further_than_line:
+ logging.debug('Stopping at line %d while looking for class/file description' % \
+ look_no_further_than_line)
+ break
+
if raw.strip() == '' and start_line > 0:
# Skip empty lines
continue
else:
assert False, 'Regexp unable to extract classname from file'
+ # Macro or class?
+ if is_macro:
+ file_class_line = '\\file ' + class_name_doxy + '.C'
+ else:
+ file_class_line = '\\class ' + class_name_doxy
+
if start_line > 0:
# Prepend \class or \file specifier (and an empty line)
- if is_macro:
- comment_lines[:0] = [ '\\file ' + class_name_doxy + '.C' ]
- else:
- comment_lines[:0] = [ '\\class ' + class_name_doxy ]
+ comment_lines[:0] = [ file_class_line ]
comment_lines.append('')
# Append author and date if they exist
else:
- logging.warning('No comment found for class %s' % Colt(class_name_doxy).magenta())
+ logging.warning('No comment found for class %s: creating a dummy entry at the beginning' % \
+ Colt(class_name_doxy).magenta())
+
+ comments.append(PrependComment(
+ [ file_class_line ],
+ 1, 1, 1, 1,
+ 0, class_name_doxy, append_empty=True
+ ))
+
+
+## Looks for a special ROOT ClassImp() entry.
+#
+# Doxygen might get confused by `ClassImp()` entries as they are macros normally written without
+# the ending `;`: this function wraps the definition inside a condition in order to make Doxygen
+# ignore it.
+#
+# @param filename Name of the current file
+# @param comments Array of comments: new ones will be appended there
+def comment_classimp(filename, comments):
+
+ recomm = r'^\s*///?(\s*.*?)\s*/*\s*$'
+
+ line_num = 0
+ reclassimp = r'^(\s*)ClassImp\((.*?)\)\s*;?\s*$'
+
+ in_classimp_cond = False
+ restartcond = r'^\s*///\s*\\cond\s+CLASSIMP\s*$'
+ reendcond = r'^\s*///\s*\\endcond\s*$'
+
+ with open(filename, 'r') as fp:
+
+ line_classimp = -1
+ line_startcond = -1
+ line_endcond = -1
+ classimp_class = None
+ classimp_indent = None
+
+ for line in fp:
+
+ line_num = line_num + 1
+
+ mclassimp = re.search(reclassimp, line)
+ if mclassimp:
+
+ # Adjust indent
+ classimp_indent = len( mclassimp.group(1) )
+
+ line_classimp = line_num
+ classimp_class = mclassimp.group(2)
+ logging.debug(
+ 'Comment found for ' +
+ Colt( 'ClassImp(' ).magenta() +
+ Colt( classimp_class ).cyan() +
+ Colt( ')' ).magenta() )
+
+ else:
+
+ mstartcond = re.search(restartcond, line)
+ if mstartcond:
+ logging.debug('Found Doxygen opening condition for ClassImp in {%s}' % line)
+ in_classimp_cond = True
+ line_startcond = line_num
+
+ elif in_classimp_cond:
+
+ mendcond = re.search(reendcond, line)
+ if mendcond:
+ logging.debug('Found Doxygen closing condition for ClassImp')
+ in_classimp_cond = False
+ line_endcond = line_num
+
+ # Did we find something?
+ if line_classimp != -1:
+
+ if line_startcond != -1:
+ comments.append(Comment(
+ ['\cond CLASSIMP'],
+ line_startcond, 1, line_startcond, 1,
+ classimp_indent, 'ClassImp(%s)' % classimp_class,
+ append_empty=False
+ ))
+ else:
+ comments.append(PrependComment(
+ ['\cond CLASSIMP'],
+ line_classimp, 1, line_classimp, 1,
+ classimp_indent, 'ClassImp(%s)' % classimp_class
+ ))
+
+ if line_endcond != -1:
+ comments.append(Comment(
+ ['\endcond'],
+ line_endcond, 1, line_endcond, 1,
+ classimp_indent, 'ClassImp(%s)' % classimp_class,
+ append_empty=False
+ ))
+ else:
+ comments.append(PrependComment(
+ ['\endcond'],
+ line_classimp+1, 1, line_classimp+1, 1,
+ classimp_indent, 'ClassImp(%s)' % classimp_class
+ ))
## Traverse the AST recursively starting from the current cursor.
# @param comments Array of comments: new ones will be appended there
# @param recursion Current recursion depth
# @param in_func True if we are inside a function or method
-def traverse_ast(cursor, filename, comments, recursion=0, in_func=False):
+# @param classdesc_line_limit Do not look for comments after this line
+#
+# @return A tuple containing the classdesc_line_limit as first item
+def traverse_ast(cursor, filename, comments, recursion=0, in_func=False, classdesc_line_limit=None):
# libclang traverses included files as well: we do not want this behavior
if cursor.location.file is not None and str(cursor.location.file) != filename:
if cursor.kind in [ clang.cindex.CursorKind.CXX_METHOD, clang.cindex.CursorKind.CONSTRUCTOR,
clang.cindex.CursorKind.DESTRUCTOR, clang.cindex.CursorKind.FUNCTION_DECL ]:
+ if classdesc_line_limit is None:
+ classdesc_line_limit = cursor.location.line
+
# cursor ran into a C++ method
logging.debug( "%5d %s%s(%s)" % (cursor.location.line, indent, Colt(kind).magenta(), Colt(text).blue()) )
comment_method(cursor, comments)
elif not is_macro and not in_func and \
cursor.kind in [ clang.cindex.CursorKind.FIELD_DECL, clang.cindex.CursorKind.VAR_DECL ]:
+ if classdesc_line_limit is None:
+ classdesc_line_limit = cursor.location.line
+
# cursor ran into a data member declaration
logging.debug( "%5d %s%s(%s)" % (cursor.location.line, indent, Colt(kind).magenta(), Colt(text).blue()) )
comment_datamember(cursor, comments)
logging.debug( "%5d %s%s(%s)" % (cursor.location.line, indent, kind, text) )
for child_cursor in cursor.get_children():
- traverse_ast(child_cursor, filename, comments, recursion+1, in_func)
+ classdesc_line_limit = traverse_ast(child_cursor, filename, comments, recursion+1, in_func, classdesc_line_limit)
if recursion == 0:
- comment_classdesc(filename, comments)
+ comment_classimp(filename, comments)
+ comment_classdesc(filename, comments, classdesc_line_limit)
+
+ return classdesc_line_limit
## Strip some HTML tags from the given string. Returns clean string.
skip_empty = False
comm = None
prev_comm = None
+ restore_lines = None
rindent = r'^(\s*)'
- def dump_comment_block(cmt):
+ def dump_comment_block(cmt, restore=None):
text_indent = ''
+ ask_skip_empty = False
+
for i in range(0, cmt.indent):
text_indent = text_indent + ' '
fhout.write('\n')
# Empty new line at the end of the comment
- fhout.write('\n')
+ if cmt.append_empty:
+ fhout.write('\n')
+ ask_skip_empty = True
+
+ # Restore lines if possible
+ if restore:
+ for lr in restore:
+ fhout.write(lr)
+ fhout.write('\n')
+
+ # Tell the caller whether it should skip the next empty line found
+ return ask_skip_empty
for line in fhin:
if comm:
+ # First thing to check: are we in the same comment as before?
+ if comm is not prev_comm and isinstance(comm, Comment) and isinstance(prev_comm, Comment) \
+ and not isinstance(prev_comm, RemoveComment):
+
+ skip_empty = dump_comment_block(prev_comm, restore_lines)
+ in_comment = False
+ restore_lines = None
+ prev_comm = None # we have just dumped it: pretend it never existed in this loop
+
+ #
+ # Check type of comment and react accordingly
+ #
+
if isinstance(comm, MemberComment):
# end comment block
if in_comment:
- dump_comment_block(prev_comm)
+ skip_empty = dump_comment_block(prev_comm, restore_lines)
in_comment = False
+ restore_lines = None
non_comment = line[ 0:comm.first_col-1 ]
elif isinstance(comm, RemoveComment):
# End comment block and skip this line
if in_comment:
- dump_comment_block(prev_comm)
+ skip_empty = dump_comment_block(prev_comm, restore_lines)
in_comment = False
+ restore_lines = None
elif prev_comm is None:
- # Beginning of a new comment block of type Comment
+ # Beginning of a new comment block of type Comment or PrependComment
in_comment = True
- # Extract the non-comment part and print it if it exists
- non_comment = line[ 0:comm.first_col-1 ].rstrip()
- if non_comment != '':
- fhout.write( non_comment + '\n' )
+ if isinstance(comm, PrependComment):
+ # Prepare array of lines to dump right after the comment
+ restore_lines = [ line.rstrip('\n') ]
+ else:
+ # Extract the non-comment part and print it if it exists
+ non_comment = line[ 0:comm.first_col-1 ].rstrip()
+ if non_comment != '':
+ fhout.write( non_comment + '\n' )
+
+ elif isinstance(comm, Comment):
+
+ if restore_lines is not None:
+ # From the 2nd line on of comment to prepend
+ restore_lines.append( line.rstrip('\n') )
+
+ else:
+ assert False, 'Unhandled parser state. line=%d comm=%s prev_comm=%s' % \
+ (line_num, comm, prev_comm)
else:
+ # Not a comment line
+
if in_comment:
# We have just exited a comment block of type Comment
- dump_comment_block(prev_comm)
+ skip_empty = dump_comment_block(prev_comm, restore_lines)
in_comment = False
- skip_empty = True
+ restore_lines = None
+ # Dump the non-comment line
line_out = line.rstrip('\n')
if skip_empty:
skip_empty = False