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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 | 558 |
556 self.LogExit('Exit DefineStruct') | 559 self.LogExit('Exit DefineStruct') |
557 return out | 560 return out |
558 | 561 |
559 | 562 |
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, cpp_style=False): |
566 lines = node.GetName().split('\n') | 569 lines = node.GetName().split('\n') |
567 return CommentLines(lines, tabs) | 570 if cpp_style: |
| 571 return '//' + '\n//'.join(filter(lambda f: f != '', lines)) + '\n' |
| 572 return CommentLines(lines) |
| 573 |
| 574 |
| 575 def Indent(self, data, tabs=0): |
| 576 """Handles indentation and 80-column line wrapping.""" |
| 577 tab = ' ' * tabs |
| 578 lines = [] |
| 579 for line in data.split('\n'): |
| 580 # Add indentation |
| 581 line = tab + line |
| 582 if len(line) <= 80: |
| 583 lines.append(line.rstrip()) |
| 584 else: |
| 585 left = line.rfind('(') + 1 |
| 586 args = line[left:].split(',') |
| 587 orig_args = args |
| 588 orig_left = left |
| 589 # Try to split on '(arg1)' or '(arg1, arg2)', not '()' |
| 590 while args[0][0] == ')': |
| 591 left = line.rfind('(', 0, left - 1) + 1 |
| 592 if left == 0: # No more parens, take the original option |
| 593 args = orig_args |
| 594 left = orig_left |
| 595 break |
| 596 args = line[left:].split(',') |
| 597 |
| 598 line_max = 0 |
| 599 for arg in args: |
| 600 if len(arg) > line_max: line_max = len(arg) |
| 601 |
| 602 if left + line_max >= 80: |
| 603 indent = '%s ' % tab |
| 604 args = (',\n%s' % indent).join([arg.strip() for arg in args]) |
| 605 lines.append('%s\n%s%s' % (line[:left], indent, args)) |
| 606 else: |
| 607 indent = ' ' * (left - 1) |
| 608 args = (',\n%s' % indent).join(args) |
| 609 lines.append('%s%s' % (line[:left], args)) |
| 610 return '\n'.join(lines) |
568 | 611 |
569 | 612 |
570 # Define a top level object. | 613 # Define a top level object. |
571 def Define(self, node, releases, tabs=0, prefix='', comment=False): | 614 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 | 615 # If this request does not match unique release, or if the release is not |
573 # available (possibly deprecated) then skip. | 616 # available (possibly deprecated) then skip. |
574 unique = node.GetUniqueReleases(releases) | 617 unique = node.GetUniqueReleases(releases) |
575 if not unique or not node.InReleases(releases): | 618 if not unique or not node.InReleases(releases): |
576 return '' | 619 return '' |
577 | 620 |
(...skipping 11 matching lines...) Expand all Loading... |
589 func = declmap.get(node.cls, None) | 632 func = declmap.get(node.cls, None) |
590 if not func: | 633 if not func: |
591 ErrOut.Log('Failed to define %s named %s' % (node.cls, node.GetName())) | 634 ErrOut.Log('Failed to define %s named %s' % (node.cls, node.GetName())) |
592 define_txt = func(self, node, releases, prefix=prefix, comment=comment) | 635 define_txt = func(self, node, releases, prefix=prefix, comment=comment) |
593 | 636 |
594 comment_txt = GetNodeComments(node, tabs=0) | 637 comment_txt = GetNodeComments(node, tabs=0) |
595 if comment_txt and comment: | 638 if comment_txt and comment: |
596 out += comment_txt | 639 out += comment_txt |
597 out += define_txt | 640 out += define_txt |
598 | 641 |
599 tab = ' ' * tabs | 642 indented_out = self.Indent(out, tabs) |
600 lines = [] | 643 self.LogExit('Exit Define') |
601 for line in out.split('\n'): | 644 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 | 645 |
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 | 646 |
624 # Clean a string representing an object definition and return then string | 647 # Clean a string representing an object definition and return then string |
625 # as a single space delimited set of tokens. | 648 # as a single space delimited set of tokens. |
626 def CleanString(instr): | 649 def CleanString(instr): |
627 instr = instr.strip() | 650 instr = instr.strip() |
628 instr = instr.split() | 651 instr = instr.split() |
629 return ' '.join(instr) | 652 return ' '.join(instr) |
630 | 653 |
631 | 654 |
632 # Test a file, by comparing all it's objects, with their comments. | 655 # Test a file, by comparing all it's objects, with their comments. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
686 if f.GetProperty('ERRORS') > 0: | 709 if f.GetProperty('ERRORS') > 0: |
687 print 'Skipping %s' % f.GetName() | 710 print 'Skipping %s' % f.GetName() |
688 continue | 711 continue |
689 for node in f.GetChildren()[2:]: | 712 for node in f.GetChildren()[2:]: |
690 print cgen.Define(node, comment=True, prefix='tst_') | 713 print cgen.Define(node, comment=True, prefix='tst_') |
691 | 714 |
692 | 715 |
693 if __name__ == '__main__': | 716 if __name__ == '__main__': |
694 sys.exit(Main(sys.argv[1:])) | 717 sys.exit(Main(sys.argv[1:])) |
695 | 718 |
OLD | NEW |