Skip to content

Commit 7e0d96c

Browse files
author
David Ungar
committed
Added debugging helpers for range-matching
1 parent ec862c5 commit 7e0d96c

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

include/swift/AST/ASTScope.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ class ASTScopeImpl {
215215

216216
bool checkSourceRangeOfThisASTNode() const;
217217

218+
/// For debugging
219+
bool doesRangeMatch(unsigned start, unsigned end, StringRef file = "",
220+
StringRef className = "");
221+
218222
private:
219223
SourceRange computeSourceRangeOfScope(bool omitAssertions = false) const;
220224
SourceRange

lib/AST/ASTScopeCreation.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,26 @@ static void dumpRangeable(SpecializeAttr *r, llvm::raw_ostream &f) {
9393
llvm::errs() << "SpecializeAttr\n";
9494
}
9595

96+
/// For Debugging
97+
template <typename T>
98+
bool doesRangeableRangeMatch(const T *x, const SourceManager &SM,
99+
unsigned start, unsigned end,
100+
StringRef file = "") {
101+
auto const r = getRangeableSourceRange(x);
102+
if (r.isInvalid())
103+
return false;
104+
if (start && SM.getLineNumber(r.Start) != start)
105+
return false;
106+
if (end && SM.getLineNumber(r.End) != end)
107+
return false;
108+
if (file.empty())
109+
return true;
110+
const auto buf = SM.findBufferContainingLoc(r.Start);
111+
return SM.getIdentifierForBuffer(buf).endswith(file);
112+
}
113+
114+
#pragma mark end of rangeable
115+
96116
static std::vector<ASTNode> asNodeVector(DeclRange dr) {
97117
std::vector<ASTNode> nodes;
98118
llvm::transform(dr, std::back_inserter(nodes),
@@ -366,7 +386,7 @@ class ScopeCreator final {
366386
parent, vd);
367387
});
368388
}
369-
// HERE
389+
370390
public:
371391
/// Create the matryoshka nested generic param scopes (if any)
372392
/// that are subscopes of the receiver. Return

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,22 @@ NullablePtr<ASTScopeImpl> ASTScopeImpl::getPriorSibling() const {
166166
return siblingsAndMe[myIndex - 1];
167167
}
168168

169+
bool ASTScopeImpl::doesRangeMatch(unsigned start, unsigned end, StringRef file,
170+
StringRef className) {
171+
if (!className.empty() && className != getClassName())
172+
return false;
173+
const auto &SM = getSourceManager();
174+
const auto r = getSourceRangeOfScope(true);
175+
if (start && start != SM.getLineNumber(r.Start))
176+
return false;
177+
if (end && end != SM.getLineNumber(r.End))
178+
return false;
179+
if (file.empty())
180+
return true;
181+
const auto buf = SM.findBufferContainingLoc(r.Start);
182+
return SM.getIdentifierForBuffer(buf).endswith(file);
183+
}
184+
169185
#pragma mark getSourceRangeOfThisASTNode
170186

171187
SourceRange SpecializeAttributeScope::getSourceRangeOfThisASTNode(

0 commit comments

Comments
 (0)