blob: 73a4d9def5c0f680d48687bebc1a00533ee622e5 [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"
James Dennett2def1e82015-07-15 19:13:39 +000018#include "clang/Basic/CharInfo.h"
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000019#include "llvm/ADT/STLExtras.h"
20
21using namespace clang;
22
23namespace {
24/// Get comment kind and bool describing if it is a trailing comment.
Dmitri Gribenkoe9585622013-04-26 20:12:49 +000025std::pair<RawComment::CommentKind, bool> getCommentKind(StringRef Comment,
26 bool ParseAllComments) {
27 const size_t MinCommentLength = ParseAllComments ? 2 : 3;
28 if ((Comment.size() < MinCommentLength) || Comment[0] != '/')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000029 return std::make_pair(RawComment::RCK_Invalid, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000030
31 RawComment::CommentKind K;
32 if (Comment[1] == '/') {
33 if (Comment.size() < 3)
Abramo Bagnarae06a8882012-07-04 07:30:26 +000034 return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000035
36 if (Comment[2] == '/')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000037 K = RawComment::RCK_BCPLSlash;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000038 else if (Comment[2] == '!')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000039 K = RawComment::RCK_BCPLExcl;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000040 else
Abramo Bagnarae06a8882012-07-04 07:30:26 +000041 return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000042 } else {
43 assert(Comment.size() >= 4);
44
45 // Comment lexer does not understand escapes in comment markers, so pretend
46 // that this is not a comment.
47 if (Comment[1] != '*' ||
48 Comment[Comment.size() - 2] != '*' ||
49 Comment[Comment.size() - 1] != '/')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000050 return std::make_pair(RawComment::RCK_Invalid, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000051
52 if (Comment[2] == '*')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000053 K = RawComment::RCK_JavaDoc;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000054 else if (Comment[2] == '!')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000055 K = RawComment::RCK_Qt;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000056 else
Abramo Bagnarae06a8882012-07-04 07:30:26 +000057 return std::make_pair(RawComment::RCK_OrdinaryC, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000058 }
59 const bool TrailingComment = (Comment.size() > 3) && (Comment[3] == '<');
60 return std::make_pair(K, TrailingComment);
61}
62
63bool mergedCommentIsTrailingComment(StringRef Comment) {
64 return (Comment.size() > 3) && (Comment[3] == '<');
65}
James Dennett2def1e82015-07-15 19:13:39 +000066
67/// Returns true if R1 and R2 both have valid locations that start on the same
68/// column.
69bool commentsStartOnSameColumn(const SourceManager &SM, const RawComment &R1,
70 const RawComment &R2) {
71 SourceLocation L1 = R1.getLocStart();
72 SourceLocation L2 = R2.getLocStart();
73 bool Invalid = false;
74 unsigned C1 = SM.getPresumedColumnNumber(L1, &Invalid);
75 if (!Invalid) {
76 unsigned C2 = SM.getPresumedColumnNumber(L2, &Invalid);
77 return !Invalid && (C1 == C2);
78 }
79 return false;
80}
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000081} // unnamed namespace
82
James Dennett2def1e82015-07-15 19:13:39 +000083/// \brief Determines whether there is only whitespace in `Buffer` between `P`
84/// and the previous line.
85/// \param Buffer The buffer to search in.
86/// \param P The offset from the beginning of `Buffer` to start from.
87/// \return true if all of the characters in `Buffer` ranging from the closest
88/// line-ending character before `P` (or the beginning of `Buffer`) to `P - 1`
89/// are whitespace.
90static bool onlyWhitespaceOnLineBefore(const char *Buffer, unsigned P) {
91 // Search backwards until we see linefeed or carriage return.
92 for (unsigned I = P; I != 0; --I) {
93 char C = Buffer[I - 1];
94 if (isVerticalWhitespace(C))
95 return true;
96 if (!isHorizontalWhitespace(C))
97 return false;
98 }
99 // We hit the beginning of the buffer.
100 return true;
101}
102
103/// Returns whether `K` is an ordinary comment kind.
104static bool isOrdinaryKind(RawComment::CommentKind K) {
105 return (K == RawComment::RCK_OrdinaryBCPL) ||
106 (K == RawComment::RCK_OrdinaryC);
107}
108
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000109RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR,
David L. Jones13d5a872018-03-02 00:07:45 +0000110 const CommentOptions &CommentOpts, bool Merged) :
Dmitri Gribenko60c7ec62012-06-27 05:48:36 +0000111 Range(SR), RawTextValid(false), BriefTextValid(false),
David L. Jones13d5a872018-03-02 00:07:45 +0000112 IsAttached(false), IsTrailingComment(false),
113 IsAlmostTrailingComment(false) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000114 // Extract raw comment text, if possible.
Dmitri Gribenkofecc2e02012-06-21 21:02:45 +0000115 if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) {
Abramo Bagnarae06a8882012-07-04 07:30:26 +0000116 Kind = RCK_Invalid;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000117 return;
118 }
119
James Dennett2def1e82015-07-15 19:13:39 +0000120 // Guess comment kind.
David L. Jones13d5a872018-03-02 00:07:45 +0000121 std::pair<CommentKind, bool> K =
122 getCommentKind(RawText, CommentOpts.ParseAllComments);
James Dennett2def1e82015-07-15 19:13:39 +0000123
124 // Guess whether an ordinary comment is trailing.
David L. Jones13d5a872018-03-02 00:07:45 +0000125 if (CommentOpts.ParseAllComments && isOrdinaryKind(K.first)) {
James Dennett2def1e82015-07-15 19:13:39 +0000126 FileID BeginFileID;
127 unsigned BeginOffset;
128 std::tie(BeginFileID, BeginOffset) =
129 SourceMgr.getDecomposedLoc(Range.getBegin());
130 if (BeginOffset != 0) {
131 bool Invalid = false;
132 const char *Buffer =
133 SourceMgr.getBufferData(BeginFileID, &Invalid).data();
134 IsTrailingComment |=
135 (!Invalid && !onlyWhitespaceOnLineBefore(Buffer, BeginOffset));
136 }
137 }
138
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000139 if (!Merged) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000140 Kind = K.first;
James Dennett2def1e82015-07-15 19:13:39 +0000141 IsTrailingComment |= K.second;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000142
143 IsAlmostTrailingComment = RawText.startswith("//<") ||
144 RawText.startswith("/*<");
145 } else {
Abramo Bagnarae06a8882012-07-04 07:30:26 +0000146 Kind = RCK_Merged;
James Dennett2def1e82015-07-15 19:13:39 +0000147 IsTrailingComment =
148 IsTrailingComment || mergedCommentIsTrailingComment(RawText);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000149 }
150}
151
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000152StringRef RawComment::getRawTextSlow(const SourceManager &SourceMgr) const {
153 FileID BeginFileID;
154 FileID EndFileID;
155 unsigned BeginOffset;
156 unsigned EndOffset;
157
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000158 std::tie(BeginFileID, BeginOffset) =
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000159 SourceMgr.getDecomposedLoc(Range.getBegin());
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000160 std::tie(EndFileID, EndOffset) = SourceMgr.getDecomposedLoc(Range.getEnd());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000161
162 const unsigned Length = EndOffset - BeginOffset;
163 if (Length < 2)
164 return StringRef();
165
166 // The comment can't begin in one file and end in another.
167 assert(BeginFileID == EndFileID);
168
169 bool Invalid = false;
170 const char *BufferStart = SourceMgr.getBufferData(BeginFileID,
171 &Invalid).data();
172 if (Invalid)
173 return StringRef();
174
175 return StringRef(BufferStart + BeginOffset, Length);
176}
177
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000178const char *RawComment::extractBriefText(const ASTContext &Context) const {
Justin Bogner14994132016-10-16 20:12:42 +0000179 // Lazily initialize RawText using the accessor before using it.
180 (void)getRawText(Context.getSourceManager());
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000181
Dmitri Gribenko4586df72012-07-27 20:37:06 +0000182 // Since we will be copying the resulting text, all allocations made during
183 // parsing are garbage after resulting string is formed. Thus we can use
184 // a separate allocator for all temporary stuff.
185 llvm::BumpPtrAllocator Allocator;
186
Fariborz Jahanian5b637072013-05-03 23:15:20 +0000187 comments::Lexer L(Allocator, Context.getDiagnostics(),
188 Context.getCommentCommandTraits(),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000189 Range.getBegin(),
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000190 RawText.begin(), RawText.end());
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000191 comments::BriefParser P(L, Context.getCommentCommandTraits());
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000192
193 const std::string Result = P.Parse();
194 const unsigned BriefTextLength = Result.size();
195 char *BriefTextPtr = new (Context) char[BriefTextLength + 1];
196 memcpy(BriefTextPtr, Result.c_str(), BriefTextLength + 1);
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000197 BriefText = BriefTextPtr;
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000198 BriefTextValid = true;
199
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000200 return BriefTextPtr;
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000201}
202
Dmitri Gribenkob2610882012-08-14 17:17:18 +0000203comments::FullComment *RawComment::parse(const ASTContext &Context,
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000204 const Preprocessor *PP,
Dmitri Gribenkob2610882012-08-14 17:17:18 +0000205 const Decl *D) const {
Justin Bogner14994132016-10-16 20:12:42 +0000206 // Lazily initialize RawText using the accessor before using it.
207 (void)getRawText(Context.getSourceManager());
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000208
Fariborz Jahanian5b637072013-05-03 23:15:20 +0000209 comments::Lexer L(Context.getAllocator(), Context.getDiagnostics(),
210 Context.getCommentCommandTraits(),
Dmitri Gribenko6bab9112012-08-31 10:35:30 +0000211 getSourceRange().getBegin(),
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000212 RawText.begin(), RawText.end());
213 comments::Sema S(Context.getAllocator(), Context.getSourceManager(),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000214 Context.getDiagnostics(),
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000215 Context.getCommentCommandTraits(),
216 PP);
Dmitri Gribenkob2610882012-08-14 17:17:18 +0000217 S.setDecl(D);
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000218 comments::Parser P(L, S, Context.getAllocator(), Context.getSourceManager(),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000219 Context.getDiagnostics(),
220 Context.getCommentCommandTraits());
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000221
Dmitri Gribenkob2610882012-08-14 17:17:18 +0000222 return P.parseFullComment();
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000223}
224
Benjamin Kramer20c28be2013-09-28 15:06:27 +0000225static bool onlyWhitespaceBetween(SourceManager &SM,
226 SourceLocation Loc1, SourceLocation Loc2,
227 unsigned MaxNewlinesAllowed) {
Dmitri Gribenko1d0f5672012-09-09 20:47:31 +0000228 std::pair<FileID, unsigned> Loc1Info = SM.getDecomposedLoc(Loc1);
229 std::pair<FileID, unsigned> Loc2Info = SM.getDecomposedLoc(Loc2);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000230
Dmitri Gribenko1d0f5672012-09-09 20:47:31 +0000231 // Question does not make sense if locations are in different files.
232 if (Loc1Info.first != Loc2Info.first)
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000233 return false;
234
235 bool Invalid = false;
Dmitri Gribenko1d0f5672012-09-09 20:47:31 +0000236 const char *Buffer = SM.getBufferData(Loc1Info.first, &Invalid).data();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000237 if (Invalid)
238 return false;
239
Benjamin Kramer20c28be2013-09-28 15:06:27 +0000240 unsigned NumNewlines = 0;
241 assert(Loc1Info.second <= Loc2Info.second && "Loc1 after Loc2!");
242 // Look for non-whitespace characters and remember any newlines seen.
243 for (unsigned I = Loc1Info.second; I != Loc2Info.second; ++I) {
244 switch (Buffer[I]) {
245 default:
246 return false;
247 case ' ':
248 case '\t':
249 case '\f':
250 case '\v':
251 break;
252 case '\r':
253 case '\n':
254 ++NumNewlines;
255
256 // Check if we have found more than the maximum allowed number of
257 // newlines.
258 if (NumNewlines > MaxNewlinesAllowed)
259 return false;
260
261 // Collapse \r\n and \n\r into a single newline.
262 if (I + 1 != Loc2Info.second &&
263 (Buffer[I + 1] == '\n' || Buffer[I + 1] == '\r') &&
264 Buffer[I] != Buffer[I + 1])
265 ++I;
266 break;
267 }
268 }
269
270 return true;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000271}
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000272
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000273void RawCommentList::addComment(const RawComment &RC,
David L. Jones13d5a872018-03-02 00:07:45 +0000274 const CommentOptions &CommentOpts,
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000275 llvm::BumpPtrAllocator &Allocator) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000276 if (RC.isInvalid())
277 return;
278
Dmitri Gribenko92307402012-06-21 22:04:37 +0000279 // Check if the comments are not in source order.
280 while (!Comments.empty() &&
Benjamin Kramer20c28be2013-09-28 15:06:27 +0000281 !SourceMgr.isBeforeInTranslationUnit(Comments.back()->getLocStart(),
282 RC.getLocStart())) {
Dmitri Gribenko92307402012-06-21 22:04:37 +0000283 // If they are, just pop a few last comments that don't fit.
284 // This happens if an \#include directive contains comments.
285 Comments.pop_back();
286 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000287
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000288 // Ordinary comments are not interesting for us.
David L. Jones13d5a872018-03-02 00:07:45 +0000289 if (RC.isOrdinary() && !CommentOpts.ParseAllComments)
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000290 return;
291
292 // If this is the first Doxygen comment, save it (because there isn't
293 // anything to merge it with).
294 if (Comments.empty()) {
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000295 Comments.push_back(new (Allocator) RawComment(RC));
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000296 return;
297 }
298
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000299 const RawComment &C1 = *Comments.back();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000300 const RawComment &C2 = RC;
301
302 // Merge comments only if there is only whitespace between them.
James Dennett2def1e82015-07-15 19:13:39 +0000303 // Can't merge trailing and non-trailing comments unless the second is
304 // non-trailing ordinary in the same column, as in the case:
305 // int x; // documents x
306 // // more text
307 // versus:
308 // int x; // documents x
309 // int y; // documents y
310 // or:
311 // int x; // documents x
312 // // documents y
313 // int y;
Dmitri Gribenko557a8d52012-08-28 01:20:53 +0000314 // Merge comments if they are on same or consecutive lines.
James Dennett2def1e82015-07-15 19:13:39 +0000315 if ((C1.isTrailingComment() == C2.isTrailingComment() ||
316 (C1.isTrailingComment() && !C2.isTrailingComment() &&
317 isOrdinaryKind(C2.getKind()) &&
318 commentsStartOnSameColumn(SourceMgr, C1, C2))) &&
Benjamin Kramer20c28be2013-09-28 15:06:27 +0000319 onlyWhitespaceBetween(SourceMgr, C1.getLocEnd(), C2.getLocStart(),
320 /*MaxNewlinesAllowed=*/1)) {
321 SourceRange MergedRange(C1.getLocStart(), C2.getLocEnd());
David L. Jones13d5a872018-03-02 00:07:45 +0000322 *Comments.back() = RawComment(SourceMgr, MergedRange, CommentOpts, true);
Benjamin Kramer20c28be2013-09-28 15:06:27 +0000323 } else {
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000324 Comments.push_back(new (Allocator) RawComment(RC));
Benjamin Kramer20c28be2013-09-28 15:06:27 +0000325 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000326}
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +0000327
328void RawCommentList::addDeserializedComments(ArrayRef<RawComment *> DeserializedComments) {
329 std::vector<RawComment *> MergedComments;
330 MergedComments.reserve(Comments.size() + DeserializedComments.size());
331
332 std::merge(Comments.begin(), Comments.end(),
333 DeserializedComments.begin(), DeserializedComments.end(),
334 std::back_inserter(MergedComments),
335 BeforeThanCompare<RawComment>(SourceMgr));
336 std::swap(Comments, MergedComments);
337}