Skip to content

Commit 055dc66

Browse files
committed
[Frontend] Recover missing input file by dummy input buffer.
Also, continue trying opening files even if any of primary files are missing so that the caller can know all files failed to open. rdar://problem/33757793
1 parent bbd1ed4 commit 055dc66

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

lib/Frontend/Frontend.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,17 +599,19 @@ bool CompilerInstance::setUpInputs() {
599599

600600
const auto &Inputs =
601601
Invocation.getFrontendOptions().InputsAndOutputs.getAllInputs();
602+
bool hasFailed = false;
602603
for (const InputFile &input : Inputs) {
603604
bool failed = false;
604605
Optional<unsigned> bufferID = getRecordedBufferID(input, failed);
605-
if (failed)
606-
return true;
606+
hasFailed |= failed;
607607

608608
if (!bufferID.hasValue() || !input.isPrimary())
609609
continue;
610610

611611
recordPrimaryInputBuffer(*bufferID);
612612
}
613+
if (hasFailed)
614+
return true;
613615

614616
// Set the primary file to the code-completion point if one exists.
615617
if (codeCompletionBufferID.hasValue() &&
@@ -631,6 +633,15 @@ Optional<unsigned> CompilerInstance::getRecordedBufferID(const InputFile &input,
631633
}
632634
auto buffers = getInputBuffersIfPresent(input);
633635

636+
// For non-primary '.swift' files, recover by dummy buffer so that the primary
637+
// files are diagnosed. Also, IDE functionalities want to proceed even with
638+
// missing files.
639+
if (!buffers.hasValue() && input.getType() == file_types::TY_Swift &&
640+
!input.isPrimary()) {
641+
buffers = ModuleBuffers(llvm::MemoryBuffer::getMemBuffer(
642+
"// missing file\n", input.getFileName()));
643+
}
644+
634645
if (!buffers.hasValue()) {
635646
failed = true;
636647
return None;

test/Frontend/missing_files.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: not %target-swift-frontend -c -parse-as-library /tmp/SOMETHING_DOES_NOT_EXIST_1.swift -primary-file %s /tmp/SOMETHING_DOES_NOT_EXIST_2.swift -o %t/out.o 2> %t/error1.output
4+
// RUN: not test -f %t/out.o
5+
// RUN: %FileCheck %s -input-file %t/error1.output --check-prefixes=CHECK,CHECK_RECOVER
6+
7+
// RUN: not %target-swift-frontend -c -parse-as-library -primary-file /tmp/SOMETHING_DOES_NOT_EXIST_1.swift -primary-file %s /tmp/SOMETHING_DOES_NOT_EXIST_2.swift -o %t/out1.o -o %t/out2.o 2> %t/error2.output
8+
// RUN: not test -f %t/out1.o
9+
// RUN: not test -f %t/out2.o
10+
// RUN: %FileCheck %s -input-file %t/error2.output --check-prefixes=CHECK,CHECK_FAIL
11+
12+
// CHECK-DAG: <unknown>:0: error: error opening input file '{{[/\\]}}tmp{{[/\\]}}SOMETHING_DOES_NOT_EXIST_1.swift' ({{.*}})
13+
// CHECK-DAG: <unknown>:0: error: error opening input file '{{[/\\]}}tmp{{[/\\]}}SOMETHING_DOES_NOT_EXIST_2.swift' ({{.*}})
14+
15+
public var x = INVALID_DECL
16+
// CHECK_RECOVER-DAG: test{{[/\\]}}Frontend{{[/\\]}}missing_files.swift:[[@LINE-1]]:16: error: cannot find 'INVALID_DECL' in scope
17+
// CHECK_FAIL-NOT: INVALID_DECL
18+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// RUN: %sourcekitd-test -req=complete -pos=1:1 %s -- /tmp/SOMETHING_DOES_NOT_EXIST_1.swift %s /tmp/SOMETHING_DOES_NOT_EXIST_2.swift | %FileCheck %s
2+
3+
// CHECK: results: [

0 commit comments

Comments
 (0)