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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
#!/usr/bin/python
#
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
IDLNamespace for PPAPI
This file defines the behavior of the AST namespace which allows for resolving
a symbol as one or more AST nodes given a version or range of versions.
"""
import sys
from idl_option import GetOption, Option, ParseOptions
from idl_log import ErrOut, InfoOut, WarnOut
from idl_version import IDLVersion
Option('label', 'Use the specifed label blocks.', default='Chrome')
Option('namespace_debug', 'Use the specified version')
#
# IDLVersionList
#
# IDLVersionList is a list based container for holding IDLVersion
# objects in order. The IDLVersionList can be added to, and searched by
# range. Objects are stored in order, and must be added in order.
#
class IDLVersionList(object):
def __init__(self):
self.nodes = []
def FindVersion(self, version):
assert type(version) == float
for node in self.nodes:
if node.IsVersion(version):
return node
return None
def FindRange(self, vmin, vmax):
assert type(vmin) == float
assert type(vmax) == float
assert vmin != vmax
out = []
for node in self.nodes:
if node.InRange(vmin, vmax):
out.append(node)
return out
def AddNode(self, node):
if GetOption('version_debug'):
InfoOut.Log('\nAdding %s %s' % (node.Location(), node))
last = None
# Check current versions in that namespace
for cver in self.nodes:
if GetOption('version_debug'): InfoOut.Log(' Checking %s' % cver)
# We should only be missing a 'version' tag for the first item.
if not node.vmin:
raise RuntimeError('Missing version')
node.Error('Missing version on overload of previous %s.' %
cver.Location())
return False
# If the node has no max, then set it to this one
if not cver.vmax:
cver.vmax = node.vmin
if GetOption('version_debug'): InfoOut.Log(' Update %s' % cver)
# if the max and min overlap, than's an error
if cver.vmax > node.vmin:
if node.vmax and cver.vmin >= node.vmax:
node.Error('Declarations out of order.')
else:
node.Error('Overlap in versions.')
return False
last = cver
# Otherwise, the previous max and current min should match
# unless this is the unlikely case of something being only
# temporarily deprecated.
if last and last.vmax != node.vmin:
node.Warn('Gap in version numbers.')
# If we made it here, this new node must be the 'newest'
# and does not overlap with anything previously added, so
# we can add it to the end of the list.
if GetOption('version_debug'): InfoOut.Log('Done %s' % node)
self.nodes.append(node)
return True
#
# IDLVersionMap
#
# A version map, can map from an float interface version, to a global
# release string.
#
class IDLVersionMap(object):
def __init__(self):
self.version_to_release = {}
self.release_to_version = {}
self.versions = []
def AddReleaseVersionMapping(self, release, version):
self.version_to_release[version] = release
self.release_to_version[release] = version
self.versions = sorted(self.version_to_release.keys())
def GetRelease(self, version):
# Check for exact match
if version in self.versions:
return self.version_to_release[version]
def GetVersion(self, release):
return self.release_to_version[release]
#
# IDLNamespace
#
# IDLNamespace provides a mapping between a symbol name and an IDLVersionList
# which contains IDLVersion objects. It provides an interface for fetching
# one or more IDLNodes based on a version or range of versions.
#
class IDLNamespace(object):
def __init__(self, parent):
self.namespace = {}
self.parent = parent
def Dump(self):
for name in self.namespace:
InfoOut.Log('NAME=%s' % name)
for cver in self.namespace[name]:
InfoOut.Log(' %s' % cver)
InfoOut.Log('')
def FindVersion(self, name, version):
verlist = self.namespace.get(name, None)
if verlist == None:
if self.parent:
return self.parent.FindVersion(name, version)
else:
return None
return verlist.FindVersion(version)
def FindRange(self, name, vmin, vmax):
verlist = self.namespace.get(name, None)
if verlist == None:
if self.parent:
return self.parent.FindRange(name, vmin, vmax)
else:
return []
return verlist.FindRange(vmin, vmax)
def FindList(self, name):
verlist = self.namespace.get(name, None)
if verlist == None:
if self.parent:
return self.parent.FindList(name)
return verlist
def AddNode(self, node):
name = node.GetName()
verlist = self.namespace.setdefault(name,IDLVersionList())
if GetOption('namespace_debug'):
print "Adding to namespace: %s" % node
return verlist.AddNode(node)
#
# Testing Code
#
#
# MockNode
#
# Mocks the IDLNode to support error, warning handling, and string functions.
#
class MockNode(IDLVersion):
def __init__(self, name, vmin, vmax):
self.name = name
self.vmin = vmin
self.vmax = vmax
self.errors = []
self.warns = []
self.properties = {
'NAME': name,
'version': vmin,
'deprecate' : vmax
}
def __str__(self):
return '%s (%s : %s)' % (self.name, self.vmin, self.vmax)
def GetName(self):
return self.name
def Error(self, msg):
if GetOption('version_debug'): print 'Error: %s' % msg
self.errors.append(msg)
def Warn(self, msg):
if GetOption('version_debug'): print 'Warn: %s' % msg
self.warns.append(msg)
def GetProperty(self, name):
return self.properties.get(name, None)
errors = 0
#
# DumpFailure
#
# Dumps all the information relevant to an add failure.
def DumpFailure(namespace, node, msg):
global errors
print '\n******************************'
print 'Failure: %s %s' % (node, msg)
for warn in node.warns:
print ' WARN: %s' % warn
for err in node.errors:
print ' ERROR: %s' % err
print '\n'
namespace.Dump()
print '******************************\n'
errors += 1
# Add expecting no errors or warnings
def AddOkay(namespace, node):
okay = namespace.AddNode(node)
if not okay or node.errors or node.warns:
DumpFailure(namespace, node, 'Expected success')
# Add expecting a specific warning
def AddWarn(namespace, node, msg):
okay = namespace.AddNode(node)
if not okay or node.errors or not node.warns:
DumpFailure(namespace, node, 'Expected warnings')
if msg not in node.warns:
DumpFailure(namespace, node, 'Expected warning: %s' % msg)
# Add expecting a specific error any any number of warnings
def AddError(namespace, node, msg):
okay = namespace.AddNode(node)
if okay or not node.errors:
DumpFailure(namespace, node, 'Expected errors')
if msg not in node.errors:
DumpFailure(namespace, node, 'Expected error: %s' % msg)
# Verify that a FindVersion call on the namespace returns the expected node.
def VerifyFindOne(namespace, name, version, node):
global errors
if (namespace.FindVersion(name, version) != node):
print "Failed to find %s as version %f of %s" % (node, version, name)
namespace.Dump()
print "\n"
errors += 1
# Verify that a FindRage call on the namespace returns a set of expected nodes.
def VerifyFindAll(namespace, name, vmin, vmax, nodes):
global errors
out = namespace.FindRange(name, vmin, vmax)
if (out != nodes):
print "Found [%s] instead of[%s] for versions %f to %f of %s" % (
' '.join([str(x) for x in out]),
' '.join([str(x) for x in nodes]),
vmin,
vmax,
name)
namespace.Dump()
print "\n"
errors += 1
def Main(args):
global errors
ParseOptions(args)
InfoOut.SetConsole(True)
namespace = IDLNamespace(None)
FooXX = MockNode('foo', None, None)
Foo1X = MockNode('foo', 1.0, None)
Foo2X = MockNode('foo', 2.0, None)
Foo3X = MockNode('foo', 3.0, None)
# Verify we succeed with undeprecated adds
AddOkay(namespace, FooXX)
AddOkay(namespace, Foo1X)
AddOkay(namespace, Foo3X)
# Verify we fail to add a node between undeprecated versions
AddError(namespace, Foo2X, 'Overlap in versions.')
BarXX = MockNode('bar', None, None)
Bar12 = MockNode('bar', 1.0, 2.0)
Bar23 = MockNode('bar', 2.0, 3.0)
Bar34 = MockNode('bar', 3.0, 4.0)
# Verify we succeed with fully qualified versions
namespace = IDLNamespace(namespace)
AddOkay(namespace, BarXX)
AddOkay(namespace, Bar12)
# Verify we warn when detecting a gap
AddWarn(namespace, Bar34, 'Gap in version numbers.')
# Verify we fail when inserting into this gap
# (NOTE: while this could be legal, it is sloppy so we disallow it)
AddError(namespace, Bar23, 'Declarations out of order.')
# Verify local namespace
VerifyFindOne(namespace, 'bar', 0.0, BarXX)
VerifyFindAll(namespace, 'bar', 0.5, 1.5, [BarXX, Bar12])
# Verify the correct version of the object is found recursively
VerifyFindOne(namespace, 'foo', 0.0, FooXX)
VerifyFindOne(namespace, 'foo', 0.5, FooXX)
VerifyFindOne(namespace, 'foo', 1.0, Foo1X)
VerifyFindOne(namespace, 'foo', 1.5, Foo1X)
VerifyFindOne(namespace, 'foo', 3.0, Foo3X)
VerifyFindOne(namespace, 'foo', 100.0, Foo3X)
# Verify the correct range of objects is found
VerifyFindAll(namespace, 'foo', 0.0, 1.0, [FooXX])
VerifyFindAll(namespace, 'foo', 0.5, 1.0, [FooXX])
VerifyFindAll(namespace, 'foo', 1.0, 1.1, [Foo1X])
VerifyFindAll(namespace, 'foo', 0.5, 1.5, [FooXX, Foo1X])
VerifyFindAll(namespace, 'foo', 0.0, 3.0, [FooXX, Foo1X])
VerifyFindAll(namespace, 'foo', 3.0, 100.0, [Foo3X])
FooBar = MockNode('foobar', 1.0, 2.0)
namespace = IDLNamespace(namespace)
AddOkay(namespace, FooBar)
if errors:
print 'Test failed with %d errors.' % errors
else:
print 'Passed.'
return errors
if __name__ == '__main__':
sys.exit(Main(sys.argv[1:]))
|