Skip to content

Commit 95af9e3

Browse files
authored
[SR-15231] #sourceLocation doesn't recognize CRLF line endings swiftlang#57553 (swiftlang#63071)
1 parent 0ca5937 commit 95af9e3

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6193,18 +6193,24 @@ ParserStatus Parser::parseLineDirective(bool isLine) {
61936193

61946194
const char *LastTokTextEnd = Tok.getText().end();
61956195

6196-
// Skip over trailing whitespace and a single \n to the start of the next
6197-
// line.
6196+
// Skip over trailing whitespace and a single \r and/or \n to the start of the
6197+
// next line.
61986198
while (*LastTokTextEnd == ' ' || *LastTokTextEnd == '\t')
61996199
++LastTokTextEnd;
6200-
SourceLoc nextLineStartLoc = Lexer::getSourceLoc(LastTokTextEnd);
6201-
6202-
if (*LastTokTextEnd == '\n')
6203-
nextLineStartLoc = nextLineStartLoc.getAdvancedLoc(1);
6204-
else {
6200+
bool hadCROrLF = false;
6201+
if (*LastTokTextEnd == '\r') {
6202+
hadCROrLF = true;
6203+
++LastTokTextEnd;
6204+
}
6205+
if (*LastTokTextEnd == '\n') {
6206+
hadCROrLF = true;
6207+
++LastTokTextEnd;
6208+
}
6209+
if (!hadCROrLF) {
62056210
diagnose(Tok.getLoc(), diag::extra_tokens_line_directive);
62066211
return makeParserError();
62076212
}
6213+
SourceLoc nextLineStartLoc = Lexer::getSourceLoc(LastTokTextEnd);
62086214

62096215
int LineOffset =
62106216
StartLine - SourceMgr.getLineAndColumnInBuffer(nextLineStartLoc).first;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %gyb %s -o %t/line-directive-crlf.swift
3+
// RUN: %target-swift-frontend -typecheck -parse-as-library -verify %t/line-directive-crlf.swift
4+
5+
// https://github.com/apple/swift/issues/57553
6+
func I57553() {
7+
% print("#sourceLocation(file: \"issue-57553.swift\", line: 100)\r\n")
8+
% print("#sourceLocation()\r\n")
9+
% print("#sourceLocation(file: \"issue-57553.swift\", line: 200)\r")
10+
% print("#sourceLocation()\r")
11+
% print("#sourceLocation(file: \"issue-57553.swift\", line: 300)\n")
12+
% print("#sourceLocation()\n")
13+
}

0 commit comments

Comments
 (0)