2012-06-20 08:34:58 +08:00
|
|
|
//===--- RawCommentList.cpp - Processing raw comments -----------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-06-20 08:34:58 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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"
|
2015-07-16 03:13:39 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2012-06-20 08:34:58 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2020-02-28 03:01:58 +08:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2012-06-20 08:34:58 +08:00
|
|
|
|
|
|
|
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] == '<');
|
|
|
|
}
|
2015-07-16 03:13:39 +08:00
|
|
|
|
|
|
|
/// Returns true if R1 and R2 both have valid locations that start on the same
|
|
|
|
/// column.
|
|
|
|
bool commentsStartOnSameColumn(const SourceManager &SM, const RawComment &R1,
|
|
|
|
const RawComment &R2) {
|
2018-08-10 05:08:08 +08:00
|
|
|
SourceLocation L1 = R1.getBeginLoc();
|
|
|
|
SourceLocation L2 = R2.getBeginLoc();
|
2015-07-16 03:13:39 +08:00
|
|
|
bool Invalid = false;
|
|
|
|
unsigned C1 = SM.getPresumedColumnNumber(L1, &Invalid);
|
|
|
|
if (!Invalid) {
|
|
|
|
unsigned C2 = SM.getPresumedColumnNumber(L2, &Invalid);
|
|
|
|
return !Invalid && (C1 == C2);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-20 08:34:58 +08:00
|
|
|
} // unnamed namespace
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Determines whether there is only whitespace in `Buffer` between `P`
|
2015-07-16 03:13:39 +08:00
|
|
|
/// and the previous line.
|
|
|
|
/// \param Buffer The buffer to search in.
|
|
|
|
/// \param P The offset from the beginning of `Buffer` to start from.
|
|
|
|
/// \return true if all of the characters in `Buffer` ranging from the closest
|
|
|
|
/// line-ending character before `P` (or the beginning of `Buffer`) to `P - 1`
|
|
|
|
/// are whitespace.
|
|
|
|
static bool onlyWhitespaceOnLineBefore(const char *Buffer, unsigned P) {
|
|
|
|
// Search backwards until we see linefeed or carriage return.
|
|
|
|
for (unsigned I = P; I != 0; --I) {
|
|
|
|
char C = Buffer[I - 1];
|
|
|
|
if (isVerticalWhitespace(C))
|
|
|
|
return true;
|
|
|
|
if (!isHorizontalWhitespace(C))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// We hit the beginning of the buffer.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether `K` is an ordinary comment kind.
|
|
|
|
static bool isOrdinaryKind(RawComment::CommentKind K) {
|
|
|
|
return (K == RawComment::RCK_OrdinaryBCPL) ||
|
|
|
|
(K == RawComment::RCK_OrdinaryC);
|
|
|
|
}
|
|
|
|
|
2012-06-20 08:34:58 +08:00
|
|
|
RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR,
|
[NFC] Move CommentOpts checks to the call sites that depend on it. (Re-applying r326501.)
When parsing comments, for example, for -Wdocumentation, slightly different
behaviour occurs when -fparse-all-comments is specified. However, these
differences are subtle:
1. All comments are saved during parsing, regardless of whether they are doc
comments or not.
2. "Maybe-doc" comments, like <, !, etc, are saved as such, instead of marking
them as ordinary comments. The maybe-doc type of comment is never saved
otherwise. (Warning on these is the impetus of -Wdocumentation.)
3. All comments are treated as doc comments in ASTContext, even if they are ordinary.
This change moves the logic for checking CommentOptions.ParseAllComments closer
to where it has an effect. The overall logic is unchanged, but checks of the
ParseAllComments flag are now done where the effect will be clearer.
Subscribers: cfe-commits
llvm-svn: 326512
2018-03-02 08:07:45 +08:00
|
|
|
const CommentOptions &CommentOpts, bool Merged) :
|
2012-06-27 13:48:36 +08:00
|
|
|
Range(SR), RawTextValid(false), BriefTextValid(false),
|
[NFC] Move CommentOpts checks to the call sites that depend on it. (Re-applying r326501.)
When parsing comments, for example, for -Wdocumentation, slightly different
behaviour occurs when -fparse-all-comments is specified. However, these
differences are subtle:
1. All comments are saved during parsing, regardless of whether they are doc
comments or not.
2. "Maybe-doc" comments, like <, !, etc, are saved as such, instead of marking
them as ordinary comments. The maybe-doc type of comment is never saved
otherwise. (Warning on these is the impetus of -Wdocumentation.)
3. All comments are treated as doc comments in ASTContext, even if they are ordinary.
This change moves the logic for checking CommentOptions.ParseAllComments closer
to where it has an effect. The overall logic is unchanged, but checks of the
ParseAllComments flag are now done where the effect will be clearer.
Subscribers: cfe-commits
llvm-svn: 326512
2018-03-02 08:07:45 +08:00
|
|
|
IsAttached(false), IsTrailingComment(false),
|
|
|
|
IsAlmostTrailingComment(false) {
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-16 03:13:39 +08:00
|
|
|
// Guess comment kind.
|
[NFC] Move CommentOpts checks to the call sites that depend on it. (Re-applying r326501.)
When parsing comments, for example, for -Wdocumentation, slightly different
behaviour occurs when -fparse-all-comments is specified. However, these
differences are subtle:
1. All comments are saved during parsing, regardless of whether they are doc
comments or not.
2. "Maybe-doc" comments, like <, !, etc, are saved as such, instead of marking
them as ordinary comments. The maybe-doc type of comment is never saved
otherwise. (Warning on these is the impetus of -Wdocumentation.)
3. All comments are treated as doc comments in ASTContext, even if they are ordinary.
This change moves the logic for checking CommentOptions.ParseAllComments closer
to where it has an effect. The overall logic is unchanged, but checks of the
ParseAllComments flag are now done where the effect will be clearer.
Subscribers: cfe-commits
llvm-svn: 326512
2018-03-02 08:07:45 +08:00
|
|
|
std::pair<CommentKind, bool> K =
|
|
|
|
getCommentKind(RawText, CommentOpts.ParseAllComments);
|
2015-07-16 03:13:39 +08:00
|
|
|
|
|
|
|
// Guess whether an ordinary comment is trailing.
|
[NFC] Move CommentOpts checks to the call sites that depend on it. (Re-applying r326501.)
When parsing comments, for example, for -Wdocumentation, slightly different
behaviour occurs when -fparse-all-comments is specified. However, these
differences are subtle:
1. All comments are saved during parsing, regardless of whether they are doc
comments or not.
2. "Maybe-doc" comments, like <, !, etc, are saved as such, instead of marking
them as ordinary comments. The maybe-doc type of comment is never saved
otherwise. (Warning on these is the impetus of -Wdocumentation.)
3. All comments are treated as doc comments in ASTContext, even if they are ordinary.
This change moves the logic for checking CommentOptions.ParseAllComments closer
to where it has an effect. The overall logic is unchanged, but checks of the
ParseAllComments flag are now done where the effect will be clearer.
Subscribers: cfe-commits
llvm-svn: 326512
2018-03-02 08:07:45 +08:00
|
|
|
if (CommentOpts.ParseAllComments && isOrdinaryKind(K.first)) {
|
2015-07-16 03:13:39 +08:00
|
|
|
FileID BeginFileID;
|
|
|
|
unsigned BeginOffset;
|
|
|
|
std::tie(BeginFileID, BeginOffset) =
|
|
|
|
SourceMgr.getDecomposedLoc(Range.getBegin());
|
|
|
|
if (BeginOffset != 0) {
|
|
|
|
bool Invalid = false;
|
|
|
|
const char *Buffer =
|
|
|
|
SourceMgr.getBufferData(BeginFileID, &Invalid).data();
|
|
|
|
IsTrailingComment |=
|
|
|
|
(!Invalid && !onlyWhitespaceOnLineBefore(Buffer, BeginOffset));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-20 08:34:58 +08:00
|
|
|
if (!Merged) {
|
|
|
|
Kind = K.first;
|
2015-07-16 03:13:39 +08:00
|
|
|
IsTrailingComment |= K.second;
|
2012-06-20 08:34:58 +08:00
|
|
|
|
|
|
|
IsAlmostTrailingComment = RawText.startswith("//<") ||
|
|
|
|
RawText.startswith("/*<");
|
|
|
|
} else {
|
2012-07-04 15:30:26 +08:00
|
|
|
Kind = RCK_Merged;
|
2015-07-16 03:13:39 +08:00
|
|
|
IsTrailingComment =
|
|
|
|
IsTrailingComment || mergedCommentIsTrailingComment(RawText);
|
2012-06-20 08:34:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-10-17 04:12:42 +08:00
|
|
|
// Lazily initialize RawText using the accessor before using it.
|
|
|
|
(void)getRawText(Context.getSourceManager());
|
2012-06-27 04:39:18 +08:00
|
|
|
|
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 {
|
2016-10-17 04:12:42 +08:00
|
|
|
// Lazily initialize RawText using the accessor before using it.
|
|
|
|
(void)getRawText(Context.getSourceManager());
|
2012-08-11 08:51:43 +08:00
|
|
|
|
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,
|
[NFC] Move CommentOpts checks to the call sites that depend on it. (Re-applying r326501.)
When parsing comments, for example, for -Wdocumentation, slightly different
behaviour occurs when -fparse-all-comments is specified. However, these
differences are subtle:
1. All comments are saved during parsing, regardless of whether they are doc
comments or not.
2. "Maybe-doc" comments, like <, !, etc, are saved as such, instead of marking
them as ordinary comments. The maybe-doc type of comment is never saved
otherwise. (Warning on these is the impetus of -Wdocumentation.)
3. All comments are treated as doc comments in ASTContext, even if they are ordinary.
This change moves the logic for checking CommentOptions.ParseAllComments closer
to where it has an effect. The overall logic is unchanged, but checks of the
ParseAllComments flag are now done where the effect will be clearer.
Subscribers: cfe-commits
llvm-svn: 326512
2018-03-02 08:07:45 +08:00
|
|
|
const CommentOptions &CommentOpts,
|
2012-07-07 02:19:34 +08:00
|
|
|
llvm::BumpPtrAllocator &Allocator) {
|
2012-06-20 08:34:58 +08:00
|
|
|
if (RC.isInvalid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Ordinary comments are not interesting for us.
|
[NFC] Move CommentOpts checks to the call sites that depend on it. (Re-applying r326501.)
When parsing comments, for example, for -Wdocumentation, slightly different
behaviour occurs when -fparse-all-comments is specified. However, these
differences are subtle:
1. All comments are saved during parsing, regardless of whether they are doc
comments or not.
2. "Maybe-doc" comments, like <, !, etc, are saved as such, instead of marking
them as ordinary comments. The maybe-doc type of comment is never saved
otherwise. (Warning on these is the impetus of -Wdocumentation.)
3. All comments are treated as doc comments in ASTContext, even if they are ordinary.
This change moves the logic for checking CommentOptions.ParseAllComments closer
to where it has an effect. The overall logic is unchanged, but checks of the
ParseAllComments flag are now done where the effect will be clearer.
Subscribers: cfe-commits
llvm-svn: 326512
2018-03-02 08:07:45 +08:00
|
|
|
if (RC.isOrdinary() && !CommentOpts.ParseAllComments)
|
2012-06-20 08:34:58 +08:00
|
|
|
return;
|
|
|
|
|
2019-08-14 02:11:44 +08:00
|
|
|
std::pair<FileID, unsigned> Loc =
|
|
|
|
SourceMgr.getDecomposedLoc(RC.getBeginLoc());
|
|
|
|
|
|
|
|
const FileID CommentFile = Loc.first;
|
|
|
|
const unsigned CommentOffset = Loc.second;
|
|
|
|
|
2012-06-20 08:34:58 +08:00
|
|
|
// If this is the first Doxygen comment, save it (because there isn't
|
|
|
|
// anything to merge it with).
|
2019-08-14 02:11:44 +08:00
|
|
|
if (OrderedComments[CommentFile].empty()) {
|
|
|
|
OrderedComments[CommentFile][CommentOffset] =
|
|
|
|
new (Allocator) RawComment(RC);
|
2012-06-20 08:34:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-14 02:11:44 +08:00
|
|
|
const RawComment &C1 = *OrderedComments[CommentFile].rbegin()->second;
|
2012-06-20 08:34:58 +08:00
|
|
|
const RawComment &C2 = RC;
|
|
|
|
|
|
|
|
// Merge comments only if there is only whitespace between them.
|
2015-07-16 03:13:39 +08:00
|
|
|
// Can't merge trailing and non-trailing comments unless the second is
|
|
|
|
// non-trailing ordinary in the same column, as in the case:
|
|
|
|
// int x; // documents x
|
|
|
|
// // more text
|
|
|
|
// versus:
|
|
|
|
// int x; // documents x
|
|
|
|
// int y; // documents y
|
|
|
|
// or:
|
|
|
|
// int x; // documents x
|
|
|
|
// // documents y
|
|
|
|
// int y;
|
2012-08-28 09:20:53 +08:00
|
|
|
// Merge comments if they are on same or consecutive lines.
|
2015-07-16 03:13:39 +08:00
|
|
|
if ((C1.isTrailingComment() == C2.isTrailingComment() ||
|
|
|
|
(C1.isTrailingComment() && !C2.isTrailingComment() &&
|
|
|
|
isOrdinaryKind(C2.getKind()) &&
|
|
|
|
commentsStartOnSameColumn(SourceMgr, C1, C2))) &&
|
2018-08-10 05:09:38 +08:00
|
|
|
onlyWhitespaceBetween(SourceMgr, C1.getEndLoc(), C2.getBeginLoc(),
|
2013-09-28 23:06:27 +08:00
|
|
|
/*MaxNewlinesAllowed=*/1)) {
|
2018-08-10 05:09:38 +08:00
|
|
|
SourceRange MergedRange(C1.getBeginLoc(), C2.getEndLoc());
|
2019-08-14 02:11:44 +08:00
|
|
|
*OrderedComments[CommentFile].rbegin()->second =
|
|
|
|
RawComment(SourceMgr, MergedRange, CommentOpts, true);
|
2013-09-28 23:06:27 +08:00
|
|
|
} else {
|
2019-08-14 02:11:44 +08:00
|
|
|
OrderedComments[CommentFile][CommentOffset] =
|
|
|
|
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
|
|
|
|
2019-08-14 02:11:44 +08:00
|
|
|
const std::map<unsigned, RawComment *> *
|
|
|
|
RawCommentList::getCommentsInFile(FileID File) const {
|
|
|
|
auto CommentsInFile = OrderedComments.find(File);
|
|
|
|
if (CommentsInFile == OrderedComments.end())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return &CommentsInFile->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RawCommentList::empty() const { return OrderedComments.empty(); }
|
|
|
|
|
|
|
|
unsigned RawCommentList::getCommentBeginLine(RawComment *C, FileID File,
|
|
|
|
unsigned Offset) const {
|
|
|
|
auto Cached = CommentBeginLine.find(C);
|
|
|
|
if (Cached != CommentBeginLine.end())
|
|
|
|
return Cached->second;
|
|
|
|
const unsigned Line = SourceMgr.getLineNumber(File, Offset);
|
|
|
|
CommentBeginLine[C] = Line;
|
|
|
|
return Line;
|
|
|
|
}
|
2014-03-27 23:40:39 +08:00
|
|
|
|
2019-08-14 02:11:44 +08:00
|
|
|
unsigned RawCommentList::getCommentEndOffset(RawComment *C) const {
|
|
|
|
auto Cached = CommentEndOffset.find(C);
|
|
|
|
if (Cached != CommentEndOffset.end())
|
|
|
|
return Cached->second;
|
|
|
|
const unsigned Offset =
|
|
|
|
SourceMgr.getDecomposedLoc(C->getSourceRange().getEnd()).second;
|
|
|
|
CommentEndOffset[C] = Offset;
|
|
|
|
return Offset;
|
2014-03-27 23:40:39 +08:00
|
|
|
}
|
2018-05-16 20:30:09 +08:00
|
|
|
|
|
|
|
std::string RawComment::getFormattedText(const SourceManager &SourceMgr,
|
|
|
|
DiagnosticsEngine &Diags) const {
|
|
|
|
llvm::StringRef CommentText = getRawText(SourceMgr);
|
|
|
|
if (CommentText.empty())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
llvm::BumpPtrAllocator Allocator;
|
|
|
|
// We do not parse any commands, so CommentOptions are ignored by
|
|
|
|
// comments::Lexer. Therefore, we just use default-constructed options.
|
|
|
|
CommentOptions DefOpts;
|
|
|
|
comments::CommandTraits EmptyTraits(Allocator, DefOpts);
|
|
|
|
comments::Lexer L(Allocator, Diags, EmptyTraits, getSourceRange().getBegin(),
|
|
|
|
CommentText.begin(), CommentText.end(),
|
|
|
|
/*ParseCommands=*/false);
|
|
|
|
|
|
|
|
std::string Result;
|
|
|
|
// A column number of the first non-whitespace token in the comment text.
|
|
|
|
// We skip whitespace up to this column, but keep the whitespace after this
|
|
|
|
// column. IndentColumn is calculated when lexing the first line and reused
|
|
|
|
// for the rest of lines.
|
|
|
|
unsigned IndentColumn = 0;
|
|
|
|
|
|
|
|
// Processes one line of the comment and adds it to the result.
|
|
|
|
// Handles skipping the indent at the start of the line.
|
|
|
|
// Returns false when eof is reached and true otherwise.
|
|
|
|
auto LexLine = [&](bool IsFirstLine) -> bool {
|
|
|
|
comments::Token Tok;
|
|
|
|
// Lex the first token on the line. We handle it separately, because we to
|
|
|
|
// fix up its indentation.
|
|
|
|
L.lex(Tok);
|
|
|
|
if (Tok.is(comments::tok::eof))
|
|
|
|
return false;
|
|
|
|
if (Tok.is(comments::tok::newline)) {
|
|
|
|
Result += "\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
llvm::StringRef TokText = L.getSpelling(Tok, SourceMgr);
|
|
|
|
bool LocInvalid = false;
|
|
|
|
unsigned TokColumn =
|
|
|
|
SourceMgr.getSpellingColumnNumber(Tok.getLocation(), &LocInvalid);
|
|
|
|
assert(!LocInvalid && "getFormattedText for invalid location");
|
|
|
|
|
|
|
|
// Amount of leading whitespace in TokText.
|
|
|
|
size_t WhitespaceLen = TokText.find_first_not_of(" \t");
|
|
|
|
if (WhitespaceLen == StringRef::npos)
|
|
|
|
WhitespaceLen = TokText.size();
|
|
|
|
// Remember the amount of whitespace we skipped in the first line to remove
|
|
|
|
// indent up to that column in the following lines.
|
|
|
|
if (IsFirstLine)
|
|
|
|
IndentColumn = TokColumn + WhitespaceLen;
|
|
|
|
|
|
|
|
// Amount of leading whitespace we actually want to skip.
|
|
|
|
// For the first line we skip all the whitespace.
|
|
|
|
// For the rest of the lines, we skip whitespace up to IndentColumn.
|
|
|
|
unsigned SkipLen =
|
|
|
|
IsFirstLine
|
|
|
|
? WhitespaceLen
|
|
|
|
: std::min<size_t>(
|
|
|
|
WhitespaceLen,
|
|
|
|
std::max<int>(static_cast<int>(IndentColumn) - TokColumn, 0));
|
|
|
|
llvm::StringRef Trimmed = TokText.drop_front(SkipLen);
|
|
|
|
Result += Trimmed;
|
|
|
|
// Lex all tokens in the rest of the line.
|
|
|
|
for (L.lex(Tok); Tok.isNot(comments::tok::eof); L.lex(Tok)) {
|
|
|
|
if (Tok.is(comments::tok::newline)) {
|
|
|
|
Result += "\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Result += L.getSpelling(Tok, SourceMgr);
|
|
|
|
}
|
|
|
|
// We've reached the end of file token.
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto DropTrailingNewLines = [](std::string &Str) {
|
2020-04-06 16:38:30 +08:00
|
|
|
while (!Str.empty() && Str.back() == '\n')
|
2018-05-16 20:30:09 +08:00
|
|
|
Str.pop_back();
|
|
|
|
};
|
|
|
|
|
Misc typos fixes in ./lib folder
Summary: Found via `codespell -q 3 -I ../clang-whitelist.txt -L uint,importd,crasher,gonna,cant,ue,ons,orign,ned`
Reviewers: teemperor
Reviewed By: teemperor
Subscribers: teemperor, jholewinski, jvesely, nhaehnle, whisperity, jfb, cfe-commits
Differential Revision: https://reviews.llvm.org/D55475
llvm-svn: 348755
2018-12-10 20:37:46 +08:00
|
|
|
// Process first line separately to remember indent for the following lines.
|
2018-05-16 20:30:09 +08:00
|
|
|
if (!LexLine(/*IsFirstLine=*/true)) {
|
|
|
|
DropTrailingNewLines(Result);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
// Process the rest of the lines.
|
|
|
|
while (LexLine(/*IsFirstLine=*/false))
|
|
|
|
;
|
|
|
|
DropTrailingNewLines(Result);
|
|
|
|
return Result;
|
|
|
|
}
|