Skip to content

Commit c5a737f

Browse files
committed
[AST] PluginRegistry returns Error/Expected instead of errorMsg out
1 parent 396c227 commit c5a737f

File tree

4 files changed

+37
-35
lines changed

4 files changed

+37
-35
lines changed

include/swift/AST/PluginRegistry.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/ADT/StringMap.h"
1515
#include "llvm/ADT/StringRef.h"
1616
#include "llvm/Support/Chrono.h"
17+
#include "llvm/Support/Error.h"
1718
#include "llvm/Support/Program.h"
1819

1920
#include <mutex>
@@ -52,10 +53,10 @@ class LoadedExecutablePlugin {
5253
void unlock() { mtx.unlock(); }
5354

5455
/// Send a message to the plugin.
55-
bool sendMessage(llvm::StringRef message) const;
56+
llvm::Error sendMessage(llvm::StringRef message) const;
5657

5758
/// Wait for a message from plugin and returns it.
58-
std::string waitForNextMessage() const;
59+
llvm::Expected<std::string> waitForNextMessage() const;
5960

6061
bool isInitialized() const { return bool(cleanup); }
6162
void setCleanup(std::function<void(void)> cleanup) {
@@ -77,9 +78,9 @@ class PluginRegistry {
7778
LoadedPluginExecutables;
7879

7980
public:
80-
bool loadLibraryPlugin(llvm::StringRef path, const char *&errorMsg);
81-
LoadedExecutablePlugin *loadExecutablePlugin(llvm::StringRef path,
82-
const char *&errorMsg);
81+
llvm::Error loadLibraryPlugin(llvm::StringRef path);
82+
llvm::Expected<LoadedExecutablePlugin *>
83+
loadExecutablePlugin(llvm::StringRef path);
8384

8485
const llvm::StringMap<void *> &getLoadedLibraryPlugins() const {
8586
return LoadedPluginLibraries;

lib/AST/ASTContext.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6227,7 +6227,6 @@ PluginRegistry *ASTContext::getPluginRegistry() const {
62276227
}
62286228

62296229
void ASTContext::loadCompilerPlugins() {
6230-
const char *errorMsg = nullptr;
62316230
auto fs = this->SourceMgr.getFileSystem();
62326231
for (auto &path : SearchPathOpts.getCompilerPluginLibraryPaths()) {
62336232
SmallString<128> resolvedPath;
@@ -6236,9 +6235,9 @@ void ASTContext::loadCompilerPlugins() {
62366235
err.message());
62376236
continue;
62386237
}
6239-
if (getPluginRegistry()->loadLibraryPlugin(resolvedPath, errorMsg)) {
6238+
if (auto error = getPluginRegistry()->loadLibraryPlugin(resolvedPath)) {
62406239
Diags.diagnose(SourceLoc(), diag::compiler_plugin_not_loaded, path,
6241-
errorMsg);
6240+
llvm::toString(std::move(error)));
62426241
}
62436242
}
62446243

@@ -6339,12 +6338,11 @@ ASTContext::lookupExecutablePluginByModuleName(Identifier moduleName) {
63396338
}
63406339

63416340
// Load the plugin.
6342-
const char *errorMsg;
6343-
auto plugin = getPluginRegistry()->loadExecutablePlugin(resolvedPath, errorMsg);
6341+
auto plugin = getPluginRegistry()->loadExecutablePlugin(resolvedPath);
63446342
if (!plugin) {
63456343
Diags.diagnose(SourceLoc(), diag::compiler_plugin_not_loaded, path,
6346-
errorMsg);
6344+
llvm::toString(plugin.takeError()));
63476345
}
63486346

6349-
return plugin;
6347+
return plugin.get();
63506348
}

lib/AST/CASTBridging.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,14 +651,19 @@ void Plugin_unlock(PluginHandle handle) {
651651
bool Plugin_sendMessage(PluginHandle handle, const BridgedData data) {
652652
auto *plugin = static_cast<LoadedExecutablePlugin *>(handle);
653653
StringRef message(data.baseAddress, data.size);
654-
return plugin->sendMessage(message);
654+
return bool(plugin->sendMessage(message));
655655
}
656656

657657
bool Plugin_waitForNextMessage(PluginHandle handle, BridgedData *out) {
658658
auto *plugin = static_cast<LoadedExecutablePlugin *>(handle);
659659
auto result = plugin->waitForNextMessage();
660-
auto outPtr = malloc(result.size());
661-
memcpy(outPtr, result.data(), result.size());
662-
*out = BridgedData{(const char *)outPtr, result.size()};
660+
if (!result) {
661+
return true;
662+
}
663+
auto &message = result.get();
664+
auto size = message.size();
665+
auto outPtr = malloc(size);
666+
memcpy(outPtr, message.data(), size);
667+
*out = BridgedData{(const char *)outPtr, size};
663668
return false;
664669
}

lib/AST/PluginRegistry.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,34 @@ extern "C" void swift_ASTGen_destroyCompilerPluginCapability(void *value);
4141

4242
using namespace swift;
4343

44-
bool PluginRegistry::loadLibraryPlugin(StringRef path, const char *&errorMsg) {
44+
llvm::Error PluginRegistry::loadLibraryPlugin(StringRef path) {
4545
if (LoadedPluginLibraries.find(path) != LoadedPluginLibraries.end()) {
4646
// Already loaded.
47-
return false;
47+
return llvm::Error::success();
4848
}
49-
errorMsg = nullptr;
5049
void *lib = nullptr;
5150
#if defined(_WIN32)
5251
lib = LoadLibraryA(path.str().c_str());
5352
if (!lib) {
54-
errorMsg = "faild";
53+
return llvm::createStringError(std::errc::not_supported, "failed");
5554
return true;
5655
}
5756
#else
5857
lib = dlopen(path.str().c_str(), RTLD_LAZY | RTLD_LOCAL);
5958
if (!lib) {
60-
errorMsg = "Unsupported platform";
61-
return true;
59+
return llvm::createStringError(std::errc::not_supported,
60+
"unsupported platform");
6261
}
6362
#endif
6463
LoadedPluginLibraries.insert({path, lib});
65-
return false;
64+
return llvm::Error::success();
6665
}
6766

68-
LoadedExecutablePlugin *
69-
PluginRegistry::loadExecutablePlugin(StringRef path, const char *&errorMsg) {
67+
llvm::Expected<LoadedExecutablePlugin *>
68+
PluginRegistry::loadExecutablePlugin(StringRef path) {
7069
llvm::sys::fs::file_status stat;
7170
if (auto err = llvm::sys::fs::status(path, stat)) {
72-
errorMsg = err.message().c_str();
71+
return llvm::errorCodeToError(err);
7372
}
7473

7574
// See if the plugin is already loaded.
@@ -84,13 +83,13 @@ PluginRegistry::loadExecutablePlugin(StringRef path, const char *&errorMsg) {
8483
}
8584

8685
if (!llvm::sys::fs::exists(stat)) {
87-
errorMsg = "not found";
88-
return nullptr;
86+
return llvm::createStringError(std::errc::no_such_file_or_directory,
87+
"not found");
8988
}
9089

9190
if (!llvm::sys::fs::can_execute(path)) {
92-
errorMsg = "not executable";
93-
return nullptr;
91+
return llvm::createStringError(std::errc::permission_denied,
92+
"not executable");
9493
}
9594

9695
// Create command line arguments.
@@ -103,8 +102,7 @@ PluginRegistry::loadExecutablePlugin(StringRef path, const char *&errorMsg) {
103102
// Launch.
104103
auto childInfo = ExecuteWithPipe(command[0], command);
105104
if (!childInfo) {
106-
errorMsg = "Failed to execute";
107-
return nullptr;
105+
return llvm::errorCodeToError(childInfo.getError());
108106
}
109107

110108
plugin = std::unique_ptr<LoadedExecutablePlugin>(new LoadedExecutablePlugin(
@@ -163,7 +161,7 @@ ssize_t LoadedExecutablePlugin::write(const void *buf, size_t nbyte) const {
163161
return nbyte - bytesToWrite;
164162
}
165163

166-
bool LoadedExecutablePlugin::sendMessage(llvm::StringRef message) const {
164+
llvm::Error LoadedExecutablePlugin::sendMessage(llvm::StringRef message) const {
167165
ssize_t writtenSize = 0;
168166

169167
const char *data = message.data();
@@ -180,10 +178,10 @@ bool LoadedExecutablePlugin::sendMessage(llvm::StringRef message) const {
180178
writtenSize = write(data, size);
181179
assert(writtenSize == ssize_t(size) && "failed to write plugin message data");
182180

183-
return false;
181+
return llvm::Error::success();
184182
}
185183

186-
std::string LoadedExecutablePlugin::waitForNextMessage() const {
184+
llvm::Expected<std::string> LoadedExecutablePlugin::waitForNextMessage() const {
187185
ssize_t readSize = 0;
188186

189187
// Read header (message size).

0 commit comments

Comments
 (0)