@@ -1351,44 +1351,17 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
1351
1351
if (!attrSourceFile)
1352
1352
return nullptr ;
1353
1353
1354
- SourceFile *declSourceFile =
1355
- moduleDecl->getSourceFileContainingLocation (attachedTo->getStartLoc ());
1356
- if (!declSourceFile && isa<ClangModuleUnit>(dc->getModuleScopeContext ())) {
1357
- // Pretty-print the declaration into a buffer so we can macro-expand
1358
- // it.
1359
- // FIXME: Turn this into a request.
1360
- llvm::SmallString<128 > buffer;
1361
- {
1362
- llvm::raw_svector_ostream out (buffer);
1363
- StreamPrinter printer (out);
1364
- attachedTo->print (
1365
- printer,
1366
- PrintOptions::printForDiagnostics (
1367
- AccessLevel::Public,
1368
- ctx.TypeCheckerOpts .PrintFullConvention ));
1369
- }
1370
-
1371
- // Create the buffer.
1372
- SourceManager &sourceMgr = ctx.SourceMgr ;
1373
- auto bufferID = sourceMgr.addMemBufferCopy (buffer);
1374
- auto memBufferStartLoc = sourceMgr.getLocForBufferStart (bufferID);
1375
- sourceMgr.setGeneratedSourceInfo (
1376
- bufferID,
1377
- GeneratedSourceInfo{
1378
- GeneratedSourceInfo::PrettyPrinted,
1379
- CharSourceRange (),
1380
- CharSourceRange (memBufferStartLoc, buffer.size ()),
1381
- ASTNode (const_cast <Decl *>(attachedTo)).getOpaqueValue (),
1382
- nullptr
1383
- }
1384
- );
1385
-
1386
- // Create a source file to go with it.
1387
- declSourceFile = new (ctx)
1388
- SourceFile (*moduleDecl, SourceFileKind::Library, bufferID);
1389
- moduleDecl->addAuxiliaryFile (*declSourceFile);
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 ());
1390
1361
}
1391
1362
1363
+ SourceFile *declSourceFile =
1364
+ moduleDecl->getSourceFileContainingLocation (attachedToLoc);
1392
1365
if (!declSourceFile)
1393
1366
return nullptr ;
1394
1367
@@ -1529,7 +1502,7 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
1529
1502
1530
1503
auto startLoc = searchDecl->getStartLoc ();
1531
1504
if (startLoc.isInvalid () && isa<ClangModuleUnit>(dc->getModuleScopeContext ())) {
1532
- startLoc = ctx. SourceMgr . getLocForBufferStart (*declSourceFile-> getBufferID ()) ;
1505
+ startLoc = attachedToLoc ;
1533
1506
}
1534
1507
1535
1508
BridgedStringRef evaluatedSourceOut{nullptr , 0 };
0 commit comments