blob: af7ddab6ea535f5147697f6ff674a0a53d7a9ac2 [file] [log] [blame]
Daniel Dunbar6d32cb82010-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 = []):
Daniel Dunbar8958dc92010-01-31 00:41:15 +000025 if not opts.showIDs:
26 return None
27
Daniel Dunbar6d32cb82010-01-30 23:59:14 +000028 if cursor is None:
29 return None
30
31 # FIXME: This is really slow. It would be nice if the index API exposed
32 # something that let us hash cursors.
33 for i,c in enumerate(cursor_list):
34 if cursor == c:
35 return i
36 cursor_list.append(cursor)
37 return len(cursor_list) - 1
38
Daniel Dunbar8958dc92010-01-31 00:41:15 +000039def get_info(node, depth=0):
40 if opts.maxDepth is not None and depth >= opts.maxDepth:
41 children = None
42 else:
43 children = [get_info(c, depth+1)
44 for c in node.get_children()]
Daniel Dunbar6d32cb82010-01-30 23:59:14 +000045 return { 'id' : get_cursor_id(node),
46 'kind' : node.kind,
47 'usr' : node.get_usr(),
48 'spelling' : node.spelling,
49 'location' : node.location,
50 'extent.start' : node.extent.start,
51 'extent.end' : node.extent.end,
52 'is_definition' : node.is_definition(),
53 'definition id' : get_cursor_id(node.get_definition()),
Daniel Dunbar8958dc92010-01-31 00:41:15 +000054 'children' : children }
Daniel Dunbar6d32cb82010-01-30 23:59:14 +000055
56def main():
57 from clang.cindex import Index
58 from pprint import pprint
59
60 from optparse import OptionParser, OptionGroup
Daniel Dunbar8958dc92010-01-31 00:41:15 +000061
62 global opts
63
Daniel Dunbar6d32cb82010-01-30 23:59:14 +000064 parser = OptionParser("usage: %prog [options] {filename} [clang-args*]")
Daniel Dunbar8958dc92010-01-31 00:41:15 +000065 parser.add_option("", "--show-ids", dest="showIDs",
66 help="Don't compute cursor IDs (very slow)",
67 default=False)
68 parser.add_option("", "--max-depth", dest="maxDepth",
69 help="Limit cursor expansion to depth N",
70 metavar="N", type=int, default=None)
Daniel Dunbar6d32cb82010-01-30 23:59:14 +000071 parser.disable_interspersed_args()
72 (opts, args) = parser.parse_args()
73
74 if len(args) == 0:
75 parser.error('invalid number arguments')
76
Daniel Dunbar6d32cb82010-01-30 23:59:14 +000077 index = Index.create()
Daniel Dunbar8bb44d52010-02-13 18:33:28 +000078 tu = index.parse(None, args)
Daniel Dunbar6d32cb82010-01-30 23:59:14 +000079 if not tu:
80 parser.error("unable to load input")
81
82 pprint(('diags', map(get_diag_info, tu.diagnostics)))
Daniel Dunbar8958dc92010-01-31 00:41:15 +000083 pprint(('nodes', get_info(tu.cursor)))
Daniel Dunbar6d32cb82010-01-30 23:59:14 +000084
85if __name__ == '__main__':
86 main()
87