Skip to content

Commit 252b300

Browse files
committed
[AST] Improve calculation of source range for brace and do stmt
This allows us to later skip brace and do statements in result builders that are unrelated to the code completion token.
1 parent 8e81a80 commit 252b300

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
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.getEndLoc()) {
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 (auto LabelOrDoLoc = getLabelLocOrKeywordLoc(DoLoc)) {
537+
return LabelOrDoLoc;
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>

lib/Sema/PlaygroundTransform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ class Instrumenter : InstrumenterBase {
593593
}
594594
}
595595

596-
if (!TopLevel && !HighPerformance) {
596+
if (!TopLevel && !HighPerformance && !BS->isImplicit()) {
597597
Elements.insert(Elements.begin(), *buildScopeEntry(BS->getSourceRange()));
598598
Elements.insert(Elements.end(), *buildScopeExit(BS->getSourceRange()));
599599
}

0 commit comments

Comments
 (0)