2014-09-18 20:53:13 +08:00
|
|
|
//===--- TodoCommentCheck.cpp - clang-tidy --------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-09-18 20:53:13 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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),
|
2019-08-15 07:52:23 +08:00
|
|
|
Handler(std::make_unique<TodoCommentHandler>(
|
2014-09-25 02:36:03 +08:00
|
|
|
*this, Context->getOptions().User)) {}
|
2014-09-18 20:53:13 +08:00
|
|
|
|
2019-09-26 21:47:29 +08:00
|
|
|
TodoCommentCheck::~TodoCommentCheck() = default;
|
|
|
|
|
2019-03-23 02:58:12 +08:00
|
|
|
void TodoCommentCheck::registerPPCallbacks(const SourceManager &SM,
|
|
|
|
Preprocessor *PP,
|
|
|
|
Preprocessor *ModuleExpanderPP) {
|
|
|
|
PP->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
|