Skip to content

Commit 4433a9f

Browse files
author
Dave Abrahams
authored
[line-directive] Add usage and explanatory text
1 parent f98d7bd commit 4433a9f

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

utils/line-directive

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,44 @@
1010
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1111
#
1212
# ----------------------------------------------------------------------------
13-
#
14-
# Converts line numbers in error messages according to "line directive"
15-
# comments.
16-
#
17-
# ----------------------------------------------------------------------------
18-
1913
from __future__ import print_function
2014

15+
usage = '''"#sourceLocation" is a directive used by tools like the Swift
16+
compiler and debugger to adjust the lines reported in diagnostics and to
17+
determine what source you see when you're stepping. "#sourceLocation"
18+
corresponds to "#line" in C/C++ which is inserted by code generators like
19+
Lex/Flex/Yacc/Bison so that you deal with the actual code you wrote and not the
20+
generated result. For dealing with errors in the Swift generated by your gyb
21+
source it's important that your tools can take you to the right line in your
22+
.gyb file rather than in generated .swift file. If you don't have such a tool,
23+
manually indirecting through the generated code is tedious, but at least it's
24+
possible since gyb leaves "#sourceLocation" information behind.
25+
26+
But Swift's "#sourceLocation" directive is suboptimal for the purposes of the
27+
freeform code generation done with gyb because it can only appear between
28+
grammatically-complete declarations and statements (or something like that). So
29+
instead of inserting "#sourceLocation" directives, gyb inserts "//
30+
###sourceLocation" comments (by default---it's tunable). This line-directive
31+
tool remaps file and line information in the output of your swift compiler (or
32+
whatever tool you are using to process generated source---gyb is not
33+
swift-specific) so that the error points to the right place in the .gyb
34+
source. You invoke it as follows:
35+
36+
line-directive <generated-sources> -- <compilation command>
37+
38+
e.g., if you have foo.swift.gyb, bar.swift.gyb, and baz.swift, instead of
39+
40+
gyb foo.swift.gyb -o foo.swift
41+
gyb bar.swift.gyb -o bar.swift
42+
swiftc foo.swift bar.swift baz.swift
43+
44+
You do this:
45+
46+
gyb foo.swift.gyb -o foo.swift
47+
gyb bar.swift.gyb -o bar.swift
48+
line-directive foo.swift bar.swift -- swiftc foo.swift bar.swift baz.swift
49+
'''
50+
2151
import bisect
2252
import os
2353
import re
@@ -639,6 +669,9 @@ def run():
639669
import doctest
640670
failure_count, _ = doctest.testmod()
641671
sys.exit(failure_count)
672+
elif len(sys.argv) == 2 and sys.argv[1] == '--help':
673+
print(usage)
674+
sys.exit(1)
642675
elif '--' not in sys.argv:
643676
source_file = sys.argv[1]
644677
source_line = int(sys.argv[2])

0 commit comments

Comments
 (0)