1 //===--- RawCommentList.cpp - Processing raw comments -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/AST/RawCommentList.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/Comment.h"
13 #include "clang/AST/CommentBriefParser.h"
14 #include "clang/AST/CommentCommandTraits.h"
15 #include "clang/AST/CommentLexer.h"
16 #include "clang/AST/CommentParser.h"
17 #include "clang/AST/CommentSema.h"
18 #include "llvm/ADT/STLExtras.h"
20 using namespace clang;
23 /// Get comment kind and bool describing if it is a trailing comment.
24 std::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] != '/')
28 return std::make_pair(RawComment::RCK_Invalid, false);
30 RawComment::CommentKind K;
31 if (Comment[1] == '/') {
32 if (Comment.size() < 3)
33 return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
35 if (Comment[2] == '/')
36 K = RawComment::RCK_BCPLSlash;
37 else if (Comment[2] == '!')
38 K = RawComment::RCK_BCPLExcl;
40 return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
42 assert(Comment.size() >= 4);
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] != '/')
49 return std::make_pair(RawComment::RCK_Invalid, false);
51 if (Comment[2] == '*')
52 K = RawComment::RCK_JavaDoc;
53 else if (Comment[2] == '!')
54 K = RawComment::RCK_Qt;
56 return std::make_pair(RawComment::RCK_OrdinaryC, false);
58 const bool TrailingComment = (Comment.size() > 3) && (Comment[3] == '<');
59 return std::make_pair(K, TrailingComment);
62 bool mergedCommentIsTrailingComment(StringRef Comment) {
63 return (Comment.size() > 3) && (Comment[3] == '<');
65 } // unnamed namespace
67 RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR,
68 bool Merged, bool ParseAllComments) :
69 Range(SR), RawTextValid(false), BriefTextValid(false),
70 IsAttached(false), IsAlmostTrailingComment(false),
71 ParseAllComments(ParseAllComments),
72 BeginLineValid(false), EndLineValid(false) {
73 // Extract raw comment text, if possible.
74 if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) {
80 // Guess comment kind.
81 std::pair<CommentKind, bool> K = getCommentKind(RawText, ParseAllComments);
83 IsTrailingComment = K.second;
85 IsAlmostTrailingComment = RawText.startswith("//<") ||
86 RawText.startswith("/*<");
89 IsTrailingComment = mergedCommentIsTrailingComment(RawText);
93 unsigned RawComment::getBeginLine(const SourceManager &SM) const {
97 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Range.getBegin());
98 BeginLine = SM.getLineNumber(LocInfo.first, LocInfo.second);
99 BeginLineValid = true;
103 unsigned RawComment::getEndLine(const SourceManager &SM) const {
107 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Range.getEnd());
108 EndLine = SM.getLineNumber(LocInfo.first, LocInfo.second);
113 StringRef RawComment::getRawTextSlow(const SourceManager &SourceMgr) const {
116 unsigned BeginOffset;
119 llvm::tie(BeginFileID, BeginOffset) =
120 SourceMgr.getDecomposedLoc(Range.getBegin());
121 llvm::tie(EndFileID, EndOffset) =
122 SourceMgr.getDecomposedLoc(Range.getEnd());
124 const unsigned Length = EndOffset - BeginOffset;
128 // The comment can't begin in one file and end in another.
129 assert(BeginFileID == EndFileID);
131 bool Invalid = false;
132 const char *BufferStart = SourceMgr.getBufferData(BeginFileID,
137 return StringRef(BufferStart + BeginOffset, Length);
140 const char *RawComment::extractBriefText(const ASTContext &Context) const {
141 // Make sure that RawText is valid.
142 getRawText(Context.getSourceManager());
144 // 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;
149 comments::Lexer L(Allocator, Context.getDiagnostics(),
150 Context.getCommentCommandTraits(),
152 RawText.begin(), RawText.end());
153 comments::BriefParser P(L, Context.getCommentCommandTraits());
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);
159 BriefText = BriefTextPtr;
160 BriefTextValid = true;
165 comments::FullComment *RawComment::parse(const ASTContext &Context,
166 const Preprocessor *PP,
167 const Decl *D) const {
168 // Make sure that RawText is valid.
169 getRawText(Context.getSourceManager());
171 comments::Lexer L(Context.getAllocator(), Context.getDiagnostics(),
172 Context.getCommentCommandTraits(),
173 getSourceRange().getBegin(),
174 RawText.begin(), RawText.end());
175 comments::Sema S(Context.getAllocator(), Context.getSourceManager(),
176 Context.getDiagnostics(),
177 Context.getCommentCommandTraits(),
180 comments::Parser P(L, S, Context.getAllocator(), Context.getSourceManager(),
181 Context.getDiagnostics(),
182 Context.getCommentCommandTraits());
184 return P.parseFullComment();
188 bool containsOnlyWhitespace(StringRef Str) {
189 return Str.find_first_not_of(" \t\f\v\r\n") == StringRef::npos;
192 bool 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);
197 // Question does not make sense if locations are in different files.
198 if (Loc1Info.first != Loc2Info.first)
201 bool Invalid = false;
202 const char *Buffer = SM.getBufferData(Loc1Info.first, &Invalid).data();
206 StringRef Text(Buffer + Loc1Info.second, Loc2Info.second - Loc1Info.second);
207 return containsOnlyWhitespace(Text);
209 } // unnamed namespace
211 void RawCommentList::addComment(const RawComment &RC,
212 llvm::BumpPtrAllocator &Allocator) {
216 // Check if the comments are not in source order.
217 while (!Comments.empty() &&
218 !SourceMgr.isBeforeInTranslationUnit(
219 Comments.back()->getSourceRange().getBegin(),
220 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.
226 if (OnlyWhitespaceSeen) {
227 if (!onlyWhitespaceBetween(SourceMgr,
229 RC.getSourceRange().getBegin()))
230 OnlyWhitespaceSeen = false;
233 PrevCommentEndLoc = RC.getSourceRange().getEnd();
235 // Ordinary comments are not interesting for us.
239 // If this is the first Doxygen comment, save it (because there isn't
240 // anything to merge it with).
241 if (Comments.empty()) {
242 Comments.push_back(new (Allocator) RawComment(RC));
243 OnlyWhitespaceSeen = true;
247 const RawComment &C1 = *Comments.back();
248 const RawComment &C2 = RC;
250 // Merge comments only if there is only whitespace between them.
251 // Can't merge trailing and non-trailing comments.
252 // Merge comments if they are on same or consecutive lines.
254 if (OnlyWhitespaceSeen &&
255 (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());
261 *Comments.back() = RawComment(SourceMgr, MergedRange, true,
262 RC.isParseAllComments());
267 Comments.push_back(new (Allocator) RawComment(RC));
269 OnlyWhitespaceSeen = true;