Skip to content

Commit 19335a1

Browse files
authored
Merge pull request #64137 from rxwei/macro-loaded-module-trace-take-2
2 parents a8e3283 + c6139f2 commit 19335a1

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

include/swift/AST/PluginRegistry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class PluginRegistry {
7878
LoadedPluginExecutables;
7979

8080
public:
81-
llvm::Error loadLibraryPlugin(llvm::StringRef path);
81+
llvm::Expected<void *> loadLibraryPlugin(llvm::StringRef path);
8282
llvm::Expected<LoadedExecutablePlugin *>
8383
loadExecutablePlugin(llvm::StringRef path);
8484

lib/AST/ASTContext.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6242,9 +6242,10 @@ void ASTContext::loadCompilerPlugins() {
62426242
err.message());
62436243
continue;
62446244
}
6245-
if (auto error = getPluginRegistry()->loadLibraryPlugin(resolvedPath)) {
6245+
auto loaded = getPluginRegistry()->loadLibraryPlugin(resolvedPath);
6246+
if (!loaded) {
62466247
Diags.diagnose(SourceLoc(), diag::compiler_plugin_not_loaded, path,
6247-
llvm::toString(std::move(error)));
6248+
llvm::toString(loaded.takeError()));
62486249
}
62496250
}
62506251

lib/AST/PluginRegistry.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ extern "C" void swift_ASTGen_destroyCompilerPluginCapability(void *value);
4343

4444
using namespace swift;
4545

46-
llvm::Error PluginRegistry::loadLibraryPlugin(StringRef path) {
47-
if (LoadedPluginLibraries.find(path) != LoadedPluginLibraries.end()) {
46+
llvm::Expected<void *> PluginRegistry::loadLibraryPlugin(StringRef path) {
47+
auto found = LoadedPluginLibraries.find(path);
48+
if (found != LoadedPluginLibraries.end()) {
4849
// Already loaded.
49-
return llvm::Error::success();
50+
return found->second;
5051
}
5152
void *lib = nullptr;
5253
#if defined(_WIN32)
@@ -62,7 +63,7 @@ llvm::Error PluginRegistry::loadLibraryPlugin(StringRef path) {
6263
}
6364
#endif
6465
LoadedPluginLibraries.insert({path, lib});
65-
return llvm::Error::success();
66+
return lib;
6667
}
6768

6869
llvm::Expected<LoadedExecutablePlugin *>

lib/FrontendTool/LoadedModuleTrace.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "swift/AST/DiagnosticEngine.h"
1616
#include "swift/AST/DiagnosticsFrontend.h"
1717
#include "swift/AST/Module.h"
18+
#include "swift/AST/PluginRegistry.h"
1819
#include "swift/Basic/FileTypes.h"
1920
#include "swift/Basic/JSONSerialization.h"
2021
#include "swift/Frontend/FrontendOptions.h"
@@ -764,6 +765,11 @@ bool swift::emitLoadedModuleTraceIfNeeded(ModuleDecl *mainModule,
764765
std::make_pair(loadedDecl->getModuleFilename(), loadedDecl));
765766
}
766767

768+
// Add compiler plugin libraries as dependencies.
769+
auto *pluginRegistry = ctxt.getPluginRegistry();
770+
for (auto &pluginEntry : pluginRegistry->getLoadedLibraryPlugins())
771+
depTracker->addDependency(pluginEntry.getKey(), /*IsSystem*/ false);
772+
767773
std::vector<SwiftModuleTraceInfo> swiftModules;
768774
computeSwiftModuleTraceInfo(ctxt, abiDependencies, pathToModuleDecl,
769775
*depTracker, opts.PrebuiltModuleCachePath,

lib/Sema/TypeCheckMacros.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,26 +304,26 @@ MacroDefinition MacroDefinitionRequest::evaluate(
304304
}
305305

306306
/// Load a plugin library based on a module name.
307-
static void *loadPluginByName(StringRef searchPath, StringRef moduleName, llvm::vfs::FileSystem &fs) {
307+
static void *loadPluginByName(StringRef searchPath,
308+
StringRef moduleName,
309+
llvm::vfs::FileSystem &fs,
310+
PluginRegistry *registry) {
308311
SmallString<128> fullPath(searchPath);
309312
llvm::sys::path::append(fullPath, "lib" + moduleName + LTDL_SHLIB_EXT);
310313
if (fs.getRealPath(fullPath, fullPath))
311314
return nullptr;
312-
313-
#if defined(_WIN32)
314-
return LoadLibraryA(fullPath.c_str());
315-
#else
316-
return dlopen(fullPath.c_str(), RTLD_LAZY);
317-
#endif
315+
auto loadResult = registry->loadLibraryPlugin(fullPath);
316+
return loadResult ? *loadResult : nullptr;
318317
}
319318

320319
void *CompilerPluginLoadRequest::evaluate(
321320
Evaluator &evaluator, ASTContext *ctx, Identifier moduleName
322321
) const {
323322
auto fs = ctx->SourceMgr.getFileSystem();
324323
auto &searchPathOpts = ctx->SearchPathOpts;
324+
auto *registry = ctx->getPluginRegistry();
325325
for (const auto &path : searchPathOpts.PluginSearchPaths) {
326-
if (auto found = loadPluginByName(path, moduleName.str(), *fs))
326+
if (auto found = loadPluginByName(path, moduleName.str(), *fs, registry))
327327
return found;
328328
}
329329

test/Macros/macro_expand.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// Diagnostics testing
66
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-experimental-feature FreestandingMacros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -module-name MacroUser -DTEST_DIAGNOSTICS
77

8-
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-experimental-feature FreestandingMacros -plugin-path %t -I %swift-host-lib-dir -module-name MacroUser -DTEST_DIAGNOSTICS
9-
108
// RUN: not %target-swift-frontend -swift-version 5 -typecheck -enable-experimental-feature FreestandingMacros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -module-name MacroUser -DTEST_DIAGNOSTICS -serialize-diagnostics-path %t/macro_expand.dia %s -emit-macro-expansion-files no-diagnostics > %t/macro-printing.txt
119
// RUN: c-index-test -read-diagnostics %t/macro_expand.dia 2>&1 | %FileCheck -check-prefix CHECK-DIAGS %s
1210

@@ -23,6 +21,12 @@
2321
// RUN: %target-run %t/main | %FileCheck %s
2422
// REQUIRES: executable_test
2523

24+
// Plugin search path and loaded module trace testing
25+
// RUN: %target-swift-frontend -swift-version 5 -emit-sil -enable-experimental-feature FreestandingMacros -plugin-path %t -I %swift-host-lib-dir %s -module-name MacroUser -emit-loaded-module-trace -o %t/loaded_module_trace
26+
// RUN: %FileCheck -check-prefix=CHECK-MODULE-TRACE %s < %t/loaded_module_trace.trace.json
27+
28+
// CHECK-MODULE-TRACE: {{libMacroDefinition.dylib|libMacroDefinition.so|MacroDefinition.dll}}
29+
2630
// FIXME: Swift parser is not enabled on Linux CI yet.
2731
// REQUIRES: OS=macosx
2832

0 commit comments

Comments
 (0)