@@ -71,22 +71,21 @@ extern "C" void swift_ASTGen_freeExpansionReplacements(
71
71
ptrdiff_t numReplacements);
72
72
73
73
extern " C" ptrdiff_t swift_ASTGen_expandFreestandingMacro (
74
- void *diagEngine, void *macro, uint8_t externalKind,
74
+ void *diagEngine, const void *macro, uint8_t externalKind,
75
75
const char *discriminator, ptrdiff_t discriminatorLength,
76
- uint8_t rawMacroRole, void *sourceFile,
77
- const void *sourceLocation, const char **evaluatedSource,
78
- ptrdiff_t *evaluatedSourceLength);
76
+ uint8_t rawMacroRole, void *sourceFile, const void *sourceLocation,
77
+ const char **evaluatedSource, ptrdiff_t *evaluatedSourceLength);
79
78
80
79
extern " C" ptrdiff_t swift_ASTGen_expandAttachedMacro (
81
- void *diagEngine, void *macro, uint8_t externalKind,
80
+ void *diagEngine, const void *macro, uint8_t externalKind,
82
81
const char *discriminator, ptrdiff_t discriminatorLength,
83
82
const char *qualifiedType, ptrdiff_t qualifiedTypeLength,
84
83
const char *conformances, ptrdiff_t conformancesLength,
85
- uint8_t rawMacroRole,
86
- void *customAttrSourceFile, const void *customAttrSourceLocation ,
87
- void *declarationSourceFile, const void *declarationSourceLocation ,
88
- void *parentDeclSourceFile , const void *parentDeclSourceLocation ,
89
- const char **evaluatedSource, ptrdiff_t *evaluatedSourceLength);
84
+ uint8_t rawMacroRole, void *customAttrSourceFile,
85
+ const void *customAttrSourceLocation, void *declarationSourceFile ,
86
+ const void *declarationSourceLocation, void *parentDeclSourceFile ,
87
+ const void *parentDeclSourceLocation , const char **evaluatedSource ,
88
+ ptrdiff_t *evaluatedSourceLength);
90
89
91
90
extern " C" bool swift_ASTGen_initializePlugin (void *handle, void *diagEngine);
92
91
extern " C" void swift_ASTGen_deinitializePlugin (void *handle);
@@ -389,7 +388,7 @@ CompilerPluginLoadRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
389
388
return nullptr ;
390
389
}
391
390
392
- static llvm::Optional< ExternalMacroDefinition>
391
+ static ExternalMacroDefinition
393
392
resolveInProcessMacro (ASTContext &ctx, Identifier moduleName,
394
393
Identifier typeName, LoadedLibraryPlugin *plugin) {
395
394
#if SWIFT_BUILD_SWIFT_SYNTAX
@@ -406,13 +405,27 @@ resolveInProcessMacro(ASTContext &ctx, Identifier moduleName,
406
405
407
406
return ExternalMacroDefinition{
408
407
ExternalMacroDefinition::PluginKind::InProcess, inProcess};
408
+ } else {
409
+ NullTerminatedStringRef err (
410
+ " type '" + moduleName.str () + " ." + typeName.str () +
411
+ " ' is not a valid macro implementation type in library plugin '" +
412
+ StringRef (plugin->getLibraryPath ()) + " '" ,
413
+ ctx);
414
+
415
+ return ExternalMacroDefinition::error (err);
409
416
}
410
417
}
418
+ NullTerminatedStringRef err (" macro implementation type '" + moduleName.str () +
419
+ " ." + typeName.str () +
420
+ " ' could not be found in library plugin '" +
421
+ StringRef (plugin->getLibraryPath ()) + " '" ,
422
+ ctx);
423
+ return ExternalMacroDefinition::error (err);
411
424
#endif
412
- return llvm::None ;
425
+ return ExternalMacroDefinition::error ( " macro is not supported " ) ;
413
426
}
414
427
415
- static llvm::Optional< ExternalMacroDefinition>
428
+ static ExternalMacroDefinition
416
429
resolveExecutableMacro (ASTContext &ctx,
417
430
LoadedExecutablePlugin *executablePlugin,
418
431
Identifier moduleName, Identifier typeName) {
@@ -426,11 +439,17 @@ resolveExecutableMacro(ASTContext &ctx,
426
439
return ExternalMacroDefinition{
427
440
ExternalMacroDefinition::PluginKind::Executable, execMacro};
428
441
}
442
+ NullTerminatedStringRef err (
443
+ " macro implementation type '" + moduleName.str () + " ." + typeName.str () +
444
+ " ' could not be found in executable plugin" +
445
+ StringRef (executablePlugin->getExecutablePath ()),
446
+ ctx);
447
+ return ExternalMacroDefinition::error (err);
429
448
#endif
430
- return llvm::None ;
449
+ return ExternalMacroDefinition::error ( " macro is not supported " ) ;
431
450
}
432
451
433
- llvm::Optional< ExternalMacroDefinition>
452
+ ExternalMacroDefinition
434
453
ExternalMacroDefinitionRequest::evaluate (Evaluator &evaluator, ASTContext *ctx,
435
454
Identifier moduleName,
436
455
Identifier typeName) const {
@@ -441,19 +460,17 @@ ExternalMacroDefinitionRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
441
460
evaluateOrDefault (evaluator, loadRequest, nullptr );
442
461
443
462
if (auto loadedLibrary = loaded.getAsLibraryPlugin ()) {
444
- if (auto inProcess = resolveInProcessMacro (
445
- *ctx, moduleName, typeName, loadedLibrary))
446
- return *inProcess;
463
+ return resolveInProcessMacro (*ctx, moduleName, typeName, loadedLibrary);
447
464
}
448
465
449
466
if (auto *executablePlugin = loaded.getAsExecutablePlugin ()) {
450
- if (auto executableMacro = resolveExecutableMacro (*ctx, executablePlugin,
451
- moduleName, typeName)) {
452
- return executableMacro;
453
- }
467
+ return resolveExecutableMacro (*ctx, executablePlugin, moduleName, typeName);
454
468
}
455
469
456
- return llvm::None;
470
+ NullTerminatedStringRef err (" plugin that can handle module '" +
471
+ moduleName.str () + " ' not found" ,
472
+ *ctx);
473
+ return ExternalMacroDefinition::error (err);
457
474
}
458
475
459
476
// / Adjust the given mangled name for a macro expansion to produce a valid
@@ -1037,11 +1054,14 @@ evaluateFreestandingMacro(FreestandingMacroExpansion *expansion,
1037
1054
auto external = macroDef.getExternalMacro ();
1038
1055
ExternalMacroDefinitionRequest request{&ctx, external.moduleName ,
1039
1056
external.macroTypeName };
1040
- auto externalDef = evaluateOrDefault (ctx.evaluator , request, llvm::None);
1041
- if (!externalDef) {
1057
+ auto externalDef =
1058
+ evaluateOrDefault (ctx.evaluator , request,
1059
+ ExternalMacroDefinition::error (" request error" ));
1060
+ if (externalDef.isError ()) {
1042
1061
ctx.Diags .diagnose (loc, diag::external_macro_not_found,
1043
1062
external.moduleName .str (),
1044
- external.macroTypeName .str (), macro->getName ());
1063
+ external.macroTypeName .str (), macro->getName (),
1064
+ externalDef.getErrorMessage ());
1045
1065
macro->diagnose (diag::decl_declared_here, macro);
1046
1066
return nullptr ;
1047
1067
}
@@ -1072,11 +1092,11 @@ evaluateFreestandingMacro(FreestandingMacroExpansion *expansion,
1072
1092
1073
1093
const char *evaluatedSourceAddress;
1074
1094
ptrdiff_t evaluatedSourceLength;
1095
+ assert (!externalDef.isError ());
1075
1096
swift_ASTGen_expandFreestandingMacro (
1076
- &ctx.Diags , externalDef->opaqueHandle ,
1077
- static_cast <uint32_t >(externalDef->kind ), discriminator->data (),
1078
- discriminator->size (),
1079
- getRawMacroRole (macroRole), astGenSourceFile,
1097
+ &ctx.Diags , externalDef.opaqueHandle ,
1098
+ static_cast <uint32_t >(externalDef.kind ), discriminator->data (),
1099
+ discriminator->size (), getRawMacroRole (macroRole), astGenSourceFile,
1080
1100
expansion->getSourceRange ().Start .getOpaquePointerValue (),
1081
1101
&evaluatedSourceAddress, &evaluatedSourceLength);
1082
1102
if (!evaluatedSourceAddress)
@@ -1312,13 +1332,14 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
1312
1332
ExternalMacroDefinitionRequest request{
1313
1333
&ctx, external.moduleName , external.macroTypeName
1314
1334
};
1315
- auto externalDef = evaluateOrDefault (ctx.evaluator , request, llvm::None);
1316
- if (!externalDef) {
1335
+ auto externalDef =
1336
+ evaluateOrDefault (ctx.evaluator , request,
1337
+ ExternalMacroDefinition::error (" failed request" ));
1338
+ if (externalDef.isError ()) {
1317
1339
attachedTo->diagnose (diag::external_macro_not_found,
1318
- external.moduleName .str (),
1319
- external.macroTypeName .str (),
1320
- macro->getName ()
1321
- );
1340
+ external.moduleName .str (),
1341
+ external.macroTypeName .str (), macro->getName (),
1342
+ externalDef.getErrorMessage ());
1322
1343
macro->diagnose (diag::decl_declared_here, macro);
1323
1344
return nullptr ;
1324
1345
}
@@ -1351,13 +1372,12 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
1351
1372
1352
1373
const char *evaluatedSourceAddress;
1353
1374
ptrdiff_t evaluatedSourceLength;
1375
+ assert (!externalDef.isError ());
1354
1376
swift_ASTGen_expandAttachedMacro (
1355
- &ctx.Diags , externalDef->opaqueHandle ,
1356
- static_cast <uint32_t >(externalDef->kind ),
1357
- discriminator->data (), discriminator->size (),
1358
- extendedType.data (), extendedType.size (),
1359
- conformanceList.data (), conformanceList.size (),
1360
- getRawMacroRole (role),
1377
+ &ctx.Diags , externalDef.opaqueHandle ,
1378
+ static_cast <uint32_t >(externalDef.kind ), discriminator->data (),
1379
+ discriminator->size (), extendedType.data (), extendedType.size (),
1380
+ conformanceList.data (), conformanceList.size (), getRawMacroRole (role),
1361
1381
astGenAttrSourceFile, attr->AtLoc .getOpaquePointerValue (),
1362
1382
astGenDeclSourceFile, searchDecl->getStartLoc ().getOpaquePointerValue (),
1363
1383
astGenParentDeclSourceFile, parentDeclLoc, &evaluatedSourceAddress,
0 commit comments