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" |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | namespace { |
| 23 | /// Get comment kind and bool describing if it is a trailing comment. |
Dmitri Gribenko | e958562 | 2013-04-26 20:12:49 +0000 | [diff] [blame] | 24 | std::pair<RawComment::CommentKind, bool> getCommentKind(StringRef Comment, |
| 25 | bool ParseAllComments) { |
| 26 | const size_t MinCommentLength = ParseAllComments ? 2 : 3; |
| 27 | if ((Comment.size() < MinCommentLength) || Comment[0] != '/') |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 28 | return std::make_pair(RawComment::RCK_Invalid, false); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 29 | |
| 30 | RawComment::CommentKind K; |
| 31 | if (Comment[1] == '/') { |
| 32 | if (Comment.size() < 3) |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 33 | return std::make_pair(RawComment::RCK_OrdinaryBCPL, false); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 34 | |
| 35 | if (Comment[2] == '/') |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 36 | K = RawComment::RCK_BCPLSlash; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 37 | else if (Comment[2] == '!') |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 38 | K = RawComment::RCK_BCPLExcl; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 39 | else |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 40 | return std::make_pair(RawComment::RCK_OrdinaryBCPL, false); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 41 | } else { |
| 42 | assert(Comment.size() >= 4); |
| 43 | |
| 44 | // Comment lexer does not understand escapes in comment markers, so pretend |
| 45 | // that this is not a comment. |
| 46 | if (Comment[1] != '*' || |
| 47 | Comment[Comment.size() - 2] != '*' || |
| 48 | Comment[Comment.size() - 1] != '/') |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 49 | return std::make_pair(RawComment::RCK_Invalid, false); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 50 | |
| 51 | if (Comment[2] == '*') |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 52 | K = RawComment::RCK_JavaDoc; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 53 | else if (Comment[2] == '!') |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 54 | K = RawComment::RCK_Qt; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 55 | else |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 56 | return std::make_pair(RawComment::RCK_OrdinaryC, false); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 57 | } |
| 58 | const bool TrailingComment = (Comment.size() > 3) && (Comment[3] == '<'); |
| 59 | return std::make_pair(K, TrailingComment); |
| 60 | } |
| 61 | |
| 62 | bool mergedCommentIsTrailingComment(StringRef Comment) { |
| 63 | return (Comment.size() > 3) && (Comment[3] == '<'); |
| 64 | } |
| 65 | } // unnamed namespace |
| 66 | |
| 67 | RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR, |
Dmitri Gribenko | a7d16ce | 2013-04-10 15:35:17 +0000 | [diff] [blame] | 68 | bool Merged, bool ParseAllComments) : |
Dmitri Gribenko | 60c7ec6 | 2012-06-27 05:48:36 +0000 | [diff] [blame] | 69 | Range(SR), RawTextValid(false), BriefTextValid(false), |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 70 | IsAttached(false), IsAlmostTrailingComment(false), |
Benjamin Kramer | 20c28be | 2013-09-28 15:06:27 +0000 | [diff] [blame^] | 71 | ParseAllComments(ParseAllComments) { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 72 | // Extract raw comment text, if possible. |
Dmitri Gribenko | fecc2e0 | 2012-06-21 21:02:45 +0000 | [diff] [blame] | 73 | if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) { |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 74 | Kind = RCK_Invalid; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | |
| 78 | if (!Merged) { |
| 79 | // Guess comment kind. |
Dmitri Gribenko | e958562 | 2013-04-26 20:12:49 +0000 | [diff] [blame] | 80 | std::pair<CommentKind, bool> K = getCommentKind(RawText, ParseAllComments); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 81 | Kind = K.first; |
| 82 | IsTrailingComment = K.second; |
| 83 | |
| 84 | IsAlmostTrailingComment = RawText.startswith("//<") || |
| 85 | RawText.startswith("/*<"); |
| 86 | } else { |
Abramo Bagnara | e06a888 | 2012-07-04 07:30:26 +0000 | [diff] [blame] | 87 | Kind = RCK_Merged; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 88 | IsTrailingComment = mergedCommentIsTrailingComment(RawText); |
| 89 | } |
| 90 | } |
| 91 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 92 | StringRef RawComment::getRawTextSlow(const SourceManager &SourceMgr) const { |
| 93 | FileID BeginFileID; |
| 94 | FileID EndFileID; |
| 95 | unsigned BeginOffset; |
| 96 | unsigned EndOffset; |
| 97 | |
| 98 | llvm::tie(BeginFileID, BeginOffset) = |
| 99 | SourceMgr.getDecomposedLoc(Range.getBegin()); |
| 100 | llvm::tie(EndFileID, EndOffset) = |
| 101 | SourceMgr.getDecomposedLoc(Range.getEnd()); |
| 102 | |
| 103 | const unsigned Length = EndOffset - BeginOffset; |
| 104 | if (Length < 2) |
| 105 | return StringRef(); |
| 106 | |
| 107 | // The comment can't begin in one file and end in another. |
| 108 | assert(BeginFileID == EndFileID); |
| 109 | |
| 110 | bool Invalid = false; |
| 111 | const char *BufferStart = SourceMgr.getBufferData(BeginFileID, |
| 112 | &Invalid).data(); |
| 113 | if (Invalid) |
| 114 | return StringRef(); |
| 115 | |
| 116 | return StringRef(BufferStart + BeginOffset, Length); |
| 117 | } |
| 118 | |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 119 | const char *RawComment::extractBriefText(const ASTContext &Context) const { |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 120 | // Make sure that RawText is valid. |
| 121 | getRawText(Context.getSourceManager()); |
| 122 | |
Dmitri Gribenko | 4586df7 | 2012-07-27 20:37:06 +0000 | [diff] [blame] | 123 | // Since we will be copying the resulting text, all allocations made during |
| 124 | // parsing are garbage after resulting string is formed. Thus we can use |
| 125 | // a separate allocator for all temporary stuff. |
| 126 | llvm::BumpPtrAllocator Allocator; |
| 127 | |
Fariborz Jahanian | 5b63707 | 2013-05-03 23:15:20 +0000 | [diff] [blame] | 128 | comments::Lexer L(Allocator, Context.getDiagnostics(), |
| 129 | Context.getCommentCommandTraits(), |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 130 | Range.getBegin(), |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 131 | RawText.begin(), RawText.end()); |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 132 | comments::BriefParser P(L, Context.getCommentCommandTraits()); |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 133 | |
| 134 | const std::string Result = P.Parse(); |
| 135 | const unsigned BriefTextLength = Result.size(); |
| 136 | char *BriefTextPtr = new (Context) char[BriefTextLength + 1]; |
| 137 | memcpy(BriefTextPtr, Result.c_str(), BriefTextLength + 1); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 138 | BriefText = BriefTextPtr; |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 139 | BriefTextValid = true; |
| 140 | |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 141 | return BriefTextPtr; |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 144 | comments::FullComment *RawComment::parse(const ASTContext &Context, |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 145 | const Preprocessor *PP, |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 146 | const Decl *D) const { |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 147 | // Make sure that RawText is valid. |
| 148 | getRawText(Context.getSourceManager()); |
| 149 | |
Fariborz Jahanian | 5b63707 | 2013-05-03 23:15:20 +0000 | [diff] [blame] | 150 | comments::Lexer L(Context.getAllocator(), Context.getDiagnostics(), |
| 151 | Context.getCommentCommandTraits(), |
Dmitri Gribenko | 6bab911 | 2012-08-31 10:35:30 +0000 | [diff] [blame] | 152 | getSourceRange().getBegin(), |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 153 | RawText.begin(), RawText.end()); |
| 154 | comments::Sema S(Context.getAllocator(), Context.getSourceManager(), |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 155 | Context.getDiagnostics(), |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 156 | Context.getCommentCommandTraits(), |
| 157 | PP); |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 158 | S.setDecl(D); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 159 | comments::Parser P(L, S, Context.getAllocator(), Context.getSourceManager(), |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 160 | Context.getDiagnostics(), |
| 161 | Context.getCommentCommandTraits()); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 162 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 163 | return P.parseFullComment(); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Benjamin Kramer | 20c28be | 2013-09-28 15:06:27 +0000 | [diff] [blame^] | 166 | static bool onlyWhitespaceBetween(SourceManager &SM, |
| 167 | SourceLocation Loc1, SourceLocation Loc2, |
| 168 | unsigned MaxNewlinesAllowed) { |
Dmitri Gribenko | 1d0f567 | 2012-09-09 20:47:31 +0000 | [diff] [blame] | 169 | std::pair<FileID, unsigned> Loc1Info = SM.getDecomposedLoc(Loc1); |
| 170 | std::pair<FileID, unsigned> Loc2Info = SM.getDecomposedLoc(Loc2); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 171 | |
Dmitri Gribenko | 1d0f567 | 2012-09-09 20:47:31 +0000 | [diff] [blame] | 172 | // Question does not make sense if locations are in different files. |
| 173 | if (Loc1Info.first != Loc2Info.first) |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 174 | return false; |
| 175 | |
| 176 | bool Invalid = false; |
Dmitri Gribenko | 1d0f567 | 2012-09-09 20:47:31 +0000 | [diff] [blame] | 177 | const char *Buffer = SM.getBufferData(Loc1Info.first, &Invalid).data(); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 178 | if (Invalid) |
| 179 | return false; |
| 180 | |
Benjamin Kramer | 20c28be | 2013-09-28 15:06:27 +0000 | [diff] [blame^] | 181 | unsigned NumNewlines = 0; |
| 182 | assert(Loc1Info.second <= Loc2Info.second && "Loc1 after Loc2!"); |
| 183 | // Look for non-whitespace characters and remember any newlines seen. |
| 184 | for (unsigned I = Loc1Info.second; I != Loc2Info.second; ++I) { |
| 185 | switch (Buffer[I]) { |
| 186 | default: |
| 187 | return false; |
| 188 | case ' ': |
| 189 | case '\t': |
| 190 | case '\f': |
| 191 | case '\v': |
| 192 | break; |
| 193 | case '\r': |
| 194 | case '\n': |
| 195 | ++NumNewlines; |
| 196 | |
| 197 | // Check if we have found more than the maximum allowed number of |
| 198 | // newlines. |
| 199 | if (NumNewlines > MaxNewlinesAllowed) |
| 200 | return false; |
| 201 | |
| 202 | // Collapse \r\n and \n\r into a single newline. |
| 203 | if (I + 1 != Loc2Info.second && |
| 204 | (Buffer[I + 1] == '\n' || Buffer[I + 1] == '\r') && |
| 205 | Buffer[I] != Buffer[I + 1]) |
| 206 | ++I; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return true; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 212 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 213 | |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 214 | void RawCommentList::addComment(const RawComment &RC, |
| 215 | llvm::BumpPtrAllocator &Allocator) { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 216 | if (RC.isInvalid()) |
| 217 | return; |
| 218 | |
Dmitri Gribenko | 9230740 | 2012-06-21 22:04:37 +0000 | [diff] [blame] | 219 | // Check if the comments are not in source order. |
| 220 | while (!Comments.empty() && |
Benjamin Kramer | 20c28be | 2013-09-28 15:06:27 +0000 | [diff] [blame^] | 221 | !SourceMgr.isBeforeInTranslationUnit(Comments.back()->getLocStart(), |
| 222 | RC.getLocStart())) { |
Dmitri Gribenko | 9230740 | 2012-06-21 22:04:37 +0000 | [diff] [blame] | 223 | // If they are, just pop a few last comments that don't fit. |
| 224 | // This happens if an \#include directive contains comments. |
| 225 | Comments.pop_back(); |
| 226 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 227 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 228 | // Ordinary comments are not interesting for us. |
| 229 | if (RC.isOrdinary()) |
| 230 | return; |
| 231 | |
| 232 | // If this is the first Doxygen comment, save it (because there isn't |
| 233 | // anything to merge it with). |
| 234 | if (Comments.empty()) { |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 235 | Comments.push_back(new (Allocator) RawComment(RC)); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 239 | const RawComment &C1 = *Comments.back(); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 240 | const RawComment &C2 = RC; |
| 241 | |
| 242 | // Merge comments only if there is only whitespace between them. |
| 243 | // Can't merge trailing and non-trailing comments. |
Dmitri Gribenko | 557a8d5 | 2012-08-28 01:20:53 +0000 | [diff] [blame] | 244 | // Merge comments if they are on same or consecutive lines. |
Benjamin Kramer | 20c28be | 2013-09-28 15:06:27 +0000 | [diff] [blame^] | 245 | if (C1.isTrailingComment() == C2.isTrailingComment() && |
| 246 | onlyWhitespaceBetween(SourceMgr, C1.getLocEnd(), C2.getLocStart(), |
| 247 | /*MaxNewlinesAllowed=*/1)) { |
| 248 | SourceRange MergedRange(C1.getLocStart(), C2.getLocEnd()); |
| 249 | *Comments.back() = RawComment(SourceMgr, MergedRange, true, |
| 250 | RC.isParseAllComments()); |
| 251 | } else { |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 252 | Comments.push_back(new (Allocator) RawComment(RC)); |
Benjamin Kramer | 20c28be | 2013-09-28 15:06:27 +0000 | [diff] [blame^] | 253 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 254 | } |