Skip to content

Commit 63cd125

Browse files
committed
Stop using SourceManager::getBufferIdentifierForLoc to find buffer IDs
The right way is findBufferContainingLoc. getBufferIdentifierForLoc is both slower and wrong in the presence of #sourceLocation. I couldn't come up with a test for the change in IDE/Utils.cpp because refactoring still seems to be broken around #sourceLocation. I'll file bugs for that.
1 parent b9af6e0 commit 63cd125

File tree

5 files changed

+74
-9
lines changed

5 files changed

+74
-9
lines changed

lib/IDE/Utils.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,8 +904,7 @@ class ClangFileRewriterHelper {
904904
}
905905

906906
void replaceText(SourceManager &SM, CharSourceRange Range, StringRef Text) {
907-
auto BufferId = SM.getIDForBufferIdentifier(SM.
908-
getBufferIdentifierForLoc(Range.getStart())).getValue();
907+
auto BufferId = SM.findBufferContainingLoc(Range.getStart());
909908
if (BufferId == InterestedId) {
910909
HasChange = true;
911910
auto StartLoc = SM.getLocOffsetInBuffer(Range.getStart(), BufferId);

lib/Parse/Lexer.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,12 +2579,11 @@ static const char *findStartOfLine(const char *bufStart, const char *current) {
25792579
}
25802580

25812581
SourceLoc Lexer::getLocForStartOfToken(SourceManager &SM, SourceLoc Loc) {
2582-
Optional<unsigned> BufferIdOp = SM.getIDForBufferIdentifier(SM.
2583-
getBufferIdentifierForLoc(Loc));
2584-
if (!BufferIdOp.hasValue())
2582+
if (!Loc.isValid())
25852583
return SourceLoc();
2586-
return getLocForStartOfToken(SM, BufferIdOp.getValue(),
2587-
SM.getLocOffsetInBuffer(Loc, BufferIdOp.getValue()));
2584+
unsigned BufferId = SM.findBufferContainingLoc(Loc);
2585+
return getLocForStartOfToken(SM, BufferId,
2586+
SM.getLocOffsetInBuffer(Loc, BufferId));
25882587
}
25892588

25902589
SourceLoc Lexer::getLocForStartOfToken(SourceManager &SM, unsigned BufferID,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
func simpleParams(a: Int, b theB: Int) {}
2+
3+
struct AccessorTest {
4+
subscript(a: Int, b theB: Int) -> Int { return a }
5+
var prop: Int {
6+
get { return 0 }
7+
set(v) {}
8+
}
9+
}
10+
11+
#sourceLocation(file: "custom.swuft", line: 2000)
12+
func customSourceLocation(a: Int) {}
13+
#sourceLocation()
14+
15+
// RUN: %sourcekitd-test -req=cursor -pos=1:19 %s -- %s | %FileCheck -check-prefix=CHECK-FUNC-A %s
16+
// CHECK-FUNC-A: s:17cursor_info_param12simpleParams1a1bySi_SitFACL_Sivp
17+
// CHECK-FUNC-A: PARENT OFFSET: 5
18+
19+
// RUN: %sourcekitd-test -req=cursor -pos=1:27 %s -- %s | %FileCheck -check-prefix=CHECK-FUNC-B %s
20+
// CHECK-FUNC-B: s:17cursor_info_param12simpleParams1a1bySi_SitF{{$}}
21+
22+
// RUN: %sourcekitd-test -req=cursor -pos=1:29 %s -- %s | %FileCheck -check-prefix=CHECK-FUNC-THEB %s
23+
// CHECK-FUNC-THEB: s:17cursor_info_param12simpleParams1a1bySi_SitF4theBL_Sivp
24+
// CHECK-FUNC-THEB-NOT: PARENT OFFSET
25+
26+
// RUN: %sourcekitd-test -req=cursor -pos=4:13 %s -- %s | %FileCheck -check-prefix=CHECK-SUBSCRIPT-A %s
27+
// FIXME: This USR is wrong; see https://bugs.swift.org/browse/SR-8660.
28+
// CHECK-SUBSCRIPT-A: s:17cursor_info_param12AccessorTestV1aL_Sivp
29+
// CHECK-SUBSCRIPT-A: PARENT OFFSET: 67
30+
31+
// RUN: %sourcekitd-test -req=cursor -pos=4:21 %s -- %s | %FileCheck -check-prefix=CHECK-SUBSCRIPT-B %s
32+
// CHECK-SUBSCRIPT-B: s:17cursor_info_param12AccessorTestV
33+
34+
// RUN: %sourcekitd-test -req=cursor -pos=4:23 %s -- %s | %FileCheck -check-prefix=CHECK-SUBSCRIPT-THEB %s
35+
// FIXME: This USR is wrong; see https://bugs.swift.org/browse/SR-8660.
36+
// CHECK-SUBSCRIPT-THEB: s:17cursor_info_param12AccessorTestV4theBL_Sivp
37+
// CHECK-SUBSCRIPT-THEB-NOT: PARENT OFFSET
38+
39+
// RUN: %sourcekitd-test -req=cursor -pos=7:9 %s -- %s | %FileCheck -check-prefix=CHECK-SETTER-V %s
40+
// CHECK-SETTER-V: s:17cursor_info_param12AccessorTestV4propSivs1vL_Sivp
41+
// CHECK-SETTER-V: PARENT OFFSET: 161
42+
43+
// RUN: %sourcekitd-test -req=cursor -pos=12:27 %s -- %s | %FileCheck -check-prefix=CHECK-CUSTOM-SOURCELOCATION %s
44+
// CHECK-CUSTOM-SOURCELOCATION: s:17cursor_info_param20customSourceLocation1aySi_tFACL_Sivp
45+
// CHECK-CUSTOM-SOURCELOCATION: PARENT OFFSET: 233

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,7 @@ getParamParentNameOffset(const ValueDecl *VD, SourceLoc Cursor) {
684684
if (Loc.isInvalid())
685685
return None;
686686
auto &SM = VD->getASTContext().SourceMgr;
687-
return SM.getLocOffsetInBuffer(Loc, SM.getIDForBufferIdentifier(SM.
688-
getBufferIdentifierForLoc(Loc)).getValue());
687+
return SM.getLocOffsetInBuffer(Loc, SM.findBufferContainingLoc(Loc));
689688
}
690689

691690
/// Returns true for failure to resolve.

unittests/Parse/LexerTests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,29 @@ TEST_F(LexerTest, getLocForStartOfToken) {
635635
}
636636
}
637637

638+
TEST_F(LexerTest, getLocForStartOfTokenWithCustomSourceLocation) {
639+
const char *Source =
640+
"aaa \n"
641+
// This next line is exactly 50 bytes to make it easy to compare with the
642+
// previous test.
643+
"#sourceLocation(file: \"custom-50.swuft\", line: 9)\n"
644+
" \tbbb \"hello\" \"-\\(val)-\"";
645+
646+
unsigned BufferID = SourceMgr.addMemBufferCopy(Source);
647+
648+
// First is character offset, second is its token offset.
649+
unsigned Offs[][2] =
650+
{ {1, 0}, {2, 0}, {3, 3}, {4, 4},
651+
{56, 56}, {59, 57}, {64, 61},
652+
// interpolated string
653+
{70, 69}, {73, 73}, {74, 73}, {75, 73}, {76, 76}, {77, 69} };
654+
655+
for (auto Pair : Offs) {
656+
ASSERT_EQ(Lexer::getLocForStartOfToken(SourceMgr, BufferID, Pair[0]),
657+
SourceMgr.getLocForOffset(BufferID, Pair[1]));
658+
}
659+
}
660+
638661
TEST_F(LexerTest, NestedSubLexers) {
639662
const char *Source = "aaa0 bbb1 ccc2 ddd3 eee4 fff5 ggg6";
640663

0 commit comments

Comments
 (0)