Skip to content

Commit 16ca723

Browse files
author
David Ungar
committed
Explain source ranges
1 parent b418588 commit 16ca723

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

include/swift/AST/ASTScope.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class ASTScopeImpl {
169169
#pragma mark - source ranges
170170

171171
public:
172-
SourceRange getSourceRange(bool forDebugging = false) const;
172+
SourceRange getSourceRange(bool omitAssertions = false) const;
173173

174174
protected:
175175
SourceManager &getSourceManager() const;
@@ -186,7 +186,18 @@ class ASTScopeImpl {
186186
virtual NullablePtr<ClosureExpr> getClosureIfClosureScope() const;
187187

188188
private:
189-
SourceRange getUncachedSourceRange(bool forDebugging = false) const;
189+
SourceRange getUncachedSourceRange(bool omitAssertions = false) const;
190+
SourceRange widenSourceRangeForIgnoredASTNodes(SourceRange range) const;
191+
192+
/// If the scope refers to a Decl whose source range tells the whole story,
193+
/// for example a NominalTypeScope, it is not necessary to widen the source
194+
/// range by examining the children. In that case we could just return
195+
/// the childlessRange here.
196+
/// But, we have not marked such scopes yet. Doing so would be an
197+
/// optimization.
198+
SourceRange widenSourceRangeForChildren(SourceRange range,
199+
bool omitAssertions) const;
200+
190201
public: // for PatternEntryDeclScope::expandMe
191202
void cacheSourceRange();
192203
private:

lib/AST/ASTScopePrinting.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ void ASTScopeImpl::printRange(llvm::raw_ostream &out) const {
123123
if (!cachedSourceRange)
124124
out << "(uncached) ";
125125
SourceRange range = cachedSourceRange
126-
? getSourceRange(/*forDebugging=*/true)
127-
: getUncachedSourceRange(/*forDebugging=*/true);
126+
? getSourceRange(/*omitAssertions=*/true)
127+
: getUncachedSourceRange(/*omitAssertions=*/true);
128128
printSourceRange(out, range, getSourceManager());
129129
}
130130

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,55 @@ using namespace ast_scope;
3535

3636
static SourceLoc getStartOfFirstParam(ClosureExpr *closure);
3737

38-
SourceRange ASTScopeImpl::getSourceRange(bool forDebugging) const {
39-
if (forDebugging && !cachedSourceRange)
38+
SourceRange ASTScopeImpl::getSourceRange(const bool omitAssertions) const {
39+
if (omitAssertions && !cachedSourceRange)
4040
return SourceRange();
4141
assert(cachedSourceRange && "should have been cached after last expandMe");
4242
return *cachedSourceRange;
4343
}
4444

45-
SourceRange ASTScopeImpl::getUncachedSourceRange(bool forDebugging) const {
46-
auto childlessRange = getChildlessSourceRange();
45+
SourceRange
46+
ASTScopeImpl::getUncachedSourceRange(const bool omitAssertions) const {
47+
const auto childlessRange = getChildlessSourceRange();
48+
const auto rangeIncludingIgnoredNodes =
49+
widenSourceRangeForIgnoredASTNodes(childlessRange);
50+
return widenSourceRangeForChildren(rangeIncludingIgnoredNodes,
51+
omitAssertions);
52+
}
53+
54+
SourceRange ASTScopeImpl::widenSourceRangeForIgnoredASTNodes(
55+
const SourceRange range) const {
56+
if (range.isInvalid())
57+
return sourceRangeOfIgnoredASTNodes;
58+
auto r = range;
4759
if (sourceRangeOfIgnoredASTNodes.isValid())
48-
childlessRange.widen(sourceRangeOfIgnoredASTNodes);
60+
r.widen(sourceRangeOfIgnoredASTNodes);
61+
return r;
62+
}
63+
64+
SourceRange
65+
ASTScopeImpl::widenSourceRangeForChildren(const SourceRange range,
66+
bool omitAssertions) const {
4967
if (getChildren().empty()) {
50-
assert(childlessRange.Start.isValid());
51-
return childlessRange;
68+
assert(omitAssertions || range.Start.isValid());
69+
return range;
5270
}
71+
// If we change the caching to compute the source range lazily,
72+
// the only children required here would be the first and last.
5373
for (auto *c: getChildren())
5474
c->cacheSourceRange();
75+
5576
const auto childStart =
56-
getChildren().front()->getSourceRange(forDebugging).Start;
57-
assert(forDebugging || childStart.isValid());
58-
const auto childEnd = getChildren().back()->getSourceRange(forDebugging).End;
77+
getChildren().front()->getSourceRange(omitAssertions).Start;
78+
const auto childEnd =
79+
getChildren().back()->getSourceRange(omitAssertions).End;
5980
auto childRange = SourceRange(childStart, childEnd);
60-
if (childlessRange.Start.isInvalid())
81+
assert(omitAssertions || childRange.isValid());
82+
83+
if (range.isInvalid())
6184
return childRange;
62-
auto r = childRange;
63-
r.widen(childlessRange);
85+
auto r = range;
86+
r.widen(childRange);
6487
return r;
6588
}
6689

0 commit comments

Comments
 (0)