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

Alan Rosenthal via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 25 19:14:03 PDT 2024


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

>From 5f84cc47cde13732ed61eccc0b17f6c420a64ab7 Mon Sep 17 00:00:00 2001
From: Alan Rosenthal <alanrosenthal at google.com>
Date: Thu, 22 Aug 2024 21:42:04 +0000
Subject: [PATCH 1/3] [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    | 21 +++++++++++++------
 .../checkers/google/readability-todo.cpp      |  4 ++++
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp b/clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp
index adad54aa24ba99..9c7b10214bcdc8 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{llvm::Regex("^// TODO: (.*) - (.*)$"),
+                    llvm::Regex("^// *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 llvm::Regex &TodoMatch : TodoMatches) {
+      if (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::array<llvm::Regex, 2> 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.

>From a9a524d2627c2f0a43e4f8a05d06184842233088 Mon Sep 17 00:00:00 2001
From: Alan Rosenthal <1288897+AlanRosenthal at users.noreply.github.com>
Date: Sun, 25 Aug 2024 22:13:23 -0400
Subject: [PATCH 2/3] PR feedback

---
 clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp    | 2 +-
 .../test/clang-tidy/checkers/google/readability-todo.cpp    | 6 ++----
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp b/clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp
index 9c7b10214bcdc8..cd0457a6ae5132 100644
--- a/clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp
+++ b/clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp
@@ -9,7 +9,7 @@
 #include "TodoCommentCheck.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Lex/Preprocessor.h"
-#include <list>
+#include <array>
 #include <optional>
 
 namespace clang::tidy::google::readability {
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 e41f84b31dabb8..daab83091e3174 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,7 +24,5 @@
 // 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.
+// TODO: b/12345 - solve the collatz conjecture
+// TODO: foo - barW

>From 5fc52c645b09843ceebce132dd46b36a42ed985e Mon Sep 17 00:00:00 2001
From: Alan Rosenthal <1288897+AlanRosenthal at users.noreply.github.com>
Date: Sun, 25 Aug 2024 22:13:55 -0400
Subject: [PATCH 3/3] fix typo

---
 .../test/clang-tidy/checkers/google/readability-todo.cpp        | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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 daab83091e3174..d0f85febbdf5b7 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
@@ -25,4 +25,4 @@
 // TODO (b/12345): allow spaces before parentheses
 // TODO(asdf) allow missing semicolon
 // TODO: b/12345 - solve the collatz conjecture
-// TODO: foo - barW
+// TODO: foo - bar



More information about the cfe-commits mailing list