|
10 | 10 | # See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
11 | 11 | #
|
12 | 12 | # ----------------------------------------------------------------------------
|
13 |
| -# |
14 |
| -# Converts line numbers in error messages according to "line directive" |
15 |
| -# comments. |
16 |
| -# |
17 |
| -# ---------------------------------------------------------------------------- |
18 |
| - |
19 | 13 | from __future__ import print_function
|
20 |
| - |
21 | 14 | import bisect
|
22 | 15 | import os
|
23 | 16 | import re
|
24 | 17 | import shlex
|
25 | 18 | import subprocess
|
26 | 19 | import sys
|
27 | 20 |
|
| 21 | +usage = '''"#sourceLocation" is a directive used by tools like the Swift |
| 22 | +compiler and debugger to adjust the lines reported in diagnostics and to |
| 23 | +determine what source you see when you're stepping. "#sourceLocation" |
| 24 | +corresponds to "#line" in C/C++ which is inserted by code generators like |
| 25 | +Lex/Flex/Yacc/Bison so that you deal with the actual code you wrote and not the |
| 26 | +generated result. For dealing with errors in the Swift generated by your gyb |
| 27 | +source it's important that your tools can take you to the right line in your |
| 28 | +.gyb file rather than in generated .swift file. If you don't have such a tool, |
| 29 | +manually indirecting through the generated code is tedious, but at least it's |
| 30 | +possible since gyb leaves "#sourceLocation" information behind. |
| 31 | +
|
| 32 | +But Swift's "#sourceLocation" directive is suboptimal for the purposes of the |
| 33 | +freeform code generation done with gyb because it can only appear between |
| 34 | +grammatically-complete declarations and statements (or something like that). So |
| 35 | +instead of inserting "#sourceLocation" directives, gyb inserts "// |
| 36 | +###sourceLocation" comments (by default---it's tunable). This line-directive |
| 37 | +tool remaps file and line information in the output of your swift compiler (or |
| 38 | +whatever tool you are using to process generated source---gyb is not |
| 39 | +swift-specific) so that the error points to the right place in the .gyb |
| 40 | +source. You invoke it as follows: |
| 41 | +
|
| 42 | + line-directive <generated-sources> -- <compilation command> |
| 43 | +
|
| 44 | +e.g., if you have foo.swift.gyb, bar.swift.gyb, and baz.swift, instead of |
| 45 | +
|
| 46 | + gyb foo.swift.gyb -o foo.swift |
| 47 | + gyb bar.swift.gyb -o bar.swift |
| 48 | + swiftc foo.swift bar.swift baz.swift |
| 49 | +
|
| 50 | +You do this: |
| 51 | +
|
| 52 | + gyb foo.swift.gyb -o foo.swift |
| 53 | + gyb bar.swift.gyb -o bar.swift |
| 54 | + line-directive foo.swift bar.swift -- swiftc foo.swift bar.swift baz.swift |
| 55 | +''' |
| 56 | + |
28 | 57 | line_pattern = re.compile(
|
29 | 58 | r'^// ###sourceLocation\(file:\s*"([^"]+)",\s*line:\s*([0-9]+)\s*\)')
|
30 | 59 |
|
@@ -639,6 +668,9 @@ def run():
|
639 | 668 | import doctest
|
640 | 669 | failure_count, _ = doctest.testmod()
|
641 | 670 | sys.exit(failure_count)
|
| 671 | + elif len(sys.argv) == 2 and sys.argv[1] == '--help': |
| 672 | + print(usage) |
| 673 | + sys.exit(1) |
642 | 674 | elif '--' not in sys.argv:
|
643 | 675 | source_file = sys.argv[1]
|
644 | 676 | source_line = int(sys.argv[2])
|
|
0 commit comments