|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding=utf8 |
| 3 | + |
| 4 | +""" |
| 5 | +Convert selected @objc attributes in a source file into access notes, removing |
| 6 | +the originals in the process. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import print_function |
| 10 | + |
| 11 | +import io |
| 12 | +import re |
| 13 | +import sys |
| 14 | + |
| 15 | +# |
| 16 | +# Entry point |
| 17 | +# |
| 18 | + |
| 19 | + |
| 20 | +def main(): |
| 21 | + if len(sys.argv) != 4: |
| 22 | + print('Too few args to ' + sys.argv[0]) |
| 23 | + print('Usage: access-note-gen.py <input-file> <output-source-file> ' + |
| 24 | + '<output-access-notes-file>') |
| 25 | + sys.exit(1) |
| 26 | + |
| 27 | + with io.open(sys.argv[1], mode='r', encoding='utf8') as input_file, \ |
| 28 | + io.open(sys.argv[2], mode='w', encoding='utf8') as output_file, \ |
| 29 | + io.open(sys.argv[3], mode='w', encoding='utf8') as access_notes_file: |
| 30 | + |
| 31 | + # Add header to access notes file |
| 32 | + access_notes_file.write(u"""\ |
| 33 | +Reason: 'fancy tests' |
| 34 | +Notes:""") |
| 35 | + |
| 36 | + # Loop over input lines, transforming them into output lines, writing access |
| 37 | + # notes as a side effect. |
| 38 | + for input_line in input_file: |
| 39 | + # Look for access-note-move comments. |
| 40 | + input_line = access_note_move_re.sub(replacer(move_at_objc_to_access_note, |
| 41 | + access_notes_file), |
| 42 | + input_line, count=1) |
| 43 | + |
| 44 | + # Look for access-note-adjust comments. |
| 45 | + input_line = adjust_comment_re.sub(replacer(adjust_comments), |
| 46 | + input_line, count=1) |
| 47 | + |
| 48 | + output_file.write(input_line) |
| 49 | + |
| 50 | + |
| 51 | +# |
| 52 | +# Offsets |
| 53 | +# |
| 54 | + |
| 55 | +"""Matches an @±N offset.""" |
| 56 | +offset_re_fragment = r'[ \t]*(?:@([+-]\d+))?[ \t]*' |
| 57 | + |
| 58 | + |
| 59 | +def offsetify(*offsets): |
| 60 | + """Sum line offsets matched by offset_re_fragment and convert them to strings |
| 61 | + like @+3 or @-2.""" |
| 62 | + |
| 63 | + offset = sum([int(o) for o in offsets if o is not None]) |
| 64 | + if offset < 0: |
| 65 | + return u"@-" + str(-offset) |
| 66 | + elif offset > 0: |
| 67 | + return u"@+" + str(offset) |
| 68 | + else: |
| 69 | + return u"" |
| 70 | + |
| 71 | + |
| 72 | +# |
| 73 | +# Adjusting comments |
| 74 | +# |
| 75 | + |
| 76 | +"""Matches expected-warning/note/remark and its offset.""" |
| 77 | +expected_other_diag_re = re.compile(r'expected-(warning|note|remark)' + |
| 78 | + offset_re_fragment) |
| 79 | + |
| 80 | +"""Matches expected-error and its offset.""" |
| 81 | +expected_error_re = re.compile(r'expected-error' + offset_re_fragment) |
| 82 | + |
| 83 | +"""Matches the string 'marked @objc'.""" |
| 84 | +marked_objc_re = re.compile(r'marked @objc') |
| 85 | + |
| 86 | +"""Matches any non-none fix-it expectation.""" |
| 87 | +fixit_re = re.compile(r'{{\d+-\d+=[^}]*}}') |
| 88 | + |
| 89 | + |
| 90 | +def adjust_comments(offset, comment_str): |
| 91 | + """Replace expected-errors with expected-remarks, and make other adjustments |
| 92 | + to diagnostics so that they reflect access notes.""" |
| 93 | + |
| 94 | + adjusted = expected_other_diag_re.sub(lambda m: u"expected-" + m.group(1) + |
| 95 | + offsetify(offset, m.group(2)), |
| 96 | + comment_str) |
| 97 | + adjusted = expected_error_re.sub(lambda m: u"expected-remark" + |
| 98 | + offsetify(offset, m.group(1)), |
| 99 | + adjusted) |
| 100 | + adjusted = marked_objc_re.sub(u"marked @objc by an access note", adjusted) |
| 101 | + adjusted = fixit_re.sub(u"{{none}}", adjusted) |
| 102 | + |
| 103 | + return u"// [expectations adjusted] " + adjusted |
| 104 | + |
| 105 | + |
| 106 | +# |
| 107 | +# Writing attrs to access notes |
| 108 | +# |
| 109 | + |
| 110 | +def move_at_objc_to_access_note(access_notes_file, arg, offset, access_note_name): |
| 111 | + """Write an @objc attribute into an access notes file, then return the |
| 112 | + string that will replace the attribute and trailing comment.""" |
| 113 | + |
| 114 | + access_notes_file.write(u""" |
| 115 | +- Name: '{}' |
| 116 | + ObjC: true""".format(access_note_name)) |
| 117 | + if arg: |
| 118 | + access_notes_file.write(u""" |
| 119 | + ObjCName: '{}'""".format(arg)) |
| 120 | + |
| 121 | + # Default to shifting expected diagnostics down 1 line. |
| 122 | + if offset is None: |
| 123 | + offset = 1 |
| 124 | + |
| 125 | + return u"// access-note-adjust" + offsetify(offset) + u" [attr moved] " + \ |
| 126 | + u"expected-remark{{access note for fancy tests adds attribute 'objc' to " + \ |
| 127 | + u"this }} expected-note{{add attribute explicitly to silence this warning}}" |
| 128 | + |
| 129 | + |
| 130 | +# |
| 131 | +# Matching lines |
| 132 | +# |
| 133 | + |
| 134 | +"""Matches '@objc(foo) // access-note-move{{access-note-name}}' |
| 135 | + or '@objc // access-note-move{{access-note-name}}'""" |
| 136 | +access_note_move_re = re.compile(r'@objc(?:\(([\w:]+)\))?[ \t]*' + |
| 137 | + r'//[ \t]*access-note-move' + |
| 138 | + offset_re_fragment + |
| 139 | + r'\{\{([^}]*)\}\}') |
| 140 | + |
| 141 | +"""Matches // access-note-adjust <comment>""" |
| 142 | +adjust_comment_re = re.compile(r'//[ \t]*access-note-adjust' + offset_re_fragment + |
| 143 | + r'(.*)') |
| 144 | + |
| 145 | + |
| 146 | +def replacer(fn, *args): |
| 147 | + """Returns a lambda which calls fn with args, followed by the groups from |
| 148 | + the match passed to the lambda.""" |
| 149 | + |
| 150 | + return lambda m: fn(*(args + m.groups())) |
| 151 | + |
| 152 | + |
| 153 | +main() |
0 commit comments