Skip to content

Commit 29926a7

Browse files
authored
Merge pull request #70580 from hborla/macro-original-source-range
[Macros] Always use `GeneratedSourceInfo::originalSourceRange` for the insertion range of a macro expansion.
2 parents c266644 + a9aa662 commit 29926a7

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

include/swift/AST/SourceFile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,11 @@ class SourceFile final : public FileUnit {
581581
/// the \c SourceFileKind is \c MacroExpansion.
582582
ASTNode getMacroExpansion() const;
583583

584+
/// For source files created to hold the source code for a macro
585+
/// expansion, this is the original source range replaced by the macro
586+
/// expansion.
587+
SourceRange getMacroInsertionRange() const;
588+
584589
/// For source files created to hold the source code created by expanding
585590
/// an attached macro, this is the custom attribute that describes the macro
586591
/// expansion.

lib/AST/ASTScopeCreation.cpp

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
271271
if (auto enclosingSF = SF->getEnclosingSourceFile()) {
272272
SourceLoc parentLoc;
273273
auto macroRole = SF->getFulfilledMacroRole();
274-
auto expansion = SF->getMacroExpansion();
275274

276275
// Determine the parent source location based on the macro role.
277276
AbstractFunctionDecl *bodyForDecl = nullptr;
@@ -283,35 +282,20 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
283282
case MacroRole::MemberAttribute:
284283
case MacroRole::Conformance:
285284
case MacroRole::Extension:
286-
parentLoc = expansion.getStartLoc();
285+
case MacroRole::Member:
286+
case MacroRole::Peer:
287+
case MacroRole::Preamble:
288+
parentLoc = SF->getMacroInsertionRange().End;;
287289
break;
288-
case MacroRole::Preamble: {
289-
// Preamble macro roles start at the beginning of the macro body.
290-
auto func = cast<AbstractFunctionDecl>(expansion.get<Decl *>());
291-
parentLoc = func->getMacroExpandedBody()->getStartLoc();
292-
break;
293-
}
294-
case MacroRole::Body:
290+
case MacroRole::Body: {
291+
// Use the end location of the function decl itself as the parentLoc
292+
// for the new function body scope. This is different from the end
293+
// location of the original source range, which is after the end of the
294+
// function decl.
295+
auto expansion = SF->getMacroExpansion();
295296
parentLoc = expansion.getEndLoc();
296297
bodyForDecl = cast<AbstractFunctionDecl>(expansion.get<Decl *>());
297298
break;
298-
case MacroRole::Peer: {
299-
ASTContext &ctx = SF->getASTContext();
300-
SourceManager &sourceMgr = ctx.SourceMgr;
301-
auto generatedSourceInfo =
302-
*sourceMgr.getGeneratedSourceInfo(*SF->getBufferID());
303-
304-
ASTNode node = ASTNode::getFromOpaqueValue(generatedSourceInfo.astNode);
305-
parentLoc = Lexer::getLocForEndOfToken(sourceMgr, node.getEndLoc());
306-
break;
307-
}
308-
case MacroRole::Member: {
309-
// For synthesized member macros, take the end loc of the
310-
// enclosing declaration (before the closing brace), because
311-
// the macro expansion is inside this scope.
312-
auto *decl = expansion.getAsDeclContext()->getAsDecl();
313-
parentLoc = decl->getEndLoc();
314-
break;
315299
}
316300
}
317301

lib/AST/ASTVerifier.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3816,8 +3816,9 @@ class Verifier : public ASTWalker {
38163816
if (!Ctx.SourceMgr.rangeContains(Enclosing, Current)) {
38173817
auto *expansionBuffer =
38183818
D->getModuleContext()->getSourceFileContainingLocation(Current.Start);
3819-
if (auto expansion = expansionBuffer->getMacroExpansion()) {
3820-
Current = expansion.getSourceRange();
3819+
3820+
if (auto expansionRange = expansionBuffer->getMacroInsertionRange()) {
3821+
Current = expansionRange;
38213822
}
38223823
}
38233824

lib/AST/Module.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,16 @@ ASTNode SourceFile::getMacroExpansion() const {
11811181
return ASTNode::getFromOpaqueValue(genInfo.astNode);
11821182
}
11831183

1184+
SourceRange SourceFile::getMacroInsertionRange() const {
1185+
if (Kind != SourceFileKind::MacroExpansion)
1186+
return SourceRange();
1187+
1188+
auto generatedInfo =
1189+
*getASTContext().SourceMgr.getGeneratedSourceInfo(*getBufferID());
1190+
auto origRange = generatedInfo.originalSourceRange;
1191+
return {origRange.getStart(), origRange.getEnd()};
1192+
}
1193+
11841194
CustomAttr *SourceFile::getAttachedMacroAttribute() const {
11851195
if (Kind != SourceFileKind::MacroExpansion)
11861196
return nullptr;

0 commit comments

Comments
 (0)