3 # ===- cindex-dump.py - cindex/Python Source Dump -------------*- python -*--===#
5 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6 # See https://llvm.org/LICENSE.txt for license information.
7 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9 # ===------------------------------------------------------------------------===#
12 A simple command line tool for dumping a source file using the Clang Index
17 def get_diag_info(diag
):
19 "severity": diag
.severity
,
20 "location": diag
.location
,
21 "spelling": diag
.spelling
,
22 "ranges": diag
.ranges
,
23 "fixits": diag
.fixits
,
27 def get_cursor_id(cursor
, cursor_list
=[]):
34 # FIXME: This is really slow. It would be nice if the index API exposed
35 # something that let us hash cursors.
36 for i
, c
in enumerate(cursor_list
):
39 cursor_list
.append(cursor
)
40 return len(cursor_list
) - 1
43 def get_info(node
, depth
=0):
44 if opts
.maxDepth
is not None and depth
>= opts
.maxDepth
:
47 children
= [get_info(c
, depth
+ 1) for c
in node
.get_children()]
49 "id": get_cursor_id(node
),
51 "usr": node
.get_usr(),
52 "spelling": node
.spelling
,
53 "location": node
.location
,
54 "extent.start": node
.extent
.start
,
55 "extent.end": node
.extent
.end
,
56 "is_definition": node
.is_definition(),
57 "definition id": get_cursor_id(node
.get_definition()),
63 from clang
.cindex
import Index
64 from pprint
import pprint
66 from optparse
import OptionParser
, OptionGroup
70 parser
= OptionParser("usage: %prog [options] {filename} [clang-args*]")
75 help="Compute cursor IDs (very slow)",
83 help="Limit cursor expansion to depth N",
88 parser
.disable_interspersed_args()
89 (opts
, args
) = parser
.parse_args()
92 parser
.error("invalid number arguments")
94 index
= Index
.create()
95 tu
= index
.parse(None, args
)
97 parser
.error("unable to load input")
99 pprint(("diags", [get_diag_info(d
) for d
in tu
.diagnostics
]))
100 pprint(("nodes", get_info(tu
.cursor
)))
103 if __name__
== "__main__":