================
@@ -77,67 +79,22 @@ void ArgumentCommentCheck::registerMatchers(MatchFinder
*Finder) {
this);
}
-static std::vector<std::pair<SourceLocation, StringRef>>
-getCommentsInRange(ASTContext *Ctx, CharSourceRange Range) {
- std::vector<std::pair<SourceLocation, StringRef>> Comments;
- auto &SM = Ctx->getSourceManager();
- const std::pair<FileID, unsigned> BeginLoc =
- SM.getDecomposedLoc(Range.getBegin()),
- EndLoc =
- SM.getDecomposedLoc(Range.getEnd());
-
- if (BeginLoc.first != EndLoc.first)
- return Comments;
-
- bool Invalid = false;
- const StringRef Buffer = SM.getBufferData(BeginLoc.first, &Invalid);
- if (Invalid)
- return Comments;
-
- const char *StrData = Buffer.data() + BeginLoc.second;
-
- Lexer TheLexer(SM.getLocForStartOfFile(BeginLoc.first), Ctx->getLangOpts(),
- Buffer.begin(), StrData, Buffer.end());
- TheLexer.SetCommentRetentionState(true);
-
- while (true) {
- Token Tok;
- if (TheLexer.LexFromRawLexer(Tok))
- break;
- if (Tok.getLocation() == Range.getEnd() || Tok.is(tok::eof))
- break;
-
- if (Tok.is(tok::comment)) {
- const std::pair<FileID, unsigned> CommentLoc =
- SM.getDecomposedLoc(Tok.getLocation());
- assert(CommentLoc.first == BeginLoc.first);
- Comments.emplace_back(
- Tok.getLocation(),
- StringRef(Buffer.begin() + CommentLoc.second, Tok.getLength()));
- } else {
- // Clear comments found before the different token, e.g. comma.
- Comments.clear();
- }
- }
-
- return Comments;
-}
-
-static std::vector<std::pair<SourceLocation, StringRef>>
-getCommentsBeforeLoc(ASTContext *Ctx, SourceLocation Loc) {
- std::vector<std::pair<SourceLocation, StringRef>> Comments;
+static std::vector<CommentToken> getCommentsBeforeLoc(ASTContext *Ctx,
+ SourceLocation Loc) {
+ std::vector<CommentToken> Comments;
while (Loc.isValid()) {
const std::optional<Token> Tok = utils::lexer::getPreviousToken(
Loc, Ctx->getSourceManager(), Ctx->getLangOpts(),
/*SkipComments=*/false);
if (!Tok || Tok->isNot(tok::comment))
break;
Loc = Tok->getLocation();
- Comments.emplace_back(
+ Comments.emplace_back(CommentToken{
Loc,
Lexer::getSourceText(CharSourceRange::getCharRange(
Loc, Loc.getLocWithOffset(Tok->getLength())),
- Ctx->getSourceManager(), Ctx->getLangOpts()));
+ Ctx->getSourceManager(), Ctx->getLangOpts()),
+ });
----------------
localspook wrote:
```suggestion
Comments.emplace_back(
Loc,
Lexer::getSourceText(CharSourceRange::getCharRange(
Loc, Loc.getLocWithOffset(Tok->getLength())),
Ctx->getSourceManager(), Ctx->getLangOpts())
);
```
(will probably need a reformatting)
https://github.com/llvm/llvm-project/pull/180371
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits