forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomit-needless-words.py
executable file
·148 lines (124 loc) · 4.45 KB
/
omit-needless-words.py
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
#!/usr/bin/env python
# This tool helps assess the impact of automatically applying
# heuristics that omit 'needless' words from APIs imported from Clang
# into Swift.
import getopt
import sys
import subprocess
# Print help
def help():
print('omit-needless-words.py [options] -m <modulename>')
print('')
print('Summary:')
print("\tDetermines the effects of omitting 'needless' words from imported APIs")
print('')
print('Options:')
print('\t-s <sdkname>\t\t\tThe SDK to use (e.g., macosx)')
print("\t--sdk=<sdkname>'\t\tDefaults to 'macosx'")
print('')
print('\t-t <triple>\t\t\tThe target triple to use (e.g., x86_64-apple-macosx10.10)')
print("\t--target=<triple>'\t\tDefaults to 'x86_64-apple-macosx10.10'")
print('')
print('\t-i <executable>\t\t\tThe swift-ide-test executable')
print("\t--swift-ide-test=<executable>\tDefaults to 'swift-ide-test'")
print('')
print('\t-d <executable>\t\t\tThe tool to use to diff the results')
print("\t--diff_tool=<executable>\tDefaults to 'opendiff'")
print('')
print('\t-b\t\t\t\tOnly omit the "before" result')
print('\t--only-before')
print('')
print('\t-a\t\t\t\tOnly omit the "after" result')
print('\t--only-after')
print('Examples:')
print('\tpython omit-needless-words.py -m AppKit')
# Configuration information
sdk = 'macosx'
target = ''
module = ''
source_filename = 'omit-needless-words.swift'
swift_ide_test = 'swift-ide-test'
diff_tool = 'opendiff'
only_before=0
only_after=0
# Parse command-line arguments.
try:
opts, args = getopt.getopt(sys.argv[1:], 'hs:t:m:i:d:ba',
['help', 'sdk=', 'target=', 'module=',
'swift-ide-test=','diff_tool=','only-before',
'only-after'])
except getopt.GetoptError:
help()
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
help()
sys.exit()
if opt in ('-s', '--sdk'):
sdk = arg
continue
if opt in ('-t', '--target'):
target = arg
continue
if opt in ('-m', '--module'):
module = arg
continue
if opt in ('-i', '--swift-ide-test'):
swift_ide_test = arg
continue
if opt in ('-d', '--diff_tool'):
diff_tool = arg
continue
if opt in ('-b', '--only-before'):
only_before=1
continue
if opt in ('-a', '--only-after'):
only_after=1
continue
help()
sys.exit(2)
if module == '':
help()
sys.exit(2)
if target == '':
if sdk == 'macosx':
target = 'x86_64-apple-macosx10.10'
if sdk == 'iphoneos':
target = 'arm64-apple-ios8.0'
if sdk == 'iphonesimulator':
target = 'x86_64-apple-ios8.0'
if sdk == 'watchos':
target = 'armv7k-apple-watchos2.0'
if sdk == 'watchos.simulator':
target = 'i386-apple-watchos2.0'
if sdk == 'appletvos':
target = 'arm64-apple-tvos9'
if sdk == 'appletvos.simulator':
target = 'x86_64-apple-tvos9'
# Figure out the SDK root for the requested SDK
sdkroot = subprocess.check_output(['xcrun', '--show-sdk-path', '--sdk', sdk]).rstrip()
print('SDK Root = %s' % (sdkroot))
swift_ide_test_cmd = [swift_ide_test, '-print-module', '-source-filename', source_filename, '-sdk', sdkroot, '-target', target, '-module-print-skip-overlay', '-skip-unavailable', '-module-print-submodules', '-skip-parameter-names', '-skip-imports', '-module-to-print=%s' % (module)]
omit_needless_words_args = ['-enable-omit-needless-words', '-enable-infer-default-arguments']
# Determine the output files.
before_filename = '%s.before.txt' % (module)
after_filename = '%s.after.txt' % (module)
# Create a .swift file we can feed into swift-ide-test
subprocess.call(['touch', source_filename])
if only_after == 0:
# Print the interface without omitting needless words
print('Writing %s...' % before_filename)
before_file = open(before_filename, 'w')
subprocess.call(swift_ide_test_cmd, stdout=before_file)
before_file.close()
if only_before == 0:
# Print the interface omitting needless words
print('Writing %s...' % after_filename)
after_file = open(after_filename, 'w')
subprocess.call(swift_ide_test_cmd + omit_needless_words_args, stdout=after_file)
after_file.close()
# Remove the .swift file we fed into swift-ide-test
subprocess.call(['rm', '-f', source_filename])
# Diff them.
if diff_tool != '':
subprocess.call([diff_tool, before_filename, after_filename])