Skip to content

Commit 9b49397

Browse files
committed
[Aync Refactoring] Better handle comment trailing whitespace
Use `getCommentRange` to trim off the comment's trailing whitespace, avoiding outputting extraneous empty lines. rdar://82072147
1 parent dc60996 commit 9b49397

File tree

5 files changed

+22
-30
lines changed

5 files changed

+22
-30
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6137,26 +6137,30 @@ class AsyncConverter : private SourceEntityWalker {
61376137
return OutputStr;
61386138
}
61396139

6140+
/// Retrieves the SourceRange of the preceding comment, or an invalid range if
6141+
/// there is no preceding comment.
6142+
CharSourceRange getPrecedingCommentRange(SourceLoc Loc) {
6143+
auto Tokens = SF->getAllTokens();
6144+
auto TokenIter = token_lower_bound(Tokens, Loc);
6145+
if (TokenIter == Tokens.end() || !TokenIter->hasComment())
6146+
return CharSourceRange();
6147+
return TokenIter->getCommentRange();
6148+
}
6149+
61406150
/// Retrieves the location for the start of a comment attached to the token
61416151
/// at the provided location, or the location itself if there is no comment.
61426152
SourceLoc getLocIncludingPrecedingComment(SourceLoc Loc) {
6143-
auto Tokens = SF->getAllTokens();
6144-
auto TokenIter = token_lower_bound(Tokens, Loc);
6145-
if (TokenIter != Tokens.end() && TokenIter->hasComment())
6146-
return TokenIter->getCommentStart();
6147-
return Loc;
6153+
auto CommentRange = getPrecedingCommentRange(Loc);
6154+
if (CommentRange.isInvalid())
6155+
return Loc;
6156+
return CommentRange.getStart();
61486157
}
61496158

6150-
/// If the provided SourceLoc has a preceding comment, print it out. Returns
6151-
/// true if a comment was printed, false otherwise.
6152-
bool printCommentIfNeeded(SourceLoc Loc, bool AddNewline = false) {
6153-
auto PrecedingLoc = getLocIncludingPrecedingComment(Loc);
6154-
if (Loc == PrecedingLoc)
6155-
return false;
6156-
if (AddNewline)
6157-
OS << "\n";
6158-
OS << CharSourceRange(SM, PrecedingLoc, Loc).str();
6159-
return true;
6159+
/// If the provided SourceLoc has a preceding comment, print it out.
6160+
void printCommentIfNeeded(SourceLoc Loc) {
6161+
auto CommentRange = getPrecedingCommentRange(Loc);
6162+
if (CommentRange.isValid())
6163+
OS << "\n" << CommentRange.str();
61606164
}
61616165

61626166
void convertNodes(const NodesToPrint &ToPrint) {
@@ -6171,8 +6175,6 @@ class AsyncConverter : private SourceEntityWalker {
61716175

61726176
// First print the nodes we've been asked to print.
61736177
for (auto Node : ToPrint.getNodes()) {
6174-
OS << "\n";
6175-
61766178
// If we need to print comments, do so now.
61776179
while (!CommentLocs.empty()) {
61786180
auto CommentLoc = CommentLocs.back().getOpaquePointerValue();
@@ -6187,16 +6189,13 @@ class AsyncConverter : private SourceEntityWalker {
61876189

61886190
printCommentIfNeeded(CommentLocs.pop_back_val());
61896191
}
6192+
OS << "\n";
61906193
convertNode(Node);
61916194
}
61926195

61936196
// We're done printing nodes. Make sure to output the remaining comments.
6194-
bool HasPrintedComment = false;
6195-
while (!CommentLocs.empty()) {
6196-
HasPrintedComment |=
6197-
printCommentIfNeeded(CommentLocs.pop_back_val(),
6198-
/*AddNewline*/ !HasPrintedComment);
6199-
}
6197+
while (!CommentLocs.empty())
6198+
printCommentIfNeeded(CommentLocs.pop_back_val());
62006199
}
62016200

62026201
void convertNode(ASTNode Node, SourceLoc StartOverride = {},

test/refactoring/ConvertAsync/basic.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,6 @@ func testPreserveComments2() {
835835
// PRESERVE-COMMENTS-ERROR-NEXT: // f
836836
// PRESERVE-COMMENTS-ERROR-NEXT: print("fun")
837837
// PRESERVE-COMMENTS-ERROR-NEXT: // g
838-
// PRESERVE-COMMENTS-ERROR-NEXT: {{ }}
839838
// PRESERVE-COMMENTS-ERROR-NEXT: }
840839

841840
// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=PRESERVE-TRAILING-COMMENT-FN %s

test/refactoring/ConvertAsync/convert_function.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ func callNonAsyncInAsyncComment(_ completion: @escaping (String) -> Void) {
226226
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: // i
227227
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: }
228228
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: // j
229-
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: {{ }}
230229
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: // k
231230
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: }
232231
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: }

test/refactoring/ConvertAsync/convert_params_single.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,12 @@ func testParamsSingle() async throws {
8787
// BOUND-COMMENT-NEXT: // l
8888
// BOUND-COMMENT-NEXT: print("after")
8989
// BOUND-COMMENT-NEXT: // m
90-
// BOUND-COMMENT-NEXT: {{ }}
9190
// BOUND-COMMENT-NEXT: } catch let bad {
9291
// BOUND-COMMENT-NEXT: // d
9392
// BOUND-COMMENT-NEXT: // e
9493
// BOUND-COMMENT-NEXT: print("got error \(bad)")
9594
// BOUND-COMMENT-NEXT: // f
9695
// BOUND-COMMENT-NEXT: // g
97-
// BOUND-COMMENT-NEXT: {{ }}
9896
// BOUND-COMMENT-NEXT: }
9997

10098

test/refactoring/ConvertAsync/convert_result.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,6 @@ func testResultConversion() async throws {
381381
// NESTEDBREAK-COMMENT-NEXT: // l
382382
// NESTEDBREAK-COMMENT-NEXT: print("after")
383383
// NESTEDBREAK-COMMENT-NEXT: // m
384-
// NESTEDBREAK-COMMENT-NEXT: {{ }}
385384
// NESTEDBREAK-COMMENT-NOT: }
386385

387386
// RUN: %refactor-check-compiles -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=ERROR-BLOCK-COMMENT %s
@@ -416,13 +415,11 @@ func testResultConversion() async throws {
416415
// ERROR-BLOCK-COMMENT-NEXT: // h
417416
// ERROR-BLOCK-COMMENT-NEXT: print("after")
418417
// ERROR-BLOCK-COMMENT-NEXT: // i
419-
// ERROR-BLOCK-COMMENT-NEXT: {{ }}
420418
// ERROR-BLOCK-COMMENT-NEXT: } catch {
421419
// ERROR-BLOCK-COMMENT-NEXT: // e
422420
// ERROR-BLOCK-COMMENT-NEXT: print("fail")
423421
// ERROR-BLOCK-COMMENT-NEXT: // f
424422
// ERROR-BLOCK-COMMENT-NEXT: // g
425-
// ERROR-BLOCK-COMMENT-NEXT: {{ }}
426423
// ERROR-BLOCK-COMMENT-NEXT: }
427424
// ERROR-BLOCK-COMMENT-NOT: }
428425

0 commit comments

Comments
 (0)