[cfe-commits] r94934 - in /cfe/trunk/bindings/python/examples: ./ cindex/ cindex/cindex-dump.py

Daniel Dunbar daniel at zuster.org
Sat Jan 30 15:59:14 PST 2010


Author: ddunbar
Date: Sat Jan 30 17:59:14 2010
New Revision: 94934

URL: http://llvm.org/viewvc/llvm-project?rev=94934&view=rev
Log:
cindex/Python: Add a simple example which dumps assorted information about a translation unit.

Added:
    cfe/trunk/bindings/python/examples/
    cfe/trunk/bindings/python/examples/cindex/
    cfe/trunk/bindings/python/examples/cindex/cindex-dump.py

Added: cfe/trunk/bindings/python/examples/cindex/cindex-dump.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/examples/cindex/cindex-dump.py?rev=94934&view=auto

==============================================================================
--- cfe/trunk/bindings/python/examples/cindex/cindex-dump.py (added)
+++ cfe/trunk/bindings/python/examples/cindex/cindex-dump.py Sat Jan 30 17:59:14 2010
@@ -0,0 +1,72 @@
+#!/usr/bin/env python
+
+#===- cindex-dump.py - cindex/Python Source Dump -------------*- python -*--===#
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+#===------------------------------------------------------------------------===#
+
+"""
+A simple command line tool for dumping a source file using the Clang Index
+Library.
+"""
+
+def get_diag_info(diag):
+    return { 'severity' : diag.severity,
+             'location' : diag.location,
+             'spelling' : diag.spelling,
+             'ranges' : diag.ranges,
+             'fixits' : diag.fixits }
+
+def get_cursor_id(cursor, cursor_list = []):
+    if cursor is None:
+        return None
+
+    # FIXME: This is really slow. It would be nice if the index API exposed
+    # something that let us hash cursors.
+    for i,c in enumerate(cursor_list):
+        if cursor == c:
+            return i
+    cursor_list.append(cursor)
+    return len(cursor_list) - 1
+
+def get_info(node):
+    return { 'id' : get_cursor_id(node),
+             'kind' : node.kind,
+             'usr' : node.get_usr(),
+             'spelling' : node.spelling,
+             'location' : node.location,
+             'extent.start' : node.extent.start,
+             'extent.end' : node.extent.end,
+             'is_definition' : node.is_definition(),
+             'definition id' : get_cursor_id(node.get_definition()),
+             'children' : map(get_info, node.get_children()) }
+
+def main():
+    from clang.cindex import Index
+    from pprint import pprint
+
+    from optparse import OptionParser, OptionGroup
+    parser = OptionParser("usage: %prog [options] {filename} [clang-args*]")
+    parser.disable_interspersed_args()
+    (opts, args) = parser.parse_args()
+
+    if len(args) == 0:
+        parser.error('invalid number arguments')
+
+    input_path = args.pop(0)
+
+    index = Index.create()
+    tu = index.parse(input_path, args)
+    if not tu:
+        parser.error("unable to load input")
+
+    pprint(('diags', map(get_diag_info, tu.diagnostics)))
+    pprint(('nodes', map(get_info, tu.cursor.get_children())))
+
+if __name__ == '__main__':
+    main()
+





More information about the cfe-commits mailing list