blob: d67eb0822f2b1797774c932f690f163ed2c66944 [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"
12#include "clang/AST/CommentLexer.h"
13#include "clang/AST/CommentBriefParser.h"
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000014#include "llvm/ADT/STLExtras.h"
15
16using namespace clang;
17
18namespace {
19/// Get comment kind and bool describing if it is a trailing comment.
20std::pair<RawComment::CommentKind, bool> getCommentKind(StringRef Comment) {
21 if (Comment.size() < 3 || Comment[0] != '/')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000022 return std::make_pair(RawComment::RCK_Invalid, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000023
24 RawComment::CommentKind K;
25 if (Comment[1] == '/') {
26 if (Comment.size() < 3)
Abramo Bagnarae06a8882012-07-04 07:30:26 +000027 return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000028
29 if (Comment[2] == '/')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000030 K = RawComment::RCK_BCPLSlash;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000031 else if (Comment[2] == '!')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000032 K = RawComment::RCK_BCPLExcl;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000033 else
Abramo Bagnarae06a8882012-07-04 07:30:26 +000034 return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000035 } else {
36 assert(Comment.size() >= 4);
37
38 // Comment lexer does not understand escapes in comment markers, so pretend
39 // that this is not a comment.
40 if (Comment[1] != '*' ||
41 Comment[Comment.size() - 2] != '*' ||
42 Comment[Comment.size() - 1] != '/')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000043 return std::make_pair(RawComment::RCK_Invalid, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000044
45 if (Comment[2] == '*')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000046 K = RawComment::RCK_JavaDoc;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000047 else if (Comment[2] == '!')
Abramo Bagnarae06a8882012-07-04 07:30:26 +000048 K = RawComment::RCK_Qt;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000049 else
Abramo Bagnarae06a8882012-07-04 07:30:26 +000050 return std::make_pair(RawComment::RCK_OrdinaryC, false);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000051 }
52 const bool TrailingComment = (Comment.size() > 3) && (Comment[3] == '<');
53 return std::make_pair(K, TrailingComment);
54}
55
56bool mergedCommentIsTrailingComment(StringRef Comment) {
57 return (Comment.size() > 3) && (Comment[3] == '<');
58}
59} // unnamed namespace
60
61RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR,
62 bool Merged) :
Dmitri Gribenko60c7ec62012-06-27 05:48:36 +000063 Range(SR), RawTextValid(false), BriefTextValid(false),
64 IsAlmostTrailingComment(false),
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000065 BeginLineValid(false), EndLineValid(false) {
66 // Extract raw comment text, if possible.
Dmitri Gribenkofecc2e02012-06-21 21:02:45 +000067 if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) {
Abramo Bagnarae06a8882012-07-04 07:30:26 +000068 Kind = RCK_Invalid;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000069 return;
70 }
71
72 if (!Merged) {
73 // Guess comment kind.
74 std::pair<CommentKind, bool> K = getCommentKind(RawText);
75 Kind = K.first;
76 IsTrailingComment = K.second;
77
78 IsAlmostTrailingComment = RawText.startswith("//<") ||
79 RawText.startswith("/*<");
80 } else {
Abramo Bagnarae06a8882012-07-04 07:30:26 +000081 Kind = RCK_Merged;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000082 IsTrailingComment = mergedCommentIsTrailingComment(RawText);
83 }
84}
85
86unsigned RawComment::getBeginLine(const SourceManager &SM) const {
87 if (BeginLineValid)
88 return BeginLine;
89
90 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Range.getBegin());
91 BeginLine = SM.getLineNumber(LocInfo.first, LocInfo.second);
92 BeginLineValid = true;
93 return BeginLine;
94}
95
96unsigned RawComment::getEndLine(const SourceManager &SM) const {
97 if (EndLineValid)
98 return EndLine;
99
100 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Range.getEnd());
101 EndLine = SM.getLineNumber(LocInfo.first, LocInfo.second);
102 EndLineValid = true;
103 return EndLine;
104}
105
106StringRef RawComment::getRawTextSlow(const SourceManager &SourceMgr) const {
107 FileID BeginFileID;
108 FileID EndFileID;
109 unsigned BeginOffset;
110 unsigned EndOffset;
111
112 llvm::tie(BeginFileID, BeginOffset) =
113 SourceMgr.getDecomposedLoc(Range.getBegin());
114 llvm::tie(EndFileID, EndOffset) =
115 SourceMgr.getDecomposedLoc(Range.getEnd());
116
117 const unsigned Length = EndOffset - BeginOffset;
118 if (Length < 2)
119 return StringRef();
120
121 // The comment can't begin in one file and end in another.
122 assert(BeginFileID == EndFileID);
123
124 bool Invalid = false;
125 const char *BufferStart = SourceMgr.getBufferData(BeginFileID,
126 &Invalid).data();
127 if (Invalid)
128 return StringRef();
129
130 return StringRef(BufferStart + BeginOffset, Length);
131}
132
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000133const char *RawComment::extractBriefText(const ASTContext &Context) const {
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000134 // Make sure that RawText is valid.
135 getRawText(Context.getSourceManager());
136
137 comments::Lexer L(Range.getBegin(), comments::CommentOptions(),
138 RawText.begin(), RawText.end());
139 comments::BriefParser P(L);
140
141 const std::string Result = P.Parse();
142 const unsigned BriefTextLength = Result.size();
143 char *BriefTextPtr = new (Context) char[BriefTextLength + 1];
144 memcpy(BriefTextPtr, Result.c_str(), BriefTextLength + 1);
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000145 BriefText = BriefTextPtr;
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000146 BriefTextValid = true;
147
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000148 return BriefTextPtr;
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000149}
150
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000151namespace {
152bool containsOnlyWhitespace(StringRef Str) {
153 return Str.find_first_not_of(" \t\f\v\r\n") == StringRef::npos;
154}
155
156bool onlyWhitespaceBetweenComments(SourceManager &SM,
157 const RawComment &C1, const RawComment &C2) {
158 std::pair<FileID, unsigned> C1EndLocInfo = SM.getDecomposedLoc(
159 C1.getSourceRange().getEnd());
160 std::pair<FileID, unsigned> C2BeginLocInfo = SM.getDecomposedLoc(
161 C2.getSourceRange().getBegin());
162
163 // Question does not make sense if comments are located in different files.
164 if (C1EndLocInfo.first != C2BeginLocInfo.first)
165 return false;
166
167 bool Invalid = false;
168 const char *Buffer = SM.getBufferData(C1EndLocInfo.first, &Invalid).data();
169 if (Invalid)
170 return false;
171
172 StringRef TextBetweenComments(Buffer + C1EndLocInfo.second,
173 C2BeginLocInfo.second - C1EndLocInfo.second);
174
175 return containsOnlyWhitespace(TextBetweenComments);
176}
177} // unnamed namespace
178
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000179void RawCommentList::addComment(const RawComment &RC,
180 llvm::BumpPtrAllocator &Allocator) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000181 if (RC.isInvalid())
182 return;
183
Dmitri Gribenko92307402012-06-21 22:04:37 +0000184 // Check if the comments are not in source order.
185 while (!Comments.empty() &&
186 !SourceMgr.isBeforeInTranslationUnit(
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000187 Comments.back()->getSourceRange().getBegin(),
Dmitri Gribenko92307402012-06-21 22:04:37 +0000188 RC.getSourceRange().getBegin())) {
189 // If they are, just pop a few last comments that don't fit.
190 // This happens if an \#include directive contains comments.
191 Comments.pop_back();
192 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000193
194 if (OnlyWhitespaceSeen) {
195 if (!onlyWhitespaceBetweenComments(SourceMgr, LastComment, RC))
196 OnlyWhitespaceSeen = false;
197 }
198
199 LastComment = RC;
200
201 // Ordinary comments are not interesting for us.
202 if (RC.isOrdinary())
203 return;
204
205 // If this is the first Doxygen comment, save it (because there isn't
206 // anything to merge it with).
207 if (Comments.empty()) {
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000208 Comments.push_back(new (Allocator) RawComment(RC));
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000209 OnlyWhitespaceSeen = true;
210 return;
211 }
212
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000213 const RawComment &C1 = *Comments.back();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000214 const RawComment &C2 = RC;
215
216 // Merge comments only if there is only whitespace between them.
217 // Can't merge trailing and non-trailing comments.
218 // Merge trailing comments if they are on same or consecutive lines.
219 if (OnlyWhitespaceSeen &&
220 (C1.isTrailingComment() == C2.isTrailingComment()) &&
221 (!C1.isTrailingComment() ||
222 C1.getEndLine(SourceMgr) + 1 >= C2.getBeginLine(SourceMgr))) {
223 SourceRange MergedRange(C1.getSourceRange().getBegin(),
224 C2.getSourceRange().getEnd());
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000225 *Comments.back() = RawComment(SourceMgr, MergedRange, true);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000226 } else
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +0000227 Comments.push_back(new (Allocator) RawComment(RC));
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000228
229 OnlyWhitespaceSeen = true;
230}
231