Skip to content

Commit 4252659

Browse files
committed
ASTScope: Add a getCharSourceRangeOfScope() method to ASTScopeImpl
1 parent b8cccb1 commit 4252659

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

include/swift/AST/ASTScope.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ class ASTScopeImpl {
157157
// source range. So include their ranges here
158158
SourceRange sourceRangeOfIgnoredASTNodes;
159159

160+
mutable Optional<CharSourceRange> cachedCharSourceRange;
161+
160162
#pragma mark - constructor / destructor
161163
public:
162164
ASTScopeImpl(){};
@@ -212,6 +214,10 @@ class ASTScopeImpl {
212214

213215
SourceRange getSourceRangeOfScope(bool omitAssertions = false) const;
214216

217+
CharSourceRange getCharSourceRangeOfScope(SourceManager &SM,
218+
bool omitAssertions = false) const;
219+
bool isCharSourceRangeCached() const;
220+
215221
/// InterpolatedStringLiteralExprs and EditorPlaceHolders respond to
216222
/// getSourceRange with the starting point. But we might be asked to lookup an
217223
/// identifer within one of them. So, find the real source range of them here.

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,27 @@ SourceRange GuardStmtBodyScope::getSourceRangeOfThisASTNode(
450450

451451
#pragma mark source range caching
452452

453+
CharSourceRange
454+
ASTScopeImpl::getCharSourceRangeOfScope(SourceManager &SM,
455+
bool omitAssertions) const {
456+
if (!isCharSourceRangeCached()) {
457+
auto range = getSourceRangeOfThisASTNode(omitAssertions);
458+
ASTScopeAssert(range.isValid(), "scope has invalid source range");
459+
ASTScopeAssert(SM.isBeforeInBuffer(range.Start, range.End) ||
460+
range.Start == range.End,
461+
"scope source range ends before start");
462+
463+
cachedCharSourceRange =
464+
Lexer::getCharSourceRangeFromSourceRange(SM, range);
465+
}
466+
467+
return *cachedCharSourceRange;
468+
}
469+
470+
bool ASTScopeImpl::isCharSourceRangeCached() const {
471+
return cachedCharSourceRange.hasValue();
472+
}
473+
453474
SourceRange
454475
ASTScopeImpl::getSourceRangeOfScope(const bool omitAssertions) const {
455476
if (!isSourceRangeCached(omitAssertions))

0 commit comments

Comments
 (0)