Skip to content

[Macros] Emit loaded module trace for plugins loaded from search paths #64137

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
Mar 6, 2023
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
2 changes: 1 addition & 1 deletion include/swift/AST/PluginRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class PluginRegistry {
LoadedPluginExecutables;

public:
llvm::Error loadLibraryPlugin(llvm::StringRef path);
llvm::Expected<void *> loadLibraryPlugin(llvm::StringRef path);
llvm::Expected<LoadedExecutablePlugin *>
loadExecutablePlugin(llvm::StringRef path);

Expand Down
5 changes: 3 additions & 2 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6241,9 +6241,10 @@ void ASTContext::loadCompilerPlugins() {
err.message());
continue;
}
if (auto error = getPluginRegistry()->loadLibraryPlugin(resolvedPath)) {
auto loaded = getPluginRegistry()->loadLibraryPlugin(resolvedPath);
if (!loaded) {
Diags.diagnose(SourceLoc(), diag::compiler_plugin_not_loaded, path,
llvm::toString(std::move(error)));
llvm::toString(loaded.takeError()));
}
}

Expand Down
9 changes: 5 additions & 4 deletions lib/AST/PluginRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ extern "C" void swift_ASTGen_destroyCompilerPluginCapability(void *value);

using namespace swift;

llvm::Error PluginRegistry::loadLibraryPlugin(StringRef path) {
if (LoadedPluginLibraries.find(path) != LoadedPluginLibraries.end()) {
llvm::Expected<void *> PluginRegistry::loadLibraryPlugin(StringRef path) {
auto found = LoadedPluginLibraries.find(path);
if (found != LoadedPluginLibraries.end()) {
// Already loaded.
return llvm::Error::success();
return found->second;
}
void *lib = nullptr;
#if defined(_WIN32)
Expand All @@ -62,7 +63,7 @@ llvm::Error PluginRegistry::loadLibraryPlugin(StringRef path) {
}
#endif
LoadedPluginLibraries.insert({path, lib});
return llvm::Error::success();
return lib;
}

llvm::Expected<LoadedExecutablePlugin *>
Expand Down
6 changes: 6 additions & 0 deletions lib/FrontendTool/LoadedModuleTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/AST/Module.h"
#include "swift/AST/PluginRegistry.h"
#include "swift/Basic/FileTypes.h"
#include "swift/Basic/JSONSerialization.h"
#include "swift/Frontend/FrontendOptions.h"
Expand Down Expand Up @@ -764,6 +765,11 @@ bool swift::emitLoadedModuleTraceIfNeeded(ModuleDecl *mainModule,
std::make_pair(loadedDecl->getModuleFilename(), loadedDecl));
}

// Add compiler plugin libraries as dependencies.
auto *pluginRegistry = ctxt.getPluginRegistry();
for (auto &pluginEntry : pluginRegistry->getLoadedLibraryPlugins())
depTracker->addDependency(pluginEntry.getKey(), /*IsSystem*/ false);

std::vector<SwiftModuleTraceInfo> swiftModules;
computeSwiftModuleTraceInfo(ctxt, abiDependencies, pathToModuleDecl,
*depTracker, opts.PrebuiltModuleCachePath,
Expand Down
16 changes: 8 additions & 8 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,26 +304,26 @@ MacroDefinition MacroDefinitionRequest::evaluate(
}

/// Load a plugin library based on a module name.
static void *loadPluginByName(StringRef searchPath, StringRef moduleName, llvm::vfs::FileSystem &fs) {
static void *loadPluginByName(StringRef searchPath,
StringRef moduleName,
llvm::vfs::FileSystem &fs,
PluginRegistry *registry) {
SmallString<128> fullPath(searchPath);
llvm::sys::path::append(fullPath, "lib" + moduleName + LTDL_SHLIB_EXT);
if (fs.getRealPath(fullPath, fullPath))
return nullptr;

#if defined(_WIN32)
return LoadLibraryA(fullPath.c_str());
#else
return dlopen(fullPath.c_str(), RTLD_LAZY);
#endif
auto loadResult = registry->loadLibraryPlugin(fullPath);
return loadResult ? *loadResult : nullptr;
}

void *CompilerPluginLoadRequest::evaluate(
Evaluator &evaluator, ASTContext *ctx, Identifier moduleName
) const {
auto fs = ctx->SourceMgr.getFileSystem();
auto &searchPathOpts = ctx->SearchPathOpts;
auto *registry = ctx->getPluginRegistry();
for (const auto &path : searchPathOpts.PluginSearchPaths) {
if (auto found = loadPluginByName(path, moduleName.str(), *fs))
if (auto found = loadPluginByName(path, moduleName.str(), *fs, registry))
return found;
}

Expand Down
8 changes: 6 additions & 2 deletions test/Macros/macro_expand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Diagnostics testing
// 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

// 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

// 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
// RUN: c-index-test -read-diagnostics %t/macro_expand.dia 2>&1 | %FileCheck -check-prefix CHECK-DIAGS %s

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

// Plugin search path and loaded module trace testing
// 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
// RUN: %FileCheck -check-prefix=CHECK-MODULE-TRACE %s < %t/loaded_module_trace.trace.json

// CHECK-MODULE-TRACE: {{libMacroDefinition.dylib|libMacroDefinition.so|MacroDefinition.dll}}

// FIXME: Swift parser is not enabled on Linux CI yet.
// REQUIRES: OS=macosx

Expand Down