]> git.uio.no Git - u/mrichter/AliRoot.git/blame - doxygen/thtml2doxy.py
doxy: support static member declarations
[u/mrichter/AliRoot.git] / doxygen / thtml2doxy.py
CommitLineData
f329fa92 1#!/usr/bin/env python
2
06ccae0f 3## @package thtml2doxy_clang
4# Translates THtml C++ comments to Doxygen using libclang as parser.
5#
6# This code relies on Python bindings for libclang: libclang's interface is pretty unstable, and
7# its Python bindings are unstable as well.
8#
9# AST (Abstract Source Tree) traversal is performed entirely using libclang used as a C++ parser,
10# instead of attempting to write a parser ourselves.
11#
12# This code (expecially AST traversal) was inspired by:
13#
14# - [Implementing a code generator with libclang](http://szelei.me/code-generator/)
15# (this refers to API calls used here)
16# - [Parsing C++ in Python with Clang](http://eli.thegreenplace.net/2011/07/03/parsing-c-in-python-with-clang)
17# (outdated, API calls described there do not work anymore, but useful to understand some basic
18# concepts)
19#
20# Usage:
21#
22# `thtml2doxy_clang [--stdout|-o] [-d] [--debug=DEBUG_LEVEL] file1 [file2 [file3...]]`
23#
24# Parameters:
25#
26# - `--stdout|-o`: output all on standard output instead of writing files in place
27# - `-d`: enable debug mode (very verbose output)
28# - `--debug=DEBUG_LEVEL`: set debug level to one of `DEBUG`, `INFO`, `WARNING`, `ERROR`,
29# `CRITICAL`
30#
31# @author Dario Berzano, CERN
32# @date 2014-12-05
33
34
f329fa92 35import sys
36import os
37import re
06ccae0f 38import logging
39import getopt
ee1793c7 40import hashlib
06ccae0f 41import clang.cindex
42
43
44## Brain-dead color output for terminal.
45class Colt(str):
46
47 def red(self):
48 return self.color('\033[31m')
49
50 def green(self):
51 return self.color('\033[32m')
52
53 def yellow(self):
54 return self.color('\033[33m')
55
56 def blue(self):
57 return self.color('\033[34m')
58
59 def magenta(self):
60 return self.color('\033[35m')
61
62 def cyan(self):
63 return self.color('\033[36m')
64
65 def color(self, c):
66 return c + self + '\033[m'
67
f329fa92 68
06ccae0f 69## Comment.
70class Comment:
71
72 def __init__(self, lines, first_line, first_col, last_line, last_col, indent, func):
3896b0ea 73 assert first_line > 0 and last_line >= first_line, 'Wrong line numbers'
06ccae0f 74 self.lines = lines
75 self.first_line = first_line
76 self.first_col = first_col
77 self.last_line = last_line
78 self.last_col = last_col
79 self.indent = indent
80 self.func = func
81
82 def has_comment(self, line):
83 return line >= self.first_line and line <= self.last_line
84
85 def __str__(self):
86 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)
87
88
89## A data member comment.
90class MemberComment:
91
21689d7a 92 def __init__(self, text, comment_flag, array_size, first_line, first_col, func):
3896b0ea 93 assert first_line > 0, 'Wrong line number'
21689d7a 94 assert comment_flag is None or comment_flag == '!' or comment_flag in [ '!', '||', '->' ]
06ccae0f 95 self.lines = [ text ]
21689d7a 96 self.comment_flag = comment_flag
06ccae0f 97 self.array_size = array_size
98 self.first_line = first_line
99 self.first_col = first_col
100 self.func = func
101
21689d7a 102 def is_transient(self):
103 return self.comment_flag == '!'
104
105 def is_dontsplit(self):
106 return self.comment_flag == '||'
107
108 def is_ptr(self):
109 return self.comment_flag == '->'
110
06ccae0f 111 def has_comment(self, line):
112 return line == self.first_line
113
114 def __str__(self):
115
21689d7a 116 if self.is_transient():
06ccae0f 117 tt = '!transient! '
21689d7a 118 elif self.is_dontsplit():
119 tt = '!dontsplit! '
120 elif self.is_ptr():
121 tt = '!ptr! '
06ccae0f 122 else:
123 tt = ''
124
125 if self.array_size is not None:
126 ars = '[%s] ' % self.array_size
127 else:
128 ars = ''
129
130 return "<MemberComment for %s: [%d,%d] %s%s%s>" % (self.func, self.first_line, self.first_col, tt, ars, self.lines[0])
131
132
133## A dummy comment that removes comment lines.
134class RemoveComment(Comment):
135
136 def __init__(self, first_line, last_line):
3896b0ea 137 assert first_line > 0 and last_line >= first_line, 'Wrong line numbers'
06ccae0f 138 self.first_line = first_line
139 self.last_line = last_line
140 self.func = '<remove>'
141
142 def __str__(self):
143 return "<RemoveComment: [%d,%d]>" % (self.first_line, self.last_line)
144
145
146## Parses method comments.
f329fa92 147#
06ccae0f 148# @param cursor Current libclang parser cursor
149# @param comments Array of comments: new ones will be appended there
150def comment_method(cursor, comments):
151
152 # we are looking for the following structure: method -> compound statement -> comment, i.e. we
153 # need to extract the first comment in the compound statement composing the method
154
155 in_compound_stmt = False
156 expect_comment = False
157 emit_comment = False
158
159 comment = []
160 comment_function = cursor.spelling or cursor.displayname
161 comment_line_start = -1
162 comment_line_end = -1
163 comment_col_start = -1
164 comment_col_end = -1
165 comment_indent = -1
166
167 for token in cursor.get_tokens():
168
169 if token.cursor.kind == clang.cindex.CursorKind.COMPOUND_STMT:
170 if not in_compound_stmt:
171 in_compound_stmt = True
172 expect_comment = True
173 comment_line_end = -1
174 else:
175 if in_compound_stmt:
176 in_compound_stmt = False
177 emit_comment = True
178
179 # tkind = str(token.kind)[str(token.kind).index('.')+1:]
180 # ckind = str(token.cursor.kind)[str(token.cursor.kind).index('.')+1:]
181
182 if in_compound_stmt:
183
184 if expect_comment:
185
186 extent = token.extent
187 line_start = extent.start.line
188 line_end = extent.end.line
189
190 if token.kind == clang.cindex.TokenKind.PUNCTUATION and token.spelling == '{':
191 pass
192
193 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)):
194 comment_line_end = line_end
195 comment_col_end = extent.end.column
196
197 if comment_indent == -1 or (extent.start.column-1) < comment_indent:
198 comment_indent = extent.start.column-1
199
200 if comment_line_start == -1:
201 comment_line_start = line_start
202 comment_col_start = extent.start.column
203 comment.extend( token.spelling.split('\n') )
204
205 # multiline comments are parsed in one go, therefore don't expect subsequent comments
206 if line_end - line_start > 0:
207 emit_comment = True
208 expect_comment = False
209
210 else:
211 emit_comment = True
212 expect_comment = False
213
214 if emit_comment:
215
6f0e3bf3 216 if comment_line_start > 0:
06ccae0f 217
ee1793c7 218 comment = refactor_comment( comment, infilename=str(cursor.location.file) )
6f0e3bf3 219
220 if len(comment) > 0:
221 logging.debug("Comment found for function %s" % Colt(comment_function).magenta())
222 comments.append( Comment(comment, comment_line_start, comment_col_start, comment_line_end, comment_col_end, comment_indent, comment_function) )
223 else:
5dccb084 224 logging.debug('Empty comment found for function %s: collapsing' % Colt(comment_function).magenta())
225 comments.append( Comment([''], comment_line_start, comment_col_start, comment_line_end, comment_col_end, comment_indent, comment_function) )
226 #comments.append(RemoveComment(comment_line_start, comment_line_end))
6f0e3bf3 227
228 else:
229 logging.warning('No comment found for function %s' % Colt(comment_function).magenta())
06ccae0f 230
231 comment = []
232 comment_line_start = -1
233 comment_line_end = -1
234 comment_col_start = -1
235 comment_col_end = -1
236 comment_indent = -1
237
238 emit_comment = False
239 break
240
241
242## Parses comments to class data members.
243#
244# @param cursor Current libclang parser cursor
245# @param comments Array of comments: new ones will be appended there
246def comment_datamember(cursor, comments):
247
248 # Note: libclang 3.5 seems to have problems parsing a certain type of FIELD_DECL, so we revert
249 # to a partial manual parsing. When parsing fails, the cursor's "extent" is not set properly,
250 # returning a line range 0-0. We therefore make the not-so-absurd assumption that the datamember
251 # definition is fully on one line, and we take the line number from cursor.location.
252
253 line_num = cursor.location.line
254 raw = None
255 prev = None
256 found = False
257
21689d7a 258 # Huge overkill: current line saved in "raw", previous in "prev"
06ccae0f 259 with open(str(cursor.location.file)) as fp:
260 cur_line = 0
261 for raw in fp:
262 cur_line = cur_line + 1
263 if cur_line == line_num:
264 found = True
265 break
266 prev = raw
267
268 assert found, 'A line that should exist was not found in file' % cursor.location.file
269
21689d7a 270 recomm = r'(//(!|\|\||->)|///?)(\[([0-9,]+)\])?<?\s*(.*?)\s*$'
271 recomm_prevline = r'^\s*///\s*(.*?)\s*$'
06ccae0f 272
273 mcomm = re.search(recomm, raw)
274 if mcomm:
54203c62 275 # If it does not match, we do not have a comment
06ccae0f 276 member_name = cursor.spelling;
21689d7a 277 comment_flag = mcomm.group(2)
06ccae0f 278 array_size = mcomm.group(4)
279 text = mcomm.group(5)
280
281 col_num = mcomm.start()+1;
282
283 if array_size is not None and prev is not None:
284 # ROOT arrays with comments already converted to Doxygen have the member description on the
285 # previous line
21689d7a 286 mcomm_prevline = re.search(recomm_prevline, prev)
287 if mcomm_prevline:
288 text = mcomm_prevline.group(1)
06ccae0f 289 comments.append(RemoveComment(line_num-1, line_num-1))
290
291 logging.debug('Comment found for member %s' % Colt(member_name).magenta())
292
293 comments.append( MemberComment(
294 text,
21689d7a 295 comment_flag,
06ccae0f 296 array_size,
297 line_num,
298 col_num,
299 member_name ))
300
06ccae0f 301
302## Parses class description (beginning of file).
303#
304# The clang parser does not work in this case so we do it manually, but it is very simple: we keep
305# the first consecutive sequence of single-line comments (//) we find - provided that it occurs
306# before any other comment found so far in the file (the comments array is inspected to ensure
307# this).
f329fa92 308#
06ccae0f 309# Multi-line comments (/* ... */) are not considered as they are commonly used to display
310# copyright notice.
f329fa92 311#
06ccae0f 312# @param filename Name of the current file
313# @param comments Array of comments: new ones will be appended there
314def comment_classdesc(filename, comments):
315
316 recomm = r'^\s*///?(\s*.*?)\s*/*\s*$'
317
318 reclass_doxy = r'(?i)^\s*\\class:?\s*(.*?)\s*$'
319 class_name_doxy = None
320
321 reauthor = r'(?i)^\s*\\?authors?:?\s*(.*?)\s*(,?\s*([0-9./-]+))?\s*$'
322 redate = r'(?i)^\s*\\?date:?\s*([0-9./-]+)\s*$'
323 author = None
324 date = None
325
326 comment_lines = []
327
328 start_line = -1
329 end_line = -1
330
331 line_num = 0
332
333 with open(filename, 'r') as fp:
334
335 for raw in fp:
336
337 line_num = line_num + 1
338
424eef90 339 if raw.strip() == '' and start_line > 0:
06ccae0f 340 # Skip empty lines
06ccae0f 341 continue
342
343 stripped = strip_html(raw)
344 mcomm = re.search(recomm, stripped)
345 if mcomm:
346
424eef90 347 if start_line == -1:
06ccae0f 348
349 # First line. Check that we do not overlap with other comments
350 comment_overlaps = False
351 for c in comments:
352 if c.has_comment(line_num):
353 comment_overlaps = True
354 break
355
356 if comment_overlaps:
357 # No need to look for other comments
358 break
359
360 start_line = line_num
361
dde83b2b 362 end_line = line_num
06ccae0f 363 append = True
364
365 mclass_doxy = re.search(reclass_doxy, mcomm.group(1))
366 if mclass_doxy:
367 class_name_doxy = mclass_doxy.group(1)
368 append = False
369 else:
370 mauthor = re.search(reauthor, mcomm.group(1))
371 if mauthor:
372 author = mauthor.group(1)
373 if date is None:
374 # Date specified in the standalone \date field has priority
9b8614a7 375 date = mauthor.group(3)
06ccae0f 376 append = False
377 else:
378 mdate = re.search(redate, mcomm.group(1))
379 if mdate:
380 date = mdate.group(1)
381 append = False
382
383 if append:
384 comment_lines.append( mcomm.group(1) )
385
386 else:
424eef90 387 if start_line > 0:
06ccae0f 388 break
389
390 if class_name_doxy is None:
391
392 # No \class specified: guess it from file name
393 reclass = r'^(.*/)?(.*?)(\..*)?$'
394 mclass = re.search( reclass, filename )
395 if mclass:
396 class_name_doxy = mclass.group(2)
397 else:
398 assert False, 'Regexp unable to extract classname from file'
399
424eef90 400 if start_line > 0:
06ccae0f 401
424eef90 402 # Prepend \class specifier (and an empty line)
403 comment_lines[:0] = [ '\\class ' + class_name_doxy ]
06ccae0f 404
424eef90 405 # Append author and date if they exist
406 comment_lines.append('')
06ccae0f 407
424eef90 408 if author is not None:
409 comment_lines.append( '\\author ' + author )
06ccae0f 410
424eef90 411 if date is not None:
412 comment_lines.append( '\\date ' + date )
413
ee1793c7 414 comment_lines = refactor_comment(comment_lines, do_strip_html=False, infilename=filename)
424eef90 415 logging.debug('Comment found for class %s' % Colt(class_name_doxy).magenta())
416 comments.append(Comment(
417 comment_lines,
418 start_line, 1, end_line, 1,
419 0, class_name_doxy
420 ))
421
422 else:
423
424 logging.warning('No comment found for class %s' % Colt(class_name_doxy).magenta())
06ccae0f 425
426
427## Traverse the AST recursively starting from the current cursor.
428#
429# @param cursor A Clang parser cursor
430# @param filename Name of the current file
431# @param comments Array of comments: new ones will be appended there
432# @param recursion Current recursion depth
433def traverse_ast(cursor, filename, comments, recursion=0):
434
435 # libclang traverses included files as well: we do not want this behavior
436 if cursor.location.file is not None and str(cursor.location.file) != filename:
437 logging.debug("Skipping processing of included %s" % cursor.location.file)
438 return
439
440 text = cursor.spelling or cursor.displayname
441 kind = str(cursor.kind)[str(cursor.kind).index('.')+1:]
442
443 indent = ''
444 for i in range(0, recursion):
445 indent = indent + ' '
446
2d3fd2ec 447 if cursor.kind in [ clang.cindex.CursorKind.CXX_METHOD, clang.cindex.CursorKind.CONSTRUCTOR,
448 clang.cindex.CursorKind.DESTRUCTOR ]:
06ccae0f 449
450 # cursor ran into a C++ method
451 logging.debug( "%5d %s%s(%s)" % (cursor.location.line, indent, Colt(kind).magenta(), Colt(text).blue()) )
452 comment_method(cursor, comments)
453
2d3fd2ec 454 elif cursor.kind in [ clang.cindex.CursorKind.FIELD_DECL, clang.cindex.CursorKind.VAR_DECL ]:
06ccae0f 455
456 # cursor ran into a data member declaration
457 logging.debug( "%5d %s%s(%s)" % (cursor.location.line, indent, Colt(kind).magenta(), Colt(text).blue()) )
458 comment_datamember(cursor, comments)
459
460 else:
461
462 logging.debug( "%5d %s%s(%s)" % (cursor.location.line, indent, kind, text) )
463
464 for child_cursor in cursor.get_children():
465 traverse_ast(child_cursor, filename, comments, recursion+1)
466
467 if recursion == 0:
468 comment_classdesc(filename, comments)
469
470
471## Strip some HTML tags from the given string. Returns clean string.
472#
473# @param s Input string
474def strip_html(s):
798167cb 475 rehtml = r'(?i)</?(P|BR)/?>'
06ccae0f 476 return re.sub(rehtml, '', s)
477
478
479## Remove garbage from comments and convert special tags from THtml to Doxygen.
480#
481# @param comment An array containing the lines of the original comment
ee1793c7 482def refactor_comment(comment, do_strip_html=True, infilename=None):
06ccae0f 483
484 recomm = r'^(/{2,}|/\*)? ?(\s*.*?)\s*((/{2,})?\s*|\*/)$'
485 regarbage = r'^(?i)\s*([\s*=-_#]+|(Begin|End)_Html)\s*$'
486
03a6b7aa 487 # Support for LaTeX blocks spanning on multiple lines
488 relatex = r'(?i)^((.*?)\s+)?(BEGIN|END)_LATEX([.,;:\s]+.*)?$'
489 in_latex = False
490 latex_block = False
491
492 # Support for LaTeX blocks on a single line
9db428b7 493 reinline_latex = r'(?i)(.*)BEGIN_LATEX\s+(.*?)\s+END_LATEX(.*)$'
494
35b193b4 495 # Match <pre> (to turn it into the ~~~ Markdown syntax)
496 reblock = r'(?i)^(\s*)</?PRE>\s*$'
497
ee1793c7 498 # Macro blocks for pictures generation
499 in_macro = False
500 current_macro = []
501 remacro = r'(?i)^\s*(BEGIN|END)_MACRO(\((.*?)\))?\s*$'
502
06ccae0f 503 new_comment = []
504 insert_blank = False
505 wait_first_non_blank = True
506 for line_comment in comment:
507
ee1793c7 508 # Check if we are in a macro block
509 mmacro = re.search(remacro, line_comment)
510 if mmacro:
511 if in_macro:
512 in_macro = False
513
514 # Dump macro
515 outimg = write_macro(infilename, current_macro) + '.png'
516 current_macro = []
517
518 # Insert image
519 new_comment.append( '![Picture from ROOT macro](%s)' % (outimg) )
520
521 logging.debug( 'Found macro for generating image %s' % Colt(outimg).magenta() )
522
523 else:
524 in_macro = True
525
526 continue
527 elif in_macro:
528 current_macro.append( line_comment )
529 continue
530
06ccae0f 531 # Strip some HTML tags
532 if do_strip_html:
533 line_comment = strip_html(line_comment)
534
535 mcomm = re.search( recomm, line_comment )
536 if mcomm:
537 new_line_comment = mcomm.group(2)
538 mgarbage = re.search( regarbage, new_line_comment )
539
540 if new_line_comment == '' or mgarbage is not None:
541 insert_blank = True
542 else:
543 if insert_blank and not wait_first_non_blank:
544 new_comment.append('')
545 insert_blank = False
546 wait_first_non_blank = False
9db428b7 547
548 # Postprocessing: LaTeX formulas in ROOT format
549 # Marked by BEGIN_LATEX ... END_LATEX and they use # in place of \
550 # There can be several ROOT LaTeX forumlas per line
551 while True:
552 minline_latex = re.search( reinline_latex, new_line_comment )
553 if minline_latex:
554 new_line_comment = '%s\\f$%s\\f$%s' % \
555 ( minline_latex.group(1), minline_latex.group(2).replace('#', '\\'),
556 minline_latex.group(3) )
557 else:
558 break
559
03a6b7aa 560 # ROOT LaTeX: do we have a Begin/End_LaTeX block?
561 # Note: the presence of LaTeX "closures" does not exclude the possibility to have a begin
562 # block here left without a corresponding ending block
563 mlatex = re.search( relatex, new_line_comment )
564 if mlatex:
565
566 # before and after parts have been already stripped
567 l_before = mlatex.group(2)
568 l_after = mlatex.group(4)
569 is_begin = mlatex.group(3).upper() == 'BEGIN' # if not, END
570
571 if l_before is None:
572 l_before = ''
573 if l_after is None:
574 l_after = ''
575
576 if is_begin:
577
578 # Begin of LaTeX part
579
580 in_latex = True
581 if l_before == '' and l_after == '':
582
583 # Opening tag alone: mark the beginning of a block: \f[ ... \f]
584 latex_block = True
585 new_comment.append( '\\f[' )
586
587 else:
588 # Mark the beginning of inline: \f$ ... \f$
589 latex_block = False
590 new_comment.append(
591 '%s \\f$%s' % ( l_before, l_after.replace('#', '\\') )
592 )
593
594 else:
595
596 # End of LaTeX part
597 in_latex = False
598
599 if latex_block:
600
601 # Closing a LaTeX block
602 if l_before != '':
603 new_comment.append( l_before.replace('#', '\\') )
604 new_comment.append( '\\f]' )
605 if l_after != '':
606 new_comment.append( l_after )
607
608 else:
609
610 # Closing a LaTeX inline
611 new_comment.append(
612 '%s\\f$%s' % ( l_before.replace('#', '\\'), l_after )
613 )
614
615 # Prevent appending lines (we have already done that)
616 new_line_comment = None
617
35b193b4 618 # If we are not in a LaTeX block, look for <pre> tags and transform them into Doxygen code
619 # blocks (using ~~~ ... ~~~). Only <pre> tags on a single line are supported
620 if new_line_comment is not None and not in_latex:
621
622 mblock = re.search( reblock, new_line_comment )
623 if mblock:
624 new_comment.append( mblock.group(1)+'~~~' )
625 new_line_comment = None
626
03a6b7aa 627 if new_line_comment is not None:
628 if in_latex:
629 new_line_comment = new_line_comment.replace('#', '\\')
630 new_comment.append( new_line_comment )
06ccae0f 631
632 else:
633 assert False, 'Comment regexp does not match'
634
635 return new_comment
636
637
ee1793c7 638## Dumps an image-generating macro to the correct place. Returns a string with the image path,
639# without the extension.
640#
641# @param infilename File name of the source file
642# @param macro_lines Array of macro lines
643def write_macro(infilename, macro_lines):
644
645 # Calculate hash
646 digh = hashlib.sha1()
647 for l in macro_lines:
648 digh.update(l)
649 digh.update('\n')
650 short_digest = digh.hexdigest()[0:7]
651
652 outdir = '%s/imgdoc' % os.path.dirname(infilename)
653 outprefix = '%s/%s_%s' % (
654 outdir,
655 os.path.basename(infilename).replace('.', '_'),
656 short_digest
657 )
658 outmacro = '%s.C' % outprefix
659
660 # Make directory
661 if not os.path.isdir(outdir):
662 # do not catch: let everything die on error
663 logging.debug('Creating directory %s' % Colt(outdir).magenta())
664 os.mkdir(outdir)
665
666 # Create file (do not catch errors either)
667 with open(outmacro, 'w') as omfp:
668 logging.debug('Writing macro %s' % Colt(outmacro).magenta())
669 for l in macro_lines:
670 omfp.write(l)
671 omfp.write('\n')
672
673 return outprefix
674
675
06ccae0f 676## Rewrites all comments from the given file handler.
677#
678# @param fhin The file handler to read from
679# @param fhout The file handler to write to
680# @param comments Array of comments
681def rewrite_comments(fhin, fhout, comments):
682
683 line_num = 0
684 in_comment = False
685 skip_empty = False
686 comm = None
687 prev_comm = None
688
689 rindent = r'^(\s*)'
690
691 for line in fhin:
692
693 line_num = line_num + 1
694
695 # Find current comment
696 prev_comm = comm
697 comm = None
698 for c in comments:
699 if c.has_comment(line_num):
700 comm = c
701
702 if comm:
703
704 if isinstance(comm, MemberComment):
705 non_comment = line[ 0:comm.first_col-1 ]
706
21689d7a 707 if comm.array_size is not None or comm.is_dontsplit() or comm.is_ptr():
06ccae0f 708
21689d7a 709 # This is a special case: comment will be split in two lines: one before the comment for
710 # Doxygen as "member description", and the other right after the comment on the same line
711 # to be parsed by ROOT's C++ parser
712
713 # Keep indent on the generated line of comment before member definition
06ccae0f 714 mindent = re.search(rindent, line)
21689d7a 715
716 # Get correct comment flag, if any
717 if comm.comment_flag is not None:
718 cflag = comm.comment_flag
719 else:
720 cflag = ''
721
722 # Get correct array size, if any
723 if comm.array_size is not None:
724 asize = '[%s]' % comm.array_size
06ccae0f 725 else:
21689d7a 726 asize = ''
06ccae0f 727
21689d7a 728 # Write on two lines
729 fhout.write('%s/// %s\n%s//%s%s\n' % (
06ccae0f 730 mindent.group(1),
731 comm.lines[0],
732 non_comment,
21689d7a 733 cflag,
734 asize
06ccae0f 735 ))
736
737 else:
738
21689d7a 739 # Single-line comments with the "transient" flag can be kept on one line in a way that
740 # they are correctly interpreted by both ROOT and Doxygen
741
742 if comm.is_transient():
06ccae0f 743 tt = '!'
744 else:
745 tt = '/'
746
747 fhout.write('%s//%s< %s\n' % (
748 non_comment,
749 tt,
750 comm.lines[0]
751 ))
752
753 elif isinstance(comm, RemoveComment):
754 # Do nothing: just skip line
755 pass
756
757 elif prev_comm is None:
758 # Beginning of a new comment block of type Comment
759 in_comment = True
760
761 # Extract the non-comment part and print it if it exists
762 non_comment = line[ 0:comm.first_col-1 ].rstrip()
763 if non_comment != '':
764 fhout.write( non_comment + '\n' )
765
766 else:
767
768 if in_comment:
769
770 # We have just exited a comment block of type Comment
771 in_comment = False
772
773 # Dump revamped comment, if applicable
774 text_indent = ''
2d3fd2ec 775
06ccae0f 776 for i in range(0,prev_comm.indent):
777 text_indent = text_indent + ' '
778
779 for lc in prev_comm.lines:
780 fhout.write( "%s/// %s\n" % (text_indent, lc) );
781 fhout.write('\n')
782 skip_empty = True
783
784 line_out = line.rstrip('\n')
785 if skip_empty:
786 skip_empty = False
787 if line_out.strip() != '':
788 fhout.write( line_out + '\n' )
789 else:
790 fhout.write( line_out + '\n' )
791
f329fa92 792
793## The main function.
794#
06ccae0f 795# Return value is the executable's return value.
f329fa92 796def main(argv):
797
06ccae0f 798 # Setup logging on stderr
799 log_level = logging.INFO
800 logging.basicConfig(
801 level=log_level,
802 format='%(levelname)-8s %(funcName)-20s %(message)s',
803 stream=sys.stderr
804 )
f329fa92 805
06ccae0f 806 # Parse command-line options
807 output_on_stdout = False
7afec95e 808 include_flags = []
06ccae0f 809 try:
7afec95e 810 opts, args = getopt.getopt( argv, 'odI:', [ 'debug=', 'stdout' ] )
06ccae0f 811 for o, a in opts:
812 if o == '--debug':
813 log_level = getattr( logging, a.upper(), None )
814 if not isinstance(log_level, int):
815 raise getopt.GetoptError('log level must be one of: DEBUG, INFO, WARNING, ERROR, CRITICAL')
816 elif o == '-d':
817 log_level = logging.DEBUG
818 elif o == '-o' or o == '--stdout':
06ccae0f 819 output_on_stdout = True
7afec95e 820 elif o == '-I':
821 if os.path.isdir(a):
822 include_flags.extend( [ '-I', a ] )
823 else:
824 logging.fatal('Include directory not found: %s' % Colt(a).magenta())
825 return 2
06ccae0f 826 else:
827 assert False, 'Unhandled argument'
828 except getopt.GetoptError as e:
829 logging.fatal('Invalid arguments: %s' % e)
830 return 1
f329fa92 831
06ccae0f 832 logging.getLogger('').setLevel(log_level)
f329fa92 833
06ccae0f 834 # Attempt to load libclang from a list of known locations
835 libclang_locations = [
836 '/usr/lib/llvm-3.5/lib/libclang.so.1',
837 '/usr/lib/libclang.so',
838 '/Library/Developer/CommandLineTools/usr/lib/libclang.dylib'
839 ]
840 libclang_found = False
f329fa92 841
06ccae0f 842 for lib in libclang_locations:
843 if os.path.isfile(lib):
844 clang.cindex.Config.set_library_file(lib)
845 libclang_found = True
846 break
f329fa92 847
06ccae0f 848 if not libclang_found:
849 logging.fatal('Cannot find libclang')
850 return 1
851
852 # Loop over all files
853 for fn in args:
854
855 logging.info('Input file: %s' % Colt(fn).magenta())
856 index = clang.cindex.Index.create()
7afec95e 857 clang_args = [ '-x', 'c++' ]
858 clang_args.extend( include_flags )
859 translation_unit = index.parse(fn, args=clang_args)
06ccae0f 860
861 comments = []
862 traverse_ast( translation_unit.cursor, fn, comments )
863 for c in comments:
864
865 logging.debug("Comment found for entity %s:" % Colt(c.func).magenta())
f329fa92 866
06ccae0f 867 if isinstance(c, MemberComment):
868
21689d7a 869 if c.is_transient():
870 flag_text = Colt('transient ').yellow()
871 elif c.is_dontsplit():
872 flag_text = Colt('dontsplit ').yellow()
873 elif c.is_ptr():
874 flag_text = Colt('ptr ').yellow()
06ccae0f 875 else:
21689d7a 876 flag_text = ''
06ccae0f 877
878 if c.array_size is not None:
879 array_text = Colt('arraysize=%s ' % c.array_size).yellow()
880 else:
881 array_text = ''
882
883 logging.debug(
884 "%s %s%s{%s}" % ( \
885 Colt("[%d,%d]" % (c.first_line, c.first_col)).green(),
21689d7a 886 flag_text,
06ccae0f 887 array_text,
888 Colt(c.lines[0]).cyan()
889 ))
890
891 elif isinstance(c, RemoveComment):
892
893 logging.debug( Colt('[%d,%d]' % (c.first_line, c.last_line)).green() )
894
895 else:
896 for l in c.lines:
897 logging.debug(
898 Colt("[%d,%d:%d,%d] " % (c.first_line, c.first_col, c.last_line, c.last_col)).green() +
899 "{%s}" % Colt(l).cyan()
900 )
f329fa92 901
902 try:
06ccae0f 903
904 if output_on_stdout:
905 with open(fn, 'r') as fhin:
906 rewrite_comments( fhin, sys.stdout, comments )
907 else:
908 fn_back = fn + '.thtml2doxy_backup'
909 os.rename( fn, fn_back )
910
911 with open(fn_back, 'r') as fhin, open(fn, 'w') as fhout:
912 rewrite_comments( fhin, fhout, comments )
913
914 os.remove( fn_back )
915 logging.info("File %s converted to Doxygen: check differences before committing!" % Colt(fn).magenta())
916 except (IOError,OSError) as e:
917 logging.error('File operation failed: %s' % e)
f329fa92 918
919 return 0
920
06ccae0f 921
f329fa92 922if __name__ == '__main__':
06ccae0f 923 sys.exit( main( sys.argv[1:] ) )