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/CommentLexer.h"
|
|
|
|
#include "clang/AST/CommentBriefParser.h"
|
2012-08-11 08:51:43 +08:00
|
|
|
#include "clang/AST/CommentSema.h"
|
|
|
|
#include "clang/AST/CommentParser.h"
|
2012-08-09 08:03:17 +08:00
|
|
|
#include "clang/AST/CommentCommandTraits.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.
|
|
|
|
std::pair<RawComment::CommentKind, bool> getCommentKind(StringRef Comment) {
|
|
|
|
if (Comment.size() < 3 || 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,
|
|
|
|
bool Merged) :
|
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),
|
2012-06-20 08:34:58 +08:00
|
|
|
BeginLineValid(false), EndLineValid(false) {
|
|
|
|
// 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.
|
|
|
|
std::pair<CommentKind, bool> K = getCommentKind(RawText);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned RawComment::getBeginLine(const SourceManager &SM) const {
|
|
|
|
if (BeginLineValid)
|
|
|
|
return BeginLine;
|
|
|
|
|
|
|
|
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Range.getBegin());
|
|
|
|
BeginLine = SM.getLineNumber(LocInfo.first, LocInfo.second);
|
|
|
|
BeginLineValid = true;
|
|
|
|
return BeginLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned RawComment::getEndLine(const SourceManager &SM) const {
|
|
|
|
if (EndLineValid)
|
|
|
|
return EndLine;
|
|
|
|
|
|
|
|
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Range.getEnd());
|
|
|
|
EndLine = SM.getLineNumber(LocInfo.first, LocInfo.second);
|
|
|
|
EndLineValid = true;
|
|
|
|
return EndLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef RawComment::getRawTextSlow(const SourceManager &SourceMgr) const {
|
|
|
|
FileID BeginFileID;
|
|
|
|
FileID EndFileID;
|
|
|
|
unsigned BeginOffset;
|
|
|
|
unsigned EndOffset;
|
|
|
|
|
|
|
|
llvm::tie(BeginFileID, BeginOffset) =
|
|
|
|
SourceMgr.getDecomposedLoc(Range.getBegin());
|
|
|
|
llvm::tie(EndFileID, EndOffset) =
|
|
|
|
SourceMgr.getDecomposedLoc(Range.getEnd());
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2012-09-11 04:32:42 +08:00
|
|
|
comments::Lexer L(Allocator, Context.getCommentCommandTraits(),
|
|
|
|
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,
|
|
|
|
const Decl *D) const {
|
2012-08-11 08:51:43 +08:00
|
|
|
// Make sure that RawText is valid.
|
|
|
|
getRawText(Context.getSourceManager());
|
|
|
|
|
2012-09-11 04:32:42 +08:00
|
|
|
comments::Lexer L(Context.getAllocator(), 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(),
|
|
|
|
Context.getCommentCommandTraits());
|
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
|
|
|
}
|
|
|
|
|
2012-06-20 08:34:58 +08:00
|
|
|
namespace {
|
|
|
|
bool containsOnlyWhitespace(StringRef Str) {
|
|
|
|
return Str.find_first_not_of(" \t\f\v\r\n") == StringRef::npos;
|
|
|
|
}
|
|
|
|
|
2012-09-10 04:47:31 +08:00
|
|
|
bool onlyWhitespaceBetween(SourceManager &SM,
|
|
|
|
SourceLocation Loc1, SourceLocation Loc2) {
|
|
|
|
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;
|
|
|
|
|
2012-09-10 04:47:31 +08:00
|
|
|
StringRef Text(Buffer + Loc1Info.second, Loc2Info.second - Loc1Info.second);
|
|
|
|
return containsOnlyWhitespace(Text);
|
2012-06-20 08:34:58 +08:00
|
|
|
}
|
|
|
|
} // unnamed namespace
|
|
|
|
|
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() &&
|
|
|
|
!SourceMgr.isBeforeInTranslationUnit(
|
2012-07-07 02:19:34 +08:00
|
|
|
Comments.back()->getSourceRange().getBegin(),
|
2012-06-22 06:04:37 +08:00
|
|
|
RC.getSourceRange().getBegin())) {
|
|
|
|
// 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
|
|
|
|
|
|
|
if (OnlyWhitespaceSeen) {
|
2012-09-10 04:47:31 +08:00
|
|
|
if (!onlyWhitespaceBetween(SourceMgr,
|
|
|
|
PrevCommentEndLoc,
|
|
|
|
RC.getSourceRange().getBegin()))
|
2012-06-20 08:34:58 +08:00
|
|
|
OnlyWhitespaceSeen = false;
|
|
|
|
}
|
|
|
|
|
2012-09-10 04:47:31 +08:00
|
|
|
PrevCommentEndLoc = RC.getSourceRange().getEnd();
|
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
|
|
|
OnlyWhitespaceSeen = true;
|
|
|
|
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.
|
|
|
|
bool Merged = false;
|
2012-06-20 08:34:58 +08:00
|
|
|
if (OnlyWhitespaceSeen &&
|
2012-08-28 09:20:53 +08:00
|
|
|
(C1.isTrailingComment() == C2.isTrailingComment())) {
|
|
|
|
unsigned C1EndLine = C1.getEndLine(SourceMgr);
|
|
|
|
unsigned C2BeginLine = C2.getBeginLine(SourceMgr);
|
|
|
|
if (C1EndLine + 1 == C2BeginLine || C1EndLine == C2BeginLine) {
|
|
|
|
SourceRange MergedRange(C1.getSourceRange().getBegin(),
|
|
|
|
C2.getSourceRange().getEnd());
|
|
|
|
*Comments.back() = RawComment(SourceMgr, MergedRange, true);
|
|
|
|
Merged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!Merged)
|
2012-07-07 02:19:34 +08:00
|
|
|
Comments.push_back(new (Allocator) RawComment(RC));
|
2012-06-20 08:34:58 +08:00
|
|
|
|
|
|
|
OnlyWhitespaceSeen = true;
|
|
|
|
}
|
|
|
|
|