Skip to content

[AST] Improve calculation of source range for brace and do stmt #61016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/swift/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ class BraceStmt final : public Stmt,
SourceLoc getLBraceLoc() const { return LBLoc; }
SourceLoc getRBraceLoc() const { return RBLoc; }

SourceRange getSourceRange() const { return SourceRange(LBLoc, RBLoc); }
SourceLoc getStartLoc() const;
SourceLoc getEndLoc() const;

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

SourceLoc getDoLoc() const { return DoLoc; }

SourceLoc getStartLoc() const { return getLabelLocOrKeywordLoc(DoLoc); }
SourceLoc getEndLoc() const { return Body->getEndLoc(); }
SourceLoc getStartLoc() const;
SourceLoc getEndLoc() const;

BraceStmt *getBody() const { return Body; }
void setBody(BraceStmt *s) { Body = s; }
Expand Down
35 changes: 35 additions & 0 deletions lib/AST/Stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,30 @@ BraceStmt *BraceStmt::create(ASTContext &ctx, SourceLoc lbloc,
return ::new(Buffer) BraceStmt(lbloc, elts, rbloc, implicit);
}

SourceLoc BraceStmt::getStartLoc() const {
if (LBLoc) {
return LBLoc;
}
for (auto elt : getElements()) {
if (auto loc = elt.getStartLoc()) {
return loc;
}
}
return SourceLoc();
}

SourceLoc BraceStmt::getEndLoc() const {
if (RBLoc) {
return RBLoc;
}
for (auto elt : llvm::reverse(getElements())) {
if (auto loc = elt.getEndLoc()) {
return loc;
}
}
return SourceLoc();
}

ASTNode BraceStmt::findAsyncNode() {
// TODO: Statements don't track their ASTContext/evaluator, so I am not making
// this a request. It probably should be a request at some point.
Expand Down Expand Up @@ -508,6 +532,17 @@ DoStmt *DoStmt::createImplicit(ASTContext &C, LabeledStmtInfo labelInfo,
/*implicit=*/true);
}

SourceLoc DoStmt::getStartLoc() const {
if (auto LabelOrDoLoc = getLabelLocOrKeywordLoc(DoLoc)) {
return LabelOrDoLoc;
}
return Body->getStartLoc();
}

SourceLoc DoStmt::getEndLoc() const {
return Body->getEndLoc();
}

namespace {

template<typename CaseIterator>
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/PlaygroundTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ class Instrumenter : InstrumenterBase {
}
}

if (!TopLevel && !HighPerformance) {
if (!TopLevel && !HighPerformance && !BS->isImplicit()) {
Elements.insert(Elements.begin(), *buildScopeEntry(BS->getSourceRange()));
Elements.insert(Elements.end(), *buildScopeExit(BS->getSourceRange()));
}
Expand Down