3 ## @package thtml2doxy_clang
4 # Translates THtml C++ comments to Doxygen using libclang as parser.
6 # This code relies on Python bindings for libclang: libclang's interface is pretty unstable, and
7 # its Python bindings are unstable as well.
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.
12 # This code (expecially AST traversal) was inspired by:
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
22 # `thtml2doxy_clang file1 [file2 [file3...]]`
24 # @author Dario Berzano <dario.berzano@cern.ch>
34 ## Brain-dead color output for terminal.
38 return self.color('\033[31m')
41 return self.color('\033[32m')
44 return self.color('\033[33m')
47 return self.color('\033[34m')
50 return self.color('\033[35m')
53 return self.color('\033[36m')
56 return c + self + '\033[m'
59 ## Traverse the AST recursively starting from the current cursor.
61 # @param cursor A Clang parser cursor
62 # @param recursion Current recursion depth
63 def traverse_ast(cursor, recursion=0):
65 text = cursor.spelling or cursor.displayname
66 kind = str(cursor.kind)[str(cursor.kind).index('.')+1:]
69 for i in range(0, recursion):
72 if cursor.kind == clang.cindex.CursorKind.CXX_METHOD:
74 # cursor ran into a C++ method
75 print "%s%s(%s)" % (indent, Colt(kind).magenta(), Colt(text).blue())
77 # we are looking for the following structure: method -> compound statement -> comment, i.e. we
78 # need to extract the first comment in the compound statement composing the method
80 in_compound_stmt = False
81 expect_comment = False
82 last_comment_line = -1
84 for token in cursor.get_tokens():
86 if token.cursor.kind == clang.cindex.CursorKind.COMPOUND_STMT:
87 if not in_compound_stmt:
88 in_compound_stmt = True
90 last_comment_line = -1
93 in_compound_stmt = False
96 # tkind = str(token.kind)[str(token.kind).index('.')+1:]
97 # ckind = str(token.cursor.kind)[str(token.cursor.kind).index('.')+1:]
103 extent = token.extent
104 line_start = extent.start.line
105 line_end = extent.end.line
107 if token.kind == clang.cindex.TokenKind.PUNCTUATION and token.spelling == '{':
110 elif token.kind == clang.cindex.TokenKind.COMMENT and (last_comment_line == -1 or line_start == last_comment_line+1):
111 #print Colt("%s %s:%s = %s" % (indent, ckind, tkind, token.spelling)).green()
112 last_comment_line = line_end
113 new_comment = refactor_comment(token.spelling)
115 for comment_line in new_comment:
116 print Colt("%s [%d-%d]" % (indent, line_start, line_end)).green(),
117 print Colt(comment_line).cyan()
120 expect_comment = False
123 # print Colt("%s %s:%s = %s" % (indent, ckind, tkind, token.spelling)).yellow()
128 print "%s%s(%s)" % (indent, kind, text)
130 for child_cursor in cursor.get_children():
131 traverse_ast(child_cursor, recursion+1)
133 ## Remove garbage from comments and convert special tags from THtml to Doxygen.
135 # @param comment The original comment
136 def refactor_comment(comment):
138 resingle = r'^/{2,}\s*(.*?)\s*(/{2,})?\s*$'
139 remulti_first = r'^/\*\s*(.*?)\s*\*?\s*$'
140 remulti_last = r'^\s*(.*?)\s*\*/$'
142 new_comment = comment.split('\n')
144 if len(new_comment) == 1:
145 msingle = re.search(resingle, comment)
147 new_comment[0] = msingle.group(1)
151 for i in range(0, len(new_comment)):
153 mmulti = re.search(remulti_first, new_comment[i])
155 new_comment[i] = mmulti.group(1)
156 elif i == len(new_comment)-1:
157 mmulti = re.search(remulti_last, new_comment[i])
159 new_comment[i] = mmulti.group(1)
161 new_comment[i] = new_comment[i].strip()
166 ## The main function.
168 # **Note:** this program only has this function.
171 # Attempt to load libclang from a list of known locations
172 libclang_locations = [
173 '/usr/lib/llvm-3.5/lib/libclang.so.1',
174 '/usr/lib/libclang.so',
175 '/Library/Developer/CommandLineTools/usr/lib/libclang.dylib'
177 libclang_found = False
179 for lib in libclang_locations:
180 if os.path.isfile(lib):
181 clang.cindex.Config.set_library_file(lib)
182 libclang_found = True
185 if not libclang_found:
186 print Colt('[Error] Cannot find libclang, aborting').red()
189 # Loop over all files
192 index = clang.cindex.Index.create()
193 translation_unit = index.parse(fn, args=['-x', 'c++'])
194 traverse_ast( translation_unit.cursor )
199 if __name__ == '__main__':
200 sys.exit( main( sys.argv ) )