blob: c704cabe69f7009f733b27a452e9d08a112cc5ed [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/CommentLexer.h"
14#include "clang/AST/CommentBriefParser.h"
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +000015#include "clang/AST/CommentSema.h"
16#include "clang/AST/CommentParser.h"
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +000017#include "clang/AST/CommentCommandTraits.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.
24std::pair<RawComment::CommentKind, bool> getCommentKind(StringRef Comment) {
25 if (Comment.size() < 3 || Comment[0] != '/')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000026 return std::make_pair(RawComment::RCK_Invalid, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000027
28 RawComment::CommentKind K;
29 if (Comment[1] == '/') {
30 if (Comment.size() < 3)
Abramo Bagnarae06a8882012-07-04 07:30:26 +000031 return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000032
33 if (Comment[2] == '/')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000034 K = RawComment::RCK_BCPLSlash;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000035 else if (Comment[2] == '!')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000036 K = RawComment::RCK_BCPLExcl;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000037 else
Abramo Bagnarae06a8882012-07-04 07:30:26 +000038 return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000039 } 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 Bagnarae06a8882012-07-04 07:30:26 +000047 return std::make_pair(RawComment::RCK_Invalid, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000048
49 if (Comment[2] == '*')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000050 K = RawComment::RCK_JavaDoc;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000051 else if (Comment[2] == '!')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000052 K = RawComment::RCK_Qt;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000053 else
Abramo Bagnarae06a8882012-07-04 07:30:26 +000054 return std::make_pair(RawComment::RCK_OrdinaryC, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000055 }
56 const bool TrailingComment = (Comment.size() > 3) && (Comment[3] == '<');
57 return std::make_pair(K, TrailingComment);
58}
59
60bool mergedCommentIsTrailingComment(StringRef Comment) {
61 return (Comment.size() > 3) && (Comment[3] == '<');
62}
63} // unnamed namespace
64
65RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR,
66 bool Merged) :
Dmitri Gribenko60c7ec62012-06-27 05:48:36 +000067 Range(SR), RawTextValid(false), BriefTextValid(false),
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +000068 IsAlmostTrailingComment(false),
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000069 BeginLineValid(false), EndLineValid(false) {
70 // Extract raw comment text, if possible.
Dmitri Gribenkofecc2e02012-06-21 21:02:45 +000071 if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) {
Abramo Bagnarae06a8882012-07-04 07:30:26 +000072 Kind = RCK_Invalid;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000073 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 Bagnarae06a8882012-07-04 07:30:26 +000085 Kind = RCK_Merged;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000086 IsTrailingComment = mergedCommentIsTrailingComment(RawText);
87 }
88}
89
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +000090const 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 Gribenkoaab83832012-06-20 00:34:58 +0000100unsigned 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
110unsigned 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
120StringRef 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 Gribenko3292d062012-07-02 17:35:10 +0000147const char *RawComment::extractBriefText(const ASTContext &Context) const {
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000148 // Make sure that RawText is valid.
149 getRawText(Context.getSourceManager());
150
Dmitri Gribenko4586df72012-07-27 20:37:06 +0000151 // 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 Gribenkoca7f80a2012-08-09 00:03:17 +0000156 comments::CommandTraits Traits;
157 comments::Lexer L(Allocator, Traits,
Dmitri Gribenko4586df72012-07-27 20:37:06 +0000158 Range.getBegin(), comments::CommentOptions(),
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000159 RawText.begin(), RawText.end());
Dmitri Gribenkoca7f80a2012-08-09 00:03:17 +0000160 comments::BriefParser P(L, Traits);
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000161
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 Gribenko3292d062012-07-02 17:35:10 +0000166 BriefText = BriefTextPtr;
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000167 BriefTextValid = true;
168
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000169 return BriefTextPtr;
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000170}
171
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +0000172comments::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 Gribenkoaab83832012-06-20 00:34:58 +0000191namespace {
192bool containsOnlyWhitespace(StringRef Str) {
193 return Str.find_first_not_of(" \t\f\v\r\n") == StringRef::npos;
194}
195
196bool 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 Gribenko7dd29d42012-07-06 18:19:34 +0000219void RawCommentList::addComment(const RawComment &RC,
220 llvm::BumpPtrAllocator &Allocator) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000221 if (RC.isInvalid())
222 return;
223
Dmitri Gribenko92307402012-06-21 22:04:37 +0000224 // Check if the comments are not in source order.
225 while (!Comments.empty() &&
226 !SourceMgr.isBeforeInTranslationUnit(
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000227 Comments.back()->getSourceRange().getBegin(),
Dmitri Gribenko92307402012-06-21 22:04:37 +0000228 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 Gribenkoaab83832012-06-20 00:34:58 +0000233
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 Gribenko7dd29d42012-07-06 18:19:34 +0000248 Comments.push_back(new (Allocator) RawComment(RC));
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000249 OnlyWhitespaceSeen = true;
250 return;
251 }
252
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000253 const RawComment &C1 = *Comments.back();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000254 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 Gribenko7dd29d42012-07-06 18:19:34 +0000265 *Comments.back() = RawComment(SourceMgr, MergedRange, true);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000266 } else
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}
271