]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - doxygen/thtml2doxy_clang.py
doxy: remove garbage lines from comments
[u/mrichter/AliRoot.git] / doxygen / thtml2doxy_clang.py
index 6072d9701282d4444839078809b3b5611013604a..0e09f1163f904af227d8c11535b81924fcc946b9 100755 (executable)
@@ -61,110 +61,250 @@ class Colt(str):
 ## Comment.
 class Comment:
 
-  def __init__(self, lines, first_line, last_line, func):
+  def __init__(self, lines, first_line, first_col, last_line, last_col, indent, func):
     self.lines = lines
     self.first_line = first_line
+    self.first_col = first_col
     self.last_line = last_line
+    self.last_col = last_col
+    self.indent = indent
     self.func = func
 
+  def has_comment(self, line):
+    return line >= self.first_line and line <= self.last_line
+
   def __str__(self):
-    return "<Comment for %s: [%d:%d] %s>" % (self.func, self.first_line, self.last_line, self.lines)
+    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)
 
 
-## Traverse the AST recursively starting from the current cursor.
-#
-#  @param cursor    A Clang parser cursor
-#  @param comments  Array of comments found (of class Comment)
-#  @param recursion Current recursion depth
-def traverse_ast(cursor, comments, recursion=0):
+## A data member comment.
+class MemberComment:
 
-  text = cursor.spelling or cursor.displayname
-  kind = str(cursor.kind)[str(cursor.kind).index('.')+1:]
+  def __init__(self, text, is_transient, array_size, first_line, first_col, func):
+    self.lines = [ text ]
+    self.is_transient = is_transient
+    self.array_size = array_size
+    self.first_line = first_line
+    self.first_col = first_col
+    self.func = func
 
-  indent = ''
-  for i in range(0, recursion):
-    indent = indent + '  '
+  def has_comment(self, line):
+    return line == self.first_line
 
-  if cursor.kind == clang.cindex.CursorKind.CXX_METHOD:
+  def __str__(self):
 
-    # cursor ran into a C++ method
-    logging.debug( "%s%s(%s)" % (indent, Colt(kind).magenta(), Colt(text).blue()) )
+    if self.is_transient:
+      tt = '!transient! '
+    else:
+      tt = ''
 
-    # we are looking for the following structure: method -> compound statement -> comment, i.e. we
-    # need to extract the first comment in the compound statement composing the method
+    if self.array_size is not None:
+      ars = '[%s] ' % self.array_size
+    else:
+      ars = ''
 
-    in_compound_stmt = False
-    expect_comment = False
-    emit_comment = False
+    return "<MemberComment for %s: [%d,%d] %s%s%s>" % (self.func, self.first_line, self.first_col, tt, ars, self.lines[0])
 
-    comment = []
-    comment_function = text
-    comment_line_start = -1
-    comment_line_end = -1
 
-    for token in cursor.get_tokens():
+## A dummy comment that removes comment lines.
+class RemoveComment(Comment):
 
-      if token.cursor.kind == clang.cindex.CursorKind.COMPOUND_STMT:
-        if not in_compound_stmt:
-          in_compound_stmt = True
-          expect_comment = True
-          comment_line_end = -1
-      else:
-        if in_compound_stmt:
-          in_compound_stmt = False
-          emit_comment = True
+  def __init__(self, first_line, last_line):
+    self.first_line = first_line
+    self.last_line = last_line
+    self.func = '<remove>'
 
-      # tkind = str(token.kind)[str(token.kind).index('.')+1:]
-      # ckind = str(token.cursor.kind)[str(token.cursor.kind).index('.')+1:]
+  def __str__(self):
+    return "<RemoveComment: [%d,%d]>" % (self.first_line, self.last_line)
 
+
+## Parses method comments.
+#
+#  @param cursor   Current libclang parser cursor
+#  @param comments Array of comments: new ones will be appended there
+def comment_method(cursor, comments):
+
+  # we are looking for the following structure: method -> compound statement -> comment, i.e. we
+  # need to extract the first comment in the compound statement composing the method
+
+  in_compound_stmt = False
+  expect_comment = False
+  emit_comment = False
+
+  comment = []
+  comment_function = cursor.spelling or cursor.displayname
+  comment_line_start = -1
+  comment_line_end = -1
+  comment_col_start = -1
+  comment_col_end = -1
+  comment_indent = -1
+
+  for token in cursor.get_tokens():
+
+    if token.cursor.kind == clang.cindex.CursorKind.COMPOUND_STMT:
+      if not in_compound_stmt:
+        in_compound_stmt = True
+        expect_comment = True
+        comment_line_end = -1
+    else:
       if in_compound_stmt:
+        in_compound_stmt = False
+        emit_comment = True
 
-        if expect_comment:
+    # tkind = str(token.kind)[str(token.kind).index('.')+1:]
+    # ckind = str(token.cursor.kind)[str(token.cursor.kind).index('.')+1:]
 
-          extent = token.extent
-          line_start = extent.start.line
-          line_end = extent.end.line
+    if in_compound_stmt:
 
-          if token.kind == clang.cindex.TokenKind.PUNCTUATION and token.spelling == '{':
-            pass
+      if expect_comment:
 
-          elif token.kind == clang.cindex.TokenKind.COMMENT and (comment_line_end == -1 or (line_start == comment_line_end+1 and line_end-line_start == 0)):
-            comment_line_end = line_end
+        extent = token.extent
+        line_start = extent.start.line
+        line_end = extent.end.line
 
-            if comment_line_start == -1:
-              comment_line_start = line_start
-            comment.extend( token.spelling.split('\n') )
+        if token.kind == clang.cindex.TokenKind.PUNCTUATION and token.spelling == '{':
+          pass
 
-            # multiline comments are parsed in one go, therefore don't expect subsequent comments
-            if line_end - line_start > 0:
-              emit_comment = True
-              expect_comment = False
+        elif token.kind == clang.cindex.TokenKind.COMMENT and (comment_line_end == -1 or (line_start == comment_line_end+1 and line_end-line_start == 0)):
+          comment_line_end = line_end
+          comment_col_end = extent.end.column
 
-          else:
+          if comment_indent == -1 or (extent.start.column-1) < comment_indent:
+            comment_indent = extent.start.column-1
+
+          if comment_line_start == -1:
+            comment_line_start = line_start
+            comment_col_start = extent.start.column
+          comment.extend( token.spelling.split('\n') )
+
+          # multiline comments are parsed in one go, therefore don't expect subsequent comments
+          if line_end - line_start > 0:
             emit_comment = True
             expect_comment = False
 
-      if emit_comment:
+        else:
+          emit_comment = True
+          expect_comment = False
+
+    if emit_comment:
 
-        comment = refactor_comment( comment )
+      comment = refactor_comment( comment )
 
-        if len(comment) > 0:
-          logging.debug("Comment found for function %s" % Colt(comment_function).magenta())
-          comments.append( Comment(comment, comment_line_start, comment_line_end, comment_function) )
+      if len(comment) > 0:
+        logging.debug("Comment found for function %s" % Colt(comment_function).magenta())
+        comments.append( Comment(comment, comment_line_start, comment_col_start, comment_line_end, comment_col_end, comment_indent, comment_function) )
+
+      comment = []
+      comment_line_start = -1
+      comment_line_end = -1
+      comment_col_start = -1
+      comment_col_end = -1
+      comment_indent = -1
+
+      emit_comment = False
+      break
 
-        comment = []
-        comment_line_start = -1
-        comment_line_end = -1
 
-        emit_comment = False
+## Parses comments to class data members.
+#
+#  @param cursor   Current libclang parser cursor
+#  @param comments Array of comments: new ones will be appended there
+def comment_datamember(cursor, comments):
+
+  # Note: libclang 3.5 seems to have problems parsing a certain type of FIELD_DECL, so we revert
+  # to a partial manual parsing. When parsing fails, the cursor's "extent" is not set properly,
+  # returning a line range 0-0. We therefore make the not-so-absurd assumption that the datamember
+  # definition is fully on one line, and we take the line number from cursor.location.
+
+  line_num = cursor.location.line
+  raw = None
+  prev = None
+  found = False
+
+  # Huge overkill
+  with open(str(cursor.location.file)) as fp:
+    cur_line = 0
+    for raw in fp:
+      cur_line = cur_line + 1
+      if cur_line == line_num:
+        found = True
         break
+      prev = raw
+
+  assert found, 'A line that should exist was not found in file' % cursor.location.file
+
+  recomm = r'(//(!)|///?)(\[(.*?)\])?<?\s*(.*?)\s*$'
+  recomm_doxyary = r'^\s*///\s*(.*?)\s*$'
+
+  mcomm = re.search(recomm, raw)
+  if mcomm:
+    member_name = cursor.spelling;
+    is_transient = mcomm.group(2) is not None
+    array_size = mcomm.group(4)
+    text = mcomm.group(5)
+
+    col_num = mcomm.start()+1;
+
+    if array_size is not None and prev is not None:
+      # ROOT arrays with comments already converted to Doxygen have the member description on the
+      # previous line
+      mcomm_doxyary = re.search(recomm_doxyary, prev)
+      if mcomm_doxyary:
+        text = mcomm_doxyary.group(1)
+        comments.append(RemoveComment(line_num-1, line_num-1))
+
+    logging.debug('Comment found for member %s' % Colt(member_name).magenta())
+
+    comments.append( MemberComment(
+      text,
+      is_transient,
+      array_size,
+      line_num,
+      col_num,
+      member_name ))
 
   else:
+    assert False, 'Regular expression does not match member comment'
+
+
+## Traverse the AST recursively starting from the current cursor.
+#
+#  @param cursor    A Clang parser cursor
+#  @param filename  Name of the current file
+#  @param comments  Array of comments: new ones will be appended there
+#  @param recursion Current recursion depth
+def traverse_ast(cursor, filename, comments, recursion=0):
 
-    logging.debug( "%s%s(%s)" % (indent, kind, text) )
+  # 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:
+    logging.debug("Skipping processing of included %s" % cursor.location.file)
+    return
+
+  text = cursor.spelling or cursor.displayname
+  kind = str(cursor.kind)[str(cursor.kind).index('.')+1:]
+
+  indent = ''
+  for i in range(0, recursion):
+    indent = indent + '  '
+
+  if cursor.kind == clang.cindex.CursorKind.CXX_METHOD or cursor.kind == clang.cindex.CursorKind.CONSTRUCTOR or cursor.kind == clang.cindex.CursorKind.DESTRUCTOR:
+
+    # 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 cursor.kind == clang.cindex.CursorKind.FIELD_DECL:
+
+    # 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)
+
+  else:
+
+    logging.debug( "%5d %s%s(%s)" % (cursor.location.line, indent, kind, text) )
 
   for child_cursor in cursor.get_children():
-    traverse_ast(child_cursor, comments, recursion+1)
+    traverse_ast(child_cursor, filename, comments, recursion+1)
 
 
 ## Remove garbage from comments and convert special tags from THtml to Doxygen.
@@ -173,6 +313,7 @@ def traverse_ast(cursor, comments, recursion=0):
 def refactor_comment(comment):
 
   recomm = r'^(/{2,}|/\*)?\s*(.*?)\s*((/{2,})?\s*|\*/)$'
+  regarbage = r'^[\s*=-_#]+$'
 
   new_comment = []
   insert_blank = False
@@ -181,7 +322,8 @@ def refactor_comment(comment):
     mcomm = re.search( recomm, line_comment )
     if mcomm:
       new_line_comment = mcomm.group(2)
-      if new_line_comment == '':
+      mgarbage = re.search( regarbage, new_line_comment )
+      if new_line_comment == '' or mgarbage is not None:
         insert_blank = True
       else:
         if insert_blank and not wait_first_non_blank:
@@ -195,13 +337,113 @@ def refactor_comment(comment):
   return new_comment
 
 
+## Rewrites all comments from the given file handler.
+#
+#  @param fhin     The file handler to read from
+#  @param fhout    The file handler to write to
+#  @param comments Array of comments
+def rewrite_comments(fhin, fhout, comments):
+
+  line_num = 0
+  in_comment = False
+  skip_empty = False
+  comm = None
+  prev_comm = None
+
+  rindent = r'^(\s*)'
+
+  for line in fhin:
+
+    line_num = line_num + 1
+
+    # Find current comment
+    prev_comm = comm
+    comm = None
+    for c in comments:
+      if c.has_comment(line_num):
+        comm = c
+
+    if comm:
+
+      if isinstance(comm, MemberComment):
+        non_comment = line[ 0:comm.first_col-1 ]
+
+        if comm.array_size is not None:
+
+          mindent = re.search(rindent, line)
+          if comm.is_transient:
+            tt = '!'
+          else:
+            tt = ''
+
+          # Special case: we need multiple lines not to confuse ROOT's C++ parser
+          fhout.write('%s/// %s\n%s//%s[%s]\n' % (
+            mindent.group(1),
+            comm.lines[0],
+            non_comment,
+            tt,
+            comm.array_size
+          ))
+
+        else:
+
+          if comm.is_transient:
+            tt = '!'
+          else:
+            tt = '/'
+
+          fhout.write('%s//%s< %s\n' % (
+            non_comment,
+            tt,
+            comm.lines[0]
+          ))
+
+      elif isinstance(comm, RemoveComment):
+        # Do nothing: just skip line
+        pass
+
+      elif prev_comm is None:
+        # Beginning of a new comment block of type Comment
+        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' )
+
+    else:
+
+      if in_comment:
+
+        # We have just exited a comment block of type Comment
+        in_comment = False
+
+        # Dump revamped comment, if applicable
+        text_indent = ''
+        for i in range(0,prev_comm.indent):
+          text_indent = text_indent + ' '
+
+        for lc in prev_comm.lines:
+          fhout.write( "%s/// %s\n" % (text_indent, lc) );
+        fhout.write('\n')
+        skip_empty = True
+
+      line_out = line.rstrip('\n')
+      if skip_empty:
+        skip_empty = False
+        if line_out.strip() != '':
+          fhout.write( line_out + '\n' )
+      else:
+        fhout.write( line_out + '\n' )
+
+
 ## The main function.
 #
 #  Return value is the executable's return value.
 def main(argv):
 
   # Setup logging on stderr
-  log_level = logging.WARNING
+  log_level = logging.INFO
   logging.basicConfig(
     level=log_level,
     format='%(levelname)-8s %(funcName)-20s %(message)s',
@@ -209,8 +451,9 @@ def main(argv):
   )
 
   # Parse command-line options
+  output_on_stdout = False
   try:
-    opts, args = getopt.getopt( argv, 'd', [ 'debug=' ] )
+    opts, args = getopt.getopt( argv, 'od', [ 'debug=', 'stdout' ] )
     for o, a in opts:
       if o == '--debug':
         log_level = getattr( logging, a.upper(), None )
@@ -218,6 +461,9 @@ def main(argv):
           raise getopt.GetoptError('log level must be one of: DEBUG, INFO, WARNING, ERROR, CRITICAL')
       elif o == '-d':
         log_level = logging.DEBUG
+      elif o == '-o' or o == '--stdout':
+        logging.debug('Output on stdout instead of replacing original files')
+        output_on_stdout = True
       else:
         assert False, 'Unhandled argument'
   except getopt.GetoptError as e:
@@ -252,14 +498,58 @@ def main(argv):
     translation_unit = index.parse(fn, args=['-x', 'c++'])
 
     comments = []
-    traverse_ast( translation_unit.cursor, comments )
+    traverse_ast( translation_unit.cursor, fn, comments )
     for c in comments:
-      logging.info("Comment found for %s:" % Colt(c.func).magenta())
-      for l in c.lines:
-        logging.warning(
-          Colt("[%d:%d] " % (c.first_line, c.last_line)).green() +
-          "{%s}" % Colt(l).cyan()
-        )
+
+      logging.debug("Comment found for entity %s:" % Colt(c.func).magenta())
+
+      if isinstance(c, MemberComment):
+
+        if c.is_transient:
+          transient_text = Colt('transient ').yellow()
+        else:
+          transient_text = ''
+
+        if c.array_size is not None:
+          array_text = Colt('arraysize=%s ' % c.array_size).yellow()
+        else:
+          array_text = ''
+
+        logging.debug(
+          "%s %s%s{%s}" % ( \
+            Colt("[%d,%d]" % (c.first_line, c.first_col)).green(),
+            transient_text,
+            array_text,
+            Colt(c.lines[0]).cyan()
+        ))
+
+      elif isinstance(c, RemoveComment):
+
+        logging.debug( Colt('[%d,%d]' % (c.first_line, c.last_line)).green() )
+
+      else:
+        for l in c.lines:
+          logging.debug(
+            Colt("[%d,%d:%d,%d] " % (c.first_line, c.first_col, c.last_line, c.last_col)).green() +
+            "{%s}" % Colt(l).cyan()
+          )
+
+    try:
+
+      if output_on_stdout:
+        with open(fn, 'r') as fhin:
+          rewrite_comments( fhin, sys.stdout, comments )
+      else:
+        fn_back = fn + '.thtml2doxy_backup'
+        os.rename( fn, fn_back )
+
+        with open(fn_back, 'r') as fhin, open(fn, 'w') as fhout:
+          rewrite_comments( fhin, fhout, comments )
+
+        os.remove( fn_back )
+        logging.info("File %s converted to Doxygen: check differences before committing!" % Colt(fn).magenta())
+    except (IOError,OSError) as e:
+      logging.error('File operation failed: %s' % e)
 
   return 0