blob: 2357f0506e0747d32d59245c828dc24be37670e1 [file] [log] [blame]
Daniel Dunbar45dc8422010-01-30 23:59:14 +00001#!/usr/bin/env python
2
3#===- cindex-dump.py - cindex/Python Source Dump -------------*- python -*--===#
4#
5# The LLVM Compiler Infrastructure
6#
7# This file is distributed under the University of Illinois Open Source
8# License. See LICENSE.TXT for details.
9#
10#===------------------------------------------------------------------------===#
11
12"""
13A simple command line tool for dumping a source file using the Clang Index
14Library.
15"""
16
17def get_diag_info(diag):
18 return { 'severity' : diag.severity,
19 'location' : diag.location,
20 'spelling' : diag.spelling,
21 'ranges' : diag.ranges,
22 'fixits' : diag.fixits }
23
24def get_cursor_id(cursor, cursor_list = []):
25 if cursor is None:
26 return None
27
28 # FIXME: This is really slow. It would be nice if the index API exposed
29 # something that let us hash cursors.
30 for i,c in enumerate(cursor_list):
31 if cursor == c:
32 return i
33 cursor_list.append(cursor)
34 return len(cursor_list) - 1
35
36def get_info(node):
37 return { 'id' : get_cursor_id(node),
38 'kind' : node.kind,
39 'usr' : node.get_usr(),
40 'spelling' : node.spelling,
41 'location' : node.location,
42 'extent.start' : node.extent.start,
43 'extent.end' : node.extent.end,
44 'is_definition' : node.is_definition(),
45 'definition id' : get_cursor_id(node.get_definition()),
46 'children' : map(get_info, node.get_children()) }
47
48def main():
49 from clang.cindex import Index
50 from pprint import pprint
51
52 from optparse import OptionParser, OptionGroup
53 parser = OptionParser("usage: %prog [options] {filename} [clang-args*]")
54 parser.disable_interspersed_args()
55 (opts, args) = parser.parse_args()
56
57 if len(args) == 0:
58 parser.error('invalid number arguments')
59
60 input_path = args.pop(0)
61
62 index = Index.create()
63 tu = index.parse(input_path, args)
64 if not tu:
65 parser.error("unable to load input")
66
67 pprint(('diags', map(get_diag_info, tu.diagnostics)))
68 pprint(('nodes', map(get_info, tu.cursor.get_children())))
69
70if __name__ == '__main__':
71 main()
72