-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathparser.py
141 lines (105 loc) · 4.56 KB
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# This file is part of libigl, a simple c++ geometry processing library.
#
# Copyright (C) 2017 Sebastian Koch <s.koch@tu-berlin.de> and Daniele Panozzo <daniele.panozzo@gmail.com>
#
# This Source Code Form is subject to the terms of the Mozilla Public License
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.
import sys
import os
from threading import Thread
import clang.cindex
import ccsyspath
import itertools
from mako.template import Template
def get_annotations(node):
return [c.displayname for c in node.get_children()
if c.kind == clang.cindex.CursorKind.ANNOTATE_ATTR]
class Function(object):
def __init__(self, cursor):
self.name = cursor.spelling
self.annotations = get_annotations(cursor)
self.access = cursor.access_specifier
# template_pars = [c.extent for c in cursor.get_children() if c.kind == clang.cindex.CursorKind.TEMPLATE_TYPE_PARAMETER]
parameter_dec = [c for c in cursor.get_children() if c.kind == clang.cindex.CursorKind.PARM_DECL]
parameters = []
for p in parameter_dec:
children = []
for c in p.get_children():
# print(c.spelling)
children.append(c.spelling)
parameters.append((p.spelling, p.type.spelling, children))
self.parameters = parameters
self.documentation = cursor.raw_comment
class Enum(object):
def __init__(self, cursor):
self.name = cursor.spelling
self.constants = [c.spelling for c in cursor.get_children() if c.kind ==
clang.cindex.CursorKind.ENUM_CONSTANT_DECL]
self.documentation = cursor.raw_comment
class Class(object):
def __init__(self, cursor):
self.name = cursor.spelling
# self.functions = []
self.annotations = get_annotations(cursor)
# for c in cursor.get_children():
# if (c.kind == clang.cindex.CursorKind.CXX_METHOD and
# c.access_specifier == clang.cindex.AccessSpecifier.PUBLIC):
# f = Function(c)
# self.functions.append(f)
def traverse(c, path, objects):
if c.location.file and not c.location.file.name.endswith(path):
return
if c.spelling == "PARULA_COLOR_MAP": # Fix to prevent python stack overflow from infinite recursion
return
# print(c.kind, c.spelling)
if c.kind == clang.cindex.CursorKind.TRANSLATION_UNIT or c.kind == clang.cindex.CursorKind.UNEXPOSED_DECL:
# Ignore other cursor kinds
pass
elif c.kind == clang.cindex.CursorKind.NAMESPACE:
objects["namespaces"].append(c.spelling)
# print("Namespace", c.spelling, c.get_children())
pass
elif c.kind == clang.cindex.CursorKind.FUNCTION_TEMPLATE:
# print("Function Template", c.spelling, c.raw_comment)
objects["functions"].append(Function(c))
return
elif c.kind == clang.cindex.CursorKind.FUNCTION_DECL:
# print("FUNCTION_DECL", c.spelling, c.raw_comment)
objects["functions"].append(Function(c))
return
elif c.kind == clang.cindex.CursorKind.ENUM_DECL:
# print("ENUM_DECL", c.spelling, c.raw_comment)
objects["enums"].append(Enum(c))
return
elif c.kind == clang.cindex.CursorKind.CLASS_DECL:
objects["classes"].append(Class(c))
return
elif c.kind == clang.cindex.CursorKind.CLASS_TEMPLATE:
objects["classes"].append(Class(c))
return
elif c.kind == clang.cindex.CursorKind.STRUCT_DECL:
objects["structs"].append(Class(c))
return
else:
# print("Unknown", c.kind, c.spelling)
pass
for child_node in c.get_children():
traverse(child_node, path, objects)
def parse(path):
index = clang.cindex.Index.create()
# Clang can't parse files with missing definitions, add static library definition or not?
args = ['-x', 'c++', '-std=c++11', '-fparse-all-comments', '-DIGL_STATIC_LIBRARY']
args.append('-I/usr/include/eigen3/') # TODO Properly add all needed includes
syspath = ccsyspath.system_include_paths('clang++') # Add the system libraries
incargs = [(b'-I' + inc).decode("utf-8") for inc in syspath]
args.extend(incargs)
tu = index.parse(path, args)
objects = {"functions": [], "enums": [], "namespaces": [], "classes": [], "structs": []}
traverse(tu.cursor, path, objects)
return objects
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python3 parser.py <headerfile_path>")
exit(-1)
parse(sys.argv[1])