@@ -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 = {},
0 commit comments