Skip to content

[Aync Refactoring] Better handle comment trailing whitespace #39161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions lib/IDE/Refactoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6137,26 +6137,30 @@ class AsyncConverter : private SourceEntityWalker {
return OutputStr;
}

/// Retrieves the SourceRange of the preceding comment, or an invalid range if
/// there is no preceding comment.
CharSourceRange getPrecedingCommentRange(SourceLoc Loc) {
auto Tokens = SF->getAllTokens();
auto TokenIter = token_lower_bound(Tokens, Loc);
if (TokenIter == Tokens.end() || !TokenIter->hasComment())
return CharSourceRange();
return TokenIter->getCommentRange();
}

/// Retrieves the location for the start of a comment attached to the token
/// at the provided location, or the location itself if there is no comment.
SourceLoc getLocIncludingPrecedingComment(SourceLoc Loc) {
auto Tokens = SF->getAllTokens();
auto TokenIter = token_lower_bound(Tokens, Loc);
if (TokenIter != Tokens.end() && TokenIter->hasComment())
return TokenIter->getCommentStart();
return Loc;
auto CommentRange = getPrecedingCommentRange(Loc);
if (CommentRange.isInvalid())
return Loc;
return CommentRange.getStart();
}

/// If the provided SourceLoc has a preceding comment, print it out. Returns
/// true if a comment was printed, false otherwise.
bool printCommentIfNeeded(SourceLoc Loc, bool AddNewline = false) {
auto PrecedingLoc = getLocIncludingPrecedingComment(Loc);
if (Loc == PrecedingLoc)
return false;
if (AddNewline)
OS << "\n";
OS << CharSourceRange(SM, PrecedingLoc, Loc).str();
return true;
/// If the provided SourceLoc has a preceding comment, print it out.
void printCommentIfNeeded(SourceLoc Loc) {
auto CommentRange = getPrecedingCommentRange(Loc);
if (CommentRange.isValid())
OS << "\n" << CommentRange.str();
}

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

// First print the nodes we've been asked to print.
for (auto Node : ToPrint.getNodes()) {
OS << "\n";

// If we need to print comments, do so now.
while (!CommentLocs.empty()) {
auto CommentLoc = CommentLocs.back().getOpaquePointerValue();
Expand All @@ -6187,16 +6189,13 @@ class AsyncConverter : private SourceEntityWalker {

printCommentIfNeeded(CommentLocs.pop_back_val());
}
OS << "\n";
convertNode(Node);
}

// We're done printing nodes. Make sure to output the remaining comments.
bool HasPrintedComment = false;
while (!CommentLocs.empty()) {
HasPrintedComment |=
printCommentIfNeeded(CommentLocs.pop_back_val(),
/*AddNewline*/ !HasPrintedComment);
}
while (!CommentLocs.empty())
printCommentIfNeeded(CommentLocs.pop_back_val());
}

void convertNode(ASTNode Node, SourceLoc StartOverride = {},
Expand Down
1 change: 0 additions & 1 deletion test/refactoring/ConvertAsync/basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,6 @@ func testPreserveComments2() {
// PRESERVE-COMMENTS-ERROR-NEXT: // f
// PRESERVE-COMMENTS-ERROR-NEXT: print("fun")
// PRESERVE-COMMENTS-ERROR-NEXT: // g
// PRESERVE-COMMENTS-ERROR-NEXT: {{ }}
// PRESERVE-COMMENTS-ERROR-NEXT: }

// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=PRESERVE-TRAILING-COMMENT-FN %s
Expand Down
1 change: 0 additions & 1 deletion test/refactoring/ConvertAsync/convert_function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ func callNonAsyncInAsyncComment(_ completion: @escaping (String) -> Void) {
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: // i
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: }
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: // j
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: {{ }}
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: // k
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: }
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: }
Expand Down
2 changes: 0 additions & 2 deletions test/refactoring/ConvertAsync/convert_params_single.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,12 @@ func testParamsSingle() async throws {
// BOUND-COMMENT-NEXT: // l
// BOUND-COMMENT-NEXT: print("after")
// BOUND-COMMENT-NEXT: // m
// BOUND-COMMENT-NEXT: {{ }}
// BOUND-COMMENT-NEXT: } catch let bad {
// BOUND-COMMENT-NEXT: // d
// BOUND-COMMENT-NEXT: // e
// BOUND-COMMENT-NEXT: print("got error \(bad)")
// BOUND-COMMENT-NEXT: // f
// BOUND-COMMENT-NEXT: // g
// BOUND-COMMENT-NEXT: {{ }}
// BOUND-COMMENT-NEXT: }


Expand Down
3 changes: 0 additions & 3 deletions test/refactoring/ConvertAsync/convert_result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ func testResultConversion() async throws {
// NESTEDBREAK-COMMENT-NEXT: // l
// NESTEDBREAK-COMMENT-NEXT: print("after")
// NESTEDBREAK-COMMENT-NEXT: // m
// NESTEDBREAK-COMMENT-NEXT: {{ }}
// NESTEDBREAK-COMMENT-NOT: }

// 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
Expand Down Expand Up @@ -416,13 +415,11 @@ func testResultConversion() async throws {
// ERROR-BLOCK-COMMENT-NEXT: // h
// ERROR-BLOCK-COMMENT-NEXT: print("after")
// ERROR-BLOCK-COMMENT-NEXT: // i
// ERROR-BLOCK-COMMENT-NEXT: {{ }}
// ERROR-BLOCK-COMMENT-NEXT: } catch {
// ERROR-BLOCK-COMMENT-NEXT: // e
// ERROR-BLOCK-COMMENT-NEXT: print("fail")
// ERROR-BLOCK-COMMENT-NEXT: // f
// ERROR-BLOCK-COMMENT-NEXT: // g
// ERROR-BLOCK-COMMENT-NEXT: {{ }}
// ERROR-BLOCK-COMMENT-NEXT: }
// ERROR-BLOCK-COMMENT-NOT: }

Expand Down