Skip to content

Commit 2dde79f

Browse files
committed
Improve calculation of source range for brace and do stmt
1 parent 01fc224 commit 2dde79f

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

include/swift/AST/Stmt.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ class BraceStmt final : public Stmt,
168168
SourceLoc getLBraceLoc() const { return LBLoc; }
169169
SourceLoc getRBraceLoc() const { return RBLoc; }
170170

171-
SourceRange getSourceRange() const { return SourceRange(LBLoc, RBLoc); }
171+
SourceLoc getStartLoc() const;
172+
SourceLoc getEndLoc() const;
172173

173174
bool empty() const { return getNumElements() == 0; }
174175
unsigned getNumElements() const { return Bits.BraceStmt.NumElements; }
@@ -563,8 +564,8 @@ class DoStmt : public LabeledStmt {
563564

564565
SourceLoc getDoLoc() const { return DoLoc; }
565566

566-
SourceLoc getStartLoc() const { return getLabelLocOrKeywordLoc(DoLoc); }
567-
SourceLoc getEndLoc() const { return Body->getEndLoc(); }
567+
SourceLoc getStartLoc() const;
568+
SourceLoc getEndLoc() const;
568569

569570
BraceStmt *getBody() const { return Body; }
570571
void setBody(BraceStmt *s) { Body = s; }

lib/AST/Stmt.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,30 @@ BraceStmt *BraceStmt::create(ASTContext &ctx, SourceLoc lbloc,
156156
return ::new(Buffer) BraceStmt(lbloc, elts, rbloc, implicit);
157157
}
158158

159+
SourceLoc BraceStmt::getStartLoc() const {
160+
if (LBLoc) {
161+
return LBLoc;
162+
}
163+
for (auto elt : getElements()) {
164+
if (auto loc = elt.getStartLoc()) {
165+
return loc;
166+
}
167+
}
168+
return SourceLoc();
169+
}
170+
171+
SourceLoc BraceStmt::getEndLoc() const {
172+
if (RBLoc) {
173+
return RBLoc;
174+
}
175+
for (auto elt : llvm::reverse(getElements())) {
176+
if (auto loc = elt.getStartLoc()) {
177+
return loc;
178+
}
179+
}
180+
return SourceLoc();
181+
}
182+
159183
ASTNode BraceStmt::findAsyncNode() {
160184
// TODO: Statements don't track their ASTContext/evaluator, so I am not making
161185
// this a request. It probably should be a request at some point.
@@ -508,6 +532,17 @@ DoStmt *DoStmt::createImplicit(ASTContext &C, LabeledStmtInfo labelInfo,
508532
/*implicit=*/true);
509533
}
510534

535+
SourceLoc DoStmt::getStartLoc() const {
536+
if (DoLoc) {
537+
return DoLoc;
538+
}
539+
return Body->getStartLoc();
540+
}
541+
542+
SourceLoc DoStmt::getEndLoc() const {
543+
return Body->getEndLoc();
544+
}
545+
511546
namespace {
512547

513548
template<typename CaseIterator>

0 commit comments

Comments
 (0)