clang-tools 23.0.0git
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
14namespace clang::tidy {
15
16namespace google::readability {
17namespace {
18
19enum class StyleKind { Parentheses, Hyphen };
20
21} // namespace
22} // namespace google::readability
23
25 static ArrayRef<std::pair<google::readability::StyleKind, StringRef>>
27 static constexpr std::pair<google::readability::StyleKind, StringRef>
28 Mapping[] = {
29 {google::readability::StyleKind::Hyphen, "Hyphen"},
30 {google::readability::StyleKind::Parentheses, "Parentheses"},
31 };
32 return {Mapping};
33 }
34};
35
36} // namespace clang::tidy
37
39class TodoCommentCheck::TodoCommentHandler : public CommentHandler {
40public:
41 TodoCommentHandler(TodoCommentCheck &Check, std::optional<std::string> User)
42 : Check(Check), User(User ? *User : "unknown"),
43 TodoMatch(R"(^// *TODO *((\‍((.*)\))?:?( )?|: *(.*) *- *)?(.*)$)") {
44 const StringRef TodoStyleString = Check.Options.get("Style", "Hyphen");
45 for (const auto &[Value, Name] :
47 if (Name == TodoStyleString) {
48 TodoStyle = Value;
49 return;
50 }
51 }
52 Check.configurationDiag(
53 "invalid value '%0' for "
54 "google-readability-todo.Style; valid values are "
55 "'Parentheses' and 'Hyphen'. Defaulting to 'Hyphen'")
56 << TodoStyleString;
57 }
58
59 bool HandleComment(Preprocessor &PP, SourceRange Range) override {
60 const StringRef Text =
61 Lexer::getSourceText(CharSourceRange::getCharRange(Range),
62 PP.getSourceManager(), PP.getLangOpts());
63
65 if (!TodoMatch.match(Text, &Matches))
66 return false;
67
68 const StyleKind ParsedStyle =
69 !Matches[3].empty() ? StyleKind::Parentheses : StyleKind::Hyphen;
70 const StringRef Username =
71 ParsedStyle == StyleKind::Parentheses ? Matches[3] : Matches[5];
72 const StringRef Comment = Matches[6];
73
74 if (!Username.empty() &&
75 (ParsedStyle == StyleKind::Parentheses || !Comment.empty())) {
76 return false;
77 }
78
79 if (Username.empty()) {
80 Check.diag(Range.getBegin(), "missing username/bug in TODO")
81 << FixItHint::CreateReplacement(
82 CharSourceRange::getCharRange(Range),
83 createReplacementString(Username, Comment));
84 }
85
86 if (Comment.empty())
87 Check.diag(Range.getBegin(), "missing details in TODO");
88
89 return false;
90 }
91
92 std::string createReplacementString(const StringRef Username,
93 const StringRef Comment) const {
94 if (TodoStyle == StyleKind::Parentheses) {
95 return ("// TODO(" + Twine(User) +
96 "): " + (Comment.empty() ? "some details" : Comment))
97 .str();
98 }
99 return ("// TODO: " + Twine(User) + " - " +
100 (Comment.empty() ? "some details" : Comment))
101 .str();
102 }
103
104 StyleKind getTodoStyle() const { return TodoStyle; }
105
106private:
107 TodoCommentCheck &Check;
108 std::string User;
109 llvm::Regex TodoMatch;
110 StyleKind TodoStyle = StyleKind::Hyphen;
111};
112
114 : ClangTidyCheck(Name, Context),
115 Handler(std::make_unique<TodoCommentHandler>(
116 *this, Context->getOptions().User)) {}
117
119
120void TodoCommentCheck::registerPPCallbacks(const SourceManager &SM,
121 Preprocessor *PP,
122 Preprocessor *ModuleExpanderPP) {
123 PP->addCommentHandler(Handler.get());
124}
125
127 Options.store(Opts, "Style", Handler->getTodoStyle());
128}
129
130} // namespace clang::tidy::google::readability
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
TodoCommentHandler(TodoCommentCheck &Check, std::optional< std::string > User)
std::string createReplacementString(const StringRef Username, const StringRef Comment) const
bool HandleComment(Preprocessor &PP, SourceRange Range) override
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
TodoCommentCheck(StringRef Name, ClangTidyContext *Context)
llvm::StringMap< ClangTidyValue > OptionMap
static ArrayRef< std::pair< google::readability::StyleKind, StringRef > > getEnumMapping()
This class should be specialized by any enum type that needs to be converted to and from an llvm::Str...
static ArrayRef< std::pair< T, StringRef > > getEnumMapping()=delete