Skip to content

Commit 7f09acc

Browse files
authored
Merge pull request #37567 from ahoppen/pr-5.5/allow-same-filename-for-sourcekit
[5.5][SourceKit] Allow same filename
2 parents d8ada66 + ca1c2cc commit 7f09acc

File tree

9 files changed

+41
-3
lines changed

9 files changed

+41
-3
lines changed

include/swift/Driver/Driver.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ class Driver {
201201
/// Indicates whether the driver should check that the input files exist.
202202
bool CheckInputFilesExist = true;
203203

204+
/// Indicates whether the driver should suppress the "same filename used
205+
/// twice" error.
206+
bool SuppressSameFileNameError = false;
207+
204208
/// Indicates that this driver never actually executes any commands but is
205209
/// just set up to retrieve the swift-frontend invocation that would be
206210
/// executed during compilation.
@@ -232,6 +236,8 @@ class Driver {
232236

233237
void setCheckInputFilesExist(bool Value) { CheckInputFilesExist = Value; }
234238

239+
void setSuppressSameFileNameError(bool Value) { SuppressSameFileNameError = Value; }
240+
235241
bool isDummyDriverForFrontendInvocation() const {
236242
return IsDummyDriverForFrontendInvocation;
237243
}

lib/Driver/Driver.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,9 +1315,11 @@ void Driver::buildInputs(const ToolChain &TC,
13151315
if (Ty == file_types::TY_Swift) {
13161316
StringRef Basename = llvm::sys::path::filename(Value);
13171317
if (!SourceFileNames.insert({Basename, Value}).second) {
1318-
Diags.diagnose(SourceLoc(), diag::error_two_files_same_name,
1319-
Basename, SourceFileNames[Basename], Value);
1320-
Diags.diagnose(SourceLoc(), diag::note_explain_two_files_same_name);
1318+
if (!SuppressSameFileNameError) {
1319+
Diags.diagnose(SourceLoc(), diag::error_two_files_same_name,
1320+
Basename, SourceFileNames[Basename], Value);
1321+
Diags.diagnose(SourceLoc(), diag::note_explain_two_files_same_name);
1322+
}
13211323
}
13221324
}
13231325
}

lib/Driver/FrontendUtil.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ bool swift::driver::getSingleFrontendInvocationFromDriverArguments(
100100
if (Diags.hadAnyError())
101101
return true;
102102

103+
TheDriver.setSuppressSameFileNameError(true);
103104
std::unique_ptr<Compilation> C =
104105
TheDriver.buildCompilation(*TC, std::move(ArgList));
105106
if (!C || C->getJobs().empty())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Foo {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Bar {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// A module should fail to be generated if the same filename is used twice and '-experimental-allow-module-with-compiler-errors' is not passed
4+
5+
// RUN: not %target-swift-frontend -emit-module -o %t/no_allow_compiler_errors.swiftmodule %S/Inputs/same_filename/A/File.swift %S/Inputs/same_filename/B/File.swift
6+
// RUN: not ls %t/no_allow_compiler_errors.swiftmodule
7+
8+
// If '-experimental-allow-module-with-compiler-errors' is passed, we should throw an error but still generate a module
9+
10+
// RUN: %target-swift-frontend -emit-module -experimental-allow-module-with-compiler-errors -o %t/allow_compiler_errors.swiftmodule %S/Inputs/same_filename/A/File.swift %S/Inputs/same_filename/B/File.swift 2>&1 | %FileCheck %s
11+
// RUN: ls %t/allow_compiler_errors.swiftmodule
12+
13+
// CHECK: filename "File.swift" used twice:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Foo {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Bar {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// We should not fail if two distinct file have the same name - this is only an issue in CodeGen
2+
// RUN: %sourcekitd-test -req=cursor -pos=1:7 %S/Inputs/invalid_compiler_args/A/File.swift -- %S/Inputs/invalid_compiler_args/A/File.swift %S/Inputs/invalid_compiler_args/B/File.swift | %FileCheck %s
3+
4+
// We can't do anything if the requested file is not in the compiler arguments
5+
// RUN: not %sourcekitd-test -req=cursor -pos=1:7 %S/Inputs/invalid_compiler_args/A/File.swift --
6+
// RUN: not %sourcekitd-test -req=cursor -pos=1:7 %S/Inputs/invalid_compiler_args/A/File.swift -- %S/Inputs/invalid_compiler_args/B/File.swift
7+
8+
// Specifying a file twice should just ignore one of them
9+
// RUN: %sourcekitd-test -req=cursor -pos=1:7 %S/Inputs/invalid_compiler_args/A/File.swift -- %S/Inputs/invalid_compiler_args/A/File.swift %S/Inputs/invalid_compiler_args/A/File.swift
10+
11+
12+
// CHECK: source.lang.swift.decl.class

0 commit comments

Comments
 (0)