Skip to content

[SourceKit] Add mechanism to load plugins for request handling into SourceKit #78421

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 7 commits into from
Jan 15, 2025
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
23 changes: 23 additions & 0 deletions include/swift/Basic/LoadDynamicLibrary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_BASIC_LOADDYNAMICLIBRARY_H
#define SWIFT_BASIC_LOADDYNAMICLIBRARY_H

#include <string>

namespace swift {
void *loadLibrary(const char *path, std::string *err);
void *getAddressOfSymbol(void *handle, const char *symbol);
} // end namespace swift

#endif // SWIFT_BASIC_LOADDYNAMICLIBRARY_H
51 changes: 1 addition & 50 deletions lib/AST/PluginRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "swift/Basic/Assertions.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/LoadDynamicLibrary.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Program.h"
#include "swift/Basic/Sandbox.h"
Expand All @@ -25,16 +26,6 @@

#include <signal.h>

#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/Windows/WindowsSupport.h"
#include <windows.h>
#else
#include <dlfcn.h>
#endif

#if HAVE_UNISTD_H
#include <unistd.h>
#elif defined(_WIN32)
Expand All @@ -43,46 +34,6 @@

using namespace swift;

namespace {
void *loadLibrary(const char *path, std::string *err);
void *getAddressOfSymbol(void *handle, const char *symbol);

#if defined(_WIN32)
void *loadLibrary(const char *path, std::string *err) {
SmallVector<wchar_t, MAX_PATH> pathUnicode;
if (std::error_code ec = llvm::sys::windows::UTF8ToUTF16(path, pathUnicode)) {
SetLastError(ec.value());
llvm::MakeErrMsg(err, std::string(path) + ": Can't convert to UTF-16");
return nullptr;
}

HMODULE handle = LoadLibraryW(pathUnicode.data());
if (handle == NULL) {
llvm::MakeErrMsg(err, std::string(path) + ": Can't open");
return nullptr;
}
return (void *)handle;
}

void *getAddressOfSymbol(void *handle, const char *symbol) {
return (void *)uintptr_t(GetProcAddress((HMODULE)handle, symbol));
}

#else
void *loadLibrary(const char *path, std::string *err) {
void *handle = ::dlopen(path, RTLD_LAZY | RTLD_LOCAL);
if (!handle)
*err = ::dlerror();
return handle;
}

void *getAddressOfSymbol(void *handle, const char *symbol) {
return ::dlsym(handle, symbol);
}

#endif
} // namespace

PluginRegistry::PluginRegistry() {
dumpMessaging = ::getenv("SWIFT_DUMP_PLUGIN_MESSAGING") != nullptr;
}
Expand Down
1 change: 1 addition & 0 deletions lib/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ add_swift_host_library(swiftBasic STATIC
ParseableOutput.cpp
JSONSerialization.cpp
LangOptions.cpp
LoadDynamicLibrary.cpp
Located.cpp
Mangler.cpp
OutputFileMap.cpp
Expand Down
58 changes: 58 additions & 0 deletions lib/Basic/LoadDynamicLibrary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "swift/Basic/LoadDynamicLibrary.h"

#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/Windows/WindowsSupport.h"
#include "swift/Basic/LLVM.h"
#include <windows.h>
#else
#include <dlfcn.h>
#endif

#if defined(_WIN32)
void *swift::loadLibrary(const char *path, std::string *err) {
SmallVector<wchar_t, MAX_PATH> pathUnicode;
if (std::error_code ec = llvm::sys::windows::UTF8ToUTF16(path, pathUnicode)) {
SetLastError(ec.value());
llvm::MakeErrMsg(err, std::string(path) + ": Can't convert to UTF-16");
return nullptr;
}

HMODULE handle = LoadLibraryW(pathUnicode.data());
if (handle == NULL) {
llvm::MakeErrMsg(err, std::string(path) + ": Can't open");
return nullptr;
}
return (void *)handle;
}

void *swift::getAddressOfSymbol(void *handle, const char *symbol) {
return (void *)uintptr_t(GetProcAddress((HMODULE)handle, symbol));
}

#else
void *swift::loadLibrary(const char *path, std::string *err) {
void *handle = ::dlopen(path, RTLD_LAZY | RTLD_LOCAL);
if (!handle)
*err = ::dlerror();
return handle;
}

void *swift::getAddressOfSymbol(void *handle, const char *symbol) {
return ::dlsym(handle, symbol);
}
#endif
51 changes: 29 additions & 22 deletions tools/SourceKit/include/SourceKit/Core/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,30 @@ namespace llvm {
namespace SourceKit {
class LangSupport;
class NotificationCenter;

class GlobalConfig {
public:
struct Settings {
struct IDEInspectionOptions {

/// Max count of reusing ASTContext for cached IDE inspection.
unsigned MaxASTContextReuseCount = 100;

/// Interval second for checking dependencies in cached IDE inspection.
unsigned CheckDependencyInterval = 5;
} IDEInspectionOpts;
};

private:
Settings State;
mutable llvm::sys::Mutex Mtx;

public:
Settings update(std::optional<unsigned> IDEInspectionMaxASTContextReuseCount,
std::optional<unsigned> IDEInspectionCheckDependencyInterval);
Settings::IDEInspectionOptions getIDEInspectionOpts() const;
class PluginSupport;

class GlobalConfig {
public:
struct Settings {
struct IDEInspectionOptions {

/// Max count of reusing ASTContext for cached IDE inspection.
unsigned MaxASTContextReuseCount = 100;

/// Interval second for checking dependencies in cached IDE inspection.
unsigned CheckDependencyInterval = 5;
} IDEInspectionOpts;
};

private:
Settings State;
mutable llvm::sys::Mutex Mtx;

public:
Settings
update(std::optional<unsigned> IDEInspectionMaxASTContextReuseCount,
std::optional<unsigned> IDEInspectionCheckDependencyInterval);
Settings::IDEInspectionOptions getIDEInspectionOpts() const;
};

/// Keeps track of all requests that are currently in progress and coordinates
Expand Down Expand Up @@ -169,13 +171,16 @@ class Context {
std::shared_ptr<NotificationCenter> NotificationCtr;
std::shared_ptr<GlobalConfig> Config;
std::shared_ptr<RequestTracker> ReqTracker;
std::shared_ptr<PluginSupport> Plugins;
std::shared_ptr<SlowRequestSimulator> SlowRequestSim;

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

Expand All @@ -192,6 +197,8 @@ class Context {

std::shared_ptr<GlobalConfig> getGlobalConfiguration() { return Config; }

std::shared_ptr<PluginSupport> getPlugins() { return Plugins; }

std::shared_ptr<SlowRequestSimulator> getSlowRequestSimulator() {
return SlowRequestSim;
}
Expand Down
2 changes: 2 additions & 0 deletions tools/SourceKit/include/SourceKit/Core/LangSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,8 @@ class LangSupport {

virtual ~LangSupport() { }

virtual void *getOpaqueSwiftIDEInspectionInstance() { return nullptr; }

virtual void globalConfigurationUpdated(std::shared_ptr<GlobalConfig> Config) {};

virtual void dependencyUpdated() {}
Expand Down
3 changes: 3 additions & 0 deletions tools/SourceKit/lib/Core/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ SourceKit::Context::Context(
StringRef DiagnosticDocumentationPath,
llvm::function_ref<std::unique_ptr<LangSupport>(Context &)>
LangSupportFactoryFn,
llvm::function_ref<std::shared_ptr<PluginSupport>(Context &)>
PluginSupportFactoryFn,
bool shouldDispatchNotificationsOnMain)
: SwiftExecutablePath(SwiftExecutablePath), RuntimeLibPath(RuntimeLibPath),
DiagnosticDocumentationPath(DiagnosticDocumentationPath),
Expand All @@ -49,6 +51,7 @@ SourceKit::Context::Context(
SlowRequestSim(new SlowRequestSimulator(ReqTracker)) {
// Should be called last after everything is initialized.
SwiftLang = LangSupportFactoryFn(*this);
Plugins = PluginSupportFactoryFn(*this);
}

SourceKit::Context::~Context() {
Expand Down
4 changes: 4 additions & 0 deletions tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ class SwiftLangSupport : public LangSupport {
// LangSupport Interface
//==========================================================================//

void *getOpaqueSwiftIDEInspectionInstance() override {
return IDEInspectionInst.get();
}

void globalConfigurationUpdated(std::shared_ptr<GlobalConfig> Config) override;

void dependencyUpdated() override;
Expand Down
3 changes: 3 additions & 0 deletions tools/SourceKit/tools/sourcekitd/bin/InProc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ swift_is_installing_component(sourcekit-inproc SOURCEKIT_INSTALLING_INPROC)

set(sourcekitdInProc_args
sourcekitdInProc.cpp
CodeCompletionSwiftInterop.cpp
LLVM_LINK_COMPONENTS support coverage
)

if (SOURCEKIT_INSTALLING_INPROC)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
add_sourcekit_framework(sourcekitdInProc
${SOURCEKITD_SOURCE_DIR}/include/sourcekitd/sourcekitd.h
${SOURCEKITD_SOURCE_DIR}/include/sourcekitd/plugin.h
${CMAKE_CURRENT_SOURCE_DIR}/CodeCompletionSwiftInterop.h
${sourcekitdInProc_args}
MODULEMAP module.modulemap
INSTALL_IN_COMPONENT sourcekit-inproc
Expand Down
Loading