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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
|
#!/usr/bin/env python3
# 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
#
# ----------------------------------------------------------------------------
import bisect
import io
import os
import re
import shlex
import subprocess
import sys
usage = '''"#sourceLocation" is a directive used by tools like the Swift
compiler and debugger to adjust the lines reported in diagnostics and to
determine what source you see when you're stepping. "#sourceLocation"
corresponds to "#line" in C/C++ which is inserted by code generators like
Lex/Flex/Yacc/Bison so that you deal with the actual code you wrote and not the
generated result. For dealing with errors in the Swift generated by your gyb
source it's important that your tools can take you to the right line in your
.gyb file rather than in generated .swift file. If you don't have such a tool,
manually indirecting through the generated code is tedious, but at least it's
possible since gyb leaves "#sourceLocation" information behind.
But Swift's "#sourceLocation" directive is suboptimal for the purposes of the
freeform code generation done with gyb because it can only appear between
grammatically-complete declarations and statements (or something like that). So
instead of inserting "#sourceLocation" directives, gyb inserts "//
###sourceLocation" comments (by default---it's tunable). This line-directive
tool remaps file and line information in the output of your swift compiler (or
whatever tool you are using to process generated source---gyb is not
swift-specific) so that the error points to the right place in the .gyb
source. You invoke it as follows:
line-directive <generated-sources> -- <compilation command>
e.g., if you have foo.swift.gyb, bar.swift.gyb, and baz.swift, instead of
gyb foo.swift.gyb -o foo.swift
gyb bar.swift.gyb -o bar.swift
swiftc foo.swift bar.swift baz.swift
You do this:
gyb foo.swift.gyb -o foo.swift
gyb bar.swift.gyb -o bar.swift
line-directive foo.swift bar.swift -- swiftc foo.swift bar.swift baz.swift
'''
line_pattern = re.compile(
r'^// ###sourceLocation\(file:\s*"([^"]+)",\s*line:\s*([0-9]+)\s*\)')
def _make_line_map(target_filename, stream=None):
"""
>>> from io 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 io.open(target_filename, encoding='utf-8')
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 *
>>> # On Windows, the name of a NamedTemporaryFile cannot be used to open
>>> # the file for a second time if delete=True. Therefore, we have to
>>> # manually handle closing and deleting this file to allow us to open
>>> # the file by its name across all platforms.
>>> t = NamedTemporaryFile(delete=False)
>>> _ = t.write(b'''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)
>>> t.close()
>>> os.remove(t.name)
"""
assert target_line_num > 0
map = fline_map(target_filename)
index = bisect.bisect_left(map, (target_line_num, '', 0))
base = map[index - 1]
return str(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 *
>>> # On Windows, the name of a NamedTemporaryFile cannot be used to open
>>> # the file for a second time if delete=True. Therefore, we have to
>>> # manually handle closing and deleting this file to allow us to open
>>> # the file by its name across all platforms.
>>> t = NamedTemporaryFile(delete=False)
>>> _ = t.write(b'''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
>>> t.close()
>>> os.remove(t.name)
"""
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:
# "Make an iterator out of shlex.shlex.get_token, then consume items
# until it returns None." (Then eagerly convert the result to a list so
# that we can close the file.)
return list(iter(shlex.shlex(files, file_path, posix=True).get_token,
None))
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 *
>>> # On Windows, the name of a NamedTemporaryFile cannot be used to open
>>> # the file for a second time if delete=True. Therefore, we have to
>>> # manually handle closing and deleting this file to allow us to open
>>> # the file by its name across all platforms.
>>> target1 = NamedTemporaryFile(delete=False)
>>> _ = target1.write(b'''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()
>>> # On Windows, the name of a NamedTemporaryFile cannot be used to open
>>> # the file for a second time if delete=True. Therefore, we have to
>>> # manually handle closing and deleting this file to allow us to open
>>> # the file by its name across all platforms.
>>> target2 = NamedTemporaryFile(delete=False)
>>> _ = target2.write(b'''// ###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
>>> # On Windows, the name of a NamedTemporaryFile cannot be used to open
>>> # the file for a second time if delete=True. Therefore, we have to
>>> # manually handle closing and deleting this file to allow us to open
>>> # the file by its name across all platforms.
>>> raw_output = NamedTemporaryFile(delete=False)
>>> target1_name, target2_name = target1.name, target2.name
>>> _ = raw_output.write('''A
... {target1_name}:2:111: error one
... B
... {target1_name}:4:222: error two
... C
... {target1_name}:8:333: error three
... D
... glitch in file {target2_name}:1 assert one
... E
... glitch in file {target2_name}, line 2 assert two
... glitch at {target2_name}, line 3 assert three
... glitch at {target2_name}:4 assert four
... glitch in [{target2_name}, line 5 assert five
... glitch in [{target2_name}:22 assert six
... '''.format(**locals()).encode('utf-8'))
>>> 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([sys.executable,
... __file__, target1.name, target2.name, '--',
... sys.executable, '-c',
... 'import sys;sys.stdout.write(open(sys.argv[1]).read())',
... raw_output.name], universal_newlines=True)
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([sys.executable, __file__, 'foo.bar',
... '21', target1.name],
... universal_newlines=True), end='')
5
>>> print(subprocess.check_output([sys.executable, __file__, 'foo.bar',
... '8', target2.name],
... universal_newlines=True), end='')
3
Simulate errors on different line numbers
>>> # On Windows, the name of a NamedTemporaryFile cannot be used to open
>>> # the file for a second time if delete=True. Therefore, we have to
>>> # manually handle closing and deleting this file to allow us to open
>>> # the file by its name across all platforms.
>>> long_output = NamedTemporaryFile(delete=False)
>>> _ = long_output.write(b'''
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 1)
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 1)
... //===--- Map.swift.gyb - Lazily map over a Sequence -----------*- swif
... //
... // 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 projec
... //
... //===-----------------------------------------------------------------
...
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 20)
...
... /// The `IteratorProtocol` used by `MapSequence` and `MapCollection`.
... /// Produces each element by passing the output of the `Base`
... /// `IteratorProtocol` through a transform function returning
... @_fixed_layout
... public struct LazyMapIterator<
... Base : IteratorProtocol, Element
... > : IteratorProtocol, Sequence {
... /// Advances to the next element and returns it, or `nil` if no
... /// exists.
... ///
... /// Once `nil` has been returned, all subsequent calls return `nil`.
... ///
... /// - Precondition: `next()` has not been applied to a copy.
... /// since the copy was made.
... @_inlineable
... public mutating func next() -> Element? {
... return _base.next().map(_transform)
... }
...
... @_inlineable
... public var base: Base { return _base }
...
... @_versioned
... internal var _base: Base
... @_versioned
... internal let _transform: (Base.Element) -> Element
...
... @_inlineable
... @_versioned
... internal init(_base: Base, _transform: @escaping (Base.Element)
... self._base = _base
... self._transform = _transform
... }
... }
...
... /// A `Sequence` whose elements consist of those in a `Base`
... /// `Sequence` passed through a transform function returning.
... /// These elements are computed lazily, each time they're read, by
... /// calling the transform function on a base element.
... @_fixed_layout
... public struct LazyMapSequence<Base : Sequence, Element>
... : LazySequenceProtocol {
...
... public typealias Elements = LazyMapSequence
...
... /// Returns an iterator over the elements of this sequence.
... ///
... /// - Complexity: O(1).
... @_inlineable
... public func makeIterator() -> LazyMapIterator<Base.Iterator, Element
... return LazyMapIterator(_base: _base.makeIterator(), _transform: _t
... }
...
... /// Returns a value less than or equal to the number of elements in
... /// `self`, **nondestructively**.
... ///
... /// - Complexity: O(*n*)
... @_inlineable
... public var underestimatedCount: Int {
... return _base.underestimatedCount
... }
...
... /// Creates an instance with elements `transform(x)` for each elemen
... /// `x` of base.
... @_inlineable
... @_versioned
... internal init(_base: Base, transform: @escaping (Base.Iterator.Eleme
... self._base = _base
... self._transform = transform
... }
...
... @_versioned
... internal var _base: Base
... @_versioned
... internal let _transform: (Base.Iterator.Element) -> Element
... }
...
... //===--- Collections -------------------------------------------------
...
... // FIXME(ABI)#45 (Conditional Conformance): `LazyMap*Collection` types
... // collapsed into one `LazyMapCollection` using conditional conformanc
... // Maybe even combined with `LazyMapSequence`.
... // rdar://problem/17144340
...
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 108)
...
... /// A `Collection` whose elements consist of those in a `Base`
... /// `Collection` passed through a transform function returning `Elemen
... /// These elements are computed lazily, each time they're read, by
... /// calling the transform function on a base element.
... @_fixed_layout
... public struct LazyMapCollection<
... Base : Collection, Element
... > : LazyCollectionProtocol,
... _CollectionWrapper
... {
... public typealias Base = Base_
... public typealias Index = Base.Index
... public typealias _Element = Base._Element
... public typealias SubSequence = Base.SubSequence
... typealias Indices = Base.Indices
...
... @_inlineable
... public subscript(position: Base.Index) -> Element {
... return _transform(_base[position])
... }
...
... /// Create an instance with elements `transform(x)` for each element
... /// `x` of base.
... @_inlineable
... @_versioned
... internal init(_base: Base, transform: @escaping (Base.Iterator.Eleme
... self._base = _base
... self._transform = transform
... }
...
... @_versioned
... internal var _base: Base
... @_versioned
... internal let _transform: (Base.Iterator.Element) -> Element
... }
...
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 108)
...
... /// A `Collection` whose elements consist of those in a `Base`
... /// `Collection` passed through a transform function returning `Elemen
... /// These elements are computed lazily, each time they're read, by
... /// calling the transform function on a base element.
... @_fixed_layout
... public struct LazyMapBidirectionalCollection<
... Base : BidirectionalCollection, Element
... > : LazyCollectionProtocol,
... _BidirectionalCollectionWrapper
... {
... public typealias Base = Base_
... public typealias Index = Base.Index
... public typealias _Element = Base._Element
... public typealias SubSequence = Base.SubSequence
... typealias Indices = Base.Indices
...
... @_inlineable
... public subscript(position: Base.Index) -> Element {
... return _transform(_base[position])
... }
...
... /// Create an instance with elements `transform(x)` for each element
... /// `x` of base.
... @_inlineable
... @_versioned
... internal init(_base: Base, transform: @escaping (Base.Iterator.Eleme
... self._base = _base
... self._transform = transform
... }
...
... @_versioned
... internal var _base: Base
... @_versioned
... internal let _transform: (Base.Iterator.Element) -> Element
... }
...
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 108)
...
... /// A `Collection` whose elements consist of those in a `Base`
... /// `Collection` passed through a transform function returning `Elemen
... /// These elements are computed lazily, each time they're read, by
... /// calling the transform function on a base element.
... @_fixed_layout
... public struct LazyMapRandomAccessCollection<
... Base : RandomAccessCollection, Element
... > : LazyCollectionProtocol,
... _RandomAccessCollectionWrapper
... {
... public typealias Base = Base_
... public typealias Index = Base.Index
... public typealias _Element = Base._Element
... public typealias SubSequence = Base.SubSequence
... typealias Indices = Base.Indices
...
... @_inlineable
... public subscript(position: Base.Index) -> Element {
... return _transform(_base[position])
... }
...
... /// Create an instance with elements `transform(x)` for each element
... /// `x` of base.
... @_inlineable
... @_versioned
... internal init(_base: Base, transform: @escaping (Base.Iterator.Eleme
... self._base = _base
... self._transform = transform
... }
...
... @_versioned
... internal var _base: Base
... @_versioned
... internal let _transform: (Base.Iterator.Element) -> Element
... }
...
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 146)
...
... //===--- Support for s.lazy ------------------------------------------
...
... extension LazySequenceProtocol {
... /// Returns a `LazyMapSequence` over this `Sequence`. The elements
... /// the result are computed lazily, each time they are read, by
... /// calling `transform` function on a base element.
... @_inlineable
... public func map<U>(
... _ transform: @escaping (Elements.Iterator.Element) -> U
... ) -> LazyMapSequence<Self.Elements, U> {
... return LazyMapSequence(_base: self.elements, transform: transform)
... }
... }
...
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 162)
...
... extension LazyCollectionProtocol
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 169)
... {
... /// Returns a `LazyMapCollection` over this `Collection`. The eleme
... /// the result are computed lazily, each time they are read, by
... /// calling `transform` function on a base element.
... @_inlineable
... public func map<U>(
... _ transform: @escaping (Elements.Iterator.Element) -> U
... ) -> LazyMapCollection<Self.Elements, U> {
... return LazyMapCollection(
... _base: self.elements,
... transform: transform)
... }
... }
...
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 162)
...
... extension LazyCollectionProtocol
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 165)
... where
... Self : BidirectionalCollection,
... Elements : BidirectionalCollection
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 169)
... {
... /// Returns a `LazyMapCollection` over this `Collection`. The eleme
... /// the result are computed lazily, each time they are read, by
... /// calling `transform` function on a base element.
... @_inlineable
... public func map<U>(
... _ transform: @escaping (Elements.Iterator.Element) -> U
... ) -> LazyMapBidirectionalCollection<Self.Elements, U> {
... return LazyMapBidirectionalCollection(
... _base: self.elements,
... transform: transform)
... }
... }
...
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 162)
...
... extension LazyCollectionProtocol
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 165)
... where
... Self : RandomAccessCollection,
... Elements : RandomAccessCollection
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 169)
... {
... /// Returns a `LazyMapCollection` over this `Collection`. The eleme
... /// the result are computed lazily, each time they are read, by
... /// calling `transform` function on a base element.
... @_inlineable
... public func map<U>(
... _ transform: @escaping (Elements.Iterator.Element) -> U
... ) -> LazyMapRandomAccessCollection<Self.Elements, U> {
... return LazyMapRandomAccessCollection(
... _base: self.elements,
... transform: transform)
... }
... }
...
... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 184)
...
... @available(*, unavailable, renamed: "LazyMapIterator")
... public struct LazyMapGenerator<Base : IteratorProtocol, Element> {}
...
... extension LazyMapSequence {
... @available(*, unavailable, message: "use '.lazy.map' on the sequence
... public init(_ base: Base, transform: (Base.Iterator.Element) -> Elem
... Builtin.unreachable()
... }
... }
...
... extension LazyMapCollection {
... @available(*, unavailable, message: "use '.lazy.map' on the collecti
... public init(_ base: Base, transform: (Base.Iterator.Element) -> Elem
... Builtin.unreachable()
... }
... }
...
... // Local Variables:
... // eval: (read-only-mode 1)
... // End:
... ''')
>>> long_output.flush()
>>> long_output_result = subprocess.check_output([sys.executable,
... __file__, long_output.name, '--',
... 'echo', long_output.name + ':112:27: error:'
... ]).rstrip().decode('ascii')
>>> print(long_output_result)
/public/core/Map.swift.gyb:117:27: error:
>>> target1.close()
>>> os.remove(target1.name)
>>> target2.close()
>>> os.remove(target2.name)
>>> raw_output.close()
>>> os.remove(raw_output.name)
"""
if len(sys.argv) <= 1:
import doctest
failure_count, _ = doctest.testmod()
sys.exit(failure_count)
elif len(sys.argv) == 2 and sys.argv[1] == '--help':
print(usage)
sys.exit(1)
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,
encoding='UTF-8'
)
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, line):
m = p.match(line)
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()
|