blob: ede47664d6b2abd2cf82e7d269ca6cf6b07e3f66 [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] != '/')
22 return std::make_pair(RawComment::CK_Invalid, false);
23
24 RawComment::CommentKind K;
25 if (Comment[1] == '/') {
26 if (Comment.size() < 3)
27 return std::make_pair(RawComment::CK_OrdinaryBCPL, false);
28
29 if (Comment[2] == '/')
30 K = RawComment::CK_BCPLSlash;
31 else if (Comment[2] == '!')
32 K = RawComment::CK_BCPLExcl;
33 else
34 return std::make_pair(RawComment::CK_OrdinaryBCPL, false);
35 } 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] != '/')
43 return std::make_pair(RawComment::CK_Invalid, false);
44
45 if (Comment[2] == '*')
46 K = RawComment::CK_JavaDoc;
47 else if (Comment[2] == '!')
48 K = RawComment::CK_Qt;
49 else
50 return std::make_pair(RawComment::CK_OrdinaryC, false);
51 }
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) :
63 Range(SR), RawTextValid(false), IsAlmostTrailingComment(false),
64 BeginLineValid(false), EndLineValid(false) {
65 // Extract raw comment text, if possible.
Dmitri Gribenkofecc2e02012-06-21 21:02:45 +000066 if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +000067 Kind = CK_Invalid;
68 return;
69 }
70
71 if (!Merged) {
72 // Guess comment kind.
73 std::pair<CommentKind, bool> K = getCommentKind(RawText);
74 Kind = K.first;
75 IsTrailingComment = K.second;
76
77 IsAlmostTrailingComment = RawText.startswith("//<") ||
78 RawText.startswith("/*<");
79 } else {
80 Kind = CK_Merged;
81 IsTrailingComment = mergedCommentIsTrailingComment(RawText);
82 }
83}
84
85unsigned RawComment::getBeginLine(const SourceManager &SM) const {
86 if (BeginLineValid)
87 return BeginLine;
88
89 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Range.getBegin());
90 BeginLine = SM.getLineNumber(LocInfo.first, LocInfo.second);
91 BeginLineValid = true;
92 return BeginLine;
93}
94
95unsigned RawComment::getEndLine(const SourceManager &SM) const {
96 if (EndLineValid)
97 return EndLine;
98
99 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Range.getEnd());
100 EndLine = SM.getLineNumber(LocInfo.first, LocInfo.second);
101 EndLineValid = true;
102 return EndLine;
103}
104
105StringRef RawComment::getRawTextSlow(const SourceManager &SourceMgr) const {
106 FileID BeginFileID;
107 FileID EndFileID;
108 unsigned BeginOffset;
109 unsigned EndOffset;
110
111 llvm::tie(BeginFileID, BeginOffset) =
112 SourceMgr.getDecomposedLoc(Range.getBegin());
113 llvm::tie(EndFileID, EndOffset) =
114 SourceMgr.getDecomposedLoc(Range.getEnd());
115
116 const unsigned Length = EndOffset - BeginOffset;
117 if (Length < 2)
118 return StringRef();
119
120 // The comment can't begin in one file and end in another.
121 assert(BeginFileID == EndFileID);
122
123 bool Invalid = false;
124 const char *BufferStart = SourceMgr.getBufferData(BeginFileID,
125 &Invalid).data();
126 if (Invalid)
127 return StringRef();
128
129 return StringRef(BufferStart + BeginOffset, Length);
130}
131
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000132StringRef RawComment::extractBriefText(const ASTContext &Context) const {
133 // Make sure that RawText is valid.
134 getRawText(Context.getSourceManager());
135
136 comments::Lexer L(Range.getBegin(), comments::CommentOptions(),
137 RawText.begin(), RawText.end());
138 comments::BriefParser P(L);
139
140 const std::string Result = P.Parse();
141 const unsigned BriefTextLength = Result.size();
142 char *BriefTextPtr = new (Context) char[BriefTextLength + 1];
143 memcpy(BriefTextPtr, Result.c_str(), BriefTextLength + 1);
144 BriefText = StringRef(BriefTextPtr, BriefTextLength);
145 BriefTextValid = true;
146
147 return BriefText;
148}
149
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000150namespace {
151bool containsOnlyWhitespace(StringRef Str) {
152 return Str.find_first_not_of(" \t\f\v\r\n") == StringRef::npos;
153}
154
155bool onlyWhitespaceBetweenComments(SourceManager &SM,
156 const RawComment &C1, const RawComment &C2) {
157 std::pair<FileID, unsigned> C1EndLocInfo = SM.getDecomposedLoc(
158 C1.getSourceRange().getEnd());
159 std::pair<FileID, unsigned> C2BeginLocInfo = SM.getDecomposedLoc(
160 C2.getSourceRange().getBegin());
161
162 // Question does not make sense if comments are located in different files.
163 if (C1EndLocInfo.first != C2BeginLocInfo.first)
164 return false;
165
166 bool Invalid = false;
167 const char *Buffer = SM.getBufferData(C1EndLocInfo.first, &Invalid).data();
168 if (Invalid)
169 return false;
170
171 StringRef TextBetweenComments(Buffer + C1EndLocInfo.second,
172 C2BeginLocInfo.second - C1EndLocInfo.second);
173
174 return containsOnlyWhitespace(TextBetweenComments);
175}
176} // unnamed namespace
177
Dmitri Gribenko93b9ecb2012-06-20 20:39:04 +0000178void RawCommentList::addComment(const RawComment &RC) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000179 if (RC.isInvalid())
180 return;
181
Dmitri Gribenko92307402012-06-21 22:04:37 +0000182 // Check if the comments are not in source order.
183 while (!Comments.empty() &&
184 !SourceMgr.isBeforeInTranslationUnit(
185 Comments.back().getSourceRange().getBegin(),
186 RC.getSourceRange().getBegin())) {
187 // If they are, just pop a few last comments that don't fit.
188 // This happens if an \#include directive contains comments.
189 Comments.pop_back();
190 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000191
192 if (OnlyWhitespaceSeen) {
193 if (!onlyWhitespaceBetweenComments(SourceMgr, LastComment, RC))
194 OnlyWhitespaceSeen = false;
195 }
196
197 LastComment = RC;
198
199 // Ordinary comments are not interesting for us.
200 if (RC.isOrdinary())
201 return;
202
203 // If this is the first Doxygen comment, save it (because there isn't
204 // anything to merge it with).
205 if (Comments.empty()) {
206 Comments.push_back(RC);
207 OnlyWhitespaceSeen = true;
208 return;
209 }
210
211 const RawComment &C1 = Comments.back();
212 const RawComment &C2 = RC;
213
214 // Merge comments only if there is only whitespace between them.
215 // Can't merge trailing and non-trailing comments.
216 // Merge trailing comments if they are on same or consecutive lines.
217 if (OnlyWhitespaceSeen &&
218 (C1.isTrailingComment() == C2.isTrailingComment()) &&
219 (!C1.isTrailingComment() ||
220 C1.getEndLine(SourceMgr) + 1 >= C2.getBeginLine(SourceMgr))) {
221 SourceRange MergedRange(C1.getSourceRange().getBegin(),
222 C2.getSourceRange().getEnd());
223 RawComment Merged(SourceMgr, MergedRange, true);
224 Comments.pop_back();
225 Comments.push_back(Merged);
226 } else
227 Comments.push_back(RC);
228
229 OnlyWhitespaceSeen = true;
230}
231