clang-tools 22.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 llvm::StringRef TodoStyleString =
45 Check.Options.get("Style", "Hyphen");
46 for (const auto &[Value, Name] :
48 if (Name == TodoStyleString) {
49 TodoStyle = Value;
50 return;
51 }
52 }
53 Check.configurationDiag(
54 "invalid value '%0' for "
55 "google-readability-todo.Style; valid values are "
56 "'Parentheses' and 'Hyphen'. Defaulting to 'Hyphen'")
57 << TodoStyleString;
58 }
59
60 bool HandleComment(Preprocessor &PP, SourceRange Range) override {
61 const StringRef Text =
62 Lexer::getSourceText(CharSourceRange::getCharRange(Range),
63 PP.getSourceManager(), PP.getLangOpts());
64
65 SmallVector<StringRef, 7> Matches;
66 if (!TodoMatch.match(Text, &Matches))
67 return false;
68
69 const StyleKind ParsedStyle =
70 !Matches[3].empty() ? StyleKind::Parentheses : StyleKind::Hyphen;
71 const StringRef Username =
72 ParsedStyle == StyleKind::Parentheses ? Matches[3] : Matches[5];
73 const StringRef Comment = Matches[6];
74
75 if (!Username.empty() &&
76 (ParsedStyle == StyleKind::Parentheses || !Comment.empty())) {
77 return false;
78 }
79
80 if (Username.empty()) {
81 Check.diag(Range.getBegin(), "missing username/bug in TODO")
82 << FixItHint::CreateReplacement(
83 CharSourceRange::getCharRange(Range),
84 createReplacementString(Username, Comment));
85 }
86
87 if (Comment.empty())
88 Check.diag(Range.getBegin(), "missing details in TODO");
89
90 return false;
91 }
92
93 std::string createReplacementString(const StringRef Username,
94 const StringRef Comment) const {
95 if (TodoStyle == StyleKind::Parentheses) {
96 return ("// TODO(" + Twine(User) +
97 "): " + (Comment.empty() ? "some details" : Comment))
98 .str();
99 }
100 return ("// TODO: " + Twine(User) + " - " +
101 (Comment.empty() ? "some details" : Comment))
102 .str();
103 }
104
105 StyleKind getTodoStyle() const { return TodoStyle; }
106
107private:
108 TodoCommentCheck &Check;
109 std::string User;
110 llvm::Regex TodoMatch;
111 StyleKind TodoStyle = StyleKind::Hyphen;
112};
113
115 : ClangTidyCheck(Name, Context),
116 Handler(std::make_unique<TodoCommentHandler>(
117 *this, Context->getOptions().User)) {}
118
120
121void TodoCommentCheck::registerPPCallbacks(const SourceManager &SM,
122 Preprocessor *PP,
123 Preprocessor *ModuleExpanderPP) {
124 PP->addCommentHandler(Handler.get());
125}
126
128 Options.store(Opts, "Style", Handler->getTodoStyle());
129}
130
131} // 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