Skip to content

Commit 2e9bf34

Browse files
committed
[SourceKit] Fix a crash that occurred when a document without an associated source file is reopened
If a file isn’t opened with a `key.sourcefile` argument, we don’t store a snapshot for it. This could cause a crash when trying to consult its snapshot to see whether an AST can be reused for cursor info.
1 parent b9379ae commit 2e9bf34

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
// RUN: %sourcekitd-test \
4+
// RUN: -json-request-path %t/1-open-without-sourcefile.json \
5+
// RUN: == -json-request-path %t/2-close.json \
6+
// RUN: == -json-request-path %t/3-reopen-without-compiler-args.json \
7+
// RUN: == -json-request-path %t/4-cursor-info.json
8+
9+
// This used to crash with a nullptr dereference because we didn't store a
10+
// snapshot in the FileContents of a.swift since it wasn't opened with a
11+
// key.sourcefile argument.
12+
13+
// BEGIN 1-open-without-sourcefile.json
14+
{
15+
key.request: source.request.editor.open,
16+
key.name: "/invalid/a.swift",
17+
key.compilerargs: [
18+
"/invalid/a.swift",
19+
"/invalid/b.swift"
20+
],
21+
key.sourcetext: "",
22+
key.enablesyntaxmap: 0,
23+
key.enablesubstructure: 0,
24+
key.enablediagnostics: 0
25+
}
26+
// BEGIN 2-close.json
27+
{
28+
key.request: source.request.editor.close,
29+
key.name: "/invalid/a.swift"
30+
}
31+
// BEGIN 3-reopen-without-compiler-args.json
32+
{
33+
key.request: source.request.editor.open,
34+
key.name: "/invalid/a.swift",
35+
key.compilerargs: [
36+
],
37+
key.sourcetext: "",
38+
key.enablesyntaxmap: 0,
39+
key.enablesubstructure: 0,
40+
key.enablediagnostics: 0
41+
}
42+
// BEGIN 4-cursor-info.json
43+
{
44+
key.request: source.request.cursorinfo,
45+
key.compilerargs: [
46+
"/invalid/a.swift",
47+
"/invalid/b.swift"
48+
],
49+
key.offset: 0,
50+
key.sourcefile: "/invalid/a.swift"
51+
}

tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,9 @@ ASTBuildOperationRef ASTProducer::getBuildOperationForConsumer(
11851185
std::vector<ImmutableTextSnapshotRef> Snapshots;
11861186
Snapshots.reserve(BuildOp->getFileContents().size());
11871187
for (auto &FileContent : BuildOp->getFileContents()) {
1188-
Snapshots.push_back(FileContent.Snapshot);
1188+
if (FileContent.Snapshot) {
1189+
Snapshots.push_back(FileContent.Snapshot);
1190+
}
11891191
}
11901192
if (BuildOp->matchesSourceState(FileSystem)) {
11911193
++Mgr->Impl.Stats->numASTCacheHits;

0 commit comments

Comments
 (0)