19 #include "llvm/ADT/STLExtras.h"
21 using namespace clang;
25 std::pair<RawComment::CommentKind, bool> getCommentKind(StringRef Comment,
26 bool ParseAllComments) {
27 const size_t MinCommentLength = ParseAllComments ? 2 : 3;
28 if ((Comment.size() < MinCommentLength) || Comment[0] !=
'/')
32 if (Comment[1] ==
'/') {
33 if (Comment.size() < 3)
36 if (Comment[2] ==
'/')
38 else if (Comment[2] ==
'!')
43 assert(Comment.size() >= 4);
47 if (Comment[1] !=
'*' ||
48 Comment[Comment.size() - 2] !=
'*' ||
49 Comment[Comment.size() - 1] !=
'/')
52 if (Comment[2] ==
'*')
54 else if (Comment[2] ==
'!')
59 const bool TrailingComment = (Comment.size() > 3) && (Comment[3] ==
'<');
60 return std::make_pair(K, TrailingComment);
63 bool mergedCommentIsTrailingComment(StringRef Comment) {
64 return (Comment.size() > 3) && (Comment[3] ==
'<');
77 return !Invalid && (C1 == C2);
92 for (
unsigned I = P;
I != 0; --
I) {
93 char C = Buffer[
I - 1];
110 bool Merged,
bool ParseAllComments) :
111 Range(SR), RawTextValid(
false), BriefTextValid(
false),
112 IsAttached(
false), IsTrailingComment(
false), IsAlmostTrailingComment(
false),
113 ParseAllComments(ParseAllComments) {
121 std::pair<CommentKind, bool> K = getCommentKind(RawText, ParseAllComments);
126 unsigned BeginOffset;
127 std::tie(BeginFileID, BeginOffset) =
129 if (BeginOffset != 0) {
130 bool Invalid =
false;
140 IsTrailingComment |= K.second;
142 IsAlmostTrailingComment = RawText.startswith(
"//<") ||
143 RawText.startswith(
"/*<");
147 IsTrailingComment || mergedCommentIsTrailingComment(RawText);
151 StringRef RawComment::getRawTextSlow(
const SourceManager &SourceMgr)
const {
154 unsigned BeginOffset;
157 std::tie(BeginFileID, BeginOffset) =
161 const unsigned Length = EndOffset - BeginOffset;
166 assert(BeginFileID == EndFileID);
168 bool Invalid =
false;
169 const char *BufferStart = SourceMgr.
getBufferData(BeginFileID,
174 return StringRef(BufferStart + BeginOffset, Length);
189 RawText.begin(), RawText.end());
192 const std::string
Result =
P.Parse();
193 const unsigned BriefTextLength = Result.size();
194 char *BriefTextPtr =
new (
Context)
char[BriefTextLength + 1];
195 memcpy(BriefTextPtr, Result.c_str(), BriefTextLength + 1);
196 BriefText = BriefTextPtr;
197 BriefTextValid =
true;
204 const Decl *D)
const {
211 RawText.begin(), RawText.end());
221 return P.parseFullComment();
226 unsigned MaxNewlinesAllowed) {
231 if (Loc1Info.first != Loc2Info.first)
234 bool Invalid =
false;
239 unsigned NumNewlines = 0;
240 assert(Loc1Info.second <= Loc2Info.second &&
"Loc1 after Loc2!");
242 for (
unsigned I = Loc1Info.second;
I != Loc2Info.second; ++
I) {
257 if (NumNewlines > MaxNewlinesAllowed)
261 if (I + 1 != Loc2Info.second &&
262 (Buffer[I + 1] ==
'\n' || Buffer[I + 1] ==
'\r') &&
263 Buffer[I] != Buffer[I + 1])
273 llvm::BumpPtrAllocator &Allocator) {
278 while (!Comments.empty() &&
292 if (Comments.empty()) {
293 Comments.push_back(
new (Allocator)
RawComment(RC));
316 commentsStartOnSameColumn(SourceMgr, C1, C2))) &&
320 *Comments.back() =
RawComment(SourceMgr, MergedRange,
true,
323 Comments.push_back(
new (Allocator)
RawComment(RC));
328 std::vector<RawComment *> MergedComments;
329 MergedComments.reserve(Comments.size() + DeserializedComments.size());
331 std::merge(Comments.begin(), Comments.end(),
332 DeserializedComments.begin(), DeserializedComments.end(),
333 std::back_inserter(MergedComments),
335 std::swap(Comments, MergedComments);
Defines the clang::ASTContext interface.
SourceLocation getEnd() const
std::unique_ptr< llvm::MemoryBuffer > Buffer
comments::CommandTraits & getCommentCommandTraits() const
StringRef getBufferData(FileID FID, bool *Invalid=nullptr) const
Return a StringRef to the source buffer data for the specified FileID.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
static LLVM_READONLY bool isHorizontalWhitespace(unsigned char c)
Returns true if this character is horizontal ASCII whitespace: ' ', '\t', '\f', '\v'.
llvm::BumpPtrAllocator & getAllocator() const
detail::InMemoryDirectory::const_iterator I
DiagnosticsEngine & getDiagnostics() const
unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid=nullptr) const
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
The result type of a method or function.
Encodes a location in the source.
SourceLocation getBegin() const
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
SourceManager & getSourceManager()
static LLVM_READONLY bool isVerticalWhitespace(unsigned char c)
Returns true if this character is vertical ASCII whitespace: '\n', '\r'.
A trivial tuple used to represent a source range.
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
This class handles loading and caching of source files into memory.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.