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) {
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) {
285 const unsigned CommentOffset =
Loc.second;
289 auto &OC = OrderedComments[CommentFile];
291 OC[CommentOffset] =
new (Allocator)
RawComment(RC);
314 commentsStartOnSameColumn(SourceMgr, C1, C2))) &&
318 *OrderedComments[CommentFile].rbegin()->second =
319 RawComment(SourceMgr, MergedRange, CommentOpts,
true);
321 OrderedComments[CommentFile][CommentOffset] =
326const std::map<unsigned, RawComment *> *
328 auto CommentsInFile = OrderedComments.find(
File);
329 if (CommentsInFile == OrderedComments.end())
332 return &CommentsInFile->second;
338 unsigned Offset)
const {
339 auto Cached = CommentBeginLine.find(
C);
340 if (Cached != CommentBeginLine.end())
341 return Cached->second;
343 CommentBeginLine[
C] =
Line;
348 auto Cached = CommentEndOffset.find(
C);
349 if (Cached != CommentEndOffset.end())
350 return Cached->second;
351 const unsigned Offset =
353 CommentEndOffset[
C] = Offset;
359 llvm::StringRef CommentText =
getRawText(SourceMgr);
360 if (CommentText.empty())
368 auto LastChar =
Result.find_last_not_of(
'\n');
374std::vector<RawComment::CommentLine>
377 llvm::StringRef CommentText =
getRawText(SourceMgr);
378 if (CommentText.empty())
381 llvm::BumpPtrAllocator Allocator;
387 CommentText.begin(), CommentText.end(),
390 std::vector<RawComment::CommentLine>
Result;
395 unsigned IndentColumn = 0;
405 unsigned PreviousLine = 0;
410 auto LexLine = [&](
bool IsFirstLine) ->
bool {
419 if (
Loc.getLine() != PreviousLine) {
421 PreviousLine =
Loc.getLine();
426 llvm::StringRef TokText = L.
getSpelling(Tok, SourceMgr);
427 bool LocInvalid =
false;
430 assert(!LocInvalid &&
"getFormattedText for invalid location");
433 size_t WhitespaceLen = TokText.find_first_not_of(
" \t");
434 if (WhitespaceLen == StringRef::npos)
435 WhitespaceLen = TokText.size();
439 IndentColumn = TokColumn + WhitespaceLen;
449 std::max<int>(
static_cast<int>(IndentColumn) - TokColumn, 0));
450 llvm::StringRef Trimmed = TokText.drop_front(SkipLen);
461 if (End.getLine() != PreviousLine) {
463 PreviousLine = End.getLine();
479 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.
FileIDAndOffset getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
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
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'.
std::pair< FileID, unsigned > FileIDAndOffset
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.