-
Notifications
You must be signed in to change notification settings - Fork 0
/
clang_autogen.py
277 lines (220 loc) · 8.44 KB
/
clang_autogen.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import clang.cindex
from clang.cindex import Config
from clang.cindex import _CXString
from clang.cindex import CursorKind
from clang.cindex import TypeKind
from clang.cindex import LinkageKind
from enum import Enum, unique
Config.set_compatibility_check(False)
Config.set_library_file("/usr/lib/llvm-6.0/lib/libclang.so")
class astTypedef(object):
def __init__(self, node):
self.top = node
self.type = node.displayname
self.underlying = node.underlying_typedef_type.spelling
self.canonical = node.type.get_canonical().spelling
class astVariable(object):
def __init__(self, node):
self.top = node
# to do here if have global variable
def ostream_basic(field, fieldname, indx):
strTmpOStream = ''
if (field.name.find('pad') != -1 or
field.name.find('reserved') != -1 or
field.name.find('Reserved') != -1) and field.array_count != 0:
return strTmpOStream
if field.array_count == 0 or field.type.kind == TypeKind.CHAR_S:
strTmpOStream += '<< " ' + ' ' * indx + fieldname + ' = " << s.'
strTmpOStream += fieldname + ' << "\\n" \n '
else:
for i in range(field.array_count):
strTmpOStream += '<< " ' + ' ' * indx + \
fieldname + '[{}] = " << s.'.format(i)
strTmpOStream += fieldname + '[{}] << "\\n" \n '.format(i)
return strTmpOStream
def ostream_substructure(struct, prefix, indx=0):
strTmpOStream = '<< "' + ' ' * indx + prefix + ' {\\n"\n '
for field in struct.fields:
substruture = struct.symbols.get(field.canonical)
fieldname = prefix + '.' + field.name
if substruture == None:
strTmpOStream += ostream_basic(field, fieldname, indx)
else:
strTmpOStream += ostream_substructure(
substruture, fieldname, indx + 1)
strTmpOStream += '<< "' + ' ' * indx + '}\\n"\n '
return strTmpOStream
class astStruct(object):
class field:
def __init__(self, node):
self.top = node
self.type = node.type
self.pcount = 0
self.array_count = 0
if self.type.kind == TypeKind.CONSTANTARRAY:
self.array_count = self.type.element_count
self.type = self.type.element_type
if self.type.kind == TypeKind.POINTER:
self.type = self.type.get_pointee()
self.pcount += 1
self.canonical = self.type.get_canonical().spelling
self.name = node.spelling
def parser_fields(self, node):
for i in node.get_children():
if i.kind == CursorKind.FIELD_DECL:
sf = astStruct.field(i)
self.fields.append(sf)
elif i.kind == CursorKind.STRUCT_DECL or i.kind == CursorKind.UNION_DECL:
struct = astStruct(i)
if i.is_anonymous():
self.fields = self.fields + struct.fields
for s in struct.symbols:
self.symbols[s] = struct.symbols[s]
else:
self.symbols[struct.typename] = struct
def __init__(self, node):
self.top = node
self.is_union = node.kind == CursorKind.UNION_DECL
if self.is_union:
self.typename = node.type.spelling
else:
self.typename = node.type.spelling
self.fields = []
self.symbols = {}
self.parser_fields(node)
@property
def ostream(self):
if not hasattr(self, '_ostream'):
strOStream = 'std::ostream& operator<<(std::ostream& os, const ' + \
self.typename + '& s)\n'
strOStream += '{\n'
strOStream += ' return os << "' + \
self.typename + ' {\\n"\n '
for field in self.fields:
substruture = self.symbols.get(field.canonical)
if substruture == None:
strOStream += ostream_basic(field, field.name, 0)
else:
strOStream += ostream_substructure(
substruture, field.name, 1)
strOStream += '<< "}\\n";\n'
strOStream += '}\n'
self._ostream = strOStream
return self._ostream
class astEnum(object):
def __init__(self, node):
self.top = node
self.name = 'enum ' + node.type.spelling
self.nam2val = {}
self.val2nam = {}
for i in node.get_children():
if i.kind == CursorKind.ENUM_CONSTANT_DECL:
self.nam2val[i.displayname] = i.enum_value
self.val2nam[i.enum_value] = i.displayname
class astFunction(object):
class param(object):
def __init__(self, node):
self.type = node.type.spelling
self.name = node.spelling
def __init__(self, node):
self.top = node
self.name = node.spelling
self.ret = node.result_type.spelling
self.params = []
for i in node.get_children():
if i.kind == CursorKind.PARM_DECL:
param = astFunction.param(i)
self.params.append(param)
class astClass(object):
def __init__(self, node):
self.top = node
self.name = node.spelling
self.methods = []
self.structs = []
self.enums = []
for i in node.get_children():
if i.kind == CursorKind.CXX_METHOD:
parsefunction(i, self.methods)
if CursorKind.ENUM_DECL == i.kind:
parseenum(i, self.enums)
elif i.kind == CursorKind.STRUCT_DECL or i.kind == CursorKind.UNION_DECL:
parsestruct(i, self.structs)
class astFile(object):
def __init__(self, node):
self.gEnums = []
self.gStructs = []
self.gVars = []
self.gClasses = []
self.gEFuncs = []
self.gIFuncs = []
self.gTypedef = []
self.top = node
self.filename = node.spelling
def printnode(node, indent, f=None):
text = node.displayname
kind = str(node.kind)[str(node.kind).index('.')+1:]
print(' ' * indent, '{} {}'.format(kind, text), file=f)
for i in node.get_children():
printnode(i, indent + 2, f=f)
def parsevariable(node, v):
var = astVariable(node)
v.append(var)
def parseenum(node, e):
enum = astEnum(node)
if len(enum.val2nam) != 0:
e.append(enum)
def parsestruct(node, s):
struct = astStruct(node)
if len(struct.fields) != 0:
s.append(struct)
def parsetypedef(node, t):
typedef = astTypedef(node)
t.append(typedef)
def parsefunction(node, f):
if node.is_function_inlined() == False:
function = astFunction(node)
f.append(function)
def parseclass(node, c):
clas = astClass(node)
c.append(clas)
def parseglobal(node, cf):
for i in node.get_children():
if CursorKind.ENUM_DECL == i.kind:
parseenum(i, cf.gEnums)
elif i.kind == CursorKind.STRUCT_DECL or i.kind == CursorKind.UNION_DECL:
parsestruct(i, cf.gStructs)
elif CursorKind.TYPEDEF_DECL == i.kind:
parsetypedef(i, cf.gTypedef)
elif CursorKind.FUNCTION_DECL == i.kind:
if i.linkage == LinkageKind.INTERNAL:
parsefunction(i, cf.gIFuncs)
elif i.linkage == LinkageKind.EXTERNAL:
parsefunction(i, cf.gEFuncs)
elif CursorKind.VAR_DECL == i.kind:
parsevariable(i, cf.gVars)
elif CursorKind.CLASS_DECL == i.kind:
parseclass(i, cf.gClasses)
elif CursorKind.UNEXPOSED_DECL == i.kind:
parseglobal(i, cf)
def dumpnode(node):
with open('./dumpnode.txt', 'w') as f:
printnode(node, 0, f=f)
def dump_struct_ostream(cf):
with open('./struct_ostream.h', 'w') as f:
print('#include <sstream>', file=f)
print('#include <string>', file=f)
print('#include "{}"\n'.format(cf.filename), file=f)
print('using std::string;', file=f)
print('using std::ostringstream;\n', file=f)
for s in cf.gStructs:
print(s.ostream, file=f)
def main():
index = clang.cindex.Index.create()
tu = index.parse('nvcuda.cpp', # '../runtime/runtimeapi/cuda_runtime.cpp',
['-x', 'c++', '-std=c++11', '-D__CODE_GENERATOR__'])
# dumpnode(tu.cursor)
cf = astFile(tu.cursor)
parseglobal(tu.cursor, cf)
dump_struct_ostream(cf)
if __name__ == '__main__':
main()