Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang-tidy] Add a checker that warns on TODO comments without username.
It also suggests a fix-it, taking the name from $USER. This will be made configurable eventually. Differential Revision: http://reviews.llvm.org/D5393 llvm-svn: 218049
- Loading branch information
Showing
6 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//===--- TodoCommentCheck.cpp - clang-tidy --------------------------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "TodoCommentCheck.h" | ||
#include "clang/Frontend/CompilerInstance.h" | ||
#include "clang/Lex/Preprocessor.h" | ||
|
||
namespace clang { | ||
namespace tidy { | ||
namespace readability { | ||
|
||
namespace { | ||
class TodoCommentHandler : public CommentHandler { | ||
public: | ||
explicit TodoCommentHandler(TodoCommentCheck &Check) | ||
: Check(Check), TodoMatch("^// *TODO(\\(.*\\))?:?( )?(.*)$") {} | ||
|
||
bool HandleComment(Preprocessor &PP, SourceRange Range) override { | ||
StringRef Text = | ||
Lexer::getSourceText(CharSourceRange::getCharRange(Range), | ||
PP.getSourceManager(), PP.getLangOpts()); | ||
|
||
SmallVector<StringRef, 4> Matches; | ||
if (!TodoMatch.match(Text, &Matches)) | ||
return false; | ||
|
||
StringRef Username = Matches[1]; | ||
StringRef Comment = Matches[3]; | ||
|
||
if (!Username.empty()) | ||
return false; | ||
|
||
// If the username is missing put in the current user's name. Not ideal but | ||
// works for running tidy locally. | ||
// FIXME: Can we get this from a more reliable source? | ||
const char *User = std::getenv("USER"); | ||
if (!User) | ||
User = "unknown"; | ||
std::string NewText = ("// TODO(" + Twine(User) + "): " + Comment).str(); | ||
|
||
Check.diag(Range.getBegin(), "missing username/bug in TODO") | ||
<< FixItHint::CreateReplacement(CharSourceRange::getCharRange(Range), | ||
NewText); | ||
return false; | ||
} | ||
|
||
private: | ||
TodoCommentCheck &Check; | ||
llvm::Regex TodoMatch; | ||
}; | ||
} // namespace | ||
|
||
void TodoCommentCheck::registerPPCallbacks(CompilerInstance &Compiler) { | ||
Compiler.getPreprocessor().addCommentHandler(new TodoCommentHandler(*this)); | ||
} | ||
|
||
} // namespace readability | ||
} // namespace tidy | ||
} // namespace clang |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//===--- TodoCommentCheck.h - clang-tidy ------------------------*- C++ -*-===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_TODOCOMMENTCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_TODOCOMMENTCHECK_H | ||
|
||
#include "../ClangTidy.h" | ||
|
||
namespace clang { | ||
namespace tidy { | ||
namespace readability { | ||
|
||
/// \brief Finds TODO comments without a username or bug number. | ||
/// | ||
/// Corresponding cpplint.py check: readability/todo | ||
class TodoCommentCheck : public ClangTidyCheck { | ||
public: | ||
TodoCommentCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
void registerPPCallbacks(CompilerInstance &Compiler) override; | ||
}; | ||
|
||
} // namespace readability | ||
} // namespace tidy | ||
} // namespace clang | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_TODOCOMMENTCHECK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
clang-tools-extra/test/clang-tidy/google-readability-todo.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s google-readability-todo %t | ||
// REQUIRES: shell | ||
|
||
// TODOfix this1 | ||
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: missing username/bug in TODO | ||
// CHECK-FIXES: // TODO({{[^)]+}}): fix this1 | ||
|
||
// TODO fix this2 | ||
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: missing username/bug in TODO | ||
// CHECK-FIXES: // TODO({{[^)]+}}): fix this2 | ||
|
||
// TODO fix this3 | ||
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: missing username/bug in TODO | ||
// CHECK-FIXES: // TODO({{[^)]+}}): fix this3 | ||
|
||
// TODO: fix this4 | ||
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: missing username/bug in TODO | ||
// CHECK-FIXES: // TODO({{[^)]+}}): fix this4 | ||
|
||
// TODO(clang)fix this5 | ||
|
||
// TODO(foo):shave yaks | ||
// TODO(bar): | ||
// TODO(foo): paint bikeshed | ||
// TODO(b/12345): find the holy grail |