Skip to content

Commit 8be6b49

Browse files
author
Nathan Hawes
authored
Merge pull request #19439 from nathawes/fix-line-col-conversion
[sourcekit] Fix line+column -> offset conversion when on the last line of a file with no terminating newline
2 parents 9c673a5 + 44f4df4 commit 8be6b49

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

lib/Basic/SourceLoc.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,10 @@ llvm::Optional<unsigned> SourceManager::resolveFromLineCol(unsigned BufferId,
320320
const char *Ptr = InputBuf->getBufferStart();
321321
const char *End = InputBuf->getBufferEnd();
322322
const char *LineStart = Ptr;
323-
for (; Ptr < End; ++Ptr) {
323+
--Line;
324+
for (; Line && (Ptr < End); ++Ptr) {
324325
if (*Ptr == '\n') {
325326
--Line;
326-
if (Line == 0)
327-
break;
328327
LineStart = Ptr+1;
329328
}
330329
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %sourcekitd-test -req=local-rename -pos=4:7 -name new_name %s -- %s
2+
3+
var Foo: Int {
4+
var missingNewlineAtEndOfFile

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,11 +1963,10 @@ static unsigned resolveFromLineCol(unsigned Line, unsigned Col,
19631963
const char *Ptr = InputBuf->getBufferStart();
19641964
const char *End = InputBuf->getBufferEnd();
19651965
const char *LineStart = Ptr;
1966-
for (; Ptr < End; ++Ptr) {
1966+
--Line;
1967+
for (; Line && (Ptr < End); ++Ptr) {
19671968
if (*Ptr == '\n') {
19681969
--Line;
1969-
if (Line == 0)
1970-
break;
19711970
LineStart = Ptr+1;
19721971
}
19731972
}

0 commit comments

Comments
 (0)