1
1
#!/usr/bin/python3
2
2
3
+ import sys
4
+
3
5
4
6
def main ():
5
7
from clang .cindex import Index
6
8
from pprint import pprint
7
9
8
- from optparse import OptionParser
9
-
10
- global opts
11
-
12
- parser = OptionParser ("usage: %prog [options] {filename} [clang-args*]" )
13
- parser .add_option ("" , "--show-ids" , dest = "showIDs" ,
14
- help = "don't compute cursor IDs(very slow)" ,
15
- default = False )
16
- parser .add_option ("" , "--max-depth" , dest = "maxDepth" ,
17
- help = "limit cursor expansion to depth N" ,
18
- metavar = "N" , type = int , default = None )
19
- parser .disable_interspersed_args ()
20
- (opts , args ) = parser .parse_args ()
10
+ from argparse import ArgumentParser
21
11
22
- if len (args ) == 0 :
23
- parser .error ('invalid number arguments' )
12
+ parser = ArgumentParser (
13
+ description = "dump TU info" ,
14
+ usage = "%(prog)s [options] {filename} [clang-args*]" )
24
15
25
- print ("type(args):" , type (args ), " args: " , args )
16
+ parser .add_argument ("--show-ids" , dest = 'showIDs' ,
17
+ help = "don't compute cursor IDs(very slow)" ,
18
+ default = False )
19
+ parser .add_argument ("--max-depth" , dest = 'maxDepth' ,
20
+ help = "limit cursor expansion to depth N" ,
21
+ metavar = "N" , type = int , default = None )
22
+ opts , unknown = parser .parse_known_args (sys .argv )
26
23
27
24
index = Index .create ()
28
- tu = index .parse (None , args )
25
+ tu = index .parse (None , args = sys . argv [ 1 :] )
29
26
if not tu :
30
27
parser .error ("unable to load input" )
31
28
32
- # pprint(('diags', map(get_diag_info, tu.diagnostics)))
33
- # pprint(('nodes', get_info(tu.cursor)))
29
+ pprint (('diags' , list ( map (get_diag_info , tu .diagnostics ) )))
30
+ pprint (('nodes' , get_info (opts , tu .cursor )))
34
31
35
32
36
33
def get_diag_info (diag ):
@@ -43,7 +40,7 @@ def get_diag_info(diag):
43
40
}
44
41
45
42
46
- def get_cursor_id (cursor , cursor_list = []):
43
+ def get_cursor_id (opts , cursor , cursor_list = []):
47
44
if not opts .showIDs :
48
45
return None
49
46
@@ -57,21 +54,21 @@ def get_cursor_id(cursor, cursor_list=[]):
57
54
return len (cursor_list ) - 1
58
55
59
56
60
- def get_info (node , depth = 0 ):
57
+ def get_info (opts , node , depth = 0 ):
61
58
if opts .maxDepth is not None and depth >= opts .maxDepth :
62
59
children = None
63
60
else :
64
- children = [get_info (c , depth + 1 ) for c in node .get_children ()]
61
+ children = [get_info (opts , c , depth + 1 ) for c in node .get_children ()]
65
62
66
63
return {
67
- 'id' : get_cursor_id (node ),
64
+ 'id' : get_cursor_id (opts , node ),
68
65
'kind' : node .kind ,
69
66
'usr' : node .get_usr (),
70
67
'spelling' : node .spelling ,
71
68
'extent.start' : node .extent .start ,
72
69
'extent.end' : node .extent .end ,
73
70
'is_definition' : node .is_definition (),
74
- 'definition_id' : get_cursor_id (node .get_definition ()),
71
+ 'definition_id' : get_cursor_id (opts , node .get_definition ()),
75
72
'children' : children
76
73
}
77
74
0 commit comments