[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 14:41:38 PDT 2024


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

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.

>From 5f983758f998d0293d057db0652d63a6dc4402d9 Mon Sep 17 00:00:00 2001
From: Alan Rosenthal <alanrosenthal at google.com>
Date: Mon, 19 Aug 2024 21:39:12 +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    | 22 ++++++++++++++-----
 .../checkers/google/readability-todo.cpp      |  4 ++++
 2 files changed, 20 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..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.



More information about the cfe-commits mailing list