1f4a2713aSLionel Sambuc#!/usr/bin/env python 2f4a2713aSLionel Sambuc 3f4a2713aSLionel Sambuc#===- cindex-dump.py - cindex/Python Source Dump -------------*- python -*--===# 4f4a2713aSLionel Sambuc# 5f4a2713aSLionel Sambuc# The LLVM Compiler Infrastructure 6f4a2713aSLionel Sambuc# 7f4a2713aSLionel Sambuc# This file is distributed under the University of Illinois Open Source 8f4a2713aSLionel Sambuc# License. See LICENSE.TXT for details. 9f4a2713aSLionel Sambuc# 10f4a2713aSLionel Sambuc#===------------------------------------------------------------------------===# 11f4a2713aSLionel Sambuc 12f4a2713aSLionel Sambuc""" 13f4a2713aSLionel SambucA simple command line tool for dumping a source file using the Clang Index 14f4a2713aSLionel SambucLibrary. 15f4a2713aSLionel Sambuc""" 16f4a2713aSLionel Sambuc 17f4a2713aSLionel Sambucdef get_diag_info(diag): 18f4a2713aSLionel Sambuc return { 'severity' : diag.severity, 19f4a2713aSLionel Sambuc 'location' : diag.location, 20f4a2713aSLionel Sambuc 'spelling' : diag.spelling, 21f4a2713aSLionel Sambuc 'ranges' : diag.ranges, 22f4a2713aSLionel Sambuc 'fixits' : diag.fixits } 23f4a2713aSLionel Sambuc 24f4a2713aSLionel Sambucdef get_cursor_id(cursor, cursor_list = []): 25f4a2713aSLionel Sambuc if not opts.showIDs: 26f4a2713aSLionel Sambuc return None 27f4a2713aSLionel Sambuc 28f4a2713aSLionel Sambuc if cursor is None: 29f4a2713aSLionel Sambuc return None 30f4a2713aSLionel Sambuc 31f4a2713aSLionel Sambuc # FIXME: This is really slow. It would be nice if the index API exposed 32f4a2713aSLionel Sambuc # something that let us hash cursors. 33f4a2713aSLionel Sambuc for i,c in enumerate(cursor_list): 34f4a2713aSLionel Sambuc if cursor == c: 35f4a2713aSLionel Sambuc return i 36f4a2713aSLionel Sambuc cursor_list.append(cursor) 37f4a2713aSLionel Sambuc return len(cursor_list) - 1 38f4a2713aSLionel Sambuc 39f4a2713aSLionel Sambucdef get_info(node, depth=0): 40f4a2713aSLionel Sambuc if opts.maxDepth is not None and depth >= opts.maxDepth: 41f4a2713aSLionel Sambuc children = None 42f4a2713aSLionel Sambuc else: 43f4a2713aSLionel Sambuc children = [get_info(c, depth+1) 44f4a2713aSLionel Sambuc for c in node.get_children()] 45f4a2713aSLionel Sambuc return { 'id' : get_cursor_id(node), 46f4a2713aSLionel Sambuc 'kind' : node.kind, 47f4a2713aSLionel Sambuc 'usr' : node.get_usr(), 48f4a2713aSLionel Sambuc 'spelling' : node.spelling, 49f4a2713aSLionel Sambuc 'location' : node.location, 50f4a2713aSLionel Sambuc 'extent.start' : node.extent.start, 51f4a2713aSLionel Sambuc 'extent.end' : node.extent.end, 52f4a2713aSLionel Sambuc 'is_definition' : node.is_definition(), 53f4a2713aSLionel Sambuc 'definition id' : get_cursor_id(node.get_definition()), 54f4a2713aSLionel Sambuc 'children' : children } 55f4a2713aSLionel Sambuc 56f4a2713aSLionel Sambucdef main(): 57f4a2713aSLionel Sambuc from clang.cindex import Index 58f4a2713aSLionel Sambuc from pprint import pprint 59f4a2713aSLionel Sambuc 60f4a2713aSLionel Sambuc from optparse import OptionParser, OptionGroup 61f4a2713aSLionel Sambuc 62f4a2713aSLionel Sambuc global opts 63f4a2713aSLionel Sambuc 64f4a2713aSLionel Sambuc parser = OptionParser("usage: %prog [options] {filename} [clang-args*]") 65f4a2713aSLionel Sambuc parser.add_option("", "--show-ids", dest="showIDs", 66*0a6a1f1dSLionel Sambuc help="Compute cursor IDs (very slow)", 67*0a6a1f1dSLionel Sambuc action="store_true", default=False) 68f4a2713aSLionel Sambuc parser.add_option("", "--max-depth", dest="maxDepth", 69f4a2713aSLionel Sambuc help="Limit cursor expansion to depth N", 70f4a2713aSLionel Sambuc metavar="N", type=int, default=None) 71f4a2713aSLionel Sambuc parser.disable_interspersed_args() 72f4a2713aSLionel Sambuc (opts, args) = parser.parse_args() 73f4a2713aSLionel Sambuc 74f4a2713aSLionel Sambuc if len(args) == 0: 75f4a2713aSLionel Sambuc parser.error('invalid number arguments') 76f4a2713aSLionel Sambuc 77f4a2713aSLionel Sambuc index = Index.create() 78f4a2713aSLionel Sambuc tu = index.parse(None, args) 79f4a2713aSLionel Sambuc if not tu: 80f4a2713aSLionel Sambuc parser.error("unable to load input") 81f4a2713aSLionel Sambuc 82f4a2713aSLionel Sambuc pprint(('diags', map(get_diag_info, tu.diagnostics))) 83f4a2713aSLionel Sambuc pprint(('nodes', get_info(tu.cursor))) 84f4a2713aSLionel Sambuc 85f4a2713aSLionel Sambucif __name__ == '__main__': 86f4a2713aSLionel Sambuc main() 87f4a2713aSLionel Sambuc 88