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