clang-tools
22.0.0git
llvm-project
clang-tools-extra
clang-tidy
google
TodoCommentCheck.cpp
Go to the documentation of this file.
1
//===----------------------------------------------------------------------===//
2
//
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
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "
TodoCommentCheck.h
"
10
#include "clang/Frontend/CompilerInstance.h"
11
#include "clang/Lex/Preprocessor.h"
12
#include <optional>
13
14
namespace
clang::tidy::google::readability
{
15
16
class
TodoCommentCheck::TodoCommentHandler
:
public
CommentHandler {
17
public
:
18
TodoCommentHandler
(
TodoCommentCheck
&Check, std::optional<std::string> User)
19
: Check(Check), User(User ? *User :
"unknown"
),
20
TodoMatch(
"^// *TODO *(\\(.*\\))?:?( )?(.*)$"
) {}
21
22
bool
HandleComment
(Preprocessor &PP, SourceRange Range)
override
{
23
const
StringRef Text =
24
Lexer::getSourceText(CharSourceRange::getCharRange(Range),
25
PP.getSourceManager(), PP.getLangOpts());
26
27
SmallVector<StringRef, 4> Matches;
28
if
(!TodoMatch.match(Text, &Matches))
29
return
false
;
30
31
const
StringRef Username = Matches[1];
32
const
StringRef Comment = Matches[3];
33
34
if
(!Username.empty())
35
return
false
;
36
37
const
std::string NewText =
38
(
"// TODO("
+ Twine(User) +
"): "
+ Comment).str();
39
40
Check.diag(Range.getBegin(),
"missing username/bug in TODO"
)
41
<< FixItHint::CreateReplacement(CharSourceRange::getCharRange(Range),
42
NewText);
43
return
false
;
44
}
45
46
private
:
47
TodoCommentCheck
&Check;
48
std::string User;
49
llvm::Regex TodoMatch;
50
};
51
52
TodoCommentCheck::TodoCommentCheck
(StringRef Name,
ClangTidyContext
*Context)
53
:
ClangTidyCheck
(Name, Context),
54
Handler(std::make_unique<
TodoCommentHandler
>(
55
*this, Context->getOptions().User)) {}
56
57
TodoCommentCheck::~TodoCommentCheck
() =
default
;
58
59
void
TodoCommentCheck::registerPPCallbacks
(
const
SourceManager &SM,
60
Preprocessor *PP,
61
Preprocessor *ModuleExpanderPP) {
62
PP->addCommentHandler(Handler.get());
63
}
64
65
}
// namespace clang::tidy::google::readability
TodoCommentCheck.h
ClangTidyCheck
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition
ClangTidyDiagnosticConsumer.h:70
clang::tidy::google::readability::TodoCommentCheck::TodoCommentHandler
Definition
TodoCommentCheck.cpp:16
clang::tidy::google::readability::TodoCommentCheck::TodoCommentHandler::TodoCommentHandler
TodoCommentHandler(TodoCommentCheck &Check, std::optional< std::string > User)
Definition
TodoCommentCheck.cpp:18
clang::tidy::google::readability::TodoCommentCheck::TodoCommentHandler::HandleComment
bool HandleComment(Preprocessor &PP, SourceRange Range) override
Definition
TodoCommentCheck.cpp:22
clang::tidy::google::readability::TodoCommentCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Definition
TodoCommentCheck.cpp:59
clang::tidy::google::readability::TodoCommentCheck::~TodoCommentCheck
~TodoCommentCheck() override
clang::tidy::google::readability::TodoCommentCheck::TodoCommentCheck
TodoCommentCheck(StringRef Name, ClangTidyContext *Context)
Definition
TodoCommentCheck.cpp:52
clang::tidy::google::readability
Definition
AvoidCStyleCastsCheck.cpp:17
Generated on
for clang-tools by
1.14.0