[clang-tools-extra] [clang-tidy] Update TODO comment check (PR #104868)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 19 14:42:27 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
@llvm/pr-subscribers-clang-tools-extra
Author: Alan Rosenthal (AlanRosenthal)
<details>
<summary>Changes</summary>
The doc for google-readability-todo reference lists two styles of TODO comments:
https://google.github.io/styleguide/cppguide.html#TODO_Comments
Previously, only `TODO(info): comment` were supported.
After this PR, `TODO: info - comment` are also supported.
---
Full diff: https://github.com/llvm/llvm-project/pull/104868.diff
2 Files Affected:
- (modified) clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp (+16-6)
- (modified) clang-tools-extra/test/clang-tidy/checkers/google/readability-todo.cpp (+4)
``````````diff
diff --git a/clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp b/clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp
index adad54aa24ba99..f5a7b4bfda6b1a 100644
--- a/clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp
+++ b/clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp
@@ -9,6 +9,7 @@
#include "TodoCommentCheck.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Preprocessor.h"
+#include <list>
#include <optional>
namespace clang::tidy::google::readability {
@@ -17,21 +18,30 @@ class TodoCommentCheck::TodoCommentHandler : public CommentHandler {
public:
TodoCommentHandler(TodoCommentCheck &Check, std::optional<std::string> User)
: Check(Check), User(User ? *User : "unknown"),
- TodoMatch("^// *TODO *(\\(.*\\))?:?( )?(.*)$") {}
+ TodoMatches{"^// *TODO *(\\(.*\\)): ?(.*)$", "^// TODO: (.*) - (.*)$"} {
+ }
bool HandleComment(Preprocessor &PP, SourceRange Range) override {
StringRef Text =
Lexer::getSourceText(CharSourceRange::getCharRange(Range),
PP.getSourceManager(), PP.getLangOpts());
+ bool Found = false;
SmallVector<StringRef, 4> Matches;
- if (!TodoMatch.match(Text, &Matches))
+ for (const std::string &TodoMatch : TodoMatches) {
+ if (llvm::Regex(TodoMatch).match(Text, &Matches)) {
+ Found = true;
+ break;
+ }
+ }
+ if (!Found) {
return false;
+ }
- StringRef Username = Matches[1];
- StringRef Comment = Matches[3];
+ StringRef Info = Matches[1];
+ StringRef Comment = Matches[2];
- if (!Username.empty())
+ if (!Info.empty())
return false;
std::string NewText = ("// TODO(" + Twine(User) + "): " + Comment).str();
@@ -45,7 +55,7 @@ class TodoCommentCheck::TodoCommentHandler : public CommentHandler {
private:
TodoCommentCheck &Check;
std::string User;
- llvm::Regex TodoMatch;
+ std::list<std::string> TodoMatches;
};
TodoCommentCheck::TodoCommentCheck(StringRef Name, ClangTidyContext *Context)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/google/readability-todo.cpp b/clang-tools-extra/test/clang-tidy/checkers/google/readability-todo.cpp
index 6b900aa92150e8..e41f84b31dabb8 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/google/readability-todo.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/google/readability-todo.cpp
@@ -24,3 +24,7 @@
// TODO(b/12345): find the holy grail
// TODO (b/12345): allow spaces before parentheses
// TODO(asdf) allow missing semicolon
+// TODO: bug 12345678 - Remove this after the 2047q4 compatibility window expires.
+// TODO: example.com/my-design-doc - Manually fix up this code the next time it's touched.
+// TODO(bug 12345678): Update this list after the Foo service is turned down.
+// TODO(John): Use a "\*" here for concatenation operator.
``````````
</details>
https://github.com/llvm/llvm-project/pull/104868
More information about the cfe-commits
mailing list