blob: 80b627293e4278f73f8b4458d7f6ae730488264f [file] [log] [blame]
Dmitri Gribenkoaa0cd852012-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 Carruthf95d4122012-06-20 09:53:52 +000010#include "clang/AST/RawCommentList.h"
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000011#include "clang/AST/ASTContext.h"
Dmitri Gribenkof50555e2012-08-11 00:51:43 +000012#include "clang/AST/Comment.h"
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000013#include "clang/AST/CommentLexer.h"
14#include "clang/AST/CommentBriefParser.h"
Dmitri Gribenkof50555e2012-08-11 00:51:43 +000015#include "clang/AST/CommentSema.h"
16#include "clang/AST/CommentParser.h"
Dmitri Gribenkoaa580812012-08-09 00:03:17 +000017#include "clang/AST/CommentCommandTraits.h"
Dmitri Gribenkoaa0cd852012-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 Bagnarac50a0e32012-07-04 07:30:26 +000026 return std::make_pair(RawComment::RCK_Invalid, false);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +000027
28 RawComment::CommentKind K;
29 if (Comment[1] == '/') {
30 if (Comment.size() < 3)
Abramo Bagnarac50a0e32012-07-04 07:30:26 +000031 return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +000032
33 if (Comment[2] == '/')
Abramo Bagnarac50a0e32012-07-04 07:30:26 +000034 K = RawComment::RCK_BCPLSlash;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +000035 else if (Comment[2] == '!')
Abramo Bagnarac50a0e32012-07-04 07:30:26 +000036 K = RawComment::RCK_BCPLExcl;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +000037 else
Abramo Bagnarac50a0e32012-07-04 07:30:26 +000038 return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
Dmitri Gribenkoaa0cd852012-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 Bagnarac50a0e32012-07-04 07:30:26 +000047 return std::make_pair(RawComment::RCK_Invalid, false);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +000048
49 if (Comment[2] == '*')
Abramo Bagnarac50a0e32012-07-04 07:30:26 +000050 K = RawComment::RCK_JavaDoc;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +000051 else if (Comment[2] == '!')
Abramo Bagnarac50a0e32012-07-04 07:30:26 +000052 K = RawComment::RCK_Qt;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +000053 else
Abramo Bagnarac50a0e32012-07-04 07:30:26 +000054 return std::make_pair(RawComment::RCK_OrdinaryC, false);
Dmitri Gribenkoaa0cd852012-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 Gribenko1e15e3b2012-06-27 05:48:36 +000067 Range(SR), RawTextValid(false), BriefTextValid(false),
Dmitri Gribenkoc41ace92012-08-14 17:17:18 +000068 IsAttached(false), IsAlmostTrailingComment(false),
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +000069 BeginLineValid(false), EndLineValid(false) {
70 // Extract raw comment text, if possible.
Dmitri Gribenko9530a8b2012-06-21 21:02:45 +000071 if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) {
Abramo Bagnarac50a0e32012-07-04 07:30:26 +000072 Kind = RCK_Invalid;
Dmitri Gribenkoaa0cd852012-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 Bagnarac50a0e32012-07-04 07:30:26 +000085 Kind = RCK_Merged;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +000086 IsTrailingComment = mergedCommentIsTrailingComment(RawText);
87 }
88}
89
90unsigned 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
100unsigned 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
110StringRef 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 Gribenkod99ef532012-07-02 17:35:10 +0000137const char *RawComment::extractBriefText(const ASTContext &Context) const {
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000138 // Make sure that RawText is valid.
139 getRawText(Context.getSourceManager());
140
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000141 // 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 Gribenkoe4330a32012-09-10 20:32:42 +0000146 comments::Lexer L(Allocator, Context.getCommentCommandTraits(),
147 Range.getBegin(),
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000148 RawText.begin(), RawText.end());
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000149 comments::BriefParser P(L, Context.getCommentCommandTraits());
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000150
151 const std::string Result = P.Parse();
152 const unsigned BriefTextLength = Result.size();
153 char *BriefTextPtr = new (Context) char[BriefTextLength + 1];
154 memcpy(BriefTextPtr, Result.c_str(), BriefTextLength + 1);
Dmitri Gribenkod99ef532012-07-02 17:35:10 +0000155 BriefText = BriefTextPtr;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000156 BriefTextValid = true;
157
Dmitri Gribenkod99ef532012-07-02 17:35:10 +0000158 return BriefTextPtr;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000159}
160
Dmitri Gribenkoc41ace92012-08-14 17:17:18 +0000161comments::FullComment *RawComment::parse(const ASTContext &Context,
Dmitri Gribenko19523542012-09-29 11:40:46 +0000162 const Preprocessor *PP,
Dmitri Gribenkoc41ace92012-08-14 17:17:18 +0000163 const Decl *D) const {
Dmitri Gribenkof50555e2012-08-11 00:51:43 +0000164 // Make sure that RawText is valid.
165 getRawText(Context.getSourceManager());
166
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000167 comments::Lexer L(Context.getAllocator(), Context.getCommentCommandTraits(),
Dmitri Gribenkoaf503a62012-08-31 10:35:30 +0000168 getSourceRange().getBegin(),
Dmitri Gribenkof50555e2012-08-11 00:51:43 +0000169 RawText.begin(), RawText.end());
170 comments::Sema S(Context.getAllocator(), Context.getSourceManager(),
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000171 Context.getDiagnostics(),
Dmitri Gribenko19523542012-09-29 11:40:46 +0000172 Context.getCommentCommandTraits(),
173 PP);
Dmitri Gribenkoc41ace92012-08-14 17:17:18 +0000174 S.setDecl(D);
Dmitri Gribenkof50555e2012-08-11 00:51:43 +0000175 comments::Parser P(L, S, Context.getAllocator(), Context.getSourceManager(),
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000176 Context.getDiagnostics(),
177 Context.getCommentCommandTraits());
Dmitri Gribenkof50555e2012-08-11 00:51:43 +0000178
Dmitri Gribenkoc41ace92012-08-14 17:17:18 +0000179 return P.parseFullComment();
Dmitri Gribenkof50555e2012-08-11 00:51:43 +0000180}
181
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000182namespace {
183bool containsOnlyWhitespace(StringRef Str) {
184 return Str.find_first_not_of(" \t\f\v\r\n") == StringRef::npos;
185}
186
Dmitri Gribenko4a665572012-09-09 20:47:31 +0000187bool onlyWhitespaceBetween(SourceManager &SM,
188 SourceLocation Loc1, SourceLocation Loc2) {
189 std::pair<FileID, unsigned> Loc1Info = SM.getDecomposedLoc(Loc1);
190 std::pair<FileID, unsigned> Loc2Info = SM.getDecomposedLoc(Loc2);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000191
Dmitri Gribenko4a665572012-09-09 20:47:31 +0000192 // Question does not make sense if locations are in different files.
193 if (Loc1Info.first != Loc2Info.first)
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000194 return false;
195
196 bool Invalid = false;
Dmitri Gribenko4a665572012-09-09 20:47:31 +0000197 const char *Buffer = SM.getBufferData(Loc1Info.first, &Invalid).data();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000198 if (Invalid)
199 return false;
200
Dmitri Gribenko4a665572012-09-09 20:47:31 +0000201 StringRef Text(Buffer + Loc1Info.second, Loc2Info.second - Loc1Info.second);
202 return containsOnlyWhitespace(Text);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000203}
204} // unnamed namespace
205
Dmitri Gribenko811c8202012-07-06 18:19:34 +0000206void RawCommentList::addComment(const RawComment &RC,
207 llvm::BumpPtrAllocator &Allocator) {
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000208 if (RC.isInvalid())
209 return;
210
Dmitri Gribenkoe601b232012-06-21 22:04:37 +0000211 // Check if the comments are not in source order.
212 while (!Comments.empty() &&
213 !SourceMgr.isBeforeInTranslationUnit(
Dmitri Gribenko811c8202012-07-06 18:19:34 +0000214 Comments.back()->getSourceRange().getBegin(),
Dmitri Gribenkoe601b232012-06-21 22:04:37 +0000215 RC.getSourceRange().getBegin())) {
216 // If they are, just pop a few last comments that don't fit.
217 // This happens if an \#include directive contains comments.
218 Comments.pop_back();
219 }
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000220
221 if (OnlyWhitespaceSeen) {
Dmitri Gribenko4a665572012-09-09 20:47:31 +0000222 if (!onlyWhitespaceBetween(SourceMgr,
223 PrevCommentEndLoc,
224 RC.getSourceRange().getBegin()))
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000225 OnlyWhitespaceSeen = false;
226 }
227
Dmitri Gribenko4a665572012-09-09 20:47:31 +0000228 PrevCommentEndLoc = RC.getSourceRange().getEnd();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000229
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 Gribenko811c8202012-07-06 18:19:34 +0000237 Comments.push_back(new (Allocator) RawComment(RC));
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000238 OnlyWhitespaceSeen = true;
239 return;
240 }
241
Dmitri Gribenko811c8202012-07-06 18:19:34 +0000242 const RawComment &C1 = *Comments.back();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000243 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.
Dmitri Gribenkoc88c6a42012-08-28 01:20:53 +0000247 // Merge comments if they are on same or consecutive lines.
248 bool Merged = false;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000249 if (OnlyWhitespaceSeen &&
Dmitri Gribenkoc88c6a42012-08-28 01:20:53 +0000250 (C1.isTrailingComment() == C2.isTrailingComment())) {
251 unsigned C1EndLine = C1.getEndLine(SourceMgr);
252 unsigned C2BeginLine = C2.getBeginLine(SourceMgr);
253 if (C1EndLine + 1 == C2BeginLine || C1EndLine == C2BeginLine) {
254 SourceRange MergedRange(C1.getSourceRange().getBegin(),
255 C2.getSourceRange().getEnd());
256 *Comments.back() = RawComment(SourceMgr, MergedRange, true);
257 Merged = true;
258 }
259 }
260 if (!Merged)
Dmitri Gribenko811c8202012-07-06 18:19:34 +0000261 Comments.push_back(new (Allocator) RawComment(RC));
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000262
263 OnlyWhitespaceSeen = true;
264}
265