Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """ Generator for C style prototypes and definitions """ | 6 """ Generator for C style prototypes and definitions """ |
| 7 | 7 |
| 8 import glob | 8 import glob |
| 9 import os | 9 import os |
| 10 import sys | 10 import sys |
| (...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 419 # | 419 # |
| 420 # GetSignature | 420 # GetSignature |
| 421 # | 421 # |
| 422 # Returns the 'C' style signature of the object | 422 # Returns the 'C' style signature of the object |
| 423 # prefix - A prefix for the object's name | 423 # prefix - A prefix for the object's name |
| 424 # func_as_ptr - Formats a function as a function pointer | 424 # func_as_ptr - Formats a function as a function pointer |
| 425 # ptr_prefix - A prefix that goes before the "*" for a function pointer | 425 # ptr_prefix - A prefix that goes before the "*" for a function pointer |
| 426 # include_name - If true, include member name in the signature. | 426 # include_name - If true, include member name in the signature. |
| 427 # If false, leave it out. In any case, prefix and ptr_prefix | 427 # If false, leave it out. In any case, prefix and ptr_prefix |
| 428 # are always included. | 428 # are always included. |
| 429 # include_version - if True, include version in the member name | |
| 429 # | 430 # |
| 430 def GetSignature(self, node, release, mode, prefix='', func_as_ptr=True, | 431 def GetSignature(self, node, release, mode, prefix='', func_as_ptr=True, |
| 431 ptr_prefix='', include_name=True): | 432 ptr_prefix='', include_name=True, include_version=False): |
| 432 self.LogEnter('GetSignature %s %s as func=%s' % | 433 self.LogEnter('GetSignature %s %s as func=%s' % |
| 433 (node, mode, func_as_ptr)) | 434 (node, mode, func_as_ptr)) |
| 434 rtype, name, arrayspec, callspec = self.GetComponents(node, release, mode) | 435 rtype, name, arrayspec, callspec = self.GetComponents(node, release, mode) |
| 436 if include_version: | |
| 437 name = self.GetStructName(node, release, True) | |
| 435 out = self.Compose(rtype, name, arrayspec, callspec, prefix, | 438 out = self.Compose(rtype, name, arrayspec, callspec, prefix, |
| 436 func_as_ptr, ptr_prefix, include_name) | 439 func_as_ptr, ptr_prefix, include_name) |
| 437 self.LogExit('Exit GetSignature: %s' % out) | 440 self.LogExit('Exit GetSignature: %s' % out) |
| 438 return out | 441 return out |
| 439 | 442 |
| 440 # Define a Typedef. | 443 # Define a Typedef. |
| 441 def DefineTypedef(self, node, releases, prefix='', comment=False): | 444 def DefineTypedef(self, node, releases, prefix='', comment=False): |
| 442 __pychecker__ = 'unusednames=comment' | 445 __pychecker__ = 'unusednames=comment' |
| 443 build_list = node.GetUniqueReleases(releases) | 446 build_list = node.GetUniqueReleases(releases) |
| 444 | 447 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 560 # | 563 # |
| 561 # Copyright and Comment | 564 # Copyright and Comment |
| 562 # | 565 # |
| 563 # Generate a comment or copyright block | 566 # Generate a comment or copyright block |
| 564 # | 567 # |
| 565 def Copyright(self, node, tabs=0): | 568 def Copyright(self, node, tabs=0): |
| 566 lines = node.GetName().split('\n') | 569 lines = node.GetName().split('\n') |
| 567 return CommentLines(lines, tabs) | 570 return CommentLines(lines, tabs) |
| 568 | 571 |
| 569 | 572 |
| 573 def Indent(self, data, tabs=0): | |
| 574 """Handles indentation and 80-column line wrapping.""" | |
| 575 tab = ' ' * tabs | |
| 576 lines = [] | |
| 577 for line in data.split('\n'): | |
| 578 # Add indentation | |
| 579 line = tab + line | |
| 580 if len(line) > 80: | |
| 581 left = line.rfind('(') + 1 | |
| 582 args = line[left:].split(',') | |
| 583 orig_args = args | |
| 584 orig_left = left | |
| 585 # Look for a better split. | |
| 586 while args[0][0] == ')': | |
|
dmichael (off chromium)
2012/11/16 18:24:44
Can you maybe put an example of the kind of code y
teravest
2012/11/16 18:52:36
Sure. Comment added.
| |
| 587 left = line.rfind('(', 0, left - 1) + 1 | |
| 588 if left == 0: # No more parens, take the original option | |
| 589 args = orig_args | |
| 590 left = orig_left | |
| 591 break | |
| 592 args = line[left:].split(',') | |
| 593 | |
| 594 line_max = 0 | |
| 595 for arg in args: | |
| 596 if len(arg) > line_max: line_max = len(arg) | |
| 597 | |
| 598 if left + line_max >= 80: | |
| 599 space = '%s ' % tab | |
|
dmichael (off chromium)
2012/11/16 18:24:44
indent might be a better name than space?
teravest
2012/11/16 18:52:36
Done. I had left this unchanged from the previous
| |
| 600 args = (',\n%s' % space).join([arg.strip() for arg in args]) | |
| 601 lines.append('%s\n%s%s' % (line[:left], space, args)) | |
| 602 else: | |
| 603 space = ' ' * (left - 1) | |
| 604 args = (',\n%s' % space).join(args) | |
| 605 lines.append('%s%s' % (line[:left], args)) | |
| 606 else: | |
| 607 lines.append(line.rstrip()) | |
|
dmichael (off chromium)
2012/11/16 18:24:44
I think I'd flip the if and the else, since in the
teravest
2012/11/16 18:52:36
Done.
| |
| 608 return '\n'.join(lines) | |
| 609 | |
| 610 | |
| 570 # Define a top level object. | 611 # Define a top level object. |
| 571 def Define(self, node, releases, tabs=0, prefix='', comment=False): | 612 def Define(self, node, releases, tabs=0, prefix='', comment=False): |
| 572 # If this request does not match unique release, or if the release is not | 613 # If this request does not match unique release, or if the release is not |
| 573 # available (possibly deprecated) then skip. | 614 # available (possibly deprecated) then skip. |
| 574 unique = node.GetUniqueReleases(releases) | 615 unique = node.GetUniqueReleases(releases) |
| 575 if not unique or not node.InReleases(releases): | 616 if not unique or not node.InReleases(releases): |
| 576 return '' | 617 return '' |
| 577 | 618 |
| 578 self.LogEnter('Define %s tab=%d prefix="%s"' % (node,tabs,prefix)) | 619 self.LogEnter('Define %s tab=%d prefix="%s"' % (node,tabs,prefix)) |
| 579 declmap = dict({ | 620 declmap = dict({ |
| 580 'Enum': CGen.DefineEnum, | 621 'Enum': CGen.DefineEnum, |
| 581 'Function': CGen.DefineMember, | 622 'Function': CGen.DefineMember, |
| 582 'Interface': CGen.DefineStruct, | 623 'Interface': CGen.DefineStruct, |
| 583 'Member': CGen.DefineMember, | 624 'Member': CGen.DefineMember, |
| 584 'Struct': CGen.DefineStruct, | 625 'Struct': CGen.DefineStruct, |
| 585 'Typedef': CGen.DefineTypedef | 626 'Typedef': CGen.DefineTypedef |
| 586 }) | 627 }) |
| 587 | 628 |
| 588 out = '' | 629 out = '' |
| 589 func = declmap.get(node.cls, None) | 630 func = declmap.get(node.cls, None) |
| 590 if not func: | 631 if not func: |
| 591 ErrOut.Log('Failed to define %s named %s' % (node.cls, node.GetName())) | 632 ErrOut.Log('Failed to define %s named %s' % (node.cls, node.GetName())) |
| 592 define_txt = func(self, node, releases, prefix=prefix, comment=comment) | 633 define_txt = func(self, node, releases, prefix=prefix, comment=comment) |
| 593 | 634 |
| 594 comment_txt = GetNodeComments(node, tabs=0) | 635 comment_txt = GetNodeComments(node, tabs=0) |
| 595 if comment_txt and comment: | 636 if comment_txt and comment: |
| 596 out += comment_txt | 637 out += comment_txt |
| 597 out += define_txt | 638 out += define_txt |
| 598 | 639 |
| 599 tab = ' ' * tabs | 640 indented_out = self.Indent(out, tabs) |
| 600 lines = [] | 641 self.LogExit('Exit Define') |
| 601 for line in out.split('\n'): | 642 return indented_out |
| 602 # Add indentation | |
| 603 line = tab + line | |
| 604 if len(line) > 80: | |
| 605 left = line.rfind('(') + 1 | |
| 606 args = line[left:].split(',') | |
| 607 line_max = 0 | |
| 608 for arg in args: | |
| 609 if len(arg) > line_max: line_max = len(arg) | |
| 610 | 643 |
| 611 if left + line_max >= 80: | |
| 612 space = '%s ' % tab | |
| 613 args = (',\n%s' % space).join([arg.strip() for arg in args]) | |
| 614 lines.append('%s\n%s%s' % (line[:left], space, args)) | |
| 615 else: | |
| 616 space = ' ' * (left - 1) | |
| 617 args = (',\n%s' % space).join(args) | |
| 618 lines.append('%s%s' % (line[:left], args)) | |
| 619 else: | |
| 620 lines.append(line.rstrip()) | |
| 621 self.LogExit('Exit Define') | |
| 622 return '\n'.join(lines) | |
| 623 | 644 |
| 624 # Clean a string representing an object definition and return then string | 645 # Clean a string representing an object definition and return then string |
| 625 # as a single space delimited set of tokens. | 646 # as a single space delimited set of tokens. |
| 626 def CleanString(instr): | 647 def CleanString(instr): |
| 627 instr = instr.strip() | 648 instr = instr.strip() |
| 628 instr = instr.split() | 649 instr = instr.split() |
| 629 return ' '.join(instr) | 650 return ' '.join(instr) |
| 630 | 651 |
| 631 | 652 |
| 632 # Test a file, by comparing all it's objects, with their comments. | 653 # Test a file, by comparing all it's objects, with their comments. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 685 for f in ast.GetListOf('File'): | 706 for f in ast.GetListOf('File'): |
| 686 if f.GetProperty('ERRORS') > 0: | 707 if f.GetProperty('ERRORS') > 0: |
| 687 print 'Skipping %s' % f.GetName() | 708 print 'Skipping %s' % f.GetName() |
| 688 continue | 709 continue |
| 689 for node in f.GetChildren()[2:]: | 710 for node in f.GetChildren()[2:]: |
| 690 print cgen.Define(node, comment=True, prefix='tst_') | 711 print cgen.Define(node, comment=True, prefix='tst_') |
| 691 | 712 |
| 692 | 713 |
| 693 if __name__ == '__main__': | 714 if __name__ == '__main__': |
| 694 sys.exit(Main(sys.argv[1:])) | 715 sys.exit(Main(sys.argv[1:])) |
| 695 | |
|
noelallen1
2012/11/15 22:03:18
Make sure to leave blank line to prevent windows p
teravest
2012/11/16 17:33:36
Done.
| |
| OLD | NEW |