forked from mozsearch/mozsearch
/
idl-analyze.py
259 lines (218 loc) · 7.15 KB
/
idl-analyze.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
import sys
import xpidl
import os.path
import json
import re
def find_line_column(text, token, pos):
while text[pos : pos + len(token)] != token:
if text[pos] == '\n':
return (0, 0)
pos += 1
line = 0
while pos > linebreaks[line]:
line += 1
line += 1
col = 0
pos -= 1
while pos > 0 and text[pos] != '\n':
col += 1
pos -= 1
return (line, col)
def parse_mangled(mangled):
def parse_inner(idents, inner):
if inner[0] == 'E':
return True
m = re.match(r'([0-9]+)([a-zA-Z0-9_]+)', inner)
if not m:
return False
length = int(m.group(1))
idents.append(m.group(2)[:length])
return parse_inner(idents, m.group(2)[length:])
if mangled[:3] != '_ZN':
return
idents = []
if not parse_inner(idents, mangled[3:]):
return
return idents
def read_cpp_analysis(fname):
base = os.path.basename(fname)
(idlName, suffix) = os.path.splitext(base)
headerName = idlName + '.h'
p = os.path.join(indexRoot, 'analysis', '__GENERATED__', 'dist', 'include', headerName)
try:
lines = open(p).readlines()
except IOError, e:
return None
decls = {}
for line in lines:
j = json.loads(line.strip())
if 'target' in j and j['kind'] == 'decl' and j['sym'].startswith('_Z'):
idents = parse_mangled(j['sym'])
if idents and len(idents) == 2:
decls.setdefault(idents[0], {})[idents[1]] = j['sym']
return decls
def method_name(m):
if m.binaryname:
return m.binaryname
return m.name[0].capitalize() + m.name[1:]
def getter_name(attr):
if attr.binaryname:
return 'Get' + attr.binaryname
return 'Get' + attr.name[0].capitalize() + attr.name[1:]
def setter_name(attr):
if attr.binaryname:
return 'Set' + attr.binaryname
return 'Set' + attr.name[0].capitalize() + attr.name[1:]
def handle_interface(analysis, iface):
(lineno, colno) = find_line_column(text, iface.name, iface.location._lexpos)
mangled = 'T_' + iface.name
# Source
j = {
'loc': '%d:%d-%d' % (lineno, colno, colno + len(iface.name)),
'source': 1,
'pretty': 'IDL class %s' % iface.name,
'sym': mangled + ('' if iface.attributes.scriptable else ',#' + iface.name),
}
print json.dumps(j)
# C++ target
j = {
'loc': '%d:%d-%d' % (lineno, colno, colno + len(iface.name)),
'target': 1,
'kind': 'idl',
'sym': mangled,
}
print json.dumps(j)
if iface.attributes.scriptable:
# JS target
j = {
'loc': '%d:%d-%d' % (lineno, colno, colno + len(iface.name)),
'target': 1,
'kind': 'idl',
'sym': '#' + iface.name,
}
print json.dumps(j)
if iface.base:
(lineno, colno) = find_line_column(text, iface.base, iface.location._lexpos)
mangled = 'T_' + iface.base
# Base source
j = {
'loc': '%d:%d-%d' % (lineno, colno, colno + len(iface.base)),
'source': 1,
'pretty': 'IDL class %s' % iface.base,
'sym': mangled + ',#' + iface.base,
}
print json.dumps(j)
#print p.name
#print 'BASE', p.base
for m in iface.members:
name = getattr(m, 'name', '')
(lineno, colno) = find_line_column(text, name, m.location._lexpos)
# Want to deal with attributes like noscript, as well as ConstMember
if isinstance(m, xpidl.Method):
mangled = analysis[method_name(m)]
# C++ target
j = {
'loc': '%d:%d-%d' % (lineno, colno, colno + len(m.name)),
'target': 1,
'kind': 'idl',
'sym': mangled,
}
print json.dumps(j)
# Source
j = {
'loc': '%d:%d-%d' % (lineno, colno, colno + len(m.name)),
'source': 1,
'pretty': 'IDL method %s' % m.name,
'sym': mangled + ('' if m.noscript else ',#' + m.name),
}
print json.dumps(j)
if not m.noscript:
# JS target
j = {
'loc': '%d:%d-%d' % (lineno, colno, colno + len(m.name)),
'target': 1,
'kind': 'idl',
'sym': '#' + m.name,
}
print json.dumps(j)
elif isinstance(m, xpidl.Attribute):
if not m.noscript:
# JS target
j = {
'loc': '%d:%d-%d' % (lineno, colno, colno + len(m.name)),
'target': 1,
'kind': 'idl',
'sym': '#' + m.name,
}
print json.dumps(j)
mangled_getter = analysis[getter_name(m)]
# C++ target (getter)
j = {
'loc': '%d:%d-%d' % (lineno, colno, colno + len(m.name)),
'target': 1,
'kind': 'idl',
'sym': mangled_getter,
}
print json.dumps(j)
if not m.readonly:
mangled_setter = analysis[setter_name(m)]
# C++ target (setter)
j = {
'loc': '%d:%d-%d' % (lineno, colno, colno + len(m.name)),
'target': 1,
'kind': 'idl',
'sym': mangled_setter,
}
print json.dumps(j)
# Source
sym = mangled_getter
if not m.readonly:
sym += ',' + mangled_setter
if not m.noscript:
sym += ',#' + m.name
j = {
'loc': '%d:%d-%d' % (lineno, colno, colno + len(m.name)),
'source': 1,
'pretty': 'IDL attribute %s' % m.name,
'sym': sym,
}
print json.dumps(j)
elif isinstance(m, xpidl.ConstMember):
# No C++ support until clang-plugin supports it.
# JS target
j = {
'loc': '%d:%d-%d' % (lineno, colno, colno + len(m.name)),
'target': 1,
'kind': 'idl',
'sym': '#' + m.name,
}
print json.dumps(j)
# JS source
j = {
'loc': '%d:%d-%d' % (lineno, colno, colno + len(m.name)),
'source': 1,
'pretty': 'IDL constant %s' % m.name,
'sym': '#' + m.name,
}
print json.dumps(j)
indexRoot = sys.argv[1]
fname = sys.argv[2]
text = open(fname).read()
analysis = read_cpp_analysis(fname)
linebreaks = []
lines = text.split('\n')
cur = 0
for l in lines:
cur += len(l) + 1
linebreaks.append(cur)
if analysis:
p = xpidl.IDLParser(outputdir='/tmp')
try:
r = p.parse(text, filename=fname)
except xpidl.IDLError, e:
print >>sys.stderr, 'Syntax error in IDL', fname
raise e
sys.exit(1)
for p in r.productions:
if isinstance(p, xpidl.Interface):
handle_interface(analysis.get(p.name, {}), p)