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