Skip to content

[SourceKit] Pass the main swift executable path when constructing a compiler invocation #58852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/IDE/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ bool initCompilerInvocation(
FrontendOptions::ActionType Action, DiagnosticEngine &Diags,
StringRef UnresolvedPrimaryFile,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
const std::string &swiftExecutablePath,
const std::string &runtimeResourcePath,
const std::string &diagnosticDocumentationPath, time_t sessionTimestamp,
std::string &Error);
Expand Down
5 changes: 4 additions & 1 deletion lib/IDE/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ bool ide::initCompilerInvocation(
FrontendOptions::ActionType Action, DiagnosticEngine &Diags,
StringRef UnresolvedPrimaryFile,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
const std::string &swiftExecutablePath,
const std::string &runtimeResourcePath,
const std::string &diagnosticDocumentationPath, time_t sessionTimestamp,
std::string &Error) {
Expand All @@ -299,7 +300,9 @@ bool ide::initCompilerInvocation(
driver::getSingleFrontendInvocationFromDriverArguments(
Args, Diags,
[&](ArrayRef<const char *> FrontendArgs) {
return Invocation.parseArgs(FrontendArgs, Diags);
return Invocation.parseArgs(
FrontendArgs, Diags, /*ConfigurationFileBuffers=*/nullptr,
/*workingDirectory=*/"", swiftExecutablePath);
},
/*ForceNoOutputs=*/true);

Expand Down
3 changes: 0 additions & 3 deletions test/SourceKit/Misc/match-module-cache-with-compiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

// REQUIRES: shell

// https://github.com/apple/swift/issues/58786
// UNSUPPORTED: OS=linux-gnu

// RUN: %empty-directory(%t)

// RUN: COMPILER_ARGS=( \
Expand Down
8 changes: 7 additions & 1 deletion tools/SourceKit/include/SourceKit/Core/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ class SlowRequestSimulator {
};

class Context {
/// The path of the swift-frontend executable.
/// Used to find clang relative to it.
std::string SwiftExecutablePath;
std::string RuntimeLibPath;
std::string DiagnosticDocumentationPath;
std::unique_ptr<LangSupport> SwiftLang;
Expand All @@ -178,12 +181,15 @@ class Context {
std::shared_ptr<SlowRequestSimulator> SlowRequestSim;

public:
Context(StringRef RuntimeLibPath, StringRef DiagnosticDocumentationPath,
Context(StringRef SwiftExecutablePath, StringRef RuntimeLibPath,
StringRef DiagnosticDocumentationPath,
llvm::function_ref<std::unique_ptr<LangSupport>(Context &)>
LangSupportFactoryFn,
bool shouldDispatchNotificationsOnMain = true);
~Context();

StringRef getSwiftExecutablePath() const { return SwiftExecutablePath; }

StringRef getRuntimeLibPath() const { return RuntimeLibPath; }
StringRef getDiagnosticDocumentationPath() const {
return DiagnosticDocumentationPath;
Expand Down
5 changes: 3 additions & 2 deletions tools/SourceKit/lib/Core/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ GlobalConfig::getCompletionOpts() const {
}

SourceKit::Context::Context(
StringRef RuntimeLibPath, StringRef DiagnosticDocumentationPath,
StringRef SwiftExecutablePath, StringRef RuntimeLibPath,
StringRef DiagnosticDocumentationPath,
llvm::function_ref<std::unique_ptr<LangSupport>(Context &)>
LangSupportFactoryFn,
bool shouldDispatchNotificationsOnMain)
: RuntimeLibPath(RuntimeLibPath),
: SwiftExecutablePath(SwiftExecutablePath), RuntimeLibPath(RuntimeLibPath),
DiagnosticDocumentationPath(DiagnosticDocumentationPath),
NotificationCtr(
new NotificationCenter(shouldDispatchNotificationsOnMain)),
Expand Down
20 changes: 12 additions & 8 deletions tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,11 @@ struct SwiftASTManager::Implementation {
std::shared_ptr<SwiftEditorDocumentFileMap> EditorDocs,
std::shared_ptr<GlobalConfig> Config,
std::shared_ptr<SwiftStatistics> Stats,
std::shared_ptr<RequestTracker> ReqTracker, StringRef RuntimeResourcePath,
StringRef DiagnosticDocumentationPath)
std::shared_ptr<RequestTracker> ReqTracker, StringRef SwiftExecutablePath,
StringRef RuntimeResourcePath, StringRef DiagnosticDocumentationPath)
: EditorDocs(EditorDocs), Config(Config), Stats(Stats),
ReqTracker(ReqTracker), RuntimeResourcePath(RuntimeResourcePath),
ReqTracker(ReqTracker), SwiftExecutablePath(SwiftExecutablePath),
RuntimeResourcePath(RuntimeResourcePath),
DiagnosticDocumentationPath(DiagnosticDocumentationPath),
SessionTimestamp(llvm::sys::toTimeT(std::chrono::system_clock::now())) {
}
Expand All @@ -556,6 +557,9 @@ struct SwiftASTManager::Implementation {
std::shared_ptr<GlobalConfig> Config;
std::shared_ptr<SwiftStatistics> Stats;
std::shared_ptr<RequestTracker> ReqTracker;
/// The path of the swift-frontend executable.
/// Used to find clang relative to it.
std::string SwiftExecutablePath;
std::string RuntimeResourcePath;
std::string DiagnosticDocumentationPath;
SourceManager SourceMgr;
Expand Down Expand Up @@ -625,10 +629,10 @@ SwiftASTManager::SwiftASTManager(
std::shared_ptr<SwiftEditorDocumentFileMap> EditorDocs,
std::shared_ptr<GlobalConfig> Config,
std::shared_ptr<SwiftStatistics> Stats,
std::shared_ptr<RequestTracker> ReqTracker, StringRef RuntimeResourcePath,
StringRef DiagnosticDocumentationPath)
std::shared_ptr<RequestTracker> ReqTracker, StringRef SwiftExecutablePath,
StringRef RuntimeResourcePath, StringRef DiagnosticDocumentationPath)
: Impl(*new Implementation(EditorDocs, Config, Stats, ReqTracker,
RuntimeResourcePath,
SwiftExecutablePath, RuntimeResourcePath,
DiagnosticDocumentationPath)) {}

SwiftASTManager::~SwiftASTManager() {
Expand Down Expand Up @@ -665,8 +669,8 @@ bool SwiftASTManager::initCompilerInvocation(
std::string &Error) {
return ide::initCompilerInvocation(
Invocation, OrigArgs, Action, Diags, UnresolvedPrimaryFile, FileSystem,
Impl.RuntimeResourcePath, Impl.DiagnosticDocumentationPath,
Impl.SessionTimestamp, Error);
Impl.SwiftExecutablePath, Impl.RuntimeResourcePath,
Impl.DiagnosticDocumentationPath, Impl.SessionTimestamp, Error);
}

bool SwiftASTManager::initCompilerInvocation(
Expand Down
1 change: 1 addition & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftASTManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ class SwiftASTManager : public std::enable_shared_from_this<SwiftASTManager> {
std::shared_ptr<GlobalConfig> Config,
std::shared_ptr<SwiftStatistics> Stats,
std::shared_ptr<RequestTracker> ReqTracker,
StringRef SwiftExecutablePath,
StringRef RuntimeResourcePath,
StringRef DiagnosticDocumentationPath);
~SwiftASTManager();
Expand Down
3 changes: 2 additions & 1 deletion tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ configureCompletionInstance(std::shared_ptr<CompletionInstance> CompletionInst,

SwiftLangSupport::SwiftLangSupport(SourceKit::Context &SKCtx)
: NotificationCtr(SKCtx.getNotificationCenter()),
SwiftExecutablePath(SKCtx.getSwiftExecutablePath()),
ReqTracker(SKCtx.getRequestTracker()), CCCache(new SwiftCompletionCache),
CompileManager(RuntimeResourcePath, DiagnosticDocumentationPath) {
llvm::SmallString<128> LibPath(SKCtx.getRuntimeLibPath());
Expand All @@ -283,7 +284,7 @@ SwiftLangSupport::SwiftLangSupport(SourceKit::Context &SKCtx)
EditorDocuments = std::make_shared<SwiftEditorDocumentFileMap>();
ASTMgr = std::make_shared<SwiftASTManager>(
EditorDocuments, SKCtx.getGlobalConfiguration(), Stats, ReqTracker,
RuntimeResourcePath, DiagnosticDocumentationPath);
SwiftExecutablePath, RuntimeResourcePath, DiagnosticDocumentationPath);

CompletionInst = std::make_shared<CompletionInstance>();

Expand Down
3 changes: 3 additions & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ struct SwiftStatistics {

class SwiftLangSupport : public LangSupport {
std::shared_ptr<NotificationCenter> NotificationCtr;
/// The path of the swift-frontend executable.
/// Used to find clang relative to it.
std::string SwiftExecutablePath;
std::string RuntimeResourcePath;
std::string DiagnosticDocumentationPath;
std::shared_ptr<SwiftASTManager> ASTMgr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ sourcekitd_variant_uid_get_value
_ZN10sourcekitd13cancelRequestEPKv
_ZN10sourcekitd13enableLoggingEN4llvm9StringRefE
_ZN10sourcekitd13handleRequestEPvPKvNSt3__18functionIFvS0_EEE
_ZN10sourcekitd17initializeServiceEN4llvm9StringRefES1_NSt3__18functionIFvPvEEE
_ZN10sourcekitd17initializeServiceEN4llvm9StringRefES1_S1_NSt3__18functionIFvPvEEE
_ZN10sourcekitd24createErrorRequestFailedEN4llvm9StringRefE
_ZN10sourcekitd24disposeCancellationTokenEPKv
_ZN9SourceKit6Logger12LoggingLevelE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ static std::string getRuntimeLibPath() {
return libPath.str().str();
}

static std::string getSwiftExecutablePath() {
llvm::SmallString<128> path;
getToolchainPrefixPath(path);
llvm::sys::path::append(path, "bin", "swift-frontend");
return path.str().str();
}

static std::string getDiagnosticDocumentationPath() {
llvm::SmallString<128> docPath;
getToolchainPrefixPath(docPath);
Expand All @@ -90,7 +97,7 @@ static std::string getDiagnosticDocumentationPath() {
void sourcekitd_initialize(void) {
if (sourcekitd::initializeClient()) {
LOG_INFO_FUNC(High, "initializing");
sourcekitd::initializeService(getRuntimeLibPath(),
sourcekitd::initializeService(getSwiftExecutablePath(), getRuntimeLibPath(),
getDiagnosticDocumentationPath(),
postNotification);
}
Expand Down
12 changes: 10 additions & 2 deletions tools/SourceKit/tools/sourcekitd/bin/XPC/Service/XPCService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ static std::string getRuntimeLibPath() {
return path.str().str();
}

static std::string getSwiftExecutablePath() {
llvm::SmallString<128> path;
getToolchainPrefixPath(path);
llvm::sys::path::append(path, "bin", "swift-frontend");
return path.str().str();
}

static std::string getDiagnosticDocumentationPath() {
llvm::SmallString<128> path;
getToolchainPrefixPath(path);
Expand Down Expand Up @@ -362,8 +369,9 @@ int main(int argc, const char *argv[]) {
^const char *(sourcekitd_uid_t uid) {
return xpcUIdentFromSKDUID(uid).c_str();
});
sourcekitd::initializeService(
getRuntimeLibPath(), getDiagnosticDocumentationPath(), postNotification);
sourcekitd::initializeService(getSwiftExecutablePath(), getRuntimeLibPath(),
getDiagnosticDocumentationPath(),
postNotification);

// Increase the file descriptor limit.
// FIXME: Portability ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,14 @@ class RequestDict {
};

/// Initialize the service. Must be called before attempting to handle requests.
/// \param swiftExecutablePath The path of the swift-frontend executable.
/// Used to find clang relative to it.
/// \param runtimeLibPath The path to the toolchain's library directory.
/// \param diagnosticDocumentationPath The path to diagnostics documentation.
/// \param postNotification Callback to post a notification.
void initializeService(
llvm::StringRef runtimeLibPath, llvm::StringRef diagnosticDocumentationPath,
llvm::StringRef swiftExecutablePath, llvm::StringRef runtimeLibPath,
llvm::StringRef diagnosticDocumentationPath,
std::function<void(sourcekitd_response_t)> postNotification);
/// Shutdown the service.
void shutdownService();
Expand Down
9 changes: 5 additions & 4 deletions tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,15 @@ static void fillDictionaryForDiagnosticInfo(ResponseBuilder::Dictionary Elem,
static SourceKit::Context *GlobalCtx = nullptr;

void sourcekitd::initializeService(
StringRef runtimeLibPath, StringRef diagnosticDocumentationPath,
llvm::StringRef swiftExecutablePath, StringRef runtimeLibPath,
StringRef diagnosticDocumentationPath,
std::function<void(sourcekitd_response_t)> postNotification) {
INITIALIZE_LLVM();
initializeSwiftModules();
llvm::EnablePrettyStackTrace();
GlobalCtx =
new SourceKit::Context(runtimeLibPath, diagnosticDocumentationPath,
SourceKit::createSwiftLangSupport);
GlobalCtx = new SourceKit::Context(swiftExecutablePath, runtimeLibPath,
diagnosticDocumentationPath,
SourceKit::createSwiftLangSupport);
auto noteCenter = GlobalCtx->getNotificationCenter();

noteCenter->addDocumentUpdateNotificationReceiver([postNotification](StringRef DocumentName) {
Expand Down
9 changes: 8 additions & 1 deletion unittests/SourceKit/SwiftLang/CursorInfoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ static StringRef getRuntimeLibPath() {
return sys::path::parent_path(SWIFTLIB_DIR);
}

static SmallString<128> getSwiftExecutablePath() {
SmallString<128> path = sys::path::parent_path(getRuntimeLibPath());
sys::path::append(path, "bin", "swift-frontend");
return path;
}

static void *createCancallationToken() {
static std::atomic<size_t> handle(1000);
return reinterpret_cast<void *>(
Expand Down Expand Up @@ -125,7 +131,8 @@ class CursorInfoTest : public ::testing::Test {
}

CursorInfoTest()
: Ctx(*new SourceKit::Context(getRuntimeLibPath(),
: Ctx(*new SourceKit::Context(getSwiftExecutablePath(),
getRuntimeLibPath(),
/*diagnosticDocumentationPath*/ "",
SourceKit::createSwiftLangSupport,
/*dispatchOnMain=*/false)) {
Expand Down
9 changes: 8 additions & 1 deletion unittests/SourceKit/SwiftLang/EditingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ static StringRef getRuntimeLibPath() {
return sys::path::parent_path(SWIFTLIB_DIR);
}

static SmallString<128> getSwiftExecutablePath() {
SmallString<128> path = sys::path::parent_path(getRuntimeLibPath());
sys::path::append(path, "bin", "swift-frontend");
return path;
}

namespace {

struct Token {
Expand Down Expand Up @@ -124,7 +130,8 @@ class EditTest : public ::testing::Test {
// This is avoiding destroying \p SourceKit::Context because another
// thread may be active trying to use it to post notifications.
// FIXME: Use shared_ptr ownership to avoid such issues.
Ctx = new SourceKit::Context(getRuntimeLibPath(),
Ctx = new SourceKit::Context(getSwiftExecutablePath(),
getRuntimeLibPath(),
/*diagnosticDocumentationPath*/ "",
SourceKit::createSwiftLangSupport,
/*dispatchOnMain=*/false);
Expand Down