Skip to content

Commit 98c46e3

Browse files
authored
Merge pull request #39224 from hamishknight/comment-etiquette-5.5
[5.5] [Aync Refactoring] Better handle comment trailing whitespace
2 parents e13a614 + eb42d33 commit 98c46e3

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
@@ -6196,26 +6196,30 @@ class AsyncConverter : private SourceEntityWalker {
61966196
return OutputStr;
61976197
}
61986198

6199+
/// Retrieves the SourceRange of the preceding comment, or an invalid range if
6200+
/// there is no preceding comment.
6201+
CharSourceRange getPrecedingCommentRange(SourceLoc Loc) {
6202+
auto Tokens = SF->getAllTokens();
6203+
auto TokenIter = token_lower_bound(Tokens, Loc);
6204+
if (TokenIter == Tokens.end() || !TokenIter->hasComment())
6205+
return CharSourceRange();
6206+
return TokenIter->getCommentRange();
6207+
}
6208+
61996209
/// Retrieves the location for the start of a comment attached to the token
62006210
/// at the provided location, or the location itself if there is no comment.
62016211
SourceLoc getLocIncludingPrecedingComment(SourceLoc Loc) {
6202-
auto Tokens = SF->getAllTokens();
6203-
auto TokenIter = token_lower_bound(Tokens, Loc);
6204-
if (TokenIter != Tokens.end() && TokenIter->hasComment())
6205-
return TokenIter->getCommentStart();
6206-
return Loc;
6212+
auto CommentRange = getPrecedingCommentRange(Loc);
6213+
if (CommentRange.isInvalid())
6214+
return Loc;
6215+
return CommentRange.getStart();
62076216
}
62086217

6209-
/// If the provided SourceLoc has a preceding comment, print it out. Returns
6210-
/// true if a comment was printed, false otherwise.
6211-
bool printCommentIfNeeded(SourceLoc Loc, bool AddNewline = false) {
6212-
auto PrecedingLoc = getLocIncludingPrecedingComment(Loc);
6213-
if (Loc == PrecedingLoc)
6214-
return false;
6215-
if (AddNewline)
6216-
OS << "\n";
6217-
OS << CharSourceRange(SM, PrecedingLoc, Loc).str();
6218-
return true;
6218+
/// If the provided SourceLoc has a preceding comment, print it out.
6219+
void printCommentIfNeeded(SourceLoc Loc) {
6220+
auto CommentRange = getPrecedingCommentRange(Loc);
6221+
if (CommentRange.isValid())
6222+
OS << "\n" << CommentRange.str();
62196223
}
62206224

62216225
void convertNodes(const NodesToPrint &ToPrint) {
@@ -6230,8 +6234,6 @@ class AsyncConverter : private SourceEntityWalker {
62306234

62316235
// First print the nodes we've been asked to print.
62326236
for (auto Node : ToPrint.getNodes()) {
6233-
OS << "\n";
6234-
62356237
// If we need to print comments, do so now.
62366238
while (!CommentLocs.empty()) {
62376239
auto CommentLoc = CommentLocs.back().getOpaquePointerValue();
@@ -6246,16 +6248,13 @@ class AsyncConverter : private SourceEntityWalker {
62466248

62476249
printCommentIfNeeded(CommentLocs.pop_back_val());
62486250
}
6251+
OS << "\n";
62496252
convertNode(Node);
62506253
}
62516254

62526255
// We're done printing nodes. Make sure to output the remaining comments.
6253-
bool HasPrintedComment = false;
6254-
while (!CommentLocs.empty()) {
6255-
HasPrintedComment |=
6256-
printCommentIfNeeded(CommentLocs.pop_back_val(),
6257-
/*AddNewline*/ !HasPrintedComment);
6258-
}
6256+
while (!CommentLocs.empty())
6257+
printCommentIfNeeded(CommentLocs.pop_back_val());
62596258
}
62606259

62616260
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)