Skip to content

Commit df2b631

Browse files
committed
[clang-tidy] Update google todo checker with style guide changes.
The [Google style guide] now allows (and recommends) writing TODOs with the following format: ```cpp // TODO: bug reference - details about what needs to be done. ``` With this change the checker accepts the new style and suggests in in the fix-it hint. The previous style is still accepted. [Google style guide]: https://google.github.io/styleguide/cppguide.html#TODO_Comments
1 parent 4f4bee4 commit df2b631

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

clang-tools-extra/clang-tidy/google/TodoCommentCheck.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,37 @@ class TodoCommentCheck::TodoCommentHandler : public CommentHandler {
1717
public:
1818
TodoCommentHandler(TodoCommentCheck &Check, std::optional<std::string> User)
1919
: Check(Check), User(User ? *User : "unknown"),
20-
TodoMatch("^// *TODO *(\\(.*\\))?:?( )?(.*)$") {}
20+
TodoMatch("^// *TODO *((\\((.*)\\))?:?( )?|: *(.*) *- *)?(.*)$") {}
2121

2222
bool HandleComment(Preprocessor &PP, SourceRange Range) override {
2323
StringRef Text =
2424
Lexer::getSourceText(CharSourceRange::getCharRange(Range),
2525
PP.getSourceManager(), PP.getLangOpts());
2626

27-
SmallVector<StringRef, 4> Matches;
27+
SmallVector<StringRef, 7> Matches;
2828
if (!TodoMatch.match(Text, &Matches))
2929
return false;
3030

31-
StringRef Username = Matches[1];
32-
StringRef Comment = Matches[3];
31+
const bool deprecated_style = !Matches[3].empty();
32+
StringRef Username = deprecated_style ? Matches[5] : Matches[3];
33+
StringRef Comment = Matches[6];
3334

34-
if (!Username.empty())
35+
if (!Username.empty() && (deprecated_style || !Comment.empty()))
3536
return false;
3637

37-
std::string NewText = ("// TODO(" + Twine(User) + "): " + Comment).str();
38+
if (Username.empty()) {
39+
std::string NewText = ("// TODO: " + Twine(User) + " - " +
40+
(Comment.empty() ? "some details" : Comment))
41+
.str();
42+
43+
Check.diag(Range.getBegin(), "missing username/bug in TODO")
44+
<< FixItHint::CreateReplacement(CharSourceRange::getCharRange(Range),
45+
NewText);
46+
}
47+
48+
if (Comment.empty())
49+
Check.diag(Range.getBegin(), "missing details in TODO");
3850

39-
Check.diag(Range.getBegin(), "missing username/bug in TODO")
40-
<< FixItHint::CreateReplacement(CharSourceRange::getCharRange(Range),
41-
NewText);
4251
return false;
4352
}
4453

clang-tools-extra/test/clang-tidy/checkers/google/readability-todo.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,36 @@
22

33
// TODOfix this1
44
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: missing username/bug in TODO
5-
// CHECK-FIXES: // TODO(some user): fix this1
5+
// CHECK-FIXES: // TODO: some user - fix this1
66

77
// TODO fix this2
88
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: missing username/bug in TODO
9-
// CHECK-FIXES: // TODO(some user): fix this2
9+
// CHECK-FIXES: // TODO: some user - fix this2
1010

1111
// TODO fix this3
1212
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: missing username/bug in TODO
13-
// CHECK-FIXES: // TODO(some user): fix this3
13+
// CHECK-FIXES: // TODO: some user - fix this3
1414

1515
// TODO: fix this4
1616
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: missing username/bug in TODO
17-
// CHECK-FIXES: // TODO(some user): fix this4
17+
// CHECK-FIXES: // TODO: some user - fix this4
18+
19+
// TODO: bug 12345 -
20+
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: missing details in TODO
21+
22+
// TODO: a message without a reference
23+
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: missing username/bug in TODO
24+
// CHECK-FIXES: // TODO: some user - a message without a reference
1825

1926
// TODO(clang)fix this5
2027

28+
// TODO: foo - shave yaks
29+
// TODO:foo - no space bewteen semicolon and username
30+
// TODO: foo- no space bewteen username and dash
31+
// TODO: foo - extra spaces between semicolon and username
32+
// TODO: foo - extra spaces between username and dash
33+
// TODO: b/12345 - use a b/ prefix
34+
// TODO: bug 12345 - use a space in username/bug reference
2135
// TODO(foo):shave yaks
2236
// TODO(bar):
2337
// TODO(foo): paint bikeshed

0 commit comments

Comments
 (0)