clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

TodoCommentCheck.cpp
Go to the documentation of this file.
1 //===--- TodoCommentCheck.cpp - clang-tidy --------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "TodoCommentCheck.h"
11 #include "clang/Frontend/CompilerInstance.h"
12 #include "clang/Lex/Preprocessor.h"
13
14 namespace clang {
15 namespace tidy {
16 namespace google {
17 namespace readability {
18
19 class TodoCommentCheck::TodoCommentHandler : public CommentHandler {
20 public:
21 TodoCommentHandler(TodoCommentCheck &Check, llvm::Optional<std::string> User)
22 : Check(Check), User(User ? *User : "unknown"),
23 TodoMatch("^// *TODO *(\\(.*\\))?:?( )?(.*)$") {}
24
25 bool HandleComment(Preprocessor &PP, SourceRange Range) override {
26 StringRef Text =
27 Lexer::getSourceText(CharSourceRange::getCharRange(Range),
28 PP.getSourceManager(), PP.getLangOpts());
29
30 SmallVector<StringRef, 4> Matches;
31 if (!TodoMatch.match(Text, &Matches))
32 return false;
33
34 StringRef Username = Matches[1];
35 StringRef Comment = Matches[3];
36
37 if (!Username.empty())
38 return false;
39
40 std::string NewText = ("// TODO(" + Twine(User) + "): " + Comment).str();
41
42 Check.diag(Range.getBegin(), "missing username/bug in TODO")
43 << FixItHint::CreateReplacement(CharSourceRange::getCharRange(Range),
44 NewText);
45 return false;
46 }
47
48 private:
49 TodoCommentCheck &Check;
50 std::string User;
51 llvm::Regex TodoMatch;
52 };
53
54 TodoCommentCheck::TodoCommentCheck(StringRef Name, ClangTidyContext *Context)
55 : ClangTidyCheck(Name, Context),
56 Handler(llvm::make_unique<TodoCommentHandler>(
57 *this, Context->getOptions().User)) {}
58
59 void TodoCommentCheck::registerPPCallbacks(CompilerInstance &Compiler) {
60 Compiler.getPreprocessor().addCommentHandler(Handler.get());
61 }
62
63 } // namespace readability
64 } // namespace google
65 } // namespace tidy
66 } // namespace clang
clang::tidy::google::readability::TodoCommentCheck::TodoCommentHandler::TodoCommentHandler
TodoCommentHandler(TodoCommentCheck &Check, llvm::Optional< std::string > User)
Definition: TodoCommentCheck.cpp:21
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
TodoCommentCheck.h
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:102
CommentHandler
clang::tidy::google::readability::TodoCommentCheck::TodoCommentHandler::HandleComment
bool HandleComment(Preprocessor &PP, SourceRange Range) override
Definition: TodoCommentCheck.cpp:25
clang::tidy::google::readability::TodoCommentCheck::TodoCommentHandler
Definition: TodoCommentCheck.cpp:19
PP
Preprocessor * PP
Definition: MacroParenthesesCheck.cpp:38
clang::tidy::google::readability::TodoCommentCheck
Finds TODO comments without a username or bug number.
Definition: TodoCommentCheck.h:23
clang::tidy::google::readability::TodoCommentCheck::TodoCommentCheck
TodoCommentCheck(StringRef Name, ClangTidyContext *Context)
Definition: TodoCommentCheck.cpp:54
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:35
Context
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:124
clang::tidy::google::readability::TodoCommentCheck::registerPPCallbacks
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.
Definition: TodoCommentCheck.cpp:59
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidy.cpp:323