Skip to content

Commit 0e2430b

Browse files
committed
[Parse] Disallow non-decimal line numbers to #sourceLocation directive
Non-decimal line numbers in a `#sourceLocation` directive are simply confusing in my opinion and should be banned.
1 parent 4d53245 commit 0e2430b

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7186,6 +7186,11 @@ ParserStatus Parser::parseLineDirective(bool isLine) {
71867186
}
71877187
SmallString<16> buffer;
71887188
auto text = stripUnderscoresIfNeeded(Tok.getText(), buffer);
7189+
if (text.find_first_not_of("0123456789") != StringRef::npos) {
7190+
// Disallow non-decimal line numbers in Swift 6.
7191+
diagnose(Tok, diag::expected_line_directive_number)
7192+
.warnUntilSwiftVersion(6);
7193+
}
71897194
if (text.getAsInteger(0, StartLine)) {
71907195
diagnose(Tok, diag::expected_line_directive_number);
71917196
return makeParserError();

test/Parse/line-directive.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ x // expected-error {{parameterless closing #sourceLocation() directive without
1414

1515
#sourceLocation(file: x.swift, line: 1) // expected-error{{expected filename string literal}}
1616

17+
// expected-warning@+1 {{expected starting line number}}
18+
#sourceLocation(file: "x.swift", line: 0xff)
19+
1720
#sourceLocation(file: "x.swift", line: 42)
1821
x x ; // should be ignored by expected_error because it is in a different file
1922
x

0 commit comments

Comments
 (0)