2012-06-20 08:34:58 +08:00
|
|
|
//===--- RawCommentList.cpp - Processing raw comments -----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-06-20 17:53:52 +08:00
|
|
|
#include "clang/AST/RawCommentList.h"
|
2012-06-27 04:39:18 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2012-08-11 08:51:43 +08:00
|
|
|
#include "clang/AST/Comment.h"
|
2012-06-27 04:39:18 +08:00
|
|
|
#include "clang/AST/CommentBriefParser.h"
|
2012-08-09 08:03:17 +08:00
|
|
|
#include "clang/AST/CommentCommandTraits.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/CommentLexer.h"
|
|
|
|
#include "clang/AST/CommentParser.h"
|
|
|
|
#include "clang/AST/CommentSema.h"
|
2012-06-20 08:34:58 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// Get comment kind and bool describing if it is a trailing comment.
|
2013-04-27 04:12:49 +08:00
|
|
|
std::pair<RawComment::CommentKind, bool> getCommentKind(StringRef Comment,
|
|
|
|
bool ParseAllComments) {
|
|
|
|
const size_t MinCommentLength = ParseAllComments ? 2 : 3;
|
|
|
|
if ((Comment.size() < MinCommentLength) || Comment[0] != '/')
|
2012-07-04 15:30:26 +08:00
|
|
|
return std::make_pair(RawComment::RCK_Invalid, false);
|
2012-06-20 08:34:58 +08:00
|
|
|
|
|
|
|
RawComment::CommentKind K;
|
|
|
|
if (Comment[1] == '/') {
|
|
|
|
if (Comment.size() < 3)
|
2012-07-04 15:30:26 +08:00
|
|
|
return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
|
2012-06-20 08:34:58 +08:00
|
|
|
|
|
|
|
if (Comment[2] == '/')
|
2012-07-04 15:30:26 +08:00
|
|
|
K = RawComment::RCK_BCPLSlash;
|
2012-06-20 08:34:58 +08:00
|
|
|
else if (Comment[2] == '!')
|
2012-07-04 15:30:26 +08:00
|
|
|
K = RawComment::RCK_BCPLExcl;
|
2012-06-20 08:34:58 +08:00
|
|
|
else
|
2012-07-04 15:30:26 +08:00
|
|
|
return std::make_pair(RawComment::RCK_OrdinaryBCPL, false);
|
2012-06-20 08:34:58 +08:00
|
|
|
} else {
|
|
|
|
assert(Comment.size() >= 4);
|
|
|
|
|
|
|
|
// Comment lexer does not understand escapes in comment markers, so pretend
|
|
|
|
// that this is not a comment.
|
|
|
|
if (Comment[1] != '*' ||
|
|
|
|
Comment[Comment.size() - 2] != '*' ||
|
|
|
|
Comment[Comment.size() - 1] != '/')
|
2012-07-04 15:30:26 +08:00
|
|
|
return std::make_pair(RawComment::RCK_Invalid, false);
|
2012-06-20 08:34:58 +08:00
|
|
|
|
|
|
|
if (Comment[2] == '*')
|
2012-07-04 15:30:26 +08:00
|
|
|
K = RawComment::RCK_JavaDoc;
|
2012-06-20 08:34:58 +08:00
|
|
|
else if (Comment[2] == '!')
|
2012-07-04 15:30:26 +08:00
|
|
|
K = RawComment::RCK_Qt;
|
2012-06-20 08:34:58 +08:00
|
|
|
else
|
2012-07-04 15:30:26 +08:00
|
|
|
return std::make_pair(RawComment::RCK_OrdinaryC, false);
|
2012-06-20 08:34:58 +08:00
|
|
|
}
|
|
|
|
const bool TrailingComment = (Comment.size() > 3) && (Comment[3] == '<');
|
|
|
|
return std::make_pair(K, TrailingComment);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mergedCommentIsTrailingComment(StringRef Comment) {
|
|
|
|
return (Comment.size() > 3) && (Comment[3] == '<');
|
|
|
|
}
|
|
|
|
} // unnamed namespace
|
|
|
|
|
|
|
|
RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR,
|
2013-04-10 23:35:17 +08:00
|
|
|
bool Merged, bool ParseAllComments) :
|
2012-06-27 13:48:36 +08:00
|
|
|
Range(SR), RawTextValid(false), BriefTextValid(false),
|
2012-08-15 01:17:18 +08:00
|
|
|
IsAttached(false), IsAlmostTrailingComment(false),
|
2013-09-28 23:06:27 +08:00
|
|
|
ParseAllComments(ParseAllComments) {
|
2012-06-20 08:34:58 +08:00
|
|
|
// Extract raw comment text, if possible.
|
2012-06-22 05:02:45 +08:00
|
|
|
if (SR.getBegin() == SR.getEnd() || getRawText(SourceMgr).empty()) {
|
2012-07-04 15:30:26 +08:00
|
|
|
Kind = RCK_Invalid;
|
2012-06-20 08:34:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Merged) {
|
|
|
|
// Guess comment kind.
|
2013-04-27 04:12:49 +08:00
|
|
|
std::pair<CommentKind, bool> K = getCommentKind(RawText, ParseAllComments);
|
2012-06-20 08:34:58 +08:00
|
|
|
Kind = K.first;
|
|
|
|
IsTrailingComment = K.second;
|
|
|
|
|
|
|
|
IsAlmostTrailingComment = RawText.startswith("//<") ||
|
|
|
|
RawText.startswith("/*<");
|
|
|
|
} else {
|
2012-07-04 15:30:26 +08:00
|
|
|
Kind = RCK_Merged;
|
2012-06-20 08:34:58 +08:00
|
|
|
IsTrailingComment = mergedCommentIsTrailingComment(RawText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef RawComment::getRawTextSlow(const SourceManager &SourceMgr) const {
|
|
|
|
FileID BeginFileID;
|
|
|
|
FileID EndFileID;
|
|
|
|
unsigned BeginOffset;
|
|
|
|
unsigned EndOffset;
|
|
|
|
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(BeginFileID, BeginOffset) =
|
2012-06-20 08:34:58 +08:00
|
|
|
SourceMgr.getDecomposedLoc(Range.getBegin());
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(EndFileID, EndOffset) = SourceMgr.getDecomposedLoc(Range.getEnd());
|
2012-06-20 08:34:58 +08:00
|
|
|
|
|
|
|
const unsigned Length = EndOffset - BeginOffset;
|
|
|
|
if (Length < 2)
|
|
|
|
return StringRef();
|
|
|
|
|
|
|
|
// The comment can't begin in one file and end in another.
|
|
|
|
assert(BeginFileID == EndFileID);
|
|
|
|
|
|
|
|
bool Invalid = false;
|
|
|
|
const char *BufferStart = SourceMgr.getBufferData(BeginFileID,
|
|
|
|
&Invalid).data();
|
|
|
|
if (Invalid)
|
|
|
|
return StringRef();
|
|
|
|
|
|
|
|
return StringRef(BufferStart + BeginOffset, Length);
|
|
|
|
}
|
|
|
|
|
2012-07-03 01:35:10 +08:00
|
|
|
const char *RawComment::extractBriefText(const ASTContext &Context) const {
|
2012-06-27 04:39:18 +08:00
|
|
|
// Make sure that RawText is valid.
|
|
|
|
getRawText(Context.getSourceManager());
|
|
|
|
|
2012-07-28 04:37:06 +08:00
|
|
|
// Since we will be copying the resulting text, all allocations made during
|
|
|
|
// parsing are garbage after resulting string is formed. Thus we can use
|
|
|
|
// a separate allocator for all temporary stuff.
|
|
|
|
llvm::BumpPtrAllocator Allocator;
|
|
|
|
|
2013-05-04 07:15:20 +08:00
|
|
|
comments::Lexer L(Allocator, Context.getDiagnostics(),
|
|
|
|
Context.getCommentCommandTraits(),
|
2012-09-11 04:32:42 +08:00
|
|
|
Range.getBegin(),
|
2012-06-27 04:39:18 +08:00
|
|
|
RawText.begin(), RawText.end());
|
2012-09-11 04:32:42 +08:00
|
|
|
comments::BriefParser P(L, Context.getCommentCommandTraits());
|
2012-06-27 04:39:18 +08:00
|
|
|
|
|
|
|
const std::string Result = P.Parse();
|
|
|
|
const unsigned BriefTextLength = Result.size();
|
|
|
|
char *BriefTextPtr = new (Context) char[BriefTextLength + 1];
|
|
|
|
memcpy(BriefTextPtr, Result.c_str(), BriefTextLength + 1);
|
2012-07-03 01:35:10 +08:00
|
|
|
BriefText = BriefTextPtr;
|
2012-06-27 04:39:18 +08:00
|
|
|
BriefTextValid = true;
|
|
|
|
|
2012-07-03 01:35:10 +08:00
|
|
|
return BriefTextPtr;
|
2012-06-27 04:39:18 +08:00
|
|
|
}
|
|
|
|
|
2012-08-15 01:17:18 +08:00
|
|
|
comments::FullComment *RawComment::parse(const ASTContext &Context,
|
2012-09-29 19:40:46 +08:00
|
|
|
const Preprocessor *PP,
|
2012-08-15 01:17:18 +08:00
|
|
|
const Decl *D) const {
|
2012-08-11 08:51:43 +08:00
|
|
|
// Make sure that RawText is valid.
|
|
|
|
getRawText(Context.getSourceManager());
|
|
|
|
|
2013-05-04 07:15:20 +08:00
|
|
|
comments::Lexer L(Context.getAllocator(), Context.getDiagnostics(),
|
|
|
|
Context.getCommentCommandTraits(),
|
2012-08-31 18:35:30 +08:00
|
|
|
getSourceRange().getBegin(),
|
2012-08-11 08:51:43 +08:00
|
|
|
RawText.begin(), RawText.end());
|
|
|
|
comments::Sema S(Context.getAllocator(), Context.getSourceManager(),
|
2012-09-11 04:32:42 +08:00
|
|
|
Context.getDiagnostics(),
|
2012-09-29 19:40:46 +08:00
|
|
|
Context.getCommentCommandTraits(),
|
|
|
|
PP);
|
2012-08-15 01:17:18 +08:00
|
|
|
S.setDecl(D);
|
2012-08-11 08:51:43 +08:00
|
|
|
comments::Parser P(L, S, Context.getAllocator(), Context.getSourceManager(),
|
2012-09-11 04:32:42 +08:00
|
|
|
Context.getDiagnostics(),
|
|
|
|
Context.getCommentCommandTraits());
|
2012-08-11 08:51:43 +08:00
|
|
|
|
2012-08-15 01:17:18 +08:00
|
|
|
return P.parseFullComment();
|
2012-08-11 08:51:43 +08:00
|
|
|
}
|
|
|
|
|
2013-09-28 23:06:27 +08:00
|
|
|
static bool onlyWhitespaceBetween(SourceManager &SM,
|
|
|
|
SourceLocation Loc1, SourceLocation Loc2,
|
|
|
|
unsigned MaxNewlinesAllowed) {
|
2012-09-10 04:47:31 +08:00
|
|
|
std::pair<FileID, unsigned> Loc1Info = SM.getDecomposedLoc(Loc1);
|
|
|
|
std::pair<FileID, unsigned> Loc2Info = SM.getDecomposedLoc(Loc2);
|
|
|
|
|
|
|
|
// Question does not make sense if locations are in different files.
|
|
|
|
if (Loc1Info.first != Loc2Info.first)
|
2012-06-20 08:34:58 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Invalid = false;
|
2012-09-10 04:47:31 +08:00
|
|
|
const char *Buffer = SM.getBufferData(Loc1Info.first, &Invalid).data();
|
2012-06-20 08:34:58 +08:00
|
|
|
if (Invalid)
|
|
|
|
return false;
|
|
|
|
|
2013-09-28 23:06:27 +08:00
|
|
|
unsigned NumNewlines = 0;
|
|
|
|
assert(Loc1Info.second <= Loc2Info.second && "Loc1 after Loc2!");
|
|
|
|
// Look for non-whitespace characters and remember any newlines seen.
|
|
|
|
for (unsigned I = Loc1Info.second; I != Loc2Info.second; ++I) {
|
|
|
|
switch (Buffer[I]) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\f':
|
|
|
|
case '\v':
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
++NumNewlines;
|
|
|
|
|
|
|
|
// Check if we have found more than the maximum allowed number of
|
|
|
|
// newlines.
|
|
|
|
if (NumNewlines > MaxNewlinesAllowed)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Collapse \r\n and \n\r into a single newline.
|
|
|
|
if (I + 1 != Loc2Info.second &&
|
|
|
|
(Buffer[I + 1] == '\n' || Buffer[I + 1] == '\r') &&
|
|
|
|
Buffer[I] != Buffer[I + 1])
|
|
|
|
++I;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-06-20 08:34:58 +08:00
|
|
|
}
|
|
|
|
|
2012-07-07 02:19:34 +08:00
|
|
|
void RawCommentList::addComment(const RawComment &RC,
|
|
|
|
llvm::BumpPtrAllocator &Allocator) {
|
2012-06-20 08:34:58 +08:00
|
|
|
if (RC.isInvalid())
|
|
|
|
return;
|
|
|
|
|
2012-06-22 06:04:37 +08:00
|
|
|
// Check if the comments are not in source order.
|
|
|
|
while (!Comments.empty() &&
|
2013-09-28 23:06:27 +08:00
|
|
|
!SourceMgr.isBeforeInTranslationUnit(Comments.back()->getLocStart(),
|
|
|
|
RC.getLocStart())) {
|
2012-06-22 06:04:37 +08:00
|
|
|
// If they are, just pop a few last comments that don't fit.
|
|
|
|
// This happens if an \#include directive contains comments.
|
|
|
|
Comments.pop_back();
|
|
|
|
}
|
2012-06-20 08:34:58 +08:00
|
|
|
|
|
|
|
// Ordinary comments are not interesting for us.
|
|
|
|
if (RC.isOrdinary())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If this is the first Doxygen comment, save it (because there isn't
|
|
|
|
// anything to merge it with).
|
|
|
|
if (Comments.empty()) {
|
2012-07-07 02:19:34 +08:00
|
|
|
Comments.push_back(new (Allocator) RawComment(RC));
|
2012-06-20 08:34:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-07 02:19:34 +08:00
|
|
|
const RawComment &C1 = *Comments.back();
|
2012-06-20 08:34:58 +08:00
|
|
|
const RawComment &C2 = RC;
|
|
|
|
|
|
|
|
// Merge comments only if there is only whitespace between them.
|
|
|
|
// Can't merge trailing and non-trailing comments.
|
2012-08-28 09:20:53 +08:00
|
|
|
// Merge comments if they are on same or consecutive lines.
|
2013-09-28 23:06:27 +08:00
|
|
|
if (C1.isTrailingComment() == C2.isTrailingComment() &&
|
|
|
|
onlyWhitespaceBetween(SourceMgr, C1.getLocEnd(), C2.getLocStart(),
|
|
|
|
/*MaxNewlinesAllowed=*/1)) {
|
|
|
|
SourceRange MergedRange(C1.getLocStart(), C2.getLocEnd());
|
|
|
|
*Comments.back() = RawComment(SourceMgr, MergedRange, true,
|
|
|
|
RC.isParseAllComments());
|
|
|
|
} else {
|
2012-07-07 02:19:34 +08:00
|
|
|
Comments.push_back(new (Allocator) RawComment(RC));
|
2013-09-28 23:06:27 +08:00
|
|
|
}
|
2012-06-20 08:34:58 +08:00
|
|
|
}
|
2014-03-27 23:40:39 +08:00
|
|
|
|
|
|
|
void RawCommentList::addDeserializedComments(ArrayRef<RawComment *> DeserializedComments) {
|
|
|
|
std::vector<RawComment *> MergedComments;
|
|
|
|
MergedComments.reserve(Comments.size() + DeserializedComments.size());
|
|
|
|
|
|
|
|
std::merge(Comments.begin(), Comments.end(),
|
|
|
|
DeserializedComments.begin(), DeserializedComments.end(),
|
|
|
|
std::back_inserter(MergedComments),
|
|
|
|
BeforeThanCompare<RawComment>(SourceMgr));
|
|
|
|
std::swap(Comments, MergedComments);
|
|
|
|
}
|
|
|
|
|