Skip to content

Commit 84778ee

Browse files
committed
[SourceKit] Pass 'swiftc' path to Driver when creating frontend args
Driver uses its path to derive the plugin paths (i.e. 'lib/swift/host/plugins' et al.) Previously it was a constant string 'swiftc' that caused SourceKit failed to find dylib plugins in the toolchain. Since 'SwiftLangSupport' knows the swift-frontend path, use it, but replacing the filename with 'swiftc', to derive the plugin paths. rdar://107849796
1 parent 5e90d32 commit 84778ee

File tree

11 files changed

+93
-37
lines changed

11 files changed

+93
-37
lines changed

include/swift/Driver/FrontendUtil.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ void ExpandResponseFilesWithRetry(llvm::StringSaver &Saver,
4848
/// \note This function is not intended to create invocations which are
4949
/// suitable for use in REPL or immediate modes.
5050
bool getSingleFrontendInvocationFromDriverArguments(
51-
ArrayRef<const char *> ArgList, DiagnosticEngine &Diags,
51+
StringRef DriverPath, ArrayRef<const char *> ArgList,
52+
DiagnosticEngine &Diags,
5253
llvm::function_ref<bool(ArrayRef<const char *> FrontendArgs)> Action,
5354
bool ForceNoOutputs = false);
5455

include/swift/IDETool/CompileInstance.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace ide {
3131

3232
/// Manages \c CompilerInstance for completion like operations.
3333
class CompileInstance {
34+
const std::string &SwiftExecutablePath;
3435
const std::string &RuntimeResourcePath;
3536
const std::string &DiagnosticDocumentationPath;
3637
const std::shared_ptr<swift::PluginRegistry> Plugins;
@@ -67,10 +68,12 @@ class CompileInstance {
6768
std::shared_ptr<std::atomic<bool>> CancellationFlag);
6869

6970
public:
70-
CompileInstance(const std::string &RuntimeResourcePath,
71+
CompileInstance(const std::string &SwiftExecutablePath,
72+
const std::string &RuntimeResourcePath,
7173
const std::string &DiagnosticDocumentationPath,
7274
std::shared_ptr<swift::PluginRegistry> Plugins = nullptr)
73-
: RuntimeResourcePath(RuntimeResourcePath),
75+
: SwiftExecutablePath(SwiftExecutablePath),
76+
RuntimeResourcePath(RuntimeResourcePath),
7477
DiagnosticDocumentationPath(DiagnosticDocumentationPath),
7578
Plugins(Plugins), CachedCIInvalidated(false), CachedReuseCount(0) {}
7679

lib/Driver/FrontendUtil.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static void removeSupplementaryOutputs(llvm::opt::ArgList &ArgList) {
5959
}
6060

6161
bool swift::driver::getSingleFrontendInvocationFromDriverArguments(
62-
ArrayRef<const char *> Argv, DiagnosticEngine &Diags,
62+
StringRef DriverPath, ArrayRef<const char *> Argv, DiagnosticEngine &Diags,
6363
llvm::function_ref<bool(ArrayRef<const char *> FrontendArgs)> Action,
6464
bool ForceNoOutputs) {
6565
SmallVector<const char *, 16> Args;
@@ -87,7 +87,7 @@ bool swift::driver::getSingleFrontendInvocationFromDriverArguments(
8787
ExpandResponseFilesWithRetry(Saver, Args);
8888

8989
// Force the driver into batch mode by specifying "swiftc" as the name.
90-
Driver TheDriver("swiftc", "swiftc", Args, Diags);
90+
Driver TheDriver(DriverPath, "swiftc", Args, Diags);
9191

9292
// Don't check for the existence of input files, since the user of the
9393
// CompilerInvocation may wish to remap inputs to source buffers.

lib/IDETool/CompileInstance.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,14 @@ bool CompileInstance::setupCI(
261261
DiagnosticDocumentationPath.c_str()});
262262
args.append(origArgs.begin(), origArgs.end());
263263

264+
SmallString<256> driverPath(SwiftExecutablePath);
265+
llvm::sys::path::remove_filename(driverPath);
266+
llvm::sys::path::append(driverPath, "swiftc");
267+
264268
CompilerInvocation invocation;
265269
bool invocationCreationFailed =
266270
driver::getSingleFrontendInvocationFromDriverArguments(
267-
args, Diags,
271+
driverPath, args, Diags,
268272
[&](ArrayRef<const char *> FrontendArgs) {
269273
return invocation.parseArgs(FrontendArgs, Diags);
270274
},

lib/IDETool/CompilerInvocation.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,14 @@ bool ide::initCompilerInvocation(
173173
StreamDiagConsumer DiagConsumer(ErrOS);
174174
Diags.addConsumer(DiagConsumer);
175175

176+
// Derive 'swiftc' path from 'swift-frontend' path (swiftExecutablePath).
177+
SmallString<256> driverPath(swiftExecutablePath);
178+
llvm::sys::path::remove_filename(driverPath);
179+
llvm::sys::path::append(driverPath, "swiftc");
180+
176181
bool InvocationCreationFailed =
177182
driver::getSingleFrontendInvocationFromDriverArguments(
178-
Args, Diags,
183+
driverPath, Args, Diags,
179184
[&](ArrayRef<const char *> FrontendArgs) {
180185
return Invocation.parseArgs(
181186
FrontendArgs, Diags, /*ConfigurationFileBuffers=*/nullptr,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
@OptionSet<UInt8>
3+
struct ShippingOptions {
4+
private enum Options: Int {
5+
case nextDay
6+
case secondDay
7+
case priority
8+
case standard
9+
}
10+
11+
static let express: ShippingOptions = [.nextDay, .secondDay]
12+
static let all: ShippingOptions = [.express, .priority, .standard]
13+
}
14+
15+
func test(opts: ShippingOptions) {
16+
let _ = ShippingOptions.nextDay
17+
}
18+
19+
// RUN: %sourcekitd-test \
20+
// RUN: -shell -- echo '## DIAGS ##' == \
21+
// RUN: -req=diags %s -- %s ==\
22+
// RUN: -shell -- echo '## CURSOR ##' == \
23+
// RUN: -req=cursor -pos=16:27 %s -- %s \
24+
// RUN: | %FileCheck %s
25+
26+
// CHECK-LABEL: ## DIAGS ##
27+
// CHECK: key.diagnostics: [
28+
// CHECK-NEXT: ]
29+
30+
// CHECK-LABEL: ## CURSOR ##
31+
// CHECK: source.lang.swift.ref.var.static

tools/SourceKit/lib/SwiftLang/SwiftCompile.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ compile::SessionManager::getSession(StringRef name) {
3131
}
3232

3333
bool inserted = false;
34-
std::tie(i, inserted) = sessions.try_emplace(
35-
name, std::make_shared<compile::Session>(
36-
RuntimeResourcePath, DiagnosticDocumentationPath, Plugins));
34+
std::tie(i, inserted) =
35+
sessions.try_emplace(name, std::make_shared<compile::Session>(
36+
SwiftExecutablePath, RuntimeResourcePath,
37+
DiagnosticDocumentationPath, Plugins));
3738
assert(inserted);
3839
return i->second;
3940
}

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ SwiftLangSupport::SwiftLangSupport(SourceKit::Context &SKCtx)
297297
SKCtx.getGlobalConfiguration());
298298

299299
CompileManager = std::make_shared<compile::SessionManager>(
300-
RuntimeResourcePath, DiagnosticDocumentationPath, Plugins);
300+
SwiftExecutablePath, RuntimeResourcePath, DiagnosticDocumentationPath,
301+
Plugins);
301302

302303
// By default, just use the in-memory cache.
303304
CCCache->inMemory = std::make_unique<ide::CodeCompletionCache>();

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,12 @@ class Session {
292292
swift::ide::CompileInstance Compiler;
293293

294294
public:
295-
Session(const std::string &RuntimeResourcePath,
295+
Session(const std::string &SwiftExecutablePath,
296+
const std::string &RuntimeResourcePath,
296297
const std::string &DiagnosticDocumentationPath,
297298
std::shared_ptr<swift::PluginRegistry> Plugins)
298-
: Compiler(RuntimeResourcePath, DiagnosticDocumentationPath, Plugins) {}
299+
: Compiler(SwiftExecutablePath, RuntimeResourcePath,
300+
DiagnosticDocumentationPath, Plugins) {}
299301

300302
bool
301303
performCompile(llvm::ArrayRef<const char *> Args,
@@ -307,6 +309,7 @@ class Session {
307309
};
308310

309311
class SessionManager {
312+
const std::string &SwiftExecutablePath;
310313
const std::string &RuntimeResourcePath;
311314
const std::string &DiagnosticDocumentationPath;
312315
const std::shared_ptr<swift::PluginRegistry> Plugins;
@@ -317,10 +320,12 @@ class SessionManager {
317320
mutable llvm::sys::Mutex mtx;
318321

319322
public:
320-
SessionManager(std::string &RuntimeResourcePath,
321-
std::string &DiagnosticDocumentationPath,
322-
std::shared_ptr<swift::PluginRegistry> Plugins)
323-
: RuntimeResourcePath(RuntimeResourcePath),
323+
SessionManager(const std::string &SwiftExecutablePath,
324+
const std::string &RuntimeResourcePath,
325+
const std::string &DiagnosticDocumentationPath,
326+
const std::shared_ptr<swift::PluginRegistry> Plugins)
327+
: SwiftExecutablePath(SwiftExecutablePath),
328+
RuntimeResourcePath(RuntimeResourcePath),
324329
DiagnosticDocumentationPath(DiagnosticDocumentationPath),
325330
Plugins(Plugins) {}
326331

tools/swift-ide-test/ModuleAPIDiff.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -910,9 +910,10 @@ int swift::doGenerateModuleAPIDescription(StringRef MainExecutablePath,
910910

911911
CompilerInvocation Invocation;
912912
bool HadError = driver::getSingleFrontendInvocationFromDriverArguments(
913-
CStringArgs, Diags, [&](ArrayRef<const char *> FrontendArgs) {
914-
return Invocation.parseArgs(FrontendArgs, Diags);
915-
});
913+
MainExecutablePath, CStringArgs, Diags,
914+
[&](ArrayRef<const char *> FrontendArgs) {
915+
return Invocation.parseArgs(FrontendArgs, Diags);
916+
});
916917

917918
if (HadError) {
918919
llvm::errs() << "error: unable to create a CompilerInvocation\n";

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4153,22 +4153,26 @@ static int doPrintUSRs(const CompilerInvocation &InitInvok,
41534153
return 0;
41544154
}
41554155

4156-
static int doTestCreateCompilerInvocation(ArrayRef<const char *> Args, bool ForceNoOutputs) {
4156+
static int doTestCreateCompilerInvocation(StringRef MainExecutablePath,
4157+
ArrayRef<const char *> Args,
4158+
bool ForceNoOutputs) {
41574159
PrintingDiagnosticConsumer PDC;
41584160
SourceManager SM;
41594161
DiagnosticEngine Diags(SM);
41604162
Diags.addConsumer(PDC);
41614163

41624164
CompilerInvocation CI;
41634165
bool HadError = driver::getSingleFrontendInvocationFromDriverArguments(
4164-
Args, Diags, [&](ArrayRef<const char *> FrontendArgs) {
4165-
llvm::outs() << "Frontend Arguments BEGIN\n";
4166-
for (const char *arg : FrontendArgs) {
4167-
llvm::outs() << arg << "\n";
4168-
}
4169-
llvm::outs() << "Frontend Arguments END\n";
4170-
return CI.parseArgs(FrontendArgs, Diags);
4171-
}, ForceNoOutputs);
4166+
MainExecutablePath, Args, Diags,
4167+
[&](ArrayRef<const char *> FrontendArgs) {
4168+
llvm::outs() << "Frontend Arguments BEGIN\n";
4169+
for (const char *arg : FrontendArgs) {
4170+
llvm::outs() << arg << "\n";
4171+
}
4172+
llvm::outs() << "Frontend Arguments END\n";
4173+
return CI.parseArgs(FrontendArgs, Diags);
4174+
},
4175+
ForceNoOutputs);
41724176

41734177
if (HadError) {
41744178
llvm::errs() << "error: unable to create a CompilerInvocation\n";
@@ -4202,6 +4206,9 @@ int main(int argc, char *argv[]) {
42024206
INITIALIZE_LLVM();
42034207
initializeSwiftModules();
42044208

4209+
std::string mainExecutablePath = llvm::sys::fs::getMainExecutable(
4210+
argv[0], reinterpret_cast<void *>(&anchorForGetMainExecutable));
4211+
42054212
if (argc > 1) {
42064213
// Handle integrated test tools which do not use
42074214
// llvm::cl::ParseCommandLineOptions.
@@ -4213,7 +4220,8 @@ int main(int argc, char *argv[]) {
42134220
ForceNoOutputs = true;
42144221
Args = Args.drop_front();
42154222
}
4216-
return doTestCreateCompilerInvocation(Args, ForceNoOutputs);
4223+
return doTestCreateCompilerInvocation(mainExecutablePath, Args,
4224+
ForceNoOutputs);
42174225
}
42184226
}
42194227

@@ -4242,10 +4250,8 @@ int main(int argc, char *argv[]) {
42424250
}
42434251

42444252
if (options::Action == ActionType::GenerateModuleAPIDescription) {
4245-
return doGenerateModuleAPIDescription(
4246-
llvm::sys::fs::getMainExecutable(
4247-
argv[0], reinterpret_cast<void *>(&anchorForGetMainExecutable)),
4248-
options::InputFilenames);
4253+
return doGenerateModuleAPIDescription(mainExecutablePath,
4254+
options::InputFilenames);
42494255
}
42504256

42514257
if (options::Action == ActionType::DumpCompletionCache) {
@@ -4312,9 +4318,7 @@ int main(int argc, char *argv[]) {
43124318
for (auto &File : options::InputFilenames)
43134319
InitInvok.getFrontendOptions().InputsAndOutputs.addInputFile(File);
43144320

4315-
InitInvok.setMainExecutablePath(
4316-
llvm::sys::fs::getMainExecutable(argv[0],
4317-
reinterpret_cast<void *>(&anchorForGetMainExecutable)));
4321+
InitInvok.setMainExecutablePath(mainExecutablePath);
43184322
InitInvok.setModuleName(options::ModuleName);
43194323

43204324
InitInvok.setSDKPath(options::SDK);

0 commit comments

Comments
 (0)