Skip to content

[Macros] Add a frontend flag -Rmacro-loading to remark on macro resolution #68154

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
Aug 26, 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
6 changes: 6 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,12 @@ REMARK(module_loaded,none,
"; source: '%2', loaded: '%3'",
(Identifier, unsigned, StringRef, StringRef))

REMARK(macro_loaded,none,
"loaded macro implementation module %0 from "
"%select{shared library '%2'|executable '%2'|"
"compiler plugin server '%2' with shared library '%3'}1",
(Identifier, unsigned, StringRef, StringRef))

REMARK(transitive_dependency_behavior,none,
"%1 has %select{a required|an optional|an ignored}2 "
"transitive dependency on '%0'",
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ namespace swift {
/// Emit remarks about contextual inconsistencies in loaded modules.
bool EnableModuleRecoveryRemarks = false;

/// Emit a remark after loading a macro implementation.
bool EnableMacroLoadingRemarks = false;

/// Emit a remark when indexing a system module.
bool EnableIndexingSystemModuleRemarks = false;

Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,10 @@ def remark_module_recovery : Flag<["-"], "Rmodule-recovery">,
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
HelpText<"Emit remarks about contextual inconsistencies in loaded modules">;

def remark_macro_loading : Flag<["-"], "Rmacro-loading">,
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
HelpText<"Emit remarks about loaded macro implementations">;

def remark_indexing_system_module : Flag<["-"], "Rindexing-system-module">,
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
HelpText<"Emit a remark when indexing a system module">;
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,

Opts.EnableModuleLoadingRemarks = Args.hasArg(OPT_remark_loading_module);
Opts.EnableModuleRecoveryRemarks = Args.hasArg(OPT_remark_module_recovery);

Opts.EnableMacroLoadingRemarks = Args.hasArg(OPT_remark_macro_loading);
Opts.EnableIndexingSystemModuleRemarks = Args.hasArg(OPT_remark_indexing_system_module);

Opts.EnableSkipExplicitInterfaceModuleBuildRemarks = Args.hasArg(OPT_remark_skip_explicit_interface_build);
Expand Down
11 changes: 11 additions & 0 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,23 @@ CompilerPluginLoadRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
if (!entry.executablePath.empty()) {
if (LoadedExecutablePlugin *executablePlugin =
loader.loadExecutablePlugin(entry.executablePath)) {
if (ctx->LangOpts.EnableMacroLoadingRemarks) {
unsigned tag = entry.libraryPath.empty() ? 1 : 2;
ctx->Diags.diagnose(SourceLoc(), diag::macro_loaded, moduleName, tag,
entry.executablePath, entry.libraryPath);
}

return initializeExecutablePlugin(*ctx, executablePlugin,
entry.libraryPath, moduleName);
}
} else if (!entry.libraryPath.empty()) {
if (LoadedLibraryPlugin *libraryPlugin =
loader.loadLibraryPlugin(entry.libraryPath)) {
if (ctx->LangOpts.EnableMacroLoadingRemarks) {
ctx->Diags.diagnose(SourceLoc(), diag::macro_loaded, moduleName, 0,
entry.libraryPath, StringRef());
}

return libraryPlugin;
}
}
Expand Down
4 changes: 3 additions & 1 deletion test/Macros/macro_expand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -I %t -DIMPORT_MACRO_LIBRARY

// RUN: not %target-swift-frontend -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -serialize-diagnostics-path %t/macro_expand.dia %s -emit-macro-expansion-files no-diagnostics > %t/macro-printing.txt
// RUN: not %target-swift-frontend -swift-version 5 -typecheck -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -serialize-diagnostics-path %t/macro_expand.dia %s -emit-macro-expansion-files no-diagnostics -Rmacro-loading > %t/macro-printing.txt
// RUN: c-index-test -read-diagnostics %t/macro_expand.dia 2>&1 | %FileCheck -check-prefix CHECK-DIAGS %s

// RUN: %FileCheck %s --check-prefix CHECK-MACRO-PRINTED < %t/macro-printing.txt
Expand All @@ -34,6 +34,8 @@

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

// CHECK-DIAGS: loaded macro implementation module 'MacroDefinition' from shared library

#if IMPORT_MACRO_LIBRARY
import freestanding_macro_library
import freestanding_macro_library_2
Expand Down
13 changes: 13 additions & 0 deletions test/Macros/macro_plugin_basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@

// RUN: %FileCheck -strict-whitespace %s < %t/macro-expansions.txt

// RUN: not %swift-target-frontend \
// RUN: -typecheck \
// RUN: -swift-version 5 \
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
// RUN: -Rmacro-loading \
// RUN: -module-name MyApp \
// RUN: %t/test.swift \
// RUN: 2>&1 | tee %t/macro-loading.txt

// RUN: %FileCheck -check-prefix=DIAGS %s < %t/macro-loading.txt

// DIAGS: loaded macro implementation module 'TestPlugin' from executable

// CHECK: ->(plugin:[[#PID:]]) {"getCapability":{"capability":{"protocolVersion":[[#PROTOCOL_VERSION:]]}}}
// CHECK: <-(plugin:[[#PID]]) {"getCapabilityResult":{"capability":{"protocolVersion":1}}}
// CHECK: ->(plugin:[[#PID]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"testString","typeName":"TestStringMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":5,"offset":301},"source":"#testString(123)"}}}
Expand Down
14 changes: 14 additions & 0 deletions test/Macros/macro_plugin_server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,26 @@
// RUN: -typecheck -verify \
// RUN: -swift-version 5 -enable-experimental-feature Macros \
// RUN: -external-plugin-path %t/plugins#%swift-plugin-server \
// RUN: -Rmacro-loading -verify-ignore-unknown \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I just missing where this one is checked? Seems like only macro-loading.txt below is.

// RUN: -module-name MyApp \
// RUN: %s \
// RUN: 2>&1 | tee %t/macro-expansions.txt

// RUN: %FileCheck -strict-whitespace %s < %t/macro-expansions.txt

// RUN: not %swift-target-frontend \
// RUN: -typecheck \
// RUN: -swift-version 5 \
// RUN: -external-plugin-path %t/plugins#%swift-plugin-server \
// RUN: -Rmacro-loading \
// RUN: -module-name MyApp \
// RUN: %s \
// RUN: 2>&1 | tee %t/macro-loading.txt

// RUN: %FileCheck -check-prefix=CHECK-DIAGS %s < %t/macro-loading.txt

// CHECK-DIAGS: loaded macro implementation module 'MacroDefinition' from compiler plugin server

// CHECK: ->(plugin:[[#PID1:]]) {"getCapability":{"capability":{"protocolVersion":[[#PROTOCOL_VERSION:]]}}}
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"getCapabilityResult":{"capability":{"features":["load-plugin-library"],"protocolVersion":[[#PROTOCOL_VERSION]]}}}
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"loadPluginLibrary":{"libraryPath":"BUILD_DIR{{.*}}plugins/libMacroDefinition.dylib","moduleName":"MacroDefinition"}}
Expand Down