[clang-tools-extra] [clang-tidy] Update TODO comment check (PR #104868)

Alan Rosenthal via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 19 15:55:53 PDT 2024


https://github.com/AlanRosenthal updated https://github.com/llvm/llvm-project/pull/104868

>From 891fe7098179188bea3bd825f615762022cb0828 Mon Sep 17 00:00:00 2001
From: Alan Rosenthal <alanrosenthal at google.com>
Date: Mon, 19 Aug 2024 22:44:30 +0000
Subject: [PATCH] [clang-tidy] Update TODO comment check

The doc for google-readability-todo reference:
https://google.github.io/styleguide/cppguide.html#TODO_Comments
Which lists two styles of TODO comments.

Previously, only `TODO(John): comment` were supported.
After this PR, `TODO: bug 123 - comment` are also supported.
---
 .../clang-tidy/google/TodoCommentCheck.cpp    | 23 +++++++++++++------
 .../checkers/google/readability-todo.cpp      |  4 ++++
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp b/clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp
index adad54aa24ba99..879b2faeb631f9 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,29 @@ 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 +54,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)
@@ -61,4 +70,4 @@ void TodoCommentCheck::registerPPCallbacks(const SourceManager &SM,
   PP->addCommentHandler(Handler.get());
 }
 
-} // namespace clang::tidy::google::readability
+} // namespace clang::tidy::google::readability
\ No newline at end of file
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.



More information about the cfe-commits mailing list