Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 1 | //===--- RawCommentList.cpp - Processing raw comments -----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Chandler Carruth | 39a3e75 | 2012-06-20 09:53:52 +0000 | [diff] [blame] | 10 | #include "clang/AST/RawCommentList.h" |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 12 | #include "clang/AST/Comment.h" |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 13 | #include "clang/AST/CommentBriefParser.h" |
Dmitri Gribenko | ca7f80a | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 14 | #include "clang/AST/CommentCommandTraits.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/AST/CommentLexer.h" |
| 16 | #include "clang/AST/CommentParser.h" |
| 17 | #include "clang/AST/CommentSema.h" |
James Dennett | 2def1e8 | 2015-07-15 19:13:39 +0000 | [diff] [blame^] | 18 | #include "clang/Basic/CharInfo.h" |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | |
| 21 | using namespace clang; |
| 22 | |
| 23 | namespace { |
| 24 | /// Get comment kind and bool describing if it is a trailing comment. |
Dmitri Gribenko | e958562 | 2013-04-26 20:12:49 +0000 | [diff] [blame] | 25 | std::pair<RawComment::CommentKind, bool> getCommentKind(StringRef Comment, |
| 26 | bool ParseAllComments) { |
| 27 | const size_t MinCommentLength = ParseAllComments ? 2 : 3; |
| 28 | if ((Comment.size() < MinCommentLength) || Comment[0] != '/') |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 29 | return std::make_pair(RawComment::RCK_Invalid, false); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 30 | |
| 31 | RawComment::CommentKind K; |
| 32 | if (Comment[1] == '/') { |
| 33 | if (Comment.size() < 3) |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 34 | return std::make_pair(RawComment::RCK_OrdinaryBCPL, false); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 35 | |
| 36 | if (Comment[2] == '/') |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 37 | K = RawComment::RCK_BCPLSlash; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 38 | else if (Comment[2] == '!') |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 39 | K = RawComment::RCK_BCPLExcl; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 40 | else |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 41 | return std::make_pair(RawComment::RCK_OrdinaryBCPL, false); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 42 | } else { |
| 43 | assert(Comment.size() >= 4); |
| 44 | |
| 45 | // Comment lexer does not understand escapes in comment markers, so pretend |
| 46 | // that this is not a comment. |
| 47 | if (Comment[1] != '*' || |
| 48 | Comment[Comment.size() - 2] != '*' || |
| 49 | Comment[Comment.size() - 1] != '/') |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 50 | return std::make_pair(RawComment::RCK_Invalid, false); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 51 | |
| 52 | if (Comment[2] == '*') |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 53 | K = RawComment::RCK_JavaDoc; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 54 | else if (Comment[2] == '!') |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 55 | K = RawComment::RCK_Qt; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 56 | else |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 57 | return std::make_pair(RawComment::RCK_OrdinaryC, false); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 58 | } |
| 59 | const bool TrailingComment = (Comment.size() > 3) && (Comment[3] == '<'); |
| 60 | return std::make_pair(K, TrailingComment); |
| 61 | } |
| 62 | |
| 63 | bool mergedCommentIsTrailingComment(StringRef Comment) { |
| 64 | return (Comment.size() > 3) && (Comment[3] == '<'); |
| 65 | } |
James Dennett | 2def1e8 | 2015-07-15 19:13:39 +0000 | [diff] [blame^] | 66 | |
| 67 | /// Returns true if R1 and R2 both have valid locations that start on the same |
| 68 | /// column. |
| 69 | bool commentsStartOnSameColumn(const SourceManager &SM, const RawComment &R1, |
| 70 | const RawComment &R2) { |
| 71 | SourceLocation L1 = R1.getLocStart(); |
| 72 | SourceLocation L2 = R2.getLocStart(); |
| 73 | bool Invalid = false; |
| 74 | unsigned C1 = SM.getPresumedColumnNumber(L1, &Invalid); |
| 75 | if (!Invalid) { |
| 76 | unsigned C2 = SM.getPresumedColumnNumber(L2, &Invalid); |
| 77 | return !Invalid && (C1 == C2); |
| 78 | } |
| 79 | return false; |
| 80 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 81 | } // unnamed namespace |
| 82 | |
James Dennett | 2def1e8 | 2015-07-15 19:13:39 +0000 | [diff] [blame^] | 83 | /// \brief Determines whether there is only whitespace in `Buffer` between `P` |
| 84 | /// and the previous line. |
| 85 | /// \param Buffer The buffer to search in. |
| 86 | /// \param P The offset from the beginning of `Buffer` to start from. |
| 87 | /// \return true if all of the characters in `Buffer` ranging from the closest |
| 88 | /// line-ending character before `P` (or the beginning of `Buffer`) to `P - 1` |
| 89 | /// are whitespace. |
| 90 | static bool onlyWhitespaceOnLineBefore(const char *Buffer, unsigned P) { |
| 91 | // Search backwards until we see linefeed or carriage return. |
| 92 | for (unsigned I = P; I != 0; --I) { |
| 93 | char C = Buffer[I - 1]; |
| 94 | if (isVerticalWhitespace(C)) |
| 95 | return true; |
| 96 | if (!isHorizontalWhitespace(C)) |
| 97 | return false; |
| 98 | } |
| 99 | // We hit the beginning of the buffer. |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | /// Returns whether `K` is an ordinary comment kind. |
| 104 | static bool isOrdinaryKind(RawComment::CommentKind K) { |
| 105 | return (K == RawComment::RCK_OrdinaryBCPL) || |
| 106 | (K == RawComment::RCK_OrdinaryC); |
| 107 | } |
| 108 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 109 | RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR, |
Dmitri Gribenko | a7d16ce | 2013-04-10 15:35:17 +0000 | [diff] [blame] | 110 | bool Merged, bool ParseAllComments) : |
Dmitri Gribenko | 60c7ec6 | 2012-06-27 05:48:36 +0000 | [diff] [blame] | 111 | Range(SR), RawTextValid(false), BriefTextValid(false), |
James Dennett | 2def1e8 | 2015-07-15 19:13:39 +0000 | [diff] [blame^] | 112 | IsAttached(false), IsTrailingComment(false), IsAlmostTrailingComment(false), |
Benjamin Kramer | 20c28be | 2013-09-28 15:06:27 +0000 | [diff] [blame] | 113 | ParseAllComments(ParseAllComments) { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 114 | // Extract raw comment text, if possible. |
Dmitri Gribenko | fecc2e0 | 2012-06-21 21:02:45 +0000 | [diff] [blame] | 115 | if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) { |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 116 | Kind = RCK_Invalid; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | |
James Dennett | 2def1e8 | 2015-07-15 19:13:39 +0000 | [diff] [blame^] | 120 | // Guess comment kind. |
| 121 | std::pair<CommentKind, bool> K = getCommentKind(RawText, ParseAllComments); |
| 122 | |
| 123 | // Guess whether an ordinary comment is trailing. |
| 124 | if (ParseAllComments && isOrdinaryKind(K.first)) { |
| 125 | FileID BeginFileID; |
| 126 | unsigned BeginOffset; |
| 127 | std::tie(BeginFileID, BeginOffset) = |
| 128 | SourceMgr.getDecomposedLoc(Range.getBegin()); |
| 129 | if (BeginOffset != 0) { |
| 130 | bool Invalid = false; |
| 131 | const char *Buffer = |
| 132 | SourceMgr.getBufferData(BeginFileID, &Invalid).data(); |
| 133 | IsTrailingComment |= |
| 134 | (!Invalid && !onlyWhitespaceOnLineBefore(Buffer, BeginOffset)); |
| 135 | } |
| 136 | } |
| 137 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 138 | if (!Merged) { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 139 | Kind = K.first; |
James Dennett | 2def1e8 | 2015-07-15 19:13:39 +0000 | [diff] [blame^] | 140 | IsTrailingComment |= K.second; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 141 | |
| 142 | IsAlmostTrailingComment = RawText.startswith("//<") || |
| 143 | RawText.startswith("/*<"); |
| 144 | } else { |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 145 | Kind = RCK_Merged; |
James Dennett | 2def1e8 | 2015-07-15 19:13:39 +0000 | [diff] [blame^] | 146 | IsTrailingComment = |
| 147 | IsTrailingComment || mergedCommentIsTrailingComment(RawText); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 151 | StringRef RawComment::getRawTextSlow(const SourceManager &SourceMgr) const { |
| 152 | FileID BeginFileID; |
| 153 | FileID EndFileID; |
| 154 | unsigned BeginOffset; |
| 155 | unsigned EndOffset; |
| 156 | |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 157 | std::tie(BeginFileID, BeginOffset) = |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 158 | SourceMgr.getDecomposedLoc(Range.getBegin()); |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 159 | std::tie(EndFileID, EndOffset) = SourceMgr.getDecomposedLoc(Range.getEnd()); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 160 | |
| 161 | const unsigned Length = EndOffset - BeginOffset; |
| 162 | if (Length < 2) |
| 163 | return StringRef(); |
| 164 | |
| 165 | // The comment can't begin in one file and end in another. |
| 166 | assert(BeginFileID == EndFileID); |
| 167 | |
| 168 | bool Invalid = false; |
| 169 | const char *BufferStart = SourceMgr.getBufferData(BeginFileID, |
| 170 | &Invalid).data(); |
| 171 | if (Invalid) |
| 172 | return StringRef(); |
| 173 | |
| 174 | return StringRef(BufferStart + BeginOffset, Length); |
| 175 | } |
| 176 | |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 177 | const char *RawComment::extractBriefText(const ASTContext &Context) const { |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 178 | // Make sure that RawText is valid. |
| 179 | getRawText(Context.getSourceManager()); |
| 180 | |
Dmitri Gribenko | 4586df7 | 2012-07-27 20:37:06 +0000 | [diff] [blame] | 181 | // Since we will be copying the resulting text, all allocations made during |
| 182 | // parsing are garbage after resulting string is formed. Thus we can use |
| 183 | // a separate allocator for all temporary stuff. |
| 184 | llvm::BumpPtrAllocator Allocator; |
| 185 | |
Fariborz Jahanian | 5b63707 | 2013-05-03 23:15:20 +0000 | [diff] [blame] | 186 | comments::Lexer L(Allocator, Context.getDiagnostics(), |
| 187 | Context.getCommentCommandTraits(), |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 188 | Range.getBegin(), |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 189 | RawText.begin(), RawText.end()); |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 190 | comments::BriefParser P(L, Context.getCommentCommandTraits()); |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 191 | |
| 192 | const std::string Result = P.Parse(); |
| 193 | const unsigned BriefTextLength = Result.size(); |
| 194 | char *BriefTextPtr = new (Context) char[BriefTextLength + 1]; |
| 195 | memcpy(BriefTextPtr, Result.c_str(), BriefTextLength + 1); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 196 | BriefText = BriefTextPtr; |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 197 | BriefTextValid = true; |
| 198 | |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 199 | return BriefTextPtr; |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 202 | comments::FullComment *RawComment::parse(const ASTContext &Context, |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 203 | const Preprocessor *PP, |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 204 | const Decl *D) const { |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 205 | // Make sure that RawText is valid. |
| 206 | getRawText(Context.getSourceManager()); |
| 207 | |
Fariborz Jahanian | 5b63707 | 2013-05-03 23:15:20 +0000 | [diff] [blame] | 208 | comments::Lexer L(Context.getAllocator(), Context.getDiagnostics(), |
| 209 | Context.getCommentCommandTraits(), |
Dmitri Gribenko | 6bab911 | 2012-08-31 10:35:30 +0000 | [diff] [blame] | 210 | getSourceRange().getBegin(), |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 211 | RawText.begin(), RawText.end()); |
| 212 | comments::Sema S(Context.getAllocator(), Context.getSourceManager(), |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 213 | Context.getDiagnostics(), |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 214 | Context.getCommentCommandTraits(), |
| 215 | PP); |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 216 | S.setDecl(D); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 217 | comments::Parser P(L, S, Context.getAllocator(), Context.getSourceManager(), |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 218 | Context.getDiagnostics(), |
| 219 | Context.getCommentCommandTraits()); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 220 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 221 | return P.parseFullComment(); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Benjamin Kramer | 20c28be | 2013-09-28 15:06:27 +0000 | [diff] [blame] | 224 | static bool onlyWhitespaceBetween(SourceManager &SM, |
| 225 | SourceLocation Loc1, SourceLocation Loc2, |
| 226 | unsigned MaxNewlinesAllowed) { |
Dmitri Gribenko | 1d0f567 | 2012-09-09 20:47:31 +0000 | [diff] [blame] | 227 | std::pair<FileID, unsigned> Loc1Info = SM.getDecomposedLoc(Loc1); |
| 228 | std::pair<FileID, unsigned> Loc2Info = SM.getDecomposedLoc(Loc2); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 229 | |
Dmitri Gribenko | 1d0f567 | 2012-09-09 20:47:31 +0000 | [diff] [blame] | 230 | // Question does not make sense if locations are in different files. |
| 231 | if (Loc1Info.first != Loc2Info.first) |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 232 | return false; |
| 233 | |
| 234 | bool Invalid = false; |
Dmitri Gribenko | 1d0f567 | 2012-09-09 20:47:31 +0000 | [diff] [blame] | 235 | const char *Buffer = SM.getBufferData(Loc1Info.first, &Invalid).data(); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 236 | if (Invalid) |
| 237 | return false; |
| 238 | |
Benjamin Kramer | 20c28be | 2013-09-28 15:06:27 +0000 | [diff] [blame] | 239 | unsigned NumNewlines = 0; |
| 240 | assert(Loc1Info.second <= Loc2Info.second && "Loc1 after Loc2!"); |
| 241 | // Look for non-whitespace characters and remember any newlines seen. |
| 242 | for (unsigned I = Loc1Info.second; I != Loc2Info.second; ++I) { |
| 243 | switch (Buffer[I]) { |
| 244 | default: |
| 245 | return false; |
| 246 | case ' ': |
| 247 | case '\t': |
| 248 | case '\f': |
| 249 | case '\v': |
| 250 | break; |
| 251 | case '\r': |
| 252 | case '\n': |
| 253 | ++NumNewlines; |
| 254 | |
| 255 | // Check if we have found more than the maximum allowed number of |
| 256 | // newlines. |
| 257 | if (NumNewlines > MaxNewlinesAllowed) |
| 258 | return false; |
| 259 | |
| 260 | // Collapse \r\n and \n\r into a single newline. |
| 261 | if (I + 1 != Loc2Info.second && |
| 262 | (Buffer[I + 1] == '\n' || Buffer[I + 1] == '\r') && |
| 263 | Buffer[I] != Buffer[I + 1]) |
| 264 | ++I; |
| 265 | break; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return true; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 270 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 271 | |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 272 | void RawCommentList::addComment(const RawComment &RC, |
| 273 | llvm::BumpPtrAllocator &Allocator) { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 274 | if (RC.isInvalid()) |
| 275 | return; |
| 276 | |
Dmitri Gribenko | 9230740 | 2012-06-21 22:04:37 +0000 | [diff] [blame] | 277 | // Check if the comments are not in source order. |
| 278 | while (!Comments.empty() && |
Benjamin Kramer | 20c28be | 2013-09-28 15:06:27 +0000 | [diff] [blame] | 279 | !SourceMgr.isBeforeInTranslationUnit(Comments.back()->getLocStart(), |
| 280 | RC.getLocStart())) { |
Dmitri Gribenko | 9230740 | 2012-06-21 22:04:37 +0000 | [diff] [blame] | 281 | // If they are, just pop a few last comments that don't fit. |
| 282 | // This happens if an \#include directive contains comments. |
| 283 | Comments.pop_back(); |
| 284 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 285 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 286 | // Ordinary comments are not interesting for us. |
| 287 | if (RC.isOrdinary()) |
| 288 | return; |
| 289 | |
| 290 | // If this is the first Doxygen comment, save it (because there isn't |
| 291 | // anything to merge it with). |
| 292 | if (Comments.empty()) { |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 293 | Comments.push_back(new (Allocator) RawComment(RC)); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 297 | const RawComment &C1 = *Comments.back(); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 298 | const RawComment &C2 = RC; |
| 299 | |
| 300 | // Merge comments only if there is only whitespace between them. |
James Dennett | 2def1e8 | 2015-07-15 19:13:39 +0000 | [diff] [blame^] | 301 | // Can't merge trailing and non-trailing comments unless the second is |
| 302 | // non-trailing ordinary in the same column, as in the case: |
| 303 | // int x; // documents x |
| 304 | // // more text |
| 305 | // versus: |
| 306 | // int x; // documents x |
| 307 | // int y; // documents y |
| 308 | // or: |
| 309 | // int x; // documents x |
| 310 | // // documents y |
| 311 | // int y; |
Dmitri Gribenko | 557a8d5 | 2012-08-28 01:20:53 +0000 | [diff] [blame] | 312 | // Merge comments if they are on same or consecutive lines. |
James Dennett | 2def1e8 | 2015-07-15 19:13:39 +0000 | [diff] [blame^] | 313 | if ((C1.isTrailingComment() == C2.isTrailingComment() || |
| 314 | (C1.isTrailingComment() && !C2.isTrailingComment() && |
| 315 | isOrdinaryKind(C2.getKind()) && |
| 316 | commentsStartOnSameColumn(SourceMgr, C1, C2))) && |
Benjamin Kramer | 20c28be | 2013-09-28 15:06:27 +0000 | [diff] [blame] | 317 | onlyWhitespaceBetween(SourceMgr, C1.getLocEnd(), C2.getLocStart(), |
| 318 | /*MaxNewlinesAllowed=*/1)) { |
| 319 | SourceRange MergedRange(C1.getLocStart(), C2.getLocEnd()); |
| 320 | *Comments.back() = RawComment(SourceMgr, MergedRange, true, |
| 321 | RC.isParseAllComments()); |
| 322 | } else { |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 323 | Comments.push_back(new (Allocator) RawComment(RC)); |
Benjamin Kramer | 20c28be | 2013-09-28 15:06:27 +0000 | [diff] [blame] | 324 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 325 | } |
Dmitri Gribenko | 9ee0e30 | 2014-03-27 15:40:39 +0000 | [diff] [blame] | 326 | |
| 327 | void RawCommentList::addDeserializedComments(ArrayRef<RawComment *> DeserializedComments) { |
| 328 | std::vector<RawComment *> MergedComments; |
| 329 | MergedComments.reserve(Comments.size() + DeserializedComments.size()); |
| 330 | |
| 331 | std::merge(Comments.begin(), Comments.end(), |
| 332 | DeserializedComments.begin(), DeserializedComments.end(), |
| 333 | std::back_inserter(MergedComments), |
| 334 | BeforeThanCompare<RawComment>(SourceMgr)); |
| 335 | std::swap(Comments, MergedComments); |
| 336 | } |
| 337 | |