Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ppapi / generators / idl_c_proto.py
blobd029f93d04d795d32aad8b7fe4d71add1eb2266c
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """ Generator for C style prototypes and definitions """
8 import glob
9 import os
10 import sys
12 from idl_log import ErrOut, InfoOut, WarnOut
13 from idl_node import IDLNode
14 from idl_ast import IDLAst
15 from idl_option import GetOption, Option, ParseOptions
16 from idl_parser import ParseFiles
18 Option('cgen_debug', 'Debug generate.')
20 class CGenError(Exception):
21 def __init__(self, msg):
22 self.value = msg
24 def __str__(self):
25 return repr(self.value)
28 def CommentLines(lines, tabs=0):
29 # Generate a C style comment block by prepending the block with '<tab>/*'
30 # and adding a '<tab> *' per line.
31 tab = ' ' * tabs
33 out = '%s/*' % tab + ('\n%s *' % tab).join(lines)
35 # Add a terminating ' */' unless the last line is blank which would mean it
36 # already has ' *'
37 if not lines[-1]:
38 out += '/\n'
39 else:
40 out += ' */\n'
41 return out
43 def Comment(node, prefix=None, tabs=0):
44 # Generate a comment block from the provided Comment node.
45 comment = node.GetName()
46 lines = comment.split('\n')
48 # If an option prefix is provided, then prepend that to the comment
49 # for this node.
50 if prefix:
51 prefix_lines = prefix.split('\n')
52 # If both the prefix and comment start with a blank line ('*') remove
53 # the extra one.
54 if prefix_lines[0] == '*' and lines[0] == '*':
55 lines = prefix_lines + lines[1:]
56 else:
57 lines = prefix_lines + lines;
58 return CommentLines(lines, tabs)
60 def GetNodeComments(node, tabs=0):
61 # Generate a comment block joining all comment nodes which are children of
62 # the provided node.
63 comment_txt = ''
64 for doc in node.GetListOf('Comment'):
65 comment_txt += Comment(doc, tabs=tabs)
66 return comment_txt
69 class CGen(object):
70 # TypeMap
72 # TypeMap modifies how an object is stored or passed, for example pointers
73 # are passed as 'const' if they are 'in' parameters, and structures are
74 # preceeded by the keyword 'struct' as well as using a pointer.
76 TypeMap = {
77 'Array': {
78 'in': 'const %s',
79 'inout': '%s',
80 'out': '%s*',
81 'store': '%s',
82 'return': '%s',
83 'ref': '%s*'
85 'Callspec': {
86 'in': '%s',
87 'inout': '%s',
88 'out': '%s',
89 'store': '%s',
90 'return': '%s'
92 'Enum': {
93 'in': '%s',
94 'inout': '%s*',
95 'out': '%s*',
96 'store': '%s',
97 'return': '%s'
99 'Interface': {
100 'in': 'const %s*',
101 'inout': '%s*',
102 'out': '%s**',
103 'return': '%s*',
104 'store': '%s*'
106 'Struct': {
107 'in': 'const %s*',
108 'inout': '%s*',
109 'out': '%s*',
110 'return': ' %s*',
111 'store': '%s',
112 'ref': '%s*'
114 'blob_t': {
115 'in': 'const %s',
116 'inout': '%s',
117 'out': '%s',
118 'return': '%s',
119 'store': '%s'
121 'mem_t': {
122 'in': 'const %s',
123 'inout': '%s',
124 'out': '%s',
125 'return': '%s',
126 'store': '%s'
128 'mem_ptr_t': {
129 'in': 'const %s',
130 'inout': '%s',
131 'out': '%s',
132 'return': '%s',
133 'store': '%s'
135 'str_t': {
136 'in': 'const %s',
137 'inout': '%s',
138 'out': '%s',
139 'return': 'const %s',
140 'store': '%s'
142 'cstr_t': {
143 'in': '%s',
144 'inout': '%s*',
145 'out': '%s*',
146 'return': '%s',
147 'store': '%s'
149 'TypeValue': {
150 'in': '%s',
151 'inout': '%s*',
152 'out': '%s*',
153 'return': '%s',
154 'store': '%s'
160 # RemapName
162 # A diction array of PPAPI types that are converted to language specific
163 # types before being returned by by the C generator
165 RemapName = {
166 'blob_t': 'void**',
167 'float_t': 'float',
168 'double_t': 'double',
169 'handle_t': 'int',
170 'mem_t': 'void*',
171 'mem_ptr_t': 'void**',
172 'str_t': 'char*',
173 'cstr_t': 'const char*',
174 'interface_t' : 'const void*'
177 def __init__(self):
178 self.dbg_depth = 0
181 # Debug Logging functions
183 def Log(self, txt):
184 if not GetOption('cgen_debug'): return
185 tabs = ' ' * self.dbg_depth
186 print '%s%s' % (tabs, txt)
188 def LogEnter(self, txt):
189 if txt: self.Log(txt)
190 self.dbg_depth += 1
192 def LogExit(self, txt):
193 self.dbg_depth -= 1
194 if txt: self.Log(txt)
197 def GetDefine(self, name, value):
198 out = '#define %s %s' % (name, value)
199 if len(out) > 80:
200 out = '#define %s \\\n %s' % (name, value)
201 return '%s\n' % out
204 # Interface strings
206 def GetMacroHelper(self, node):
207 macro = node.GetProperty('macro')
208 if macro: return macro
209 name = node.GetName()
210 name = name.upper()
211 return "%s_INTERFACE" % name
213 def GetInterfaceMacro(self, node, version = None):
214 name = self.GetMacroHelper(node)
215 if version is None:
216 return name
217 return '%s_%s' % (name, str(version).replace('.', '_'))
219 def GetInterfaceString(self, node, version = None):
220 # If an interface name is specified, use that
221 name = node.GetProperty('iname')
222 if not name:
223 # Otherwise, the interface name is the object's name
224 # With '_Dev' replaced by '(Dev)' if it's a Dev interface.
225 name = node.GetName()
226 if name.endswith('_Dev'):
227 name = '%s(Dev)' % name[:-4]
228 if version is None:
229 return name
230 return "%s;%s" % (name, version)
234 # Return the array specification of the object.
236 def GetArraySpec(self, node):
237 assert(node.cls == 'Array')
238 fixed = node.GetProperty('FIXED')
239 if fixed:
240 return '[%s]' % fixed
241 else:
242 return '[]'
245 # GetTypeName
247 # For any valid 'typed' object such as Member or Typedef
248 # the typenode object contains the typename
250 # For a given node return the type name by passing mode.
252 def GetTypeName(self, node, release, prefix=''):
253 self.LogEnter('GetTypeName of %s rel=%s' % (node, release))
255 # For Members, Params, and Typedefs get the type it refers to otherwise
256 # the node in question is it's own type (struct, union etc...)
257 if node.IsA('Member', 'Param', 'Typedef'):
258 typeref = node.GetType(release)
259 else:
260 typeref = node
262 if typeref is None:
263 node.Error('No type at release %s.' % release)
264 raise CGenError('No type for %s' % node)
266 # If the type is a (BuiltIn) Type then return it's name
267 # remapping as needed
268 if typeref.IsA('Type'):
269 name = CGen.RemapName.get(typeref.GetName(), None)
270 if name is None: name = typeref.GetName()
271 name = '%s%s' % (prefix, name)
273 # For Interfaces, use the name + version
274 elif typeref.IsA('Interface'):
275 rel = typeref.first_release[release]
276 name = 'struct %s%s' % (prefix, self.GetStructName(typeref, rel, True))
278 # For structures, preceed with 'struct' or 'union' as appropriate
279 elif typeref.IsA('Struct'):
280 if typeref.GetProperty('union'):
281 name = 'union %s%s' % (prefix, typeref.GetName())
282 else:
283 name = 'struct %s%s' % (prefix, typeref.GetName())
285 # If it's an enum, or typedef then return the Enum's name
286 elif typeref.IsA('Enum', 'Typedef'):
287 if not typeref.LastRelease(release):
288 first = node.first_release[release]
289 ver = '_' + node.GetVersion(first).replace('.','_')
290 else:
291 ver = ''
292 # The enum may have skipped having a typedef, we need prefix with 'enum'.
293 if typeref.GetProperty('notypedef'):
294 name = 'enum %s%s%s' % (prefix, typeref.GetName(), ver)
295 else:
296 name = '%s%s%s' % (prefix, typeref.GetName(), ver)
298 else:
299 raise RuntimeError('Getting name of non-type %s.' % node)
300 self.LogExit('GetTypeName %s is %s' % (node, name))
301 return name
305 # GetRootType
307 # For a given node return basic type of that object. This is
308 # either a 'Type', 'Callspec', or 'Array'
310 def GetRootTypeMode(self, node, release, mode):
311 self.LogEnter('GetRootType of %s' % node)
312 # If it has an array spec, then treat it as an array regardless of type
313 if node.GetOneOf('Array'):
314 rootType = 'Array'
315 # Or if it has a callspec, treat it as a function
316 elif node.GetOneOf('Callspec'):
317 rootType, mode = self.GetRootTypeMode(node.GetType(release), release,
318 'return')
320 # If it's a plain typedef, try that object's root type
321 elif node.IsA('Member', 'Param', 'Typedef'):
322 rootType, mode = self.GetRootTypeMode(node.GetType(release),
323 release, mode)
325 # If it's an Enum, then it's normal passing rules
326 elif node.IsA('Enum'):
327 rootType = node.cls
329 # If it's an Interface or Struct, we may be passing by value
330 elif node.IsA('Interface', 'Struct'):
331 if mode == 'return':
332 if node.GetProperty('returnByValue'):
333 rootType = 'TypeValue'
334 else:
335 rootType = node.cls
336 else:
337 if node.GetProperty('passByValue'):
338 rootType = 'TypeValue'
339 else:
340 rootType = node.cls
342 # If it's an Basic Type, check if it's a special type
343 elif node.IsA('Type'):
344 if node.GetName() in CGen.TypeMap:
345 rootType = node.GetName()
346 else:
347 rootType = 'TypeValue'
348 else:
349 raise RuntimeError('Getting root type of non-type %s.' % node)
350 self.LogExit('RootType is "%s"' % rootType)
351 return rootType, mode
354 def GetTypeByMode(self, node, release, mode):
355 self.LogEnter('GetTypeByMode of %s mode=%s release=%s' %
356 (node, mode, release))
357 name = self.GetTypeName(node, release)
358 ntype, mode = self.GetRootTypeMode(node, release, mode)
359 out = CGen.TypeMap[ntype][mode] % name
360 self.LogExit('GetTypeByMode %s = %s' % (node, out))
361 return out
364 # Get the passing mode of the object (in, out, inout).
365 def GetParamMode(self, node):
366 self.Log('GetParamMode for %s' % node)
367 if node.GetProperty('in'): return 'in'
368 if node.GetProperty('out'): return 'out'
369 if node.GetProperty('inout'): return 'inout'
370 return 'return'
373 # GetComponents
375 # Returns the signature components of an object as a tuple of
376 # (rtype, name, arrays, callspec) where:
377 # rtype - The store or return type of the object.
378 # name - The name of the object.
379 # arrays - A list of array dimensions as [] or [<fixed_num>].
380 # args - None if not a function, otherwise a list of parameters.
382 def GetComponents(self, node, release, mode):
383 self.LogEnter('GetComponents mode %s for %s %s' % (mode, node, release))
385 # Generate passing type by modifying root type
386 rtype = self.GetTypeByMode(node, release, mode)
387 # If this is an array output, change it from type* foo[] to type** foo.
388 # type* foo[] means an array of pointers to type, which is confusing.
389 arrayspec = [self.GetArraySpec(array) for array in node.GetListOf('Array')]
390 if mode == 'out' and len(arrayspec) == 1 and arrayspec[0] == '[]':
391 rtype += '*'
392 del arrayspec[0]
394 if node.IsA('Enum', 'Interface', 'Struct'):
395 rname = node.GetName()
396 else:
397 rname = node.GetType(release).GetName()
399 if rname in CGen.RemapName:
400 rname = CGen.RemapName[rname]
401 if '%' in rtype:
402 rtype = rtype % rname
403 name = node.GetName()
404 callnode = node.GetOneOf('Callspec')
405 if callnode:
406 callspec = []
407 for param in callnode.GetListOf('Param'):
408 if not param.IsRelease(release):
409 continue
410 mode = self.GetParamMode(param)
411 ptype, pname, parray, pspec = self.GetComponents(param, release, mode)
412 callspec.append((ptype, pname, parray, pspec))
413 else:
414 callspec = None
416 self.LogExit('GetComponents: %s, %s, %s, %s' %
417 (rtype, name, arrayspec, callspec))
418 return (rtype, name, arrayspec, callspec)
421 def Compose(self, rtype, name, arrayspec, callspec, prefix, func_as_ptr,
422 include_name, unsized_as_ptr):
423 self.LogEnter('Compose: %s %s' % (rtype, name))
424 arrayspec = ''.join(arrayspec)
426 # Switch unsized array to a ptr. NOTE: Only last element can be unsized.
427 if unsized_as_ptr and arrayspec[-2:] == '[]':
428 prefix += '*'
429 arrayspec=arrayspec[:-2]
431 if not include_name:
432 name = prefix + arrayspec
433 else:
434 name = prefix + name + arrayspec
435 if callspec is None:
436 out = '%s %s' % (rtype, name)
437 else:
438 params = []
439 for ptype, pname, parray, pspec in callspec:
440 params.append(self.Compose(ptype, pname, parray, pspec, '', True,
441 include_name=True,
442 unsized_as_ptr=unsized_as_ptr))
443 if func_as_ptr:
444 name = '(*%s)' % name
445 if not params:
446 params = ['void']
447 out = '%s %s(%s)' % (rtype, name, ', '.join(params))
448 self.LogExit('Exit Compose: %s' % out)
449 return out
452 # GetSignature
454 # Returns the 'C' style signature of the object
455 # prefix - A prefix for the object's name
456 # func_as_ptr - Formats a function as a function pointer
457 # include_name - If true, include member name in the signature.
458 # If false, leave it out. In any case, prefix is always
459 # included.
460 # include_version - if True, include version in the member name
462 def GetSignature(self, node, release, mode, prefix='', func_as_ptr=True,
463 include_name=True, include_version=False):
464 self.LogEnter('GetSignature %s %s as func=%s' %
465 (node, mode, func_as_ptr))
466 rtype, name, arrayspec, callspec = self.GetComponents(node, release, mode)
467 if include_version:
468 name = self.GetStructName(node, release, True)
470 # If not a callspec (such as a struct) use a ptr instead of []
471 unsized_as_ptr = not callspec
473 out = self.Compose(rtype, name, arrayspec, callspec, prefix,
474 func_as_ptr, include_name, unsized_as_ptr)
476 self.LogExit('Exit GetSignature: %s' % out)
477 return out
479 # Define a Typedef.
480 def DefineTypedef(self, node, releases, prefix='', comment=False):
481 __pychecker__ = 'unusednames=comment'
482 build_list = node.GetUniqueReleases(releases)
484 out = 'typedef %s;\n' % self.GetSignature(node, build_list[-1], 'return',
485 prefix, True,
486 include_version=False)
487 # Version mangle any other versions
488 for index, rel in enumerate(build_list[:-1]):
489 out += '\n'
490 out += 'typedef %s;\n' % self.GetSignature(node, rel, 'return',
491 prefix, True,
492 include_version=True)
493 self.Log('DefineTypedef: %s' % out)
494 return out
496 # Define an Enum.
497 def DefineEnum(self, node, releases, prefix='', comment=False):
498 __pychecker__ = 'unusednames=comment,releases'
499 self.LogEnter('DefineEnum %s' % node)
500 name = '%s%s' % (prefix, node.GetName())
501 notypedef = node.GetProperty('notypedef')
502 unnamed = node.GetProperty('unnamed')
504 if unnamed:
505 out = 'enum {'
506 elif notypedef:
507 out = 'enum %s {' % name
508 else:
509 out = 'typedef enum {'
510 enumlist = []
511 for child in node.GetListOf('EnumItem'):
512 value = child.GetProperty('VALUE')
513 comment_txt = GetNodeComments(child, tabs=1)
514 if value:
515 item_txt = '%s%s = %s' % (prefix, child.GetName(), value)
516 else:
517 item_txt = '%s%s' % (prefix, child.GetName())
518 enumlist.append('%s %s' % (comment_txt, item_txt))
519 self.LogExit('Exit DefineEnum')
521 if unnamed or notypedef:
522 out = '%s\n%s\n};\n' % (out, ',\n'.join(enumlist))
523 else:
524 out = '%s\n%s\n} %s;\n' % (out, ',\n'.join(enumlist), name)
525 return out
527 def DefineMember(self, node, releases, prefix='', comment=False):
528 __pychecker__ = 'unusednames=prefix,comment'
529 release = releases[0]
530 self.LogEnter('DefineMember %s' % node)
531 if node.GetProperty('ref'):
532 out = '%s;' % self.GetSignature(node, release, 'ref', '', True)
533 else:
534 out = '%s;' % self.GetSignature(node, release, 'store', '', True)
535 self.LogExit('Exit DefineMember')
536 return out
538 def GetStructName(self, node, release, include_version=False):
539 suffix = ''
540 if include_version:
541 ver_num = node.GetVersion(release)
542 suffix = ('_%s' % ver_num).replace('.', '_')
543 return node.GetName() + suffix
545 def DefineStructInternals(self, node, release,
546 include_version=False, comment=True):
547 channel = node.GetProperty('FILE').release_map.GetChannel(release)
548 if channel == 'dev':
549 channel_comment = ' /* dev */'
550 else:
551 channel_comment = ''
552 out = ''
553 if node.GetProperty('union'):
554 out += 'union %s {%s\n' % (
555 self.GetStructName(node, release, include_version), channel_comment)
556 else:
557 out += 'struct %s {%s\n' % (
558 self.GetStructName(node, release, include_version), channel_comment)
560 channel = node.GetProperty('FILE').release_map.GetChannel(release)
561 # Generate Member Functions
562 members = []
563 for child in node.GetListOf('Member'):
564 if channel == 'stable' and child.NodeIsDevOnly():
565 continue
566 member = self.Define(child, [release], tabs=1, comment=comment)
567 if not member:
568 continue
569 members.append(member)
570 out += '%s\n};\n' % '\n'.join(members)
571 return out
574 def DefineStruct(self, node, releases, prefix='', comment=False):
575 __pychecker__ = 'unusednames=comment,prefix'
576 self.LogEnter('DefineStruct %s' % node)
577 out = ''
578 build_list = node.GetUniqueReleases(releases)
580 newest_stable = None
581 newest_dev = None
582 for rel in build_list:
583 channel = node.GetProperty('FILE').release_map.GetChannel(rel)
584 if channel == 'stable':
585 newest_stable = rel
586 if channel == 'dev':
587 newest_dev = rel
588 last_rel = build_list[-1]
590 # TODO(noelallen) : Bug 157017 finish multiversion support
591 if node.IsA('Struct'):
592 if len(build_list) != 1:
593 node.Error('Can not support multiple versions of node.')
594 assert len(build_list) == 1
595 # Build the most recent one versioned, with comments
596 out = self.DefineStructInternals(node, last_rel,
597 include_version=False, comment=True)
599 if node.IsA('Interface'):
600 # Build the most recent one versioned, with comments
601 out = self.DefineStructInternals(node, last_rel,
602 include_version=True, comment=True)
603 if last_rel == newest_stable:
604 # Define an unversioned typedef for the most recent version
605 out += '\ntypedef struct %s %s;\n' % (
606 self.GetStructName(node, last_rel, include_version=True),
607 self.GetStructName(node, last_rel, include_version=False))
609 # Build the rest without comments and with the version number appended
610 for rel in build_list[0:-1]:
611 channel = node.GetProperty('FILE').release_map.GetChannel(rel)
612 # Skip dev channel interface versions that are
613 # Not the newest version, and
614 # Don't have an equivalent stable version.
615 if channel == 'dev' and rel != newest_dev:
616 if not node.DevInterfaceMatchesStable(rel):
617 continue
618 out += '\n' + self.DefineStructInternals(node, rel,
619 include_version=True,
620 comment=False)
621 if rel == newest_stable:
622 # Define an unversioned typedef for the most recent version
623 out += '\ntypedef struct %s %s;\n' % (
624 self.GetStructName(node, rel, include_version=True),
625 self.GetStructName(node, rel, include_version=False))
627 self.LogExit('Exit DefineStruct')
628 return out
632 # Copyright and Comment
634 # Generate a comment or copyright block
636 def Copyright(self, node, cpp_style=False):
637 lines = node.GetName().split('\n')
638 if cpp_style:
639 return '//' + '\n//'.join(filter(lambda f: f != '', lines)) + '\n'
640 return CommentLines(lines)
643 def Indent(self, data, tabs=0):
644 """Handles indentation and 80-column line wrapping."""
645 tab = ' ' * tabs
646 lines = []
647 for line in data.split('\n'):
648 # Add indentation
649 line = tab + line
650 space_break = line.rfind(' ', 0, 80)
651 if len(line) <= 80 or 'http://' in line:
652 # Ignore normal line and URLs permitted by the style guide.
653 lines.append(line.rstrip())
654 elif not '(' in line and space_break >= 0:
655 # Break long typedefs on nearest space.
656 lines.append(line[0:space_break])
657 lines.append(' ' + line[space_break + 1:])
658 else:
659 left = line.rfind('(') + 1
660 args = line[left:].split(',')
661 orig_args = args
662 orig_left = left
663 # Try to split on '(arg1)' or '(arg1, arg2)', not '()'
664 while args[0][0] == ')':
665 left = line.rfind('(', 0, left - 1) + 1
666 if left == 0: # No more parens, take the original option
667 args = orig_args
668 left = orig_left
669 break
670 args = line[left:].split(',')
672 line_max = 0
673 for arg in args:
674 if len(arg) > line_max: line_max = len(arg)
676 if left + line_max >= 80:
677 indent = '%s ' % tab
678 args = (',\n%s' % indent).join([arg.strip() for arg in args])
679 lines.append('%s\n%s%s' % (line[:left], indent, args))
680 else:
681 indent = ' ' * (left - 1)
682 args = (',\n%s' % indent).join(args)
683 lines.append('%s%s' % (line[:left], args))
684 return '\n'.join(lines)
687 # Define a top level object.
688 def Define(self, node, releases, tabs=0, prefix='', comment=False):
689 # If this request does not match unique release, or if the release is not
690 # available (possibly deprecated) then skip.
691 unique = node.GetUniqueReleases(releases)
692 if not unique or not node.InReleases(releases):
693 return ''
695 self.LogEnter('Define %s tab=%d prefix="%s"' % (node,tabs,prefix))
696 declmap = dict({
697 'Enum': CGen.DefineEnum,
698 'Function': CGen.DefineMember,
699 'Interface': CGen.DefineStruct,
700 'Member': CGen.DefineMember,
701 'Struct': CGen.DefineStruct,
702 'Typedef': CGen.DefineTypedef
705 out = ''
706 func = declmap.get(node.cls, None)
707 if not func:
708 ErrOut.Log('Failed to define %s named %s' % (node.cls, node.GetName()))
709 define_txt = func(self, node, releases, prefix=prefix, comment=comment)
711 comment_txt = GetNodeComments(node, tabs=0)
712 if comment_txt and comment:
713 out += comment_txt
714 out += define_txt
716 indented_out = self.Indent(out, tabs)
717 self.LogExit('Exit Define')
718 return indented_out
721 # Clean a string representing an object definition and return then string
722 # as a single space delimited set of tokens.
723 def CleanString(instr):
724 instr = instr.strip()
725 instr = instr.split()
726 return ' '.join(instr)
729 # Test a file, by comparing all it's objects, with their comments.
730 def TestFile(filenode):
731 cgen = CGen()
733 errors = 0
734 for node in filenode.GetChildren()[2:]:
735 instr = node.GetOneOf('Comment')
736 if not instr: continue
737 instr.Dump()
738 instr = CleanString(instr.GetName())
740 outstr = cgen.Define(node, releases=['M14'])
741 if GetOption('verbose'):
742 print outstr + '\n'
743 outstr = CleanString(outstr)
745 if instr != outstr:
746 ErrOut.Log('Failed match of\n>>%s<<\nto:\n>>%s<<\nFor:\n' %
747 (instr, outstr))
748 node.Dump(1, comments=True)
749 errors += 1
750 return errors
753 # Build and resolve the AST and compare each file individual.
754 def TestFiles(filenames):
755 if not filenames:
756 idldir = os.path.split(sys.argv[0])[0]
757 idldir = os.path.join(idldir, 'test_cgen', '*.idl')
758 filenames = glob.glob(idldir)
760 filenames = sorted(filenames)
761 ast = ParseFiles(filenames)
763 total_errs = 0
764 for filenode in ast.GetListOf('File'):
765 errs = TestFile(filenode)
766 if errs:
767 ErrOut.Log('%s test failed with %d error(s).' %
768 (filenode.GetName(), errs))
769 total_errs += errs
771 if total_errs:
772 ErrOut.Log('Failed generator test.')
773 else:
774 InfoOut.Log('Passed generator test.')
775 return total_errs
777 def main(args):
778 filenames = ParseOptions(args)
779 if GetOption('test'):
780 return TestFiles(filenames)
781 ast = ParseFiles(filenames)
782 cgen = CGen()
783 for f in ast.GetListOf('File'):
784 if f.GetProperty('ERRORS') > 0:
785 print 'Skipping %s' % f.GetName()
786 continue
787 for node in f.GetChildren()[2:]:
788 print cgen.Define(node, ast.releases, comment=True, prefix='tst_')
791 if __name__ == '__main__':
792 sys.exit(main(sys.argv[1:]))