1 //===--- TodoCommentCheck.cpp - clang-tidy --------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "TodoCommentCheck.h"
10 #include "clang/Frontend/CompilerInstance.h"
11 #include "clang/Lex/Preprocessor.h"
14 namespace clang::tidy::google::readability
{
16 class TodoCommentCheck::TodoCommentHandler
: public CommentHandler
{
18 TodoCommentHandler(TodoCommentCheck
&Check
, std::optional
<std::string
> User
)
19 : Check(Check
), User(User
? *User
: "unknown"),
20 TodoMatch("^// *TODO *(\\(.*\\))?:?( )?(.*)$") {}
22 bool HandleComment(Preprocessor
&PP
, SourceRange Range
) override
{
24 Lexer::getSourceText(CharSourceRange::getCharRange(Range
),
25 PP
.getSourceManager(), PP
.getLangOpts());
27 SmallVector
<StringRef
, 4> Matches
;
28 if (!TodoMatch
.match(Text
, &Matches
))
31 StringRef Username
= Matches
[1];
32 StringRef Comment
= Matches
[3];
34 if (!Username
.empty())
37 std::string NewText
= ("// TODO(" + Twine(User
) + "): " + Comment
).str();
39 Check
.diag(Range
.getBegin(), "missing username/bug in TODO")
40 << FixItHint::CreateReplacement(CharSourceRange::getCharRange(Range
),
46 TodoCommentCheck
&Check
;
48 llvm::Regex TodoMatch
;
51 TodoCommentCheck::TodoCommentCheck(StringRef Name
, ClangTidyContext
*Context
)
52 : ClangTidyCheck(Name
, Context
),
53 Handler(std::make_unique
<TodoCommentHandler
>(
54 *this, Context
->getOptions().User
)) {}
56 TodoCommentCheck::~TodoCommentCheck() = default;
58 void TodoCommentCheck::registerPPCallbacks(const SourceManager
&SM
,
60 Preprocessor
*ModuleExpanderPP
) {
61 PP
->addCommentHandler(Handler
.get());
64 } // namespace clang::tidy::google::readability