|
23 | 23 | #include "swift/AST/ASTContext.h" |
24 | 24 | #include "swift/AST/ASTMangler.h" |
25 | 25 | #include "swift/AST/ASTNode.h" |
| 26 | +#include "swift/AST/ASTPrinter.h" |
26 | 27 | #include "swift/AST/DiagnosticsFrontend.h" |
27 | 28 | #include "swift/AST/Expr.h" |
28 | 29 | #include "swift/AST/FreestandingMacroExpansion.h" |
|
39 | 40 | #include "swift/Basic/Lazy.h" |
40 | 41 | #include "swift/Basic/SourceManager.h" |
41 | 42 | #include "swift/Basic/StringExtras.h" |
| 43 | +#include "swift/ClangImporter/ClangModule.h" |
42 | 44 | #include "swift/Bridging/ASTGen.h" |
43 | 45 | #include "swift/Bridging/Macros.h" |
44 | 46 | #include "swift/Demangling/Demangler.h" |
@@ -1021,7 +1023,10 @@ createMacroSourceFile(std::unique_ptr<llvm::MemoryBuffer> buffer, |
1021 | 1023 | auto macroSourceFile = new (ctx) SourceFile( |
1022 | 1024 | *dc->getParentModule(), SourceFileKind::MacroExpansion, macroBufferID, |
1023 | 1025 | /*parsingOpts=*/{}, /*isPrimary=*/false); |
1024 | | - macroSourceFile->setImports(dc->getParentSourceFile()->getImports()); |
| 1026 | + if (auto parentSourceFile = dc->getParentSourceFile()) |
| 1027 | + macroSourceFile->setImports(parentSourceFile->getImports()); |
| 1028 | + else if (isa<ClangModuleUnit>(dc->getModuleScopeContext())) |
| 1029 | + macroSourceFile->setImports({}); |
1025 | 1030 | return macroSourceFile; |
1026 | 1031 | } |
1027 | 1032 |
|
@@ -1346,8 +1351,17 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, |
1346 | 1351 | if (!attrSourceFile) |
1347 | 1352 | return nullptr; |
1348 | 1353 |
|
1349 | | - auto declSourceFile = |
1350 | | - moduleDecl->getSourceFileContainingLocation(attachedTo->getStartLoc()); |
| 1354 | + // If the declaration has no source location and comes from a Clang module, |
| 1355 | + // pretty-print the declaration and use that location. |
| 1356 | + SourceLoc attachedToLoc = attachedTo->getLoc(); |
| 1357 | + if (attachedToLoc.isInvalid() && |
| 1358 | + isa<ClangModuleUnit>(dc->getModuleScopeContext())) { |
| 1359 | + attachedToLoc = evaluateOrDefault( |
| 1360 | + ctx.evaluator, PrettyPrintDeclRequest{attachedTo}, SourceLoc()); |
| 1361 | + } |
| 1362 | + |
| 1363 | + SourceFile *declSourceFile = |
| 1364 | + moduleDecl->getSourceFileContainingLocation(attachedToLoc); |
1351 | 1365 | if (!declSourceFile) |
1352 | 1366 | return nullptr; |
1353 | 1367 |
|
@@ -1486,13 +1500,18 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, |
1486 | 1500 | if (auto var = dyn_cast<VarDecl>(attachedTo)) |
1487 | 1501 | searchDecl = var->getParentPatternBinding(); |
1488 | 1502 |
|
| 1503 | + auto startLoc = searchDecl->getStartLoc(); |
| 1504 | + if (startLoc.isInvalid() && isa<ClangModuleUnit>(dc->getModuleScopeContext())) { |
| 1505 | + startLoc = attachedToLoc; |
| 1506 | + } |
| 1507 | + |
1489 | 1508 | BridgedStringRef evaluatedSourceOut{nullptr, 0}; |
1490 | 1509 | assert(!externalDef.isError()); |
1491 | 1510 | swift_Macros_expandAttachedMacro( |
1492 | 1511 | &ctx.Diags, externalDef.get(), discriminator->c_str(), |
1493 | 1512 | extendedType.c_str(), conformanceList.c_str(), getRawMacroRole(role), |
1494 | 1513 | astGenAttrSourceFile, attr->AtLoc.getOpaquePointerValue(), |
1495 | | - astGenDeclSourceFile, searchDecl->getStartLoc().getOpaquePointerValue(), |
| 1514 | + astGenDeclSourceFile, startLoc.getOpaquePointerValue(), |
1496 | 1515 | astGenParentDeclSourceFile, parentDeclLoc, &evaluatedSourceOut); |
1497 | 1516 | if (!evaluatedSourceOut.unbridged().data()) |
1498 | 1517 | return nullptr; |
|
0 commit comments