Benjamin Kramer | 6be414d | 2014-09-18 12:53:13 +0000 | [diff] [blame^] | 1 | //===--- TodoCommentCheck.cpp - clang-tidy --------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "TodoCommentCheck.h" |
| 11 | #include "clang/Frontend/CompilerInstance.h" |
| 12 | #include "clang/Lex/Preprocessor.h" |
| 13 | |
| 14 | namespace clang { |
| 15 | namespace tidy { |
| 16 | namespace readability { |
| 17 | |
| 18 | namespace { |
| 19 | class TodoCommentHandler : public CommentHandler { |
| 20 | public: |
| 21 | explicit TodoCommentHandler(TodoCommentCheck &Check) |
| 22 | : Check(Check), TodoMatch("^// *TODO(\\(.*\\))?:?( )?(.*)$") {} |
| 23 | |
| 24 | bool HandleComment(Preprocessor &PP, SourceRange Range) override { |
| 25 | StringRef Text = |
| 26 | Lexer::getSourceText(CharSourceRange::getCharRange(Range), |
| 27 | PP.getSourceManager(), PP.getLangOpts()); |
| 28 | |
| 29 | SmallVector<StringRef, 4> Matches; |
| 30 | if (!TodoMatch.match(Text, &Matches)) |
| 31 | return false; |
| 32 | |
| 33 | StringRef Username = Matches[1]; |
| 34 | StringRef Comment = Matches[3]; |
| 35 | |
| 36 | if (!Username.empty()) |
| 37 | return false; |
| 38 | |
| 39 | // If the username is missing put in the current user's name. Not ideal but |
| 40 | // works for running tidy locally. |
| 41 | // FIXME: Can we get this from a more reliable source? |
| 42 | const char *User = std::getenv("USER"); |
| 43 | if (!User) |
| 44 | User = "unknown"; |
| 45 | std::string NewText = ("// TODO(" + Twine(User) + "): " + Comment).str(); |
| 46 | |
| 47 | Check.diag(Range.getBegin(), "missing username/bug in TODO") |
| 48 | << FixItHint::CreateReplacement(CharSourceRange::getCharRange(Range), |
| 49 | NewText); |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | TodoCommentCheck &Check; |
| 55 | llvm::Regex TodoMatch; |
| 56 | }; |
| 57 | } // namespace |
| 58 | |
| 59 | void TodoCommentCheck::registerPPCallbacks(CompilerInstance &Compiler) { |
| 60 | Compiler.getPreprocessor().addCommentHandler(new TodoCommentHandler(*this)); |
| 61 | } |
| 62 | |
| 63 | } // namespace readability |
| 64 | } // namespace tidy |
| 65 | } // namespace clang |