18#include "llvm/Support/Allocator.h"
24std::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] !=
'/')
31 if (Comment[1] ==
'/') {
32 if (Comment.size() < 3)
35 if (Comment[2] ==
'/')
37 else if (Comment[2] ==
'!')
42 assert(Comment.size() >= 4);
46 if (Comment[1] !=
'*' ||
47 Comment[Comment.size() - 2] !=
'*' ||
48 Comment[Comment.size() - 1] !=
'/')
51 if (Comment[2] ==
'*')
53 else if (Comment[2] ==
'!')
58 const bool TrailingComment = (Comment.size() > 3) && (Comment[3] ==
'<');
59 return std::make_pair(K, TrailingComment);
62bool mergedCommentIsTrailingComment(StringRef Comment) {
63 return (Comment.size() > 3) && (Comment[3] ==
'<');
73 unsigned C1 =
SM.getPresumedColumnNumber(L1, &
Invalid);
75 unsigned C2 =
SM.getPresumedColumnNumber(L2, &
Invalid);
91 for (
unsigned I =
P; I != 0; --I) {
92 char C = Buffer[I - 1];
112 IsAlmostTrailingComment(
false) {
120 std::pair<CommentKind, bool> K =
126 unsigned BeginOffset;
127 std::tie(BeginFileID, BeginOffset) =
129 if (BeginOffset != 0) {
140 IsTrailingComment |= K.second;
142 IsAlmostTrailingComment =
143 RawText.starts_with(
"//<") || RawText.starts_with(
"/*<");
147 IsTrailingComment || mergedCommentIsTrailingComment(RawText);
151StringRef RawComment::getRawTextSlow(
const SourceManager &SourceMgr)
const {
154 unsigned BeginOffset;
157 std::tie(BeginFileID, BeginOffset) =
161 const unsigned Length = EndOffset - BeginOffset;
166 assert(BeginFileID == EndFileID);
169 const char *BufferStart = SourceMgr.
getBufferData(BeginFileID,
174 return StringRef(BufferStart + BeginOffset, Length);
177const char *RawComment::extractBriefText(
const ASTContext &Context)
const {
184 llvm::BumpPtrAllocator Allocator;
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) {
227 std::pair<FileID, unsigned> Loc1Info =
SM.getDecomposedLoc(Loc1);
228 std::pair<FileID, unsigned> Loc2Info =
SM.getDecomposedLoc(Loc2);
231 if (Loc1Info.first != Loc2Info.first)
235 const char *Buffer =
SM.getBufferData(Loc1Info.first, &
Invalid).data();
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])
274 llvm::BumpPtrAllocator &Allocator) {
282 std::pair<FileID, unsigned>
Loc =
286 const unsigned CommentOffset =
Loc.second;
290 if (OrderedComments[CommentFile].
empty()) {
291 OrderedComments[CommentFile][CommentOffset] =
296 const RawComment &C1 = *OrderedComments[CommentFile].rbegin()->second;
315 commentsStartOnSameColumn(SourceMgr, C1, C2))) &&
319 *OrderedComments[CommentFile].rbegin()->second =
320 RawComment(SourceMgr, MergedRange, CommentOpts,
true);
322 OrderedComments[CommentFile][CommentOffset] =
327const std::map<unsigned, RawComment *> *
329 auto CommentsInFile = OrderedComments.find(
File);
330 if (CommentsInFile == OrderedComments.end())
333 return &CommentsInFile->second;
339 unsigned Offset)
const {
340 auto Cached = CommentBeginLine.find(
C);
341 if (Cached != CommentBeginLine.end())
342 return Cached->second;
344 CommentBeginLine[
C] =
Line;
349 auto Cached = CommentEndOffset.find(
C);
350 if (Cached != CommentEndOffset.end())
351 return Cached->second;
352 const unsigned Offset =
354 CommentEndOffset[
C] = Offset;
360 llvm::StringRef CommentText =
getRawText(SourceMgr);
361 if (CommentText.empty())
369 auto LastChar =
Result.find_last_not_of(
'\n');
375std::vector<RawComment::CommentLine>
378 llvm::StringRef CommentText =
getRawText(SourceMgr);
379 if (CommentText.empty())
382 llvm::BumpPtrAllocator Allocator;
388 CommentText.begin(), CommentText.end(),
391 std::vector<RawComment::CommentLine>
Result;
396 unsigned IndentColumn = 0;
406 unsigned PreviousLine = 0;
411 auto LexLine = [&](
bool IsFirstLine) ->
bool {
420 if (
Loc.getLine() != PreviousLine) {
422 PreviousLine =
Loc.getLine();
427 llvm::StringRef TokText = L.
getSpelling(Tok, SourceMgr);
428 bool LocInvalid =
false;
431 assert(!LocInvalid &&
"getFormattedText for invalid location");
434 size_t WhitespaceLen = TokText.find_first_not_of(
" \t");
435 if (WhitespaceLen == StringRef::npos)
436 WhitespaceLen = TokText.size();
440 IndentColumn = TokColumn + WhitespaceLen;
450 std::max<int>(
static_cast<int>(IndentColumn) - TokColumn, 0));
451 llvm::StringRef Trimmed = TokText.drop_front(SkipLen);
462 if (End.getLine() != PreviousLine) {
464 PreviousLine = End.getLine();
480 while (LexLine(
false))
Defines the clang::ASTContext interface.
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
SourceManager & getSourceManager()
comments::CommandTraits & getCommentCommandTraits() const
llvm::BumpPtrAllocator & getAllocator() const
DiagnosticsEngine & getDiagnostics() const
Decl - This represents one declaration (or definition), e.g.
Concrete class used by the front-end to report problems and issues.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Represents an unpacked "presumed" location which can be presented to the user.
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
StringRef getBufferData(FileID FID, bool *Invalid=nullptr) const
Return a StringRef to the source buffer data for the specified FileID.
unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid=nullptr) const
Given a SourceLocation, return the spelling line number for the position indicated.
unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid=nullptr) const
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
The JSON file list parser is used to communicate input to InstallAPI.
LLVM_READONLY bool isVerticalWhitespace(unsigned char c)
Returns true if this character is vertical ASCII whitespace: '\n', '\r'.
LLVM_READONLY bool isHorizontalWhitespace(unsigned char c)
Returns true if this character is horizontal ASCII whitespace: ' ', '\t', '\f', '\v'.
@ Result
The result type of a method or function.