clang-tools  8.0.0
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 
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
TodoCommentHandler(TodoCommentCheck &Check, llvm::Optional< std::string > User)
Some operations such as code completion produce a set of candidates.
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
bool HandleComment(Preprocessor &PP, SourceRange Range) override
static constexpr llvm::StringLiteral Name
Finds TODO comments without a username or bug number.
TodoCommentCheck(StringRef Name, ClangTidyContext *Context)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:438