Skip to content

Commit 5fb8b5f

Browse files
committed
Use the pretty-printed declaration for applying macros to imported decls
1 parent e35472c commit 5fb8b5f

File tree

1 file changed

+10
-37
lines changed

1 file changed

+10
-37
lines changed

lib/Sema/TypeCheckMacros.cpp

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,44 +1351,17 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
13511351
if (!attrSourceFile)
13521352
return nullptr;
13531353

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());
13901361
}
13911362

1363+
SourceFile *declSourceFile =
1364+
moduleDecl->getSourceFileContainingLocation(attachedToLoc);
13921365
if (!declSourceFile)
13931366
return nullptr;
13941367

@@ -1529,7 +1502,7 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
15291502

15301503
auto startLoc = searchDecl->getStartLoc();
15311504
if (startLoc.isInvalid() && isa<ClangModuleUnit>(dc->getModuleScopeContext())) {
1532-
startLoc = ctx.SourceMgr.getLocForBufferStart(*declSourceFile->getBufferID());
1505+
startLoc = attachedToLoc;
15331506
}
15341507

15351508
BridgedStringRef evaluatedSourceOut{nullptr, 0};

0 commit comments

Comments
 (0)