Skip to content

Commit a9aa662

Browse files
committed
[Macros] Introduce SourceFile::getMacroInsertionRange for getting the
original insertion range of a macro expansion and update ASTScope creation and ASTVerifier to use it.
1 parent 8e13732 commit a9aa662

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
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: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,9 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
284284
case MacroRole::Extension:
285285
case MacroRole::Member:
286286
case MacroRole::Peer:
287-
case MacroRole::Preamble: {
288-
auto &ctx = SF->getASTContext();
289-
auto generatedSourceInfo =
290-
*ctx.SourceMgr.getGeneratedSourceInfo(*SF->getBufferID());
291-
parentLoc = generatedSourceInfo.originalSourceRange.getEnd();
287+
case MacroRole::Preamble:
288+
parentLoc = SF->getMacroInsertionRange().End;;
292289
break;
293-
}
294290
case MacroRole::Body: {
295291
// Use the end location of the function decl itself as the parentLoc
296292
// for the new function body scope. This is different from the end

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)