Skip to content

[line-directive] Add usage and explanatory text #21654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions utils/line-directive
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,50 @@
# 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 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*\)')

Expand Down Expand Up @@ -639,6 +668,9 @@ def run():
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])
Expand Down