@@ -6137,26 +6137,30 @@ class AsyncConverter : private SourceEntityWalker {
6137
6137
return OutputStr;
6138
6138
}
6139
6139
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
+
6140
6150
// / Retrieves the location for the start of a comment attached to the token
6141
6151
// / at the provided location, or the location itself if there is no comment.
6142
6152
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 ();
6148
6157
}
6149
6158
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 ();
6160
6164
}
6161
6165
6162
6166
void convertNodes (const NodesToPrint &ToPrint) {
@@ -6171,8 +6175,6 @@ class AsyncConverter : private SourceEntityWalker {
6171
6175
6172
6176
// First print the nodes we've been asked to print.
6173
6177
for (auto Node : ToPrint.getNodes ()) {
6174
- OS << " \n " ;
6175
-
6176
6178
// If we need to print comments, do so now.
6177
6179
while (!CommentLocs.empty ()) {
6178
6180
auto CommentLoc = CommentLocs.back ().getOpaquePointerValue ();
@@ -6187,16 +6189,13 @@ class AsyncConverter : private SourceEntityWalker {
6187
6189
6188
6190
printCommentIfNeeded (CommentLocs.pop_back_val ());
6189
6191
}
6192
+ OS << " \n " ;
6190
6193
convertNode (Node);
6191
6194
}
6192
6195
6193
6196
// 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 ());
6200
6199
}
6201
6200
6202
6201
void convertNode (ASTNode Node, SourceLoc StartOverride = {},
0 commit comments