Skip to content

Commit ca1c2cc

Browse files
committed
[SourceKit] Allow same filename
When compiling a module, we can’t have two files with the same filename because the filename is used to disambiguate private declarations during CodeGen. For SourceKit this is not an issue and we shouldn’t stop providing semantic functionality because of it. Fixes rdar://77618144
1 parent b7edb16 commit ca1c2cc

File tree

6 files changed

+26
-3
lines changed

6 files changed

+26
-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: 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)