Skip to content

Commit 6cf09fa

Browse files
author
David Ungar
authored
Merge pull request swiftlang#24905 from davidungar/interpolated-string-literal-trailing-quote-loc
[NameLookup: ASTScope] Add trailing quote location to interpolated string literal.
2 parents 0fce705 + d37fd2f commit 6cf09fa

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

include/swift/AST/Expr.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,15 +953,25 @@ 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 \c ASTScope subsystem because lookups can be
958+
/// targeted to inside an \c InterpolatedStringLiteralExpr. It would be nicer
959+
/// to use \c EndLoc for this value, but then \c Lexer::getLocForEndOfToken()
960+
/// would not work for \c stringLiteral->getEndLoc().
961+
SourceLoc TrailingQuoteLoc;
956962
TapExpr *AppendingExpr;
957963
Expr *SemanticExpr;
958964

959965
public:
960-
InterpolatedStringLiteralExpr(SourceLoc Loc, unsigned LiteralCapacity,
966+
InterpolatedStringLiteralExpr(SourceLoc Loc,
967+
SourceLoc TrailingQuoteLoc,
968+
unsigned LiteralCapacity,
961969
unsigned InterpolationCount,
962970
TapExpr *AppendingExpr)
963971
: LiteralExpr(ExprKind::InterpolatedStringLiteral, /*Implicit=*/false),
964-
Loc(Loc), AppendingExpr(AppendingExpr), SemanticExpr() {
972+
Loc(Loc),
973+
TrailingQuoteLoc(TrailingQuoteLoc),
974+
AppendingExpr(AppendingExpr), SemanticExpr() {
965975
Bits.InterpolatedStringLiteralExpr.InterpolationCount = InterpolationCount;
966976
Bits.InterpolatedStringLiteralExpr.LiteralCapacity = LiteralCapacity;
967977
}
@@ -998,6 +1008,11 @@ class InterpolatedStringLiteralExpr : public LiteralExpr {
9981008
// token, so the range should be (Start == End).
9991009
return Loc;
10001010
}
1011+
SourceLoc getTrailingQuoteLoc() const {
1012+
// Except when computing a SourceRange for an ASTScope. Then the range
1013+
// must be (Start - TrainingQuoteLoc).
1014+
return TrailingQuoteLoc;
1015+
}
10011016

10021017
/// Call the \c callback with information about each segment in turn.
10031018
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,8 +2039,8 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
20392039
// Return an error, but include an empty InterpolatedStringLiteralExpr
20402040
// so that parseDeclPoundDiagnostic() can figure out why this string
20412041
// literal was bad.
2042-
return makeParserErrorResult(
2043-
new (Context) InterpolatedStringLiteralExpr(Loc, 0, 0, nullptr));
2042+
return makeParserErrorResult(new (Context) InterpolatedStringLiteralExpr(
2043+
Loc, Loc.getAdvancedLoc(CloseQuoteBegin), 0, 0, nullptr));
20442044
}
20452045

20462046
unsigned LiteralCapacity = 0;
@@ -2093,7 +2093,8 @@ ParserResult<Expr> Parser::parseExprStringLiteral() {
20932093
}
20942094

20952095
return makeParserResult(Status, new (Context) InterpolatedStringLiteralExpr(
2096-
Loc, LiteralCapacity, InterpolationCount,
2096+
Loc, Loc.getAdvancedLoc(CloseQuoteBegin),
2097+
LiteralCapacity, InterpolationCount,
20972098
AppendingExpr));
20982099
}
20992100

test/Parse/source_locs.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Check source locations (only InterpolatedStringLiteral for now).
2+
3+
func string_interpolation() {
4+
"\("abc")"
5+
}
6+
7+
// RUN: %target-swift-frontend -dump-ast %s | %FileCheck %s
8+
// CHECK: (interpolated_string_literal_expr {{.*}} trailing_quote_loc=SOURCE_DIR/test/Parse/source_locs.swift:4:12 {{.*}}

0 commit comments

Comments
 (0)