Skip to content

Commit 339d31f

Browse files
committed
[Macros] Add a frontend flag -Rmacro-loading to remark on macro resolution
Macro implementations can come from various locations associated with different search paths. Add a frontend flag `-Rmacro-loading` to emit a remark when each macro implementation module is resolved, providing the kind of macro (shared library, executable, shared library loaded via the plugin server) and appropriate paths. This allows one to tell from the build load which macros are used. Addresses rdar://110780311.
1 parent 1f05436 commit 339d31f

File tree

8 files changed

+55
-2
lines changed

8 files changed

+55
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,12 @@ REMARK(module_loaded,none,
11201120
"; source: '%2', loaded: '%3'",
11211121
(Identifier, unsigned, StringRef, StringRef))
11221122

1123+
REMARK(macro_loaded,none,
1124+
"loaded macro implementation module %0 from "
1125+
"%select{shared library '%2'|executable '%2'|"
1126+
"compiler plugin server '%2' with shared library '%3'}1",
1127+
(Identifier, unsigned, StringRef, StringRef))
1128+
11231129
REMARK(transitive_dependency_behavior,none,
11241130
"%1 has %select{a required|an optional|an ignored}2 "
11251131
"transitive dependency on '%0'",

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ namespace swift {
246246
/// Emit remarks about contextual inconsistencies in loaded modules.
247247
bool EnableModuleRecoveryRemarks = false;
248248

249+
/// Emit a remark after loading a macro implementation.
250+
bool EnableMacroLoadingRemarks = false;
251+
249252
/// Emit a remark when indexing a system module.
250253
bool EnableIndexingSystemModuleRemarks = false;
251254

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ def remark_module_recovery : Flag<["-"], "Rmodule-recovery">,
389389
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
390390
HelpText<"Emit remarks about contextual inconsistencies in loaded modules">;
391391

392+
def remark_macro_loading : Flag<["-"], "Rmacro-loading">,
393+
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
394+
HelpText<"Emit remarks about loaded macro implementations">;
395+
392396
def remark_indexing_system_module : Flag<["-"], "Rindexing-system-module">,
393397
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
394398
HelpText<"Emit a remark when indexing a system module">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
10161016

10171017
Opts.EnableModuleLoadingRemarks = Args.hasArg(OPT_remark_loading_module);
10181018
Opts.EnableModuleRecoveryRemarks = Args.hasArg(OPT_remark_module_recovery);
1019-
1019+
Opts.EnableMacroLoadingRemarks = Args.hasArg(OPT_remark_macro_loading);
10201020
Opts.EnableIndexingSystemModuleRemarks = Args.hasArg(OPT_remark_indexing_system_module);
10211021

10221022
Opts.EnableSkipExplicitInterfaceModuleBuildRemarks = Args.hasArg(OPT_remark_skip_explicit_interface_build);

lib/Sema/TypeCheckMacros.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,23 @@ CompilerPluginLoadRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
359359
if (!entry.executablePath.empty()) {
360360
if (LoadedExecutablePlugin *executablePlugin =
361361
loader.loadExecutablePlugin(entry.executablePath)) {
362+
if (ctx->LangOpts.EnableMacroLoadingRemarks) {
363+
unsigned tag = entry.libraryPath.empty() ? 1 : 2;
364+
ctx->Diags.diagnose(SourceLoc(), diag::macro_loaded, moduleName, tag,
365+
entry.executablePath, entry.libraryPath);
366+
}
367+
362368
return initializeExecutablePlugin(*ctx, executablePlugin,
363369
entry.libraryPath, moduleName);
364370
}
365371
} else if (!entry.libraryPath.empty()) {
366372
if (LoadedLibraryPlugin *libraryPlugin =
367373
loader.loadLibraryPlugin(entry.libraryPath)) {
374+
if (ctx->LangOpts.EnableMacroLoadingRemarks) {
375+
ctx->Diags.diagnose(SourceLoc(), diag::macro_loaded, moduleName, 0,
376+
entry.libraryPath, StringRef());
377+
}
378+
368379
return libraryPlugin;
369380
}
370381
}

test/Macros/macro_expand.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

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

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

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

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

37+
// CHECK-DIAGS: loaded macro implementation module 'MacroDefinition' from shared library
38+
3739
#if IMPORT_MACRO_LIBRARY
3840
import freestanding_macro_library
3941
import freestanding_macro_library_2

test/Macros/macro_plugin_basic.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@
2121

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

24+
// RUN: not %swift-target-frontend \
25+
// RUN: -typecheck \
26+
// RUN: -swift-version 5 \
27+
// RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \
28+
// RUN: -Rmacro-loading \
29+
// RUN: -module-name MyApp \
30+
// RUN: %t/test.swift \
31+
// RUN: 2>&1 | tee %t/macro-loading.txt
32+
33+
// RUN: %FileCheck -check-prefix=DIAGS %s < %t/macro-loading.txt
34+
35+
// DIAGS: loaded macro implementation module 'TestPlugin' from executable
36+
2437
// CHECK: ->(plugin:[[#PID:]]) {"getCapability":{"capability":{"protocolVersion":[[#PROTOCOL_VERSION:]]}}}
2538
// CHECK: <-(plugin:[[#PID]]) {"getCapabilityResult":{"capability":{"protocolVersion":1}}}
2639
// 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)"}}}

test/Macros/macro_plugin_server.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,26 @@
2525
// RUN: -typecheck -verify \
2626
// RUN: -swift-version 5 -enable-experimental-feature Macros \
2727
// RUN: -external-plugin-path %t/plugins#%swift-plugin-server \
28+
// RUN: -Rmacro-loading -verify-ignore-unknown \
2829
// RUN: -module-name MyApp \
2930
// RUN: %s \
3031
// RUN: 2>&1 | tee %t/macro-expansions.txt
3132

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

35+
// RUN: not %swift-target-frontend \
36+
// RUN: -typecheck \
37+
// RUN: -swift-version 5 \
38+
// RUN: -external-plugin-path %t/plugins#%swift-plugin-server \
39+
// RUN: -Rmacro-loading \
40+
// RUN: -module-name MyApp \
41+
// RUN: %s \
42+
// RUN: 2>&1 | tee %t/macro-loading.txt
43+
44+
// RUN: %FileCheck -check-prefix=CHECK-DIAGS %s < %t/macro-loading.txt
45+
46+
// CHECK-DIAGS: loaded macro implementation module 'MacroDefinition' from compiler plugin server
47+
3448
// CHECK: ->(plugin:[[#PID1:]]) {"getCapability":{"capability":{"protocolVersion":[[#PROTOCOL_VERSION:]]}}}
3549
// CHECK-NEXT: <-(plugin:[[#PID1]]) {"getCapabilityResult":{"capability":{"features":["load-plugin-library"],"protocolVersion":[[#PROTOCOL_VERSION]]}}}
3650
// CHECK-NEXT: ->(plugin:[[#PID1]]) {"loadPluginLibrary":{"libraryPath":"BUILD_DIR{{.*}}plugins/libMacroDefinition.dylib","moduleName":"MacroDefinition"}}

0 commit comments

Comments
 (0)