Skip to content

Commit 5c568af

Browse files
author
Nathan Hawes
committed
[incrParse] Fix bug mapping a node's location back to its location in the cached syntax tree
Also fix Edit::intersectsOrTouchesRange check only returning true when the ranges overlapped, rather than when they overlapped or 'touched'. Resolves rdar://problem/45108439
1 parent 25c02ce commit 5c568af

File tree

7 files changed

+131
-6
lines changed

7 files changed

+131
-6
lines changed

include/swift/Parse/SyntaxParsingCache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct SourceEdit {
3838
/// Check if the characters replaced by this edit fall into the given range
3939
/// or are directly adjacent to it
4040
bool intersectsOrTouchesRange(size_t RangeStart, size_t RangeEnd) {
41-
return !(End <= RangeStart || Start >= RangeEnd);
41+
return End >= RangeStart && Start <= RangeEnd;
4242
}
4343
};
4444

lib/Basic/SourceLoc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,9 @@ llvm::Optional<unsigned> SourceManager::resolveFromLineCol(unsigned BufferId,
331331
return None;
332332
}
333333
Ptr = LineStart;
334-
for (; Ptr < End; ++Ptr) {
334+
335+
// The <= here is to allow for non-inclusive range end positions at EOF
336+
for (; Ptr <= End; ++Ptr) {
335337
--Col;
336338
if (Col == 0)
337339
return Ptr - InputBuf->getBufferStart();

lib/Parse/SyntaxParsingCache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ llvm::Optional<Syntax> SyntaxParsingCache::lookUp(size_t NewPosition,
8585
size_t OldPosition = NewPosition;
8686
for (auto I = Edits.rbegin(), E = Edits.rend(); I != E; ++I) {
8787
auto Edit = *I;
88-
if (Edit.End <= OldPosition) {
88+
if (Edit.End < OldPosition) {
8989
OldPosition =
9090
OldPosition - Edit.ReplacementLength + Edit.originalLength();
9191
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{
2+
"id": 39,
3+
"kind": "SourceFile",
4+
"layout": [
5+
{
6+
"id": 38,
7+
"kind": "CodeBlockItemList",
8+
"layout": [
9+
{
10+
"id": 13,
11+
"omitted": true
12+
},
13+
{
14+
"id": 35,
15+
"kind": "CodeBlockItem",
16+
"layout": [
17+
{
18+
"id": 34,
19+
"kind": "SequenceExpr",
20+
"layout": [
21+
{
22+
"id": 33,
23+
"kind": "ExprList",
24+
"layout": [
25+
{
26+
"id": 28,
27+
"kind": "DiscardAssignmentExpr",
28+
"layout": [
29+
{
30+
"id": 27,
31+
"tokenKind": {
32+
"kind": "kw__"
33+
},
34+
"leadingTrivia": [
35+
{
36+
"kind": "Newline",
37+
"value": 1
38+
}
39+
],
40+
"trailingTrivia": [
41+
{
42+
"kind": "Space",
43+
"value": 1
44+
}
45+
],
46+
"presence": "Present"
47+
}
48+
],
49+
"presence": "Present"
50+
},
51+
{
52+
"id": 30,
53+
"kind": "AssignmentExpr",
54+
"layout": [
55+
{
56+
"id": 29,
57+
"tokenKind": {
58+
"kind": "equal"
59+
},
60+
"leadingTrivia": [],
61+
"trailingTrivia": [
62+
{
63+
"kind": "Space",
64+
"value": 1
65+
}
66+
],
67+
"presence": "Present"
68+
}
69+
],
70+
"presence": "Present"
71+
},
72+
{
73+
"id": 32,
74+
"kind": "IdentifierExpr",
75+
"layout": [
76+
{
77+
"id": 31,
78+
"tokenKind": {
79+
"kind": "identifier",
80+
"text": "xx"
81+
},
82+
"leadingTrivia": [],
83+
"trailingTrivia": [],
84+
"presence": "Present"
85+
},
86+
null
87+
],
88+
"presence": "Present"
89+
}
90+
],
91+
"presence": "Present"
92+
}
93+
],
94+
"presence": "Present"
95+
},
96+
null,
97+
null
98+
],
99+
"presence": "Present"
100+
}
101+
],
102+
"presence": "Present"
103+
},
104+
{
105+
"id": 37,
106+
"tokenKind": {
107+
"kind": "eof",
108+
"text": ""
109+
},
110+
"leadingTrivia": [],
111+
"trailingTrivia": [],
112+
"presence": "Present"
113+
}
114+
],
115+
"presence": "Present"
116+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %incr-transfer-tree --expected-incremental-syntax-tree %S/Outputs/extend-identifier-at-eof.json %s
2+
3+
func foo() {}
4+
_ = x<<<|||x>>>

test/incrParse/simple.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
// RUN: %validate-incrparse %s --test-case LAST_CHARACTER_OF_STRUCT
1414
// RUN: %validate-incrparse %s --test-case ADD_ARRAY_CLOSE_BRACKET
1515
// RUN: %validate-incrparse %s --test-case ADD_IF_OPEN_BRACE
16+
// RUN: %validate-incrparse %s --test-case EXTEND_IDENTIFIER
1617

1718
func start() {}
1819

1920
<reparse REPLACE>
2021
func foo() {
2122
}
2223

23-
_ = <<REPLACE<6|||7>>></reparse REPLACE>
24-
_ = <<REPLACE_BY_LONGER<6|||"Hello World">>>
24+
_ = <<REPLACE<6|||7>>>
25+
_ = </reparse REPLACE><<REPLACE_BY_LONGER<6|||"Hello World">>>
2526
_ = <<REPLACE_BY_SHORTER<"Hello again"|||"a">>>
2627
<<INSERT<|||foo()>>>
2728
<<REMOVE<print("abc")|||>>>
@@ -52,3 +53,5 @@ var computedVar: [Int] {
5253
if true <<ADD_IF_OPEN_BRACE<|||{>>>
5354
_ = 5
5455
}
56+
57+
let y<<EXTEND_IDENTIFIER<|||ou>>> = 42

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1996,7 +1996,7 @@ static unsigned resolveFromLineCol(unsigned Line, unsigned Col,
19961996
exit(1);
19971997
}
19981998
Ptr = LineStart;
1999-
for (; Ptr < End; ++Ptr) {
1999+
for (; Ptr <= End; ++Ptr) {
20002000
--Col;
20012001
if (Col == 0)
20022002
return Ptr - InputBuf->getBufferStart();

0 commit comments

Comments
 (0)