@@ -6196,26 +6196,30 @@ class AsyncConverter : private SourceEntityWalker {
6196
6196
return OutputStr;
6197
6197
}
6198
6198
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
+
6199
6209
// / Retrieves the location for the start of a comment attached to the token
6200
6210
// / at the provided location, or the location itself if there is no comment.
6201
6211
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 ();
6207
6216
}
6208
6217
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 ();
6219
6223
}
6220
6224
6221
6225
void convertNodes (const NodesToPrint &ToPrint) {
@@ -6230,8 +6234,6 @@ class AsyncConverter : private SourceEntityWalker {
6230
6234
6231
6235
// First print the nodes we've been asked to print.
6232
6236
for (auto Node : ToPrint.getNodes ()) {
6233
- OS << " \n " ;
6234
-
6235
6237
// If we need to print comments, do so now.
6236
6238
while (!CommentLocs.empty ()) {
6237
6239
auto CommentLoc = CommentLocs.back ().getOpaquePointerValue ();
@@ -6246,16 +6248,13 @@ class AsyncConverter : private SourceEntityWalker {
6246
6248
6247
6249
printCommentIfNeeded (CommentLocs.pop_back_val ());
6248
6250
}
6251
+ OS << " \n " ;
6249
6252
convertNode (Node);
6250
6253
}
6251
6254
6252
6255
// 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 ());
6259
6258
}
6260
6259
6261
6260
void convertNode (ASTNode Node, SourceLoc StartOverride = {},
0 commit comments