Skip to content

Commit a496d3f

Browse files
committed
WIP
1 parent 30908b5 commit a496d3f

File tree

18 files changed

+125
-234
lines changed

18 files changed

+125
-234
lines changed

include/swift/AST/PluginRegistry.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class CompilerPlugin {
4848
bool shouldDumpMessaging() const { return dumpMessaging; }
4949

5050
public:
51-
NullTerminatedStringRef getPath() { return {path.c_str(), path.size()}; }
51+
NullTerminatedStringRef getPath() const { return {path.c_str(), path.size()}; }
5252

5353
void lock() { mtx.lock(); }
5454
void unlock() { mtx.unlock(); }
@@ -80,8 +80,6 @@ class CompilerPlugin {
8080

8181
/// Represents a in-process plugin server.
8282
class InProcessPlugins : public CompilerPlugin {
83-
std::string serverPath;
84-
8583
/// PluginServer
8684
llvm::sys::DynamicLibrary server;
8785

@@ -100,9 +98,9 @@ class InProcessPlugins : public CompilerPlugin {
10098
handleMessageFn(handleMessageFn) {}
10199

102100
public:
103-
static std::unique_ptr<InProcessPlugins> create(const char *serverPath);
101+
static llvm::Expected<InProcessPlugins *> create(const char *serverPath);
104102

105-
llvm::StringRef getServerPath() const { return serverPath; }
103+
llvm::StringRef getServerPath() const { return getPath(); }
106104

107105
/// Send a message to the plugin.
108106
llvm::Error sendMessage(llvm::StringRef message) override;

lib/AST/PluginRegistry.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,22 @@ CompilerPlugin::~CompilerPlugin() {
5050
this->cleanup();
5151
}
5252

53-
std::unique_ptr<InProcessPlugins>
53+
llvm::Expected<InProcessPlugins *>
5454
InProcessPlugins::create(const char *serverPath) {
55-
auto server = llvm::sys::DynamicLibrary::getLibrary(serverPath);
55+
std::string err;
56+
auto server = llvm::sys::DynamicLibrary::getLibrary(serverPath, &err);
57+
if (!server.isValid()) {
58+
return llvm::createStringError(llvm::inconvertibleErrorCode(), err);
59+
}
60+
5661
auto funcPtr =
5762
server.getAddressOfSymbol("swift_inproc_plugins_handle_message");
5863
if (!funcPtr) {
59-
return nullptr;
64+
return llvm::createStringError(llvm::inconvertibleErrorCode(),
65+
"entry point not found in '%s'", serverPath);
6066
}
61-
return std::unique_ptr<InProcessPlugins>(new InProcessPlugins(
62-
serverPath, server, reinterpret_cast<HandleMessageFunction>(funcPtr)));
67+
return new InProcessPlugins(serverPath, server,
68+
reinterpret_cast<HandleMessageFunction>(funcPtr));
6369
}
6470

6571
llvm::Error InProcessPlugins::sendMessage(llvm::StringRef message) {
@@ -110,16 +116,21 @@ llvm::Expected<CompilerPlugin *>
110116
PluginRegistry::getInProcessPlugins(llvm::StringRef serverPath) {
111117
std::lock_guard<std::mutex> lock(mtx);
112118
if (!inProcessPlugins) {
113-
inProcessPlugins = InProcessPlugins::create(serverPath.str().c_str());
114-
if (!inProcessPlugins) {
115-
return llvm::createStringError(
116-
llvm::inconvertibleErrorCode(),
117-
"failed to load in-process plugin server: " + serverPath);
119+
auto server = InProcessPlugins::create(serverPath.str().c_str());
120+
if (!server) {
121+
return llvm::handleErrors(
122+
server.takeError(), [&](const llvm::ErrorInfoBase &err) {
123+
return llvm::createStringError(
124+
err.convertToErrorCode(),
125+
"failed to load in-process plugin server: " + serverPath +
126+
"; " + err.message());
127+
});
118128
}
129+
inProcessPlugins = std::unique_ptr<InProcessPlugins>(server.get());
119130
} else if (inProcessPlugins->getServerPath() != serverPath) {
120131
return llvm::createStringError(
121132
llvm::inconvertibleErrorCode(),
122-
"loading multiple in-process servers are not supported");
133+
"loading multiple in-process servers are not supported: '%s' and '%s'", inProcessPlugins->getServerPath().str().c_str(), serverPath.str().c_str());
123134
}
124135
inProcessPlugins->setDumpMessaging(dumpMessaging);
125136

lib/ASTGen/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ add_pure_swift_host_library(swiftASTGen STATIC
3636
Sources/ASTGen/Regex.swift
3737
Sources/ASTGen/SourceFile.swift
3838
Sources/ASTGen/SourceManager.swift
39-
Sources/ASTGen/SourceManager+MacroExpansionContext.swift
4039
Sources/ASTGen/Stmts.swift
4140
Sources/ASTGen/TypeAttrs.swift
4241
Sources/ASTGen/Types.swift

lib/ASTGen/Sources/ASTGen/PluginHost.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ class PluginDiagnosticsEngine {
319319
messageSuffix: String? = nil
320320
) {
321321
for diagnostic in diagnostics {
322-
self.emit(diagnostic)
322+
self.emit(diagnostic, messageSuffix: messageSuffix)
323323
}
324324
}
325325

@@ -378,6 +378,26 @@ class PluginDiagnosticsEngine {
378378
}
379379
}
380380

381+
extension String {
382+
/// Retrieve the base name of a string that represents a path, removing the
383+
/// directory.
384+
var basename: String {
385+
guard
386+
let lastSlash = lastIndex(where: {
387+
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) || os(Android) || os(Linux)
388+
["/"].contains($0)
389+
#else
390+
["/", "\\"].contains($0)
391+
#endif
392+
})
393+
else {
394+
return self
395+
}
396+
397+
return String(self[index(after: lastSlash)...])
398+
}
399+
}
400+
381401
extension PluginMessage.Syntax {
382402
init?(syntax: Syntax, in sourceFilePtr: UnsafePointer<ExportedSourceFile>) {
383403
let kind: PluginMessage.Syntax.Kind

lib/ASTGen/Sources/ASTGen/SourceManager+MacroExpansionContext.swift

Lines changed: 0 additions & 192 deletions
This file was deleted.

lib/CompilerSwiftSyntax/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function(includeSwiftSyntax)
1717
# can be used from tools that has its own swift-syntax libraries as SwiftPM dependencies.
1818
set(SWIFT_MODULE_ABI_NAME_PREFIX "_Compiler")
1919
set(SWIFTSYNTAX_TARGET_NAMESPACE "_Compiler")
20-
set(SWIFT_EMIT_MODULE OFF)
20+
set(SWIFTSYNTAX_EMIT_MODULE OFF)
2121

2222
file(TO_CMAKE_PATH "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}" swift_syntax_path)
2323
FetchContent_Declare(CompilerSwiftSyntax SOURCE_DIR "${swift_syntax_path}")

lib/Driver/DarwinToolChains.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,16 @@ toolchains::Darwin::addPluginArguments(const ArgList &Args,
136136
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
137137
programPath, /*shared=*/true, pluginPath);
138138

139-
auto defaultPluginPath = pluginPath;
140-
llvm::sys::path::append(defaultPluginPath, "host", "plugins");
139+
// In-process plugin server path.
140+
auto inProcPluginServerPath = pluginPath;
141+
llvm::sys::path::append(inProcPluginServerPath, "host",
142+
"libSwiftInProcPluginServer.dylib");
143+
Arguments.push_back("-in-process-plugin-server-path");
144+
Arguments.push_back(Args.MakeArgString(inProcPluginServerPath));
141145

142146
// Default plugin path.
147+
auto defaultPluginPath = pluginPath;
148+
llvm::sys::path::append(defaultPluginPath, "host", "plugins");
143149
Arguments.push_back("-plugin-path");
144150
Arguments.push_back(Args.MakeArgString(defaultPluginPath));
145151

lib/Driver/UnixToolChains.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,16 @@ toolchains::GenericUnix::addPluginArguments(const ArgList &Args,
5757
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
5858
programPath, /*shared=*/true, pluginPath);
5959

60-
auto defaultPluginPath = pluginPath;
61-
llvm::sys::path::append(defaultPluginPath, "host", "plugins");
60+
// In-process plugin server path.
61+
auto inProcPluginServerPath = pluginPath;
62+
llvm::sys::path::append(inProcPluginServerPath, "host",
63+
"libSwiftInProcPluginServer.so");
64+
Arguments.push_back("-in-process-plugin-server-path");
65+
Arguments.push_back(Args.MakeArgString(inProcPluginServerPath));
6266

6367
// Default plugin path.
68+
auto defaultPluginPath = pluginPath;
69+
llvm::sys::path::append(defaultPluginPath, "host", "plugins");
6470
Arguments.push_back("-plugin-path");
6571
Arguments.push_back(Args.MakeArgString(defaultPluginPath));
6672

lib/Driver/WindowsToolChains.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ toolchains::Windows::addPluginArguments(const ArgList &Args,
4949
SmallString<261> LibraryPath = StringRef(getDriver().getSwiftProgramPath());
5050
llvm::sys::path::remove_filename(LibraryPath); // Remove `swift`
5151

52+
// In-process plugin server path.
53+
SmallString<261> InProcPluginServerPath = LibraryPath;
54+
llvm::sys::path::append(InProcPluginServerPath,
55+
"SwiftInProcPluginServer.dll");
56+
Arguments.push_back("-in-process-plugin-server-path");
57+
Arguments.push_back(Args.MakeArgString(InProcPluginServerPath));
58+
5259
// Default plugin path.
5360
Arguments.push_back("-plugin-path");
5461
Arguments.push_back(Args.MakeArgString(LibraryPath));

0 commit comments

Comments
 (0)