Skip to content

Commit 444fcfa

Browse files
committed
[Macros] Tweak macro resolution error messages
1 parent 28aad9d commit 444fcfa

File tree

8 files changed

+58
-51
lines changed

8 files changed

+58
-51
lines changed

lib/AST/PluginLoader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ PluginLoader::loadLibraryPlugin(StringRef path) {
178178
plugin.takeError(), [&](const llvm::ErrorInfoBase &err) {
179179
return llvm::createStringError(
180180
err.convertToErrorCode(),
181-
"compiler plugin not loaded: %s; loader error: %s",
181+
"compiler plugin '%s' could not be loaded; %s",
182182
resolvedPath.data(), err.message().data());
183183
});
184184
}
@@ -206,7 +206,7 @@ PluginLoader::loadExecutablePlugin(StringRef path) {
206206
plugin.takeError(), [&](const llvm::ErrorInfoBase &err) {
207207
return llvm::createStringError(
208208
err.convertToErrorCode(),
209-
"compiler plugin not loaded: %s; loader error: %s",
209+
"compiler plugin '%s' could not be loaded: %s",
210210
resolvedPath.data(), err.message().data());
211211
});
212212
}

lib/ASTGen/Sources/ASTGen/PluginHost.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,12 @@ func swift_ASTGen_pluginServerLoadLibraryPlugin(
5858
opaqueHandle: UnsafeMutableRawPointer,
5959
libraryPath: UnsafePointer<CChar>,
6060
moduleName: UnsafePointer<CChar>,
61-
cxxDiagnosticEngine: UnsafeMutableRawPointer?
61+
errorOut: UnsafeMutablePointer<BridgedString>?
6262
) -> Bool {
6363
let plugin = CompilerPlugin(opaqueHandle: opaqueHandle)
64-
let diagEngine = PluginDiagnosticsEngine(cxxDiagnosticEngine: cxxDiagnosticEngine)
6564

6665
if plugin.capability?.features.contains(.loadPluginLibrary) != true {
67-
// This happens only if invalid plugin server was passed to `-external-plugin-path`.
68-
diagEngine?.diagnose(
69-
message: "compiler plugin not loaded: '\(libraryPath); invalid plugin server",
70-
severity: .warning)
66+
errorOut?.pointee = allocateBridgedString("compiler plugin not loaded: '\(libraryPath); invalid plugin server")
7167
return false
7268
}
7369
assert(plugin.capability?.features.contains(.loadPluginLibrary) == true)
@@ -81,12 +77,15 @@ func swift_ASTGen_pluginServerLoadLibraryPlugin(
8177
guard case .loadPluginLibraryResult(let loaded, let diagnostics) = result else {
8278
throw PluginError.invalidReponseKind
8379
}
84-
diagEngine?.emit(diagnostics);
85-
return loaded
80+
if loaded {
81+
assert(diagnostics.isEmpty)
82+
return true
83+
}
84+
var errorMsgs = diagnostics.map({$0.message}).joined(separator: ", ");
85+
errorOut?.pointee = allocateBridgedString(errorMsgs);
86+
return false
8687
} catch {
87-
diagEngine?.diagnose(
88-
message: "compiler plugin not loaded: '\(libraryPath); \(error)",
89-
severity: .warning)
88+
errorOut?.pointee = allocateBridgedString("\(error)")
9089
return false
9190
}
9291
}

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2075,7 +2075,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20752075
};
20762076
auto externalDef =
20772077
evaluateOrDefault(Ctx.evaluator, request,
2078-
ExternalMacroDefinition::error("failed request"));
2078+
ExternalMacroDefinition::error("unknown error"));
20792079
if (externalDef.isError()) {
20802080
MD->diagnose(diag::external_macro_not_found, external.moduleName.str(),
20812081
external.macroTypeName.str(), MD->getName(),

lib/Sema/TypeCheckMacros.cpp

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ extern "C" bool swift_ASTGen_initializePlugin(void *handle, void *diagEngine);
8181
extern "C" void swift_ASTGen_deinitializePlugin(void *handle);
8282
extern "C" bool swift_ASTGen_pluginServerLoadLibraryPlugin(
8383
void *handle, const char *libraryPath, const char *moduleName,
84-
void *diagEngine);
84+
BridgedString *errorOut);
8585

8686
static inline StringRef toStringRef(BridgedString bridged) {
8787
return {reinterpret_cast<const char *>(bridged.data), size_t(bridged.length)};
@@ -292,9 +292,8 @@ initializeExecutablePlugin(ASTContext &ctx,
292292
#if SWIFT_BUILD_SWIFT_SYNTAX
293293
if (!swift_ASTGen_initializePlugin(executablePlugin, &ctx.Diags)) {
294294
return llvm::createStringError(
295-
llvm::inconvertibleErrorCode(),
296-
"failed to initialize executable plugin '" +
297-
StringRef(executablePlugin->getExecutablePath()) + "'");
295+
llvm::inconvertibleErrorCode(), "'%s' produced malformed response",
296+
executablePlugin->getExecutablePath().data());
298297
}
299298

300299
// Resend the compiler capability on reconnect.
@@ -322,23 +321,28 @@ initializeExecutablePlugin(ASTContext &ctx,
322321
std::string resolvedLibraryPathStr(resolvedLibraryPath);
323322
std::string moduleNameStr(moduleName.str());
324323

324+
BridgedString errorOut{nullptr, 0};
325325
bool loaded = swift_ASTGen_pluginServerLoadLibraryPlugin(
326326
executablePlugin, resolvedLibraryPathStr.c_str(), moduleNameStr.c_str(),
327-
&ctx.Diags);
328-
if (!loaded)
327+
&errorOut);
328+
if (!loaded) {
329+
SWIFT_DEFER { swift_ASTGen_freeBridgedString(errorOut); };
329330
return llvm::createStringError(
330331
llvm::inconvertibleErrorCode(),
331-
"failed to load library plugin '" + resolvedLibraryPathStr +
332-
"' in plugin server '" +
333-
StringRef(executablePlugin->getExecutablePath()) + "'");
332+
"failed to load library plugin '%s' in plugin server '%s'; %s",
333+
resolvedLibraryPathStr.c_str(),
334+
executablePlugin->getExecutablePath().data(), errorOut.data);
335+
}
336+
337+
assert(errorOut.data == nullptr);
334338

335339
// Set a callback to load the library again on reconnections.
336340
auto *callback = new std::function<void(void)>(
337341
[executablePlugin, resolvedLibraryPathStr, moduleNameStr]() {
338342
(void)swift_ASTGen_pluginServerLoadLibraryPlugin(
339343
executablePlugin, resolvedLibraryPathStr.c_str(),
340344
moduleNameStr.c_str(),
341-
/*diags=*/nullptr);
345+
/*errorOut=*/nullptr);
342346
});
343347
executablePlugin->addOnReconnect(callback);
344348

@@ -359,7 +363,7 @@ CompilerPluginLoadRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
359363
PluginLoader &loader = ctx->getPluginLoader();
360364
const auto &entry = loader.lookupPluginByModuleName(moduleName);
361365

362-
std::string errorMessage;
366+
SmallString<0> errorMessage;
363367

364368
if (!entry.executablePath.empty()) {
365369
llvm::Expected<LoadedExecutablePlugin *> executablePlugin =
@@ -376,9 +380,12 @@ CompilerPluginLoadRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
376380
}
377381
if (executablePlugin)
378382
return executablePlugin.get();
379-
llvm::handleAllErrors(
380-
executablePlugin.takeError(),
381-
[&](const llvm::ErrorInfoBase &err) { errorMessage += err.message(); });
383+
llvm::handleAllErrors(executablePlugin.takeError(),
384+
[&](const llvm::ErrorInfoBase &err) {
385+
if (!errorMessage.empty())
386+
errorMessage += ", ";
387+
errorMessage += err.message();
388+
});
382389
} else if (!entry.libraryPath.empty()) {
383390

384391
llvm::Expected<LoadedLibraryPlugin *> libraryPlugin =
@@ -393,6 +400,8 @@ CompilerPluginLoadRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
393400
} else {
394401
llvm::handleAllErrors(libraryPlugin.takeError(),
395402
[&](const llvm::ErrorInfoBase &err) {
403+
if (!errorMessage.empty())
404+
errorMessage += ", ";
396405
errorMessage += err.message();
397406
});
398407
}
@@ -401,9 +410,8 @@ CompilerPluginLoadRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
401410
NullTerminatedStringRef err(errorMessage, *ctx);
402411
return CompilerPluginLoadResult::error(err);
403412
} else {
404-
NullTerminatedStringRef errMsg("plugin that can handle module '" +
405-
moduleName.str() + "' not found",
406-
*ctx);
413+
NullTerminatedStringRef errMsg(
414+
"plugin for module '" + moduleName.str() + "' not found", *ctx);
407415
return CompilerPluginLoadResult::error(errMsg);
408416
}
409417
}
@@ -427,22 +435,22 @@ resolveInProcessMacro(ASTContext &ctx, Identifier moduleName,
427435
ExternalMacroDefinition::PluginKind::InProcess, inProcess};
428436
} else {
429437
NullTerminatedStringRef err(
430-
"type '" + moduleName.str() + "." + typeName.str() +
438+
"'" + moduleName.str() + "." + typeName.str() +
431439
"' is not a valid macro implementation type in library plugin '" +
432440
StringRef(plugin->getLibraryPath()) + "'",
433441
ctx);
434442

435443
return ExternalMacroDefinition::error(err);
436444
}
437445
}
438-
NullTerminatedStringRef err("macro implementation type '" + moduleName.str() +
439-
"." + typeName.str() +
446+
NullTerminatedStringRef err("'" + moduleName.str() + "." + typeName.str() +
440447
"' could not be found in library plugin '" +
441448
StringRef(plugin->getLibraryPath()) + "'",
442449
ctx);
443450
return ExternalMacroDefinition::error(err);
444451
#endif
445-
return ExternalMacroDefinition::error("macro is not supported");
452+
return ExternalMacroDefinition::error(
453+
"the current compiler was not built with macro support");
446454
}
447455

448456
static ExternalMacroDefinition
@@ -461,13 +469,14 @@ resolveExecutableMacro(ASTContext &ctx,
461469
// NOTE: this is not reachable because executable macro resolution always
462470
// succeeds.
463471
NullTerminatedStringRef err(
464-
"macro implementation type '" + moduleName.str() + "." + typeName.str() +
472+
"'" + moduleName.str() + "." + typeName.str() +
465473
"' could not be found in executable plugin" +
466474
StringRef(executablePlugin->getExecutablePath()),
467475
ctx);
468476
return ExternalMacroDefinition::error(err);
469477
#endif
470-
return ExternalMacroDefinition::error("macro is not supported");
478+
return ExternalMacroDefinition::error(
479+
"the current compiler was not built with macro support");
471480
}
472481

473482
ExternalMacroDefinition

test/Macros/macro_expand.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,8 @@ struct HasNestedType {
616616
#if TEST_DIAGNOSTICS
617617
@freestanding(expression)
618618
macro missingMacro() = #externalMacro(module: "MacroDefinition", type: "BluhBlah")
619-
// expected-warning@-1 {{external macro implementation type 'MacroDefinition.BluhBlah' could not be found for macro 'missingMacro()'; macro implementation type 'MacroDefinition.BluhBlah' could not be found in library plugin '}}
619+
// expected-warning@-1 {{external macro implementation type 'MacroDefinition.BluhBlah' could not be found for macro 'missingMacro()'; 'MacroDefinition.BluhBlah' could not be found in library plugin '}}
620620
@freestanding(expression)
621621
macro notMacro() = #externalMacro(module: "MacroDefinition", type: "NotMacroStruct")
622-
// expected-warning@-1 {{macro implementation type 'MacroDefinition.NotMacroStruct' could not be found for macro 'notMacro()'; type 'MacroDefinition.NotMacroStruct' is not a valid macro implementation type in library plugin '}}
622+
// expected-warning@-1 {{macro implementation type 'MacroDefinition.NotMacroStruct' could not be found for macro 'notMacro()'; 'MacroDefinition.NotMacroStruct' is not a valid macro implementation type in library plugin '}}
623623
#endif

test/Macros/macro_plugin_broken.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
// RUN: c-index-test -read-diagnostics %t/macro_expand.dia 2>&1 | %FileCheck -check-prefix CHECK %s
2020

21-
// CHECK: test.swift:1:33: warning: external macro implementation type 'TestPlugin.FooMacro' could not be found for macro 'fooMacro'; failed to initialize executable plugin '{{.*}}broken-plugin'
22-
// CHECK: test.swift:4:7: error: external macro implementation type 'TestPlugin.FooMacro' could not be found for macro 'fooMacro'; failed to initialize executable plugin '{{.*}}broken-plugin'
21+
// CHECK: test.swift:1:33: warning: external macro implementation type 'TestPlugin.FooMacro' could not be found for macro 'fooMacro'; '{{.*}}broken-plugin' produced malformed response
22+
// CHECK: test.swift:4:7: error: external macro implementation type 'TestPlugin.FooMacro' could not be found for macro 'fooMacro'; '{{.*}}broken-plugin' produced malformed response
2323
// CHECK: +-{{.+}}test.swift:1:33: note: 'fooMacro' declared here
2424

2525
//--- test.swift

test/Macros/macro_plugin_broken_shlib.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
// RUN: c-index-test -read-diagnostics %t/macro_expand.dia 2>&1 | %FileCheck -check-prefix SERVER %s
1919

2020
// SERVER-NOT: {{error|warning}}
21-
// SERVER: (null):0:0: error: loader error: dlopen(BUILD_DIR/{{.*}}/libTestPlugin.dylib, 0x0009): tried: 'BUILD_DIR/{{.*}}/plugins/libTestPlugin.dylib'
22-
// SERVER: test.swift:1:33: warning: external macro implementation type 'TestPlugin.FooMacro' could not be found for macro 'fooMacro'; failed to load library plugin 'BUILD_DIR/{{.*}}/libTestPlugin.dylib' in plugin server 'BUILD_DIR/{{.*}}/swift-plugin-server'
23-
// SERVER: test.swift:4:7: error: external macro implementation type 'TestPlugin.FooMacro' could not be found for macro 'fooMacro'; failed to load library plugin 'BUILD_DIR/{{.*}}/libTestPlugin.dylib' in plugin server 'BUILD_DIR/{{.*}}/swift-plugin-server'
21+
// SERVER: test.swift:1:33: warning: external macro implementation type 'TestPlugin.FooMacro' could not be found for macro 'fooMacro'; failed to load library plugin 'BUILD_DIR/{{.*}}/libTestPlugin.dylib' in plugin server 'BUILD_DIR/{{.*}}/swift-plugin-server'; loader error: dlopen(BUILD_DIR/{{.*}}/libTestPlugin.dylib, {{.*}}): tried: 'BUILD_DIR/{{.*}}/plugins/libTestPlugin.dylib'
22+
// SERVER: test.swift:4:7: error: external macro implementation type 'TestPlugin.FooMacro' could not be found for macro 'fooMacro'; failed to load library plugin 'BUILD_DIR/{{.*}}/libTestPlugin.dylib' in plugin server 'BUILD_DIR/{{.*}}/swift-plugin-server'; loader error: dlopen(BUILD_DIR/{{.*}}/libTestPlugin.dylib, {{.*}}): tried: 'BUILD_DIR/{{.*}}/plugins/libTestPlugin.dylib'
2423
// SERVER: test.swift:1:33: note: 'fooMacro' declared here
2524
// SERVER-NOT: {{error|warning}}
2625

@@ -35,8 +34,8 @@
3534
// RUN: c-index-test -read-diagnostics %t/macro_expand_inproc.dia 2>&1 | %FileCheck -check-prefix INPROC %s
3635

3736
// INPROC-NOT: {{error|warning}}
38-
// INPROC: test.swift:1:33: warning: external macro implementation type 'TestPlugin.FooMacro' could not be found for macro 'fooMacro'; compiler plugin not loaded: BUILD_DIR/{{.*}}/libTestPlugin.dylib; loader error: dlopen(BUILD_DIR/{{.*}}/libTestPlugin.dylib, 0x0005): tried: 'BUILD_DIR/{{.*}}/libTestPlugin.dylib'
39-
// INPROC: test.swift:4:7: error: external macro implementation type 'TestPlugin.FooMacro' could not be found for macro 'fooMacro'; compiler plugin not loaded: BUILD_DIR/{{.*}}/libTestPlugin.dylib; loader error: dlopen(BUILD_DIR/{{.*}}/libTestPlugin.dylib, 0x0005): tried: 'BUILD_DIR/{{.*}}/libTestPlugin.dylib'
37+
// INPROC: test.swift:1:33: warning: external macro implementation type 'TestPlugin.FooMacro' could not be found for macro 'fooMacro'; compiler plugin 'BUILD_DIR/{{.*}}/libTestPlugin.dylib' could not be loaded; dlopen(BUILD_DIR/{{.*}}/libTestPlugin.dylib, 0x0005): tried: 'BUILD_DIR/{{.*}}/libTestPlugin.dylib'
38+
// INPROC: test.swift:4:7: error: external macro implementation type 'TestPlugin.FooMacro' could not be found for macro 'fooMacro'; compiler plugin 'BUILD_DIR/{{.*}}/libTestPlugin.dylib' could not be loaded; dlopen(BUILD_DIR/{{.*}}/libTestPlugin.dylib, 0x0005): tried: 'BUILD_DIR/{{.*}}/libTestPlugin.dylib'
4039
// INPROC: test.swift:1:33: note: 'fooMacro' declared here
4140
// INPROC-NOT: {{error|warning}}
4241

test/Macros/macros_diagnostics.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ func testDiags(a: Int, b: Int) {
132132

133133
func shadow(a: Int, b: Int, stringify: Int) {
134134
_ = #stringify(a + b)
135-
// expected-error@-1{{external macro implementation type 'MacroDefinition.StringifyMacro' could not be found for macro 'stringify'; plugin that can handle module 'MacroDefinition' not found}}
135+
// expected-error@-1{{external macro implementation type 'MacroDefinition.StringifyMacro' could not be found for macro 'stringify'; plugin for module 'MacroDefinition' not found}}
136136
}
137137

138138
func testMissing() {
139-
#missingMacro1("hello") // expected-error{{external macro implementation type 'MissingModule.MissingType' could not be found for macro 'missingMacro1'; plugin that can handle module 'MissingModule' not found}}
139+
#missingMacro1("hello") // expected-error{{external macro implementation type 'MissingModule.MissingType' could not be found for macro 'missingMacro1'; plugin for module 'MissingModule' not found}}
140140
}
141141

142142
@freestanding(expression) macro undefined() // expected-error{{macro 'undefined()' requires a definition}}
@@ -148,12 +148,12 @@ func testExternalMacroOutOfPlace() {
148148

149149
@freestanding(expression)
150150
public macro macroWithDefaults(_: Int = 17) = #externalMacro(module: "A", type: "B")
151-
// expected-warning@-1{{external macro implementation type 'A.B' could not be found for macro 'macroWithDefaults'; plugin that can handle module 'A' not found}}
151+
// expected-warning@-1{{external macro implementation type 'A.B' could not be found for macro 'macroWithDefaults'; plugin for module 'A' not found}}
152152
// expected-note@-2{{'macroWithDefaults' declared here}}
153153

154154
func callMacroWithDefaults() {
155155
_ = #macroWithDefaults()
156-
// expected-error@-1 {{external macro implementation type 'A.B' could not be found for macro 'macroWithDefaults'; plugin that can handle module 'A' not found}}
156+
// expected-error@-1 {{external macro implementation type 'A.B' could not be found for macro 'macroWithDefaults'; plugin for module 'A' not found}}
157157
}
158158

159159
// Make sure we don't allow macros to prevent type folding.
@@ -164,7 +164,7 @@ public macro MacroOrType() = #externalMacro(module: "A", type: "MacroOrType")
164164
@freestanding(codeItem, names: named(foo))
165165
public macro badCodeItemMacro() = #externalMacro(module: "A", type: "B")
166166
// expected-error@-2{{'codeItem' macros are not allowed to introduce names}}
167-
// expected-warning@-2{{external macro implementation type 'A.B' could not be found for macro 'badCodeItemMacro()'; plugin that can handle module 'A' not found}}
167+
// expected-warning@-2{{external macro implementation type 'A.B' could not be found for macro 'badCodeItemMacro()'; plugin for module 'A' not found}}
168168

169169
struct MacroOrType {
170170
typealias Nested = Int

0 commit comments

Comments
 (0)