Skip to content

Commit 43f68ca

Browse files
authored
Merge pull request #21654 from apple/dabrahams/line-directive-usage
2 parents 75033ed + c4d76fd commit 43f68ca

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

utils/line-directive

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,50 @@
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
20-
2114
import bisect
2215
import os
2316
import re
2417
import shlex
2518
import subprocess
2619
import sys
2720

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+
2857
line_pattern = re.compile(
2958
r'^// ###sourceLocation\(file:\s*"([^"]+)",\s*line:\s*([0-9]+)\s*\)')
3059

@@ -639,6 +668,9 @@ def run():
639668
import doctest
640669
failure_count, _ = doctest.testmod()
641670
sys.exit(failure_count)
671+
elif len(sys.argv) == 2 and sys.argv[1] == '--help':
672+
print(usage)
673+
sys.exit(1)
642674
elif '--' not in sys.argv:
643675
source_file = sys.argv[1]
644676
source_line = int(sys.argv[2])

0 commit comments

Comments
 (0)