2014-09-18 20:53:13 +08:00
|
|
|
//===--- 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 {
|
2015-03-05 21:46:14 +08:00
|
|
|
namespace google {
|
2014-09-18 20:53:13 +08:00
|
|
|
namespace readability {
|
|
|
|
|
2014-09-19 02:59:50 +08:00
|
|
|
class TodoCommentCheck::TodoCommentHandler : public CommentHandler {
|
2014-09-18 20:53:13 +08:00
|
|
|
public:
|
2014-09-25 02:36:03 +08:00
|
|
|
TodoCommentHandler(TodoCommentCheck &Check, llvm::Optional<std::string> User)
|
|
|
|
: Check(Check), User(User ? *User : "unknown"),
|
2014-10-06 07:40:30 +08:00
|
|
|
TodoMatch("^// *TODO *(\\(.*\\))?:?( )?(.*)$") {}
|
2014-09-18 20:53:13 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
2014-09-25 02:36:03 +08:00
|
|
|
std::string User;
|
2014-09-18 20:53:13 +08:00
|
|
|
llvm::Regex TodoMatch;
|
|
|
|
};
|
2014-09-19 02:59:50 +08:00
|
|
|
|
|
|
|
TodoCommentCheck::TodoCommentCheck(StringRef Name, ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
2014-09-25 02:36:03 +08:00
|
|
|
Handler(llvm::make_unique<TodoCommentHandler>(
|
|
|
|
*this, Context->getOptions().User)) {}
|
2014-09-18 20:53:13 +08:00
|
|
|
|
|
|
|
void TodoCommentCheck::registerPPCallbacks(CompilerInstance &Compiler) {
|
2014-09-19 02:59:50 +08:00
|
|
|
Compiler.getPreprocessor().addCommentHandler(Handler.get());
|
2014-09-18 20:53:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace readability
|
2015-03-05 21:46:14 +08:00
|
|
|
} // namespace google
|
2014-09-18 20:53:13 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|