blob: 24b129a5c532563b4d8f7fb53f17250641adff37 [file] [log] [blame]
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00001//===--- 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 Carruth39a3e752012-06-20 09:53:52 +000010#include "clang/AST/RawCommentList.h"
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +000011#include "clang/AST/ASTContext.h"
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +000012#include "clang/AST/Comment.h"
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +000013#include "clang/AST/CommentBriefParser.h"
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000014#include "clang/AST/CommentCommandTraits.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/AST/CommentLexer.h"
16#include "clang/AST/CommentParser.h"
17#include "clang/AST/CommentSema.h"
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000018#include "llvm/ADT/STLExtras.h"
19
20using namespace clang;
21
22namespace {
23/// Get comment kind and bool describing if it is a trailing comment.
Dmitri Gribenkoe9585622013-04-26 20:12:49 +000024std::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 Bagnarae06a8882012-07-04 07:30:26 +000028 return std::make_pair(RawComment::RCK_Invalid, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000029
30 RawComment::CommentKind K;
31 if (Comment[1] == '/') {
32 if (Comment.size() < 3)
Abramo Bagnarae06a8882012-07-04 07:30:26 +000033 return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000034
35 if (Comment[2] == '/')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000036 K = RawComment::RCK_BCPLSlash;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000037 else if (Comment[2] == '!')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000038 K = RawComment::RCK_BCPLExcl;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000039 else
Abramo Bagnarae06a8882012-07-04 07:30:26 +000040 return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000041 } 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 Bagnarae06a8882012-07-04 07:30:26 +000049 return std::make_pair(RawComment::RCK_Invalid, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000050
51 if (Comment[2] == '*')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000052 K = RawComment::RCK_JavaDoc;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000053 else if (Comment[2] == '!')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000054 K = RawComment::RCK_Qt;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000055 else
Abramo Bagnarae06a8882012-07-04 07:30:26 +000056 return std::make_pair(RawComment::RCK_OrdinaryC, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000057 }
58 const bool TrailingComment = (Comment.size() > 3) && (Comment[3] == '<');
59 return std::make_pair(K, TrailingComment);
60}
61
62bool mergedCommentIsTrailingComment(StringRef Comment) {
63 return (Comment.size() > 3) && (Comment[3] == '<');
64}
65} // unnamed namespace
66
67RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR,
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +000068 bool Merged, bool ParseAllComments) :
Dmitri Gribenko60c7ec62012-06-27 05:48:36 +000069 Range(SR), RawTextValid(false), BriefTextValid(false),
Dmitri Gribenkob2610882012-08-14 17:17:18 +000070 IsAttached(false), IsAlmostTrailingComment(false),
Benjamin Kramer20c28be2013-09-28 15:06:27 +000071 ParseAllComments(ParseAllComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000072 // Extract raw comment text, if possible.
Dmitri Gribenkofecc2e02012-06-21 21:02:45 +000073 if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) {
Abramo Bagnarae06a8882012-07-04 07:30:26 +000074 Kind = RCK_Invalid;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000075 return;
76 }
77
78 if (!Merged) {
79 // Guess comment kind.
Dmitri Gribenkoe9585622013-04-26 20:12:49 +000080 std::pair<CommentKind, bool> K = getCommentKind(RawText, ParseAllComments);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000081 Kind = K.first;
82 IsTrailingComment = K.second;
83
84 IsAlmostTrailingComment = RawText.startswith("//<") ||
85 RawText.startswith("/*<");
86 } else {
Abramo Bagnarae06a8882012-07-04 07:30:26 +000087 Kind = RCK_Merged;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000088 IsTrailingComment = mergedCommentIsTrailingComment(RawText);
89 }
90}
91
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000092StringRef RawComment::getRawTextSlow(const SourceManager &SourceMgr) const {
93 FileID BeginFileID;
94 FileID EndFileID;
95 unsigned BeginOffset;
96 unsigned EndOffset;
97
Benjamin Kramer867ea1d2014-03-02 13:01:17 +000098 std::tie(BeginFileID, BeginOffset) =
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000099 SourceMgr.getDecomposedLoc(Range.getBegin());
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000100 std::tie(EndFileID, EndOffset) = SourceMgr.getDecomposedLoc(Range.getEnd());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000101
102 const unsigned Length = EndOffset - BeginOffset;
103 if (Length < 2)
104 return StringRef();
105
106 // The comment can't begin in one file and end in another.
107 assert(BeginFileID == EndFileID);
108
109 bool Invalid = false;
110 const char *BufferStart = SourceMgr.getBufferData(BeginFileID,
111 &Invalid).data();
112 if (Invalid)
113 return StringRef();
114
115 return StringRef(BufferStart + BeginOffset, Length);
116}
117
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000118const char *RawComment::extractBriefText(const ASTContext &Context) const {
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000119 // Make sure that RawText is valid.
120 getRawText(Context.getSourceManager());
121
Dmitri Gribenko4586df72012-07-27 20:37:06 +0000122 // Since we will be copying the resulting text, all allocations made during
123 // parsing are garbage after resulting string is formed. Thus we can use
124 // a separate allocator for all temporary stuff.
125 llvm::BumpPtrAllocator Allocator;
126
Fariborz Jahanian5b637072013-05-03 23:15:20 +0000127 comments::Lexer L(Allocator, Context.getDiagnostics(),
128 Context.getCommentCommandTraits(),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000129 Range.getBegin(),
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000130 RawText.begin(), RawText.end());
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000131 comments::BriefParser P(L, Context.getCommentCommandTraits());
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000132
133 const std::string Result = P.Parse();
134 const unsigned BriefTextLength = Result.size();
135 char *BriefTextPtr = new (Context) char[BriefTextLength + 1];
136 memcpy(BriefTextPtr, Result.c_str(), BriefTextLength + 1);
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000137 BriefText = BriefTextPtr;
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000138 BriefTextValid = true;
139
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000140 return BriefTextPtr;
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000141}
142
Dmitri Gribenkob2610882012-08-14 17:17:18 +0000143comments::FullComment *RawComment::parse(const ASTContext &Context,
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000144 const Preprocessor *PP,
Dmitri Gribenkob2610882012-08-14 17:17:18 +0000145 const Decl *D) const {
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000146 // Make sure that RawText is valid.
147 getRawText(Context.getSourceManager());
148
Fariborz Jahanian5b637072013-05-03 23:15:20 +0000149 comments::Lexer L(Context.getAllocator(), Context.getDiagnostics(),
150 Context.getCommentCommandTraits(),
Dmitri Gribenko6bab9112012-08-31 10:35:30 +0000151 getSourceRange().getBegin(),
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000152 RawText.begin(), RawText.end());
153 comments::Sema S(Context.getAllocator(), Context.getSourceManager(),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000154 Context.getDiagnostics(),
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000155 Context.getCommentCommandTraits(),
156 PP);
Dmitri Gribenkob2610882012-08-14 17:17:18 +0000157 S.setDecl(D);
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000158 comments::Parser P(L, S, Context.getAllocator(), Context.getSourceManager(),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000159 Context.getDiagnostics(),
160 Context.getCommentCommandTraits());
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000161
Dmitri Gribenkob2610882012-08-14 17:17:18 +0000162 return P.parseFullComment();
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000163}
164
Benjamin Kramer20c28be2013-09-28 15:06:27 +0000165static bool onlyWhitespaceBetween(SourceManager &SM,
166 SourceLocation Loc1, SourceLocation Loc2,
167 unsigned MaxNewlinesAllowed) {
Dmitri Gribenko1d0f5672012-09-09 20:47:31 +0000168 std::pair<FileID, unsigned> Loc1Info = SM.getDecomposedLoc(Loc1);
169 std::pair<FileID, unsigned> Loc2Info = SM.getDecomposedLoc(Loc2);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000170
Dmitri Gribenko1d0f5672012-09-09 20:47:31 +0000171 // Question does not make sense if locations are in different files.
172 if (Loc1Info.first != Loc2Info.first)
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000173 return false;
174
175 bool Invalid = false;
Dmitri Gribenko1d0f5672012-09-09 20:47:31 +0000176 const char *Buffer = SM.getBufferData(Loc1Info.first, &Invalid).data();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000177 if (Invalid)
178 return false;
179
Benjamin Kramer20c28be2013-09-28 15:06:27 +0000180 unsigned NumNewlines = 0;
181 assert(Loc1Info.second <= Loc2Info.second && "Loc1 after Loc2!");
182 // Look for non-whitespace characters and remember any newlines seen.
183 for (unsigned I = Loc1Info.second; I != Loc2Info.second; ++I) {
184 switch (Buffer[I]) {
185 default:
186 return false;
187 case ' ':
188 case '\t':
189 case '\f':
190 case '\v':
191 break;
192 case '\r':
193 case '\n':
194 ++NumNewlines;
195
196 // Check if we have found more than the maximum allowed number of
197 // newlines.
198 if (NumNewlines > MaxNewlinesAllowed)
199 return false;
200
201 // Collapse \r\n and \n\r into a single newline.
202 if (I + 1 != Loc2Info.second &&
203 (Buffer[I + 1] == '\n' || Buffer[I + 1] == '\r') &&
204 Buffer[I] != Buffer[I + 1])
205 ++I;
206 break;
207 }
208 }
209
210 return true;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000211}
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000212
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000213void RawCommentList::addComment(const RawComment &RC,
214 llvm::BumpPtrAllocator &Allocator) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000215 if (RC.isInvalid())
216 return;
217
Dmitri Gribenko92307402012-06-21 22:04:37 +0000218 // Check if the comments are not in source order.
219 while (!Comments.empty() &&
Benjamin Kramer20c28be2013-09-28 15:06:27 +0000220 !SourceMgr.isBeforeInTranslationUnit(Comments.back()->getLocStart(),
221 RC.getLocStart())) {
Dmitri Gribenko92307402012-06-21 22:04:37 +0000222 // If they are, just pop a few last comments that don't fit.
223 // This happens if an \#include directive contains comments.
224 Comments.pop_back();
225 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000226
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000227 // Ordinary comments are not interesting for us.
228 if (RC.isOrdinary())
229 return;
230
231 // If this is the first Doxygen comment, save it (because there isn't
232 // anything to merge it with).
233 if (Comments.empty()) {
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000234 Comments.push_back(new (Allocator) RawComment(RC));
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000235 return;
236 }
237
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000238 const RawComment &C1 = *Comments.back();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000239 const RawComment &C2 = RC;
240
241 // Merge comments only if there is only whitespace between them.
242 // Can't merge trailing and non-trailing comments.
Dmitri Gribenko557a8d52012-08-28 01:20:53 +0000243 // Merge comments if they are on same or consecutive lines.
Benjamin Kramer20c28be2013-09-28 15:06:27 +0000244 if (C1.isTrailingComment() == C2.isTrailingComment() &&
245 onlyWhitespaceBetween(SourceMgr, C1.getLocEnd(), C2.getLocStart(),
246 /*MaxNewlinesAllowed=*/1)) {
247 SourceRange MergedRange(C1.getLocStart(), C2.getLocEnd());
248 *Comments.back() = RawComment(SourceMgr, MergedRange, true,
249 RC.isParseAllComments());
250 } else {
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000251 Comments.push_back(new (Allocator) RawComment(RC));
Benjamin Kramer20c28be2013-09-28 15:06:27 +0000252 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000253}
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +0000254
255void RawCommentList::addDeserializedComments(ArrayRef<RawComment *> DeserializedComments) {
256 std::vector<RawComment *> MergedComments;
257 MergedComments.reserve(Comments.size() + DeserializedComments.size());
258
259 std::merge(Comments.begin(), Comments.end(),
260 DeserializedComments.begin(), DeserializedComments.end(),
261 std::back_inserter(MergedComments),
262 BeforeThanCompare<RawComment>(SourceMgr));
263 std::swap(Comments, MergedComments);
264}
265