forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line-directive
executable file
·338 lines (298 loc) · 10.5 KB
/
line-directive
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env python
# line-directive.py - Transform line numbers in error messages -*- python -*-
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
# ----------------------------------------------------------------------------
#
# Converts line numbers in error messages according to "line directive"
# comments.
#
# ----------------------------------------------------------------------------
from __future__ import print_function
import bisect
import os
import re
import subprocess
import sys
line_pattern = re.compile(
r'^// ###sourceLocation\(file:\s*"([^"]+)",\s*line:\s*([0-9]+)\s*\)')
def _make_line_map(target_filename, stream=None):
"""
>>> from StringIO import StringIO
>>> _make_line_map('box',
... StringIO('''// ###sourceLocation(file: "foo.bar", line: 3)
... line 2
... line 3
... line 4
... // ###sourceLocation(file: "baz.txt", line: 20)
... line 6
... line 7
... '''))
[(0, 'box', 1), (1, 'foo.bar', 3), (5, 'baz.txt', 20)]
"""
result = [(0, target_filename, 1)]
input = stream or open(target_filename)
for i, l in enumerate(input.readlines()):
m = line_pattern.match(l)
if m:
result.append((i + 1, m.group(1), int(m.group(2))))
return result
_line_maps = {}
def fline_map(target_filename):
map = _line_maps.get(target_filename)
if map is None:
map = _make_line_map(target_filename)
_line_maps[target_filename] = map
return map
def map_line_to_source_file(target_filename, target_line_num):
"""
>>> from tempfile import *
>>> t = NamedTemporaryFile()
>>> t.write('''line 1
... line 2
... // ###sourceLocation(file: "foo.bar", line: 20)
... line 4
... line 5
... // ###sourceLocation(file: "baz.txt", line: 5)
... line 7
... line 8
... ''')
>>> t.flush()
>>> (t2, l) = map_line_to_source_file(t.name, 1)
>>> t2 == t.name, l
(True, 1)
>>> (t2, l) = map_line_to_source_file(t.name, 2)
>>> t2 == t.name, l
(True, 2)
>>> (t2, l) = map_line_to_source_file(t.name, 3)
>>> t2 == t.name, l
(True, 3)
>>> map_line_to_source_file(t.name, 4)
('foo.bar', 20)
>>> map_line_to_source_file(t.name, 5)
('foo.bar', 21)
>>> map_line_to_source_file(t.name, 6)
('foo.bar', 22)
>>> map_line_to_source_file(t.name, 7)
('baz.txt', 5)
>>> map_line_to_source_file(t.name, 8)
('baz.txt', 6)
>>> map_line_to_source_file(t.name, 42)
('baz.txt', 40)
"""
assert(target_line_num > 0)
map = fline_map(target_filename)
index = bisect.bisect_left(map, (target_line_num, '', 0))
base = map[index - 1]
return base[1], base[2] + (target_line_num - base[0] - 1)
def map_line_from_source_file(source_filename,
source_line_num,
target_filename):
"""
>>> from tempfile import *
>>> t = NamedTemporaryFile()
>>> t.write('''line 1
... line 2
... // ###sourceLocation(file: "foo.bar", line: 20)
... line 4
... line 5
... // ###sourceLocation(file: "baz.txt", line: 5)
... line 7
... line 8
... ''')
>>> t.flush()
>>> map_line_from_source_file(t.name, 1, t.name)
1
>>> map_line_from_source_file(t.name, 2, t.name)
2
>>> map_line_from_source_file(t.name, 3, t.name)
3
>>> try: map_line_from_source_file(t.name, 4, t.name)
... except RuntimeError: pass
>>> try: map_line_from_source_file('foo.bar', 19, t.name)
... except RuntimeError: pass
>>> map_line_from_source_file('foo.bar', 20, t.name)
4
>>> map_line_from_source_file('foo.bar', 21, t.name)
5
>>> map_line_from_source_file('foo.bar', 22, t.name)
6
>>> try: map_line_from_source_file('foo.bar', 23, t.name)
... except RuntimeError: pass
>>> map_line_from_source_file('baz.txt', 5, t.name)
7
>>> map_line_from_source_file('baz.txt', 6, t.name)
8
>>> map_line_from_source_file('baz.txt', 33, t.name)
35
>>> try: map_line_from_source_file(t.name, 33, t.name)
... except RuntimeError: pass
>>> try: map_line_from_source_file('foo.bar', 2, t.name)
... except RuntimeError: pass
"""
assert(source_line_num > 0)
map = fline_map(target_filename)
for i, (target_line_num, found_source_filename,
found_source_line_num) in enumerate(map):
if found_source_filename != source_filename:
continue
if found_source_line_num > source_line_num:
continue
result = target_line_num + (source_line_num - found_source_line_num)
if i + 1 == len(map) or map[i + 1][0] > result:
return result + 1
raise RuntimeError("line not found")
def read_response_file(file_path):
with open(file_path, 'r') as files:
return filter(None, files.read().split(';'))
def expand_response_files(files):
expanded_files = []
for file_path in files:
# Read a list of files from a response file.
if file_path[0] == '@':
expanded_files.extend(read_response_file(file_path[1:]))
else:
expanded_files.append(file_path)
return expanded_files
def run():
"""Simulate a couple of gyb-generated files
>>> from tempfile import *
>>> target1 = NamedTemporaryFile()
>>> target1.write('''line 1
... line 2
... // ###sourceLocation(file: "foo.bar", line: 20)
... line 4
... line 5
... // ###sourceLocation(file: "baz.txt", line: 5)
... line 7
... line 8
... ''')
>>> target1.flush()
>>> target2 = NamedTemporaryFile()
>>> target2.write('''// ###sourceLocation(file: "foo.bar", line: 7)
... line 2
... line 3
... // ###sourceLocation(file: "fox.box", line: 11)
... line 5
... line 6
... ''')
>>> target2.flush()
Simulate the raw output of compilation
>>> raw_output = NamedTemporaryFile()
>>> target1_name, target2_name = target1.name, target2.name
>>> raw_output.write('''A
... %(target1_name)s:2:111: error one
... B
... %(target1_name)s:4:222: error two
... C
... %(target1_name)s:8:333: error three
... D
... glitch in file %(target2_name)s:1 assert one
... E
... glitch in file %(target2_name)s, line 2 assert two
... glitch at %(target2_name)s, line 3 assert three
... glitch at %(target2_name)s:4 assert four
... glitch in [%(target2_name)s, line 5 assert five
... glitch in [%(target2_name)s:22 assert six
... ''' % locals())
>>> raw_output.flush()
Run this tool on the two targets, using a portable version of Unix 'cat' to
dump the output file.
>>> import subprocess
>>> output = subprocess.check_output([
... __file__, target1.name, target2.name, '--',
... sys.executable, '-c',
... 'import sys;sys.stdout.write(open(sys.argv[1]).read())',
... raw_output.name])
Replace temporary filenames and check it.
>>> print output.replace(target1.name,'TARGET1-NAME')
... .replace(target2.name,'TARGET2-NAME') + 'EOF'
A
TARGET1-NAME:2:111: error one
B
foo.bar:20:222: error two
C
baz.txt:6:333: error three
D
glitch in file TARGET2-NAME:1 assert one
E
glitch in file foo.bar, line 7 assert two
glitch at foo.bar, line 8 assert three
glitch at foo.bar:9 assert four
glitch in [fox.box, line 11 assert five
glitch in [fox.box:28 assert six
EOF
>>> print subprocess.check_output([__file__, 'foo.bar', '21',
... target1.name]),
5
>>> print subprocess.check_output([__file__, 'foo.bar', '8',
... target2.name]),
3
"""
if len(sys.argv) <= 1:
import doctest
doctest.testmod()
elif '--' not in sys.argv:
source_file = sys.argv[1]
source_line = int(sys.argv[2])
target_file = sys.argv[3]
print(map_line_from_source_file(source_file, source_line, target_file))
else:
dashes = sys.argv.index('--')
sources = expand_response_files(sys.argv[1:dashes])
# The first argument of command_args is the process to open.
# subprocess.Popen doesn't normalize arguments. This means that trying
# to open a non-normalized file (e.g. C:/swift/./bin/swiftc.exe) on
# Windows results in file/directory not found errors, as Popen
# delegates to the Win32 CreateProcess API. Unix systems handle
# non-normalized paths, so don't have this problem.
# Arguments passed to the process are normalized by the process.
command_args = expand_response_files(sys.argv[dashes + 1:])
command_args[0] = os.path.normpath(command_args[0])
command = subprocess.Popen(
command_args,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
universal_newlines=True
)
sources = '(?P<file>' + '|'.join(re.escape(s) for s in sources) + ')'
error_pattern = re.compile(
'^' + sources +
':(?P<line>[0-9]+):(?P<column>[0-9]+):;(?P<tail>.*?)\n?$')
assertion_pattern = re.compile(
'^(?P<head>.*( file | at |#[0-9]+: |[[]))' +
sources +
'(?P<middle>, line |:)(?P<line>[0-9]+)(?P<tail>.*?)\n?$')
while True:
input = command.stdout.readline()
if input == '':
break
output = input
def decode_match(p, l):
m = p.match(l)
if m is None:
return ()
file, line_num = map_line_to_source_file(
m.group('file'), int(m.group('line')))
return ((m, file, line_num),)
for (m, file, line_num) in decode_match(error_pattern, input):
output = '%s:%s:%s:%s\n' % (
file, line_num, int(m.group(3)), m.group(4))
break
else:
for (m, file, line_num) in decode_match(assertion_pattern,
input):
output = '%s%s%s%s%s\n' % (
m.group('head'), file, m.group('middle'), line_num,
m.group('tail'))
sys.stdout.write(output)
sys.exit(command.wait())
if __name__ == '__main__':
run()