blob: 92b96dc8e5a159220c24a3be347f57d27e92ef25 [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),
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +000071 ParseAllComments(ParseAllComments),
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000072 BeginLineValid(false), EndLineValid(false) {
73 // Extract raw comment text, if possible.
Dmitri Gribenkofecc2e02012-06-21 21:02:45 +000074 if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) {
Abramo Bagnarae06a8882012-07-04 07:30:26 +000075 Kind = RCK_Invalid;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000076 return;
77 }
78
79 if (!Merged) {
80 // Guess comment kind.
Dmitri Gribenkoe9585622013-04-26 20:12:49 +000081 std::pair<CommentKind, bool> K = getCommentKind(RawText, ParseAllComments);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000082 Kind = K.first;
83 IsTrailingComment = K.second;
84
85 IsAlmostTrailingComment = RawText.startswith("//<") ||
86 RawText.startswith("/*<");
87 } else {
Abramo Bagnarae06a8882012-07-04 07:30:26 +000088 Kind = RCK_Merged;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000089 IsTrailingComment = mergedCommentIsTrailingComment(RawText);
90 }
91}
92
93unsigned 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
103unsigned 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
113StringRef 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 Gribenko3292d062012-07-02 17:35:10 +0000140const char *RawComment::extractBriefText(const ASTContext &Context) const {
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000141 // Make sure that RawText is valid.
142 getRawText(Context.getSourceManager());
143
Dmitri Gribenko4586df72012-07-27 20:37:06 +0000144 // 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 Jahanian5b637072013-05-03 23:15:20 +0000149 comments::Lexer L(Allocator, Context.getDiagnostics(),
150 Context.getCommentCommandTraits(),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000151 Range.getBegin(),
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000152 RawText.begin(), RawText.end());
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000153 comments::BriefParser P(L, Context.getCommentCommandTraits());
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000154
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 Gribenko3292d062012-07-02 17:35:10 +0000159 BriefText = BriefTextPtr;
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000160 BriefTextValid = true;
161
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000162 return BriefTextPtr;
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000163}
164
Dmitri Gribenkob2610882012-08-14 17:17:18 +0000165comments::FullComment *RawComment::parse(const ASTContext &Context,
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000166 const Preprocessor *PP,
Dmitri Gribenkob2610882012-08-14 17:17:18 +0000167 const Decl *D) const {
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000168 // Make sure that RawText is valid.
169 getRawText(Context.getSourceManager());
170
Fariborz Jahanian5b637072013-05-03 23:15:20 +0000171 comments::Lexer L(Context.getAllocator(), Context.getDiagnostics(),
172 Context.getCommentCommandTraits(),
Dmitri Gribenko6bab9112012-08-31 10:35:30 +0000173 getSourceRange().getBegin(),
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000174 RawText.begin(), RawText.end());
175 comments::Sema S(Context.getAllocator(), Context.getSourceManager(),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000176 Context.getDiagnostics(),
Dmitri Gribenko6743e042012-09-29 11:40:46 +0000177 Context.getCommentCommandTraits(),
178 PP);
Dmitri Gribenkob2610882012-08-14 17:17:18 +0000179 S.setDecl(D);
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000180 comments::Parser P(L, S, Context.getAllocator(), Context.getSourceManager(),
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000181 Context.getDiagnostics(),
182 Context.getCommentCommandTraits());
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000183
Dmitri Gribenkob2610882012-08-14 17:17:18 +0000184 return P.parseFullComment();
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000185}
186
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000187namespace {
188bool containsOnlyWhitespace(StringRef Str) {
189 return Str.find_first_not_of(" \t\f\v\r\n") == StringRef::npos;
190}
191
Dmitri Gribenko1d0f5672012-09-09 20:47:31 +0000192bool 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 Gribenkoaab83832012-06-20 00:34:58 +0000196
Dmitri Gribenko1d0f5672012-09-09 20:47:31 +0000197 // Question does not make sense if locations are in different files.
198 if (Loc1Info.first != Loc2Info.first)
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000199 return false;
200
201 bool Invalid = false;
Dmitri Gribenko1d0f5672012-09-09 20:47:31 +0000202 const char *Buffer = SM.getBufferData(Loc1Info.first, &Invalid).data();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000203 if (Invalid)
204 return false;
205
Dmitri Gribenko1d0f5672012-09-09 20:47:31 +0000206 StringRef Text(Buffer + Loc1Info.second, Loc2Info.second - Loc1Info.second);
207 return containsOnlyWhitespace(Text);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000208}
209} // unnamed namespace
210
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000211void RawCommentList::addComment(const RawComment &RC,
212 llvm::BumpPtrAllocator &Allocator) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000213 if (RC.isInvalid())
214 return;
215
Dmitri Gribenko92307402012-06-21 22:04:37 +0000216 // Check if the comments are not in source order.
217 while (!Comments.empty() &&
218 !SourceMgr.isBeforeInTranslationUnit(
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000219 Comments.back()->getSourceRange().getBegin(),
Dmitri Gribenko92307402012-06-21 22:04:37 +0000220 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 Gribenkoaab83832012-06-20 00:34:58 +0000225
226 if (OnlyWhitespaceSeen) {
Dmitri Gribenko1d0f5672012-09-09 20:47:31 +0000227 if (!onlyWhitespaceBetween(SourceMgr,
228 PrevCommentEndLoc,
229 RC.getSourceRange().getBegin()))
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000230 OnlyWhitespaceSeen = false;
231 }
232
Dmitri Gribenko1d0f5672012-09-09 20:47:31 +0000233 PrevCommentEndLoc = RC.getSourceRange().getEnd();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000234
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 Gribenko7dd29d42012-07-06 18:19:34 +0000242 Comments.push_back(new (Allocator) RawComment(RC));
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000243 OnlyWhitespaceSeen = true;
244 return;
245 }
246
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000247 const RawComment &C1 = *Comments.back();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000248 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 Gribenko557a8d52012-08-28 01:20:53 +0000252 // Merge comments if they are on same or consecutive lines.
253 bool Merged = false;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000254 if (OnlyWhitespaceSeen &&
Dmitri Gribenko557a8d52012-08-28 01:20:53 +0000255 (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 Gribenkoa7d16ce2013-04-10 15:35:17 +0000261 *Comments.back() = RawComment(SourceMgr, MergedRange, true,
262 RC.isParseAllComments());
Dmitri Gribenko557a8d52012-08-28 01:20:53 +0000263 Merged = true;
264 }
265 }
266 if (!Merged)
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000267 Comments.push_back(new (Allocator) RawComment(RC));
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000268
269 OnlyWhitespaceSeen = true;
270}