Skip to content

Commit 1e3dd5c

Browse files
committed
[AST] Improve source range info for TapExpr
Previously we would only base the start loc on the `SubExpr`, but that isn't set until CSApply. Change it to take both `SubExpr` and `Body`'s source range into account. Also tighten up the invariant that a TapExpr must be created with a non-null BraceStmt.
1 parent 7a38f17 commit 1e3dd5c

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

include/swift/AST/Expr.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -880,12 +880,8 @@ class TapExpr : public Expr {
880880
void setBody(BraceStmt * b) { Body = b; }
881881

882882
SourceLoc getLoc() const { return SubExpr ? SubExpr->getLoc() : SourceLoc(); }
883-
884-
SourceLoc getStartLoc() const {
885-
return SubExpr ? SubExpr->getStartLoc() : SourceLoc();
886-
}
887883

888-
SourceLoc getEndLoc() const;
884+
SourceRange getSourceRange() const;
889885

890886
static bool classof(const Expr *E) {
891887
return E->getKind() == ExprKind::Tap;

lib/AST/Expr.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,29 +2611,33 @@ void InterpolatedStringLiteralExpr::forEachSegment(ASTContext &Ctx,
26112611
TapExpr::TapExpr(Expr * SubExpr, BraceStmt *Body)
26122612
: Expr(ExprKind::Tap, /*Implicit=*/true),
26132613
SubExpr(SubExpr), Body(Body) {
2614-
if (Body) {
2615-
assert(!Body->empty() &&
2616-
Body->getFirstElement().isDecl(DeclKind::Var) &&
2617-
"First element of Body should be a variable to init with the subExpr");
2618-
}
2614+
assert(Body);
2615+
assert(!Body->empty() &&
2616+
Body->getFirstElement().isDecl(DeclKind::Var) &&
2617+
"First element of Body should be a variable to init with the subExpr");
26192618
}
26202619

26212620
VarDecl * TapExpr::getVar() const {
26222621
return dyn_cast<VarDecl>(Body->getFirstElement().dyn_cast<Decl *>());
26232622
}
26242623

2625-
SourceLoc TapExpr::getEndLoc() const {
2626-
// Include the body in the range, assuming the body follows the SubExpr.
2627-
// Also, be (perhaps overly) defensive about null pointers & invalid
2628-
// locations.
2629-
if (auto *const b = getBody()) {
2630-
const auto be = b->getEndLoc();
2631-
if (be.isValid())
2632-
return be;
2633-
}
2634-
if (auto *const se = getSubExpr())
2635-
return se->getEndLoc();
2636-
return SourceLoc();
2624+
SourceRange TapExpr::getSourceRange() const {
2625+
if (!SubExpr)
2626+
return Body->getSourceRange();
2627+
2628+
SourceLoc start = SubExpr->getStartLoc();
2629+
if (!start.isValid())
2630+
start = Body->getStartLoc();
2631+
if (!start.isValid())
2632+
return SourceRange();
2633+
2634+
SourceLoc end = Body->getEndLoc();
2635+
if (!end.isValid())
2636+
end = SubExpr->getEndLoc();
2637+
if (!end.isValid())
2638+
return SourceRange();
2639+
2640+
return SourceRange(start, end);
26372641
}
26382642

26392643
RegexLiteralExpr *

0 commit comments

Comments
 (0)