Daniel Dunbar | 6d32cb8 | 2010-01-30 23:59:14 +0000 | [diff] [blame] | 1 | #!/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 | """ |
| 13 | A simple command line tool for dumping a source file using the Clang Index |
| 14 | Library. |
| 15 | """ |
| 16 | |
| 17 | def 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 | |
| 24 | def get_cursor_id(cursor, cursor_list = []): |
Daniel Dunbar | 8958dc9 | 2010-01-31 00:41:15 +0000 | [diff] [blame] | 25 | if not opts.showIDs: |
| 26 | return None |
| 27 | |
Daniel Dunbar | 6d32cb8 | 2010-01-30 23:59:14 +0000 | [diff] [blame] | 28 | 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 Dunbar | 8958dc9 | 2010-01-31 00:41:15 +0000 | [diff] [blame] | 39 | def 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 Dunbar | 6d32cb8 | 2010-01-30 23:59:14 +0000 | [diff] [blame] | 45 | 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 Dunbar | 8958dc9 | 2010-01-31 00:41:15 +0000 | [diff] [blame] | 54 | 'children' : children } |
Daniel Dunbar | 6d32cb8 | 2010-01-30 23:59:14 +0000 | [diff] [blame] | 55 | |
| 56 | def main(): |
| 57 | from clang.cindex import Index |
| 58 | from pprint import pprint |
| 59 | |
| 60 | from optparse import OptionParser, OptionGroup |
Daniel Dunbar | 8958dc9 | 2010-01-31 00:41:15 +0000 | [diff] [blame] | 61 | |
| 62 | global opts |
| 63 | |
Daniel Dunbar | 6d32cb8 | 2010-01-30 23:59:14 +0000 | [diff] [blame] | 64 | parser = OptionParser("usage: %prog [options] {filename} [clang-args*]") |
Daniel Dunbar | 8958dc9 | 2010-01-31 00:41:15 +0000 | [diff] [blame] | 65 | 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 Dunbar | 6d32cb8 | 2010-01-30 23:59:14 +0000 | [diff] [blame] | 71 | parser.disable_interspersed_args() |
| 72 | (opts, args) = parser.parse_args() |
| 73 | |
| 74 | if len(args) == 0: |
| 75 | parser.error('invalid number arguments') |
| 76 | |
Daniel Dunbar | 6d32cb8 | 2010-01-30 23:59:14 +0000 | [diff] [blame] | 77 | index = Index.create() |
Daniel Dunbar | 8bb44d5 | 2010-02-13 18:33:28 +0000 | [diff] [blame] | 78 | tu = index.parse(None, args) |
Daniel Dunbar | 6d32cb8 | 2010-01-30 23:59:14 +0000 | [diff] [blame] | 79 | if not tu: |
| 80 | parser.error("unable to load input") |
| 81 | |
| 82 | pprint(('diags', map(get_diag_info, tu.diagnostics))) |
Daniel Dunbar | 8958dc9 | 2010-01-31 00:41:15 +0000 | [diff] [blame] | 83 | pprint(('nodes', get_info(tu.cursor))) |
Daniel Dunbar | 6d32cb8 | 2010-01-30 23:59:14 +0000 | [diff] [blame] | 84 | |
| 85 | if __name__ == '__main__': |
| 86 | main() |
| 87 | |