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