Skip to content

Commit 7a8b3c8

Browse files
author
David Ungar
committed
Add trailing quote location to interpolated string literal.
1 parent 22e64f0 commit 7a8b3c8

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

include/swift/AST/Expr.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,15 +953,23 @@ class TapExpr : public Expr {
953953
class InterpolatedStringLiteralExpr : public LiteralExpr {
954954
/// Points at the beginning quote.
955955
SourceLoc Loc;
956+
/// Points at the ending quote.
957+
/// Needed for the upcoming ASTScope subsystem because lookups can be targeted
958+
/// to inside an InterpolatedStringLiteralExpr.
959+
SourceLoc TrailingQuoteLoc;
956960
TapExpr *AppendingExpr;
957961
Expr *SemanticExpr;
958962

959963
public:
960-
InterpolatedStringLiteralExpr(SourceLoc Loc, unsigned LiteralCapacity,
964+
InterpolatedStringLiteralExpr(SourceLoc Loc,
965+
SourceLoc TrailingQuoteLoc,
966+
unsigned LiteralCapacity,
961967
unsigned InterpolationCount,
962968
TapExpr *AppendingExpr)
963969
: LiteralExpr(ExprKind::InterpolatedStringLiteral, /*Implicit=*/false),
964-
Loc(Loc), AppendingExpr(AppendingExpr), SemanticExpr() {
970+
Loc(Loc),
971+
TrailingQuoteLoc(TrailingQuoteLoc),
972+
AppendingExpr(AppendingExpr), SemanticExpr() {
965973
Bits.InterpolatedStringLiteralExpr.InterpolationCount = InterpolationCount;
966974
Bits.InterpolatedStringLiteralExpr.LiteralCapacity = LiteralCapacity;
967975
}
@@ -998,6 +1006,11 @@ class InterpolatedStringLiteralExpr : public LiteralExpr {
9981006
// token, so the range should be (Start == End).
9991007
return Loc;
10001008
}
1009+
SourceLoc getTrailingQuoteLoc() const {
1010+
// Except when computing a SourceRange for an ASTScope. Then the range
1011+
// must be (Start - TrainingQuoteLoc).
1012+
return TrailingQuoteLoc;
1013+
}
10011014

10021015
/// Call the \c callback with information about each segment in turn.
10031016
void forEachSegment(ASTContext &Ctx,

lib/AST/ASTDumper.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,19 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
19301930
}
19311931
void visitInterpolatedStringLiteralExpr(InterpolatedStringLiteralExpr *E) {
19321932
printCommon(E, "interpolated_string_literal_expr");
1933-
PrintWithColorRAII(OS, LiteralValueColor) << " literal_capacity="
1933+
1934+
// Print the trailing quote location
1935+
if (auto Ty = GetTypeOfExpr(E)) {
1936+
auto &Ctx = Ty->getASTContext();
1937+
auto TQL = E->getTrailingQuoteLoc();
1938+
if (TQL.isValid()) {
1939+
PrintWithColorRAII(OS, LocationColor) << " trailing_quote_loc=";
1940+
TQL.print(PrintWithColorRAII(OS, LocationColor).getOS(),
1941+
Ctx.SourceMgr);
1942+
}
1943+
}
1944+
PrintWithColorRAII(OS, LiteralValueColor)
1945+
<< " literal_capacity="
19341946
<< E->getLiteralCapacity() << " interpolation_count="
19351947
<< E->getInterpolationCount() << '\n';
19361948
printRec(E->getAppendingExpr());

lib/AST/Expr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ static LiteralExpr *
813813
shallowCloneImpl(const InterpolatedStringLiteralExpr *E, ASTContext &Ctx,
814814
llvm::function_ref<Type(const Expr *)> getType) {
815815
auto res = new (Ctx) InterpolatedStringLiteralExpr(E->getLoc(),
816+
E->getTrailingQuoteLoc(),
816817
E->getLiteralCapacity(),
817818
E->getInterpolationCount(),
818819
E->getAppendingExpr());

lib/Parse/ParseExpr.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,7 +2040,9 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
20402040
// so that parseDeclPoundDiagnostic() can figure out why this string
20412041
// literal was bad.
20422042
return makeParserErrorResult(
2043-
new (Context) InterpolatedStringLiteralExpr(Loc, 0, 0, nullptr));
2043+
new (Context) InterpolatedStringLiteralExpr(Loc,
2044+
EndLoc.getAdvancedLoc(-1),
2045+
0, 0, nullptr));
20442046
}
20452047

20462048
unsigned LiteralCapacity = 0;
@@ -2093,7 +2095,8 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
20932095
}
20942096

20952097
return makeParserResult(Status, new (Context) InterpolatedStringLiteralExpr(
2096-
Loc, LiteralCapacity, InterpolationCount,
2098+
Loc, EndLoc.getAdvancedLoc(-1),
2099+
LiteralCapacity, InterpolationCount,
20972100
AppendingExpr));
20982101
}
20992102

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Check the trailingQuoteLoc field of InterpolatedStringLiteral
2+
3+
"\("abc")"
4+
5+
// RUN: %target-swift-frontend -dump-ast %s | %FileCheck %s
6+
// CHECK: (interpolated_string_literal_expr {{.*}} trailing_quote_loc={{.*}}:3:10 {{.*}}

0 commit comments

Comments
 (0)